【问题标题】:How to implement forgot password in cakephp3.x如何在 cakephp3.x 中实现忘记密码
【发布时间】:2016-08-13 03:38:21
【问题描述】:

我正在尝试在 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']);

                }


        }
    }
}

【问题讨论】:

    标签: php cakephp-3.0


    【解决方案1】:

    您实际上并没有说明具体的问题/问题,但我想我可能知道什么可以提供帮助。

    整个DefaultPasswordHasher 位应在UsersEntity 文件中,如教程中的:Blog tutorial

    像示例中那样将散列正确放置在实体中,只要您使用 PatchEntity 或 NewEntity,它就会自动被调用(我想,请确认一下?)。

    其次,$this-&gt;[model]-&gt;save() 函数适用于实体,而不仅仅是数据。所以你会找到用户的实体,修补实体然后保存它:

    ...} else {
    $newpass = 'randomstring';
    $user = $this->Users->PatchEntity($user, ['password' => $newpass]);
    
    if ($this->Users->save($user)) ...
    

    【讨论】:

      猜你喜欢
      • 2018-07-07
      • 2016-09-13
      • 2011-07-26
      • 1970-01-01
      • 2017-07-06
      • 1970-01-01
      • 2015-02-03
      • 2019-12-07
      • 2010-11-21
      相关资源
      最近更新 更多