发送新密码 - CakePHP 2.x
Send a new password - CakePHP 2.x
我正尝试在用户忘记旧密码时为他们设置一个新密码。
我有代码,它正在工作(创建一个新的随机密码,更新数据库并通过电子邮件将其发送到输入的电子邮件)但是当我尝试使用新密码登录时它说密码无效(显然旧密码也不正确)
我的代码是:
function forgot() {
if(!empty($this->data)) {
$user = $this->User->findByEmail($this->data['User']['email']);
$user_email = $this->data['User']['email'];
if($user) {
$user['User']['tmp_password'] = $this->User->createTempPassword(10);
$user['User']['password'] = $this->Auth->password($user['User']['tmp_password']);
if($this->User->save($user, false)) {
$this->User->set('User.password', $user['User']['password'], array('User.email' => $user_email));
$this->User->save();
$this->__sendPasswordEmail($user,$user['User']['tmp_password']);
$this->Session->setFlash('An email has been sent with your new password.');
$this->redirect($this->referer());
}
} else {
$this->Session->setFlash('No user was found with the submitted email address.');
}
}
}
public function beforeSave($options = array()) {
// hash our password
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
// if we get a new password, hash it
if (isset($this->data[$this->alias]['password_update']) && !empty($this->data[$this->alias]['password_update'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password_update']);
}
// fallback to our parent
return parent::beforeSave($options);
}
function createTempPassword() {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
我阅读了一些类似的问题,我发现密码散列可能是一个问题,但我没有看到它在我的代码中的何处交互,因为我使用相同的散列方法。 (您可能会问我为什么使用 $this->User->set / Save - 好吧,我只是想弄清楚这个问题,不是吗。)
密码如下:BT9DPRsN - bcfbde69a31197d18589e81dd41af6dbc3c21557
谢谢。
评论这一行,它会为你工作$user['User']['password'] = $this->Auth->password($user['User']['tmp_password']);
这背后的原因是因为您已经在 cakephp 回调函数中对密码进行了哈希处理 beforeSave
每当您尝试将密码字段保存在数据库中时,它都会先对其进行哈希处理,然后再保存。希望对您有所帮助。
我想你已经对密码进行了两次哈希处理,
第一个在 Controller 的 beforeSave() 中,第二个在 Model(AppModel or User) 的 beforeSave()
您应该使用一次,(可以在最近的 Controller 中清除语句 return beforeSave() 或仅在 AppModel 中定义 beforeSave() )。
http://rao5s.vn
我正尝试在用户忘记旧密码时为他们设置一个新密码。 我有代码,它正在工作(创建一个新的随机密码,更新数据库并通过电子邮件将其发送到输入的电子邮件)但是当我尝试使用新密码登录时它说密码无效(显然旧密码也不正确)
我的代码是:
function forgot() {
if(!empty($this->data)) {
$user = $this->User->findByEmail($this->data['User']['email']);
$user_email = $this->data['User']['email'];
if($user) {
$user['User']['tmp_password'] = $this->User->createTempPassword(10);
$user['User']['password'] = $this->Auth->password($user['User']['tmp_password']);
if($this->User->save($user, false)) {
$this->User->set('User.password', $user['User']['password'], array('User.email' => $user_email));
$this->User->save();
$this->__sendPasswordEmail($user,$user['User']['tmp_password']);
$this->Session->setFlash('An email has been sent with your new password.');
$this->redirect($this->referer());
}
} else {
$this->Session->setFlash('No user was found with the submitted email address.');
}
}
}
public function beforeSave($options = array()) {
// hash our password
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
// if we get a new password, hash it
if (isset($this->data[$this->alias]['password_update']) && !empty($this->data[$this->alias]['password_update'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password_update']);
}
// fallback to our parent
return parent::beforeSave($options);
}
function createTempPassword() {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
我阅读了一些类似的问题,我发现密码散列可能是一个问题,但我没有看到它在我的代码中的何处交互,因为我使用相同的散列方法。 (您可能会问我为什么使用 $this->User->set / Save - 好吧,我只是想弄清楚这个问题,不是吗。)
密码如下:BT9DPRsN - bcfbde69a31197d18589e81dd41af6dbc3c21557
谢谢。
评论这一行,它会为你工作$user['User']['password'] = $this->Auth->password($user['User']['tmp_password']);
这背后的原因是因为您已经在 cakephp 回调函数中对密码进行了哈希处理 beforeSave
每当您尝试将密码字段保存在数据库中时,它都会先对其进行哈希处理,然后再保存。希望对您有所帮助。
我想你已经对密码进行了两次哈希处理,
第一个在 Controller 的 beforeSave() 中,第二个在 Model(AppModel or User) 的 beforeSave()
您应该使用一次,(可以在最近的 Controller 中清除语句 return beforeSave() 或仅在 AppModel 中定义 beforeSave() )。
http://rao5s.vn