Yii CPasswordHelper不验证密码

时间:2015-02-25 09:10:32

标签: php authentication yii hash authorization

您好我正在尝试使用yii身份验证方法为我的网站创建登录页面。我不知道我是否写得对。 这是我的登录表单:

 <?php $this->beginWidget('WrapViewWidget'); ?>
    <div id="login-header">
        here you can create Question ...
    </div>
    <div class="container">
        <div id="login-content">
            <?php  $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
            'id' => 'login-form',
            'layout' => TbHtml::FORM_LAYOUT_HORIZONTAL,
            'enableClientValidation' => true,
            'clientOptions' => array(
                'validateOnSubmit' => true,
            ),
        )); ?>
        <div class="span5">
            <?php echo $form->labelEx($model, 'username'); ?>
            <?php echo $form->textField($model, 'username',array('class'=> 'form-control'); ?>
            <?php echo $form->error($model, 'username'); ?>
        </div>
        <div class="span5">
            <?php echo $form->labelEx($model, 'password'); ?>
            <?php echo $form->passwordField($model, 'password', array('class' => 'form-control'); ?>
            <?php echo $form->error($model, 'password'); ?>
        </div>
        <div class="span5 rememberMe">
            <?php echo CHtml::activeCheckBox($model, 'rememberMe'); ?>
            <?php echo CHtml::activeLabel($model, 'rememberMe'); ?>
        </div>
        <div>
            <?php echo TbHtml::submitButton('Login', array('class' => 'btn-login')); ?>
        </div>
        <?php $this->endWidget(); ?>
    </div>
</div>
<?php $this->endWidget(); ?>

这是我的userIdentity:

class UserIdentity extends CUserIdentity{
    private $_id;

    public function __construct($username, $password)
    {
        $this->username = $username;
        $this->password = $password;
    }
    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))
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        else {
            $this->_id = $record->user_id;
            $this->setState('username', $record->username);
            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    public function getId()
    {
       return $this->_id;
    }

    public function getUsername()
    {
       return $this->username;
    }

    public function getPassword()
    {
        return $this->password;
    }

} 

我在我的用户模型中实现了beforeSave像这样:

public function beforeSave()
{
    if (parent::beforeSave()) {
        $this->password=CPasswordHelper::hashPassword($this->password);
        return true;
    }
    return false;
}

我在LoginForm中有这些方法:

public function authenticate($attribute,$params)
{
    $this->_identity=new UserIdentity($this->username,$this->password);
    if(!$this->_identity->authenticate())
        $this->addError('password','username or password is wrong!');
}

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;
}
public function rules()
{
    return array(
        // username and password are required
        array('username, password', 'required'),
        // rememberMe needs to be a boolean
        array('rememberMe', 'boolean'),

        array('email', 'email'),
        // password needs to be authenticated
        array('password', 'authenticate'),
    );
}

我尝试了很多东西,我确信我输入了正确的密码。但我不知道我的错误在哪里。我可能有愚蠢的错误。谢谢你。

0 个答案:

没有答案