2014-09-26 53 views
0

您好我對yii框架相當陌生,目前正嘗試通過數據庫身份驗證建立登錄。但同時即時嘗試登錄我得到這個錯誤說在Yii登錄中不接受正確的密碼

請修復以下輸入錯誤: 密碼不正確。

但當我檢查數據庫表即時輸入正確的密碼。

任何人可以幫助我,如果這

繼承人的控制器

<?php 

class SiteController extends Controller 

{ 

public function actions() 
{ 
    return array(

     'captcha'=>array(
      'class'=>'CCaptchaAction', 
      'backColor'=>0xFFFFFF, 
     ), 

     'page'=>array(
      'class'=>'CViewAction', 
     ), 
    ); 
} 
public function actionIndex() 
{ 

    $this->render('index'); 
} 


public function actionError() 
{ 
    if($error=Yii::app()->errorHandler->error) 
    { 
     if(Yii::app()->request->isAjaxRequest) 
      echo $error['message']; 
     else 
      $this->render('error', $error); 
    } 
} 


public function actionContact() 
{ 
    $model=new ContactForm; 
    if(isset($_POST['ContactForm'])) 
    { 
     $model->attributes=$_POST['ContactForm']; 
     if($model->validate()) 
     { 
      $name='=?UTF-8?B?'.base64_encode($model->name).'?='; 
      $subject='=?UTF-8?B?'.base64_encode($model->subject).'?='; 
      $headers="From: $name <{$model->email}>\r\n". 
       "Reply-To: {$model->email}\r\n". 
       "MIME-Version: 1.0\r\n". 
       "Content-Type: text/plain; charset=UTF-8"; 

      mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers); 
      Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.'); 
      $this->refresh(); 
     } 
    } 
    $this->render('contact',array('model'=>$model)); 
} 


public function actionLogin() 
{ 
     $form=new LoginForm; 
     if(isset($_POST['LoginForm'])) 
     { 
      $form->attributes=$_POST['LoginForm']; 
      if($form->validate() && $form->login()) $this->redirect(Yii::app()->user->returnUrl); 
     } 

      $this->render('login',array('form'=>$form)); 
} 

public function actionLogout() 
{ 
    Yii::app()->user->logout(); 
    $this->redirect(Yii::app()->homeUrl); 
} 

}

herers模型

<?php 

class LoginForm extends CFormModel 

{ 
    public $email; 
    public $password; 


    private $_identity; 

public function rules() 
    { 
     return array(
     array('email, password', 'required'), 
     array('email', 'email'), 
     array('password', 'authenticate'), 
); 
    } 
public function attributeLabels() 
{ 
      return array('email'=>'Email Address'); 
} 
public function authenticate($attribute,$params) 
{ 
      if(!$this->hasErrors()) // we only want to authenticate when no input errors 
       { 
       $identity=new UserIdentity($this->email,$this->password); 
       $identity->authenticate(); 
       switch($identity->errorCode) 
       { 
        case UserIdentity::ERROR_NONE: 
       Yii::app()->user->login($identity); 
       break; 
        case UserIdentity::ERROR_USERNAME_INVALID: 
       $this->addError('email','Email address is incorrect.'); 
       break; 
     default: // UserIdentity::ERROR_PASSWORD_INVALID 
       $this->addError('password','Password is incorrect.'); 
       break; 
       } 
      } 
    } 
public function login() 
{ 
    if($this->_identity===null) 
    { 
     $this->_identity=new UserIdentity($this->username,$this->password); 
     $this->_identity->authenticate(); 
    } 
    if($this->_identity->errorCode===UserIdentity::ERROR_NONE) 
    { 
     $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days 
     Yii::app()->user->login($this->_identity,$duration); 
     return true; 
    } 
    else 
     return false; 
} 

}

這裏ŧ他認爲

<?php 
/* @var $this SiteController */ 
/* @var $model LoginForm */ 
/* @var $form CActiveForm */ 

$this->pageTitle=Yii::app()->name . ' - Login'; 
$this->breadcrumbs=array(
    'Login', 
); 
?> 
<h1>Login</h1> 

