以下操作确实有效:
// 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。不过,我可以在数据库中看到更新的密码哈希。
我是不是错过了什么?请帮助
setPassword()
内部也通过save()
调用beforeSave()
。很明显,你把它散列了两次,使它不可能再被使用了。