我试图在CakePHP 3.x中实现忘记密码功能。 我创建了一个接受用户电子邮件的表单:
<?= $this->Form->create()?>
<div class="form-group">
<?= $this->Form->input('email', array('class' => 'form-group','autocomplete' => 'off' ,'required' => 'required'))?>
</div>
<div class="form-group">
<?= $this->Form->button('Reset Password', array('class' => 'form-group primary'))?>
</div>
<?= $this->Form->end()?>
在我的控制器中,我试图通过电子邮件找到用户,如果电子邮件存在,则会生成随机密码,并且将更新该电子邮件ID的密码:
use Cake\ORM\TableRegistry;
use Cake\Auth\DefaultPasswordHasher;
public function forgotPassword($email = null){
if($this->request->is('post')) {
$email = $this->request->data['email'];
$emails = TableRegistry::get('Users');
$user = $emails->find()->where(['email' => $email ])->first();
if (!$user) {
$this->Flash->error(__('No user with that email found.'));
return $this->redirect(['controller' => 'Users','action' => 'forgotPassword']);
}else{
$random = 'a';
$hasher = new DefaultPasswordHasher();
$val = $hasher->hash($random);
$data = $this->Users->password = $val;
if ($this->Users->save($data)) {
$this->Flash->success(__('Password changed Succesfully.'));
return $this->redirect(['controller' => 'Users','action' => 'forgotPassword']);
}
}
}
}
答案 0 :(得分:0)
你实际上没有说过具体的问题/问题,但我想我可能知道什么可以提供帮助。
整个DefaultPasswordHasher
位应该在UsersEntity文件中,就像在教程中一样:Blog tutorial
在实例中正确放置散列,就像在示例中一样,只要你使用PatchEntity或NewEntity就会自动调用它(我想,请确认一下吗?)。
其次,$this->[model]->save()
函数适用于实体,而不仅仅适用于数据。所以你会找到用户的实体,修补实体然后保存它:
...} else {
$newpass = 'randomstring';
$user = $this->Users->PatchEntity($user, ['password' => $newpass]);
if ($this->Users->save($user)) ...