<p>Please fill out the following form with your login credentials:</p> 

<div class="form"> 
<?php $myWidget=$this->beginWidget('CActiveForm', array(
    'id'=>'login-form', 
    'enableClientValidation'=>true, 
    'clientOptions'=>array(
     'validateOnSubmit'=>true, 
    ), 
)); ?> 

    <p class="note">Fields with <span class="required">*</span> are required.</p> 
<div> 
    <?php echo CHtml::beginForm(); ?> 

    <?php echo CHtml::errorSummary($form); ?> 

    <div> 
    <?php echo CHtml::activeLabel($form,'email'); ?> 
    <?php echo CHtml::activeTextField($form,'email') ?> 
    </div> 

    <div> 
    <?php echo CHtml::activeLabel($form,'password'); ?> 
    <?php echo CHtml::activePasswordField($form,'password') ?> 
    </div> 

    <div> 
    <?php echo CHtml::submitButton('Login'); ?> 
    </div> 

    <?php echo CHtml::endForm(); ?> 

endWidget(); ?>

+0

有沒有人誰是熟悉並且與警予登錄系統 – 2014-09-26 04:48:58

回答

1

你必須在UserIdentity類中寫入你的認證邏輯,而不是在LoginForm模型中。

  1. LoginForm的模型,例如: -

    public function authenticate($attribute, $params) { 
        if (!$this->hasErrors()) { 
         $this->_identity = new UserIdentity($this->email, $this->password); 
         if (!$this->_identity->authenticate()) 
         $this->addError('password', 'Incorrect username or password.'); 
        } 
    } 
    
    public function login() { 
    
        if ($this->_identity === null) { 
         $this->_identity = new UserIdentity($this->email, $this->password); 
         $this->_identity->authenticate(); 
    } 
    if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) { 
        $duration = $this->rememberMe ? 3600 * 24 * 30 : 0; // 30 days 
        Yii::app()->user->login($this->_identity, $duration); 
        return true; 
    } else 
        return false; 
    } 
    
  2. 對於數據庫認證,就必須使用components\UserIdentity.php

    public function authenticate() { 
    
    Yii::app()->getModule('auth')->getModule('user'); #import your module. 
    
    $record = User::model() 
         ->findByAttributes(array('email' => CHtml::encode($this->email))); #database call 
    
    if ($record === null) 
        $this->errorCode = self::ERROR_USERNAME_INVALID; 
    #else if ($record->password !== crypt($this->password, $record->password)) 
    else if ($record->password !== $this->password) 
        $this->errorCode = self::ERROR_PASSWORD_INVALID; 
    else { 
        $this->_uid = $record->user_id; 
        $this->setState('title', $record->user_name); 
        $this->setState('uid', $this->_uid); 
        $this->errorCode = self::ERROR_NONE; 
    } 
    return !$this->errorCode; 
    

    }

  3. 如果添加身份驗證函數內部authetication邏輯你有基於角色的登錄那麼你必須在config/main.php中添加WebUser類。

    components' => array(
         'user' => array(
          // enable cookie-based authentication 
          'class' => 'WebUser', 
          'allowAutoLogin' => true, 
          'loginUrl'=>array('/site/login'), 
          'returnUrl'=>array('/site/index'), 
         ), 
    } 
    
  4. 對於基於角色的評估檢查,你必須寫components\WebUser.php類 -

    class WebUser extends CWebUser { 
    
    public function checkAccess($operation, $params = array()) { 
        if (empty($this->id)) { 
         // Not identified => no rights 
         return false; 
        } 
        $role = $this->getState("roles"); 
        if ($role === '3') {    
         return true; // super admin role has access to everything 
        }else if ($role === '1') {    
         return true; // admin(manager) role has access to everything 
        }   
        // allow access if the operation request is the current user's role 
        return ($operation === $role); 
    } 
    
    } 
    

欲瞭解更多信息,請查看Authentication and Authorization

+0

感謝隊友生病了! – 2014-09-26 06:15:38