密码不能比较.Yii


The passwords is not compares. Yii

我是Yii的新人,我只是无法建立一个工作授权…我的LoginForm模型中的authenticate()方法返回错误Incorrect username or password。这是我的代码…组件/UserIdentity

class UserIdentity extends CUserIdentity
{
private $_id;
/**
 * Authenticates a user.
 * @return boolean whether authentication succeeds.
 */
public function authenticate()
{
    $record=User::model()->findByAttributes(array('username'=>$this->username));
    if($record===null)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    else if(!CPasswordHelper::verifyPassword($this->password,$record->password))
    //else if($this->password!==$record->password)
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
    {
        $this->_id=$record->id;
        //$this->setState('title', $record->title);
        $this->errorCode=self::ERROR_NONE;
    }
    return !$this->errorCode;
}
/**
 * @return integer the ID of the user record
 */
public function getId()
{
    return $this->_id;
}
}

loginform model authenticate funcs

/**
 * Authenticates the password.
 * This is the 'authenticate' validator as declared in rules().
 */
public function authenticate($attribute,$params)
{
    if(!$this->hasErrors())
    {
      /*var_dump(CPasswordHelper::hashPassword($this->password));*/
        $this->_identity=new UserIdentity($this->username,$this->password);
        if(!$this->_identity->authenticate())
            $this->addError('password','Incorrect username or password.');
    }
}
/**
 * Logs in the user using the given username and password in the model.
 * @return boolean whether login is successful
 */
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;
}

User模型散列

protected function beforeSave()
{
    ...
    $this->password = CPasswordHelper::hashPassword($this->password);
    return parent::beforeSave();
}

和控制器代码

public function actionLogin()
{
    if (!defined('CRYPT_BLOWFISH')||!CRYPT_BLOWFISH)
        throw new CHttpException(500,"This application requires that PHP was compiled with Blowfish support for crypt().");
    $model=new LoginForm;
    // if it is ajax validation request
    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
    // collect user input data
    if(isset($_POST['LoginForm']))
    {
        $model->attributes=$_POST['LoginForm'];
        // validate user input and redirect to the previous page if valid
        if($model->validate() && $model->login())
            $this->redirect(array('/admin/'));
    }
    // display the login form
    $this->render('login',array('model'=>$model));
}

我认为我使用CPasswordHelper错误,如果是,请给我正确的例子。谢谢你

在我看来,无论如何,您将$this->errorCode设置为某些值,即使它是ERROR_NONE中的任何内容。然后返回!$this->errorCode,它应该始终为false。ERROR_NONE常数的值是多少?

编辑:发现是0。https://github.com/yiisoft/yii/blob/1.1.14/framework/web/auth/CBaseUserIdentity.php

那么,试着抛出一个错误异常,这样你就可以看到它在什么时候失败了。

下次我使用Mysql工作台时,我应该检查数据库列…问题是varchar限制在password字段