0

以下确实有效:

// app/Controller/UsersController.php
$this->User->save(array('pwd'=>$new_pwd),false);

以下不起作用:

// app/Controller/UsersController.php
$this->User->setPassword($new_pwd);

User模型具有beforeSave()有效的方法和无效的自定义方法setPassword()

// app/Model/User.php
public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['pwd'])&&!empty($this->data[$this->alias]['pwd'])) {
        $new_password = $this->data[$this->alias]['pwd'];
        $passwordHasher = new BlowfishPasswordHasher();
        $this->data[$this->alias]['pwd'] = $passwordHasher->hash($new_password);
    }
    return true;
}

public function setPassword($new_password) {
    $passwordHasher = new BlowfishPasswordHasher();
    $result = $this->save(array(
        'pwd' => $passwordHasher->hash($new_password),
    ), false);
    return $result;
}

因此,setPassword()当我尝试使用以这种方式保存的密码登录时,它或多或少是相同的,$this->Auth->login()返回 false。我可以看到数据库中更新的密码哈希。

我错过了什么吗?请帮忙

4

1 回答 1

2

setPassword()内部也调用beforeSave()via save()。很明显,您正在对其进行两次哈希处理,使其无法再使用。

于 2015-12-26T23:07:55.913 回答