我得到以前的pass_key值而不是现在的值

时间:2018-02-26 03:26:56

标签: php cakephp cakephp-3.0

  1. 首次点击忘记密码不会在电子邮件视图中显示pass_key,但会存储在pass_key字段中
  2. 第二次点击忘记密码,依此类推,将从之前的pass_key插入中获取pass_key的值
  3. UsersController.php

    <?php
    public function forgotPassword()
        {
            if ($this->request->is('post')) {
                $query = $this->Users->findByEmail($this->request->data['email']);
                $user = $query->first();
                if (is_null($user)) {
                    $this->Flash->error('Email address does not exist. Please try again');
                } else {
                    $passkey =Text::uuid(); // create key 
                    $timeout = time() + DAY;
                    $url = Router::url(['controller' => 'users', 'action' => 'reset'], true) . '/' . $passkey; // Im just generate this link in the view and pass
                    $status = $this->Users->updateAll(array('Users.pass_key' => $passkey,'timeout' => $timeout), array('Users.id' => $user->id));
                 if($status){
    
                        $this->getMailer('User')->send('forgotPasswordEmail', [$user]);             
                       $this->Flash->success(__('We already sent a link to your email'));
                return $this->redirect(['action' => 'login']);
                    } else {
                        $this->Flash->error('Error saving reset passkey/timeout');
                    }
                  }
            }
        }
    ?>
    

    UserMailer.php

    <?php
    namespace App\Mailer;
    use Cake\Mailer\Mailer;
    class UserMailer extends Mailer
    {
    
        public function forgotPasswordEmail($user)
        {
            // attach a text file 
            $this->attachments([
                'text for user.txt'=> [
                    'file'=> 'files/instruction.txt',
                        'mimetype'=>'plain/text',
                        'contentId'=>'3734hf38'
                ],
    
    
            // attach an image file 
                'edit.png'=>[
                    'file'=>'files/ourlogo.png',
                        'mimetype'=>'image/png',
                        'contentId'=>'734h3r38'
                    ]
            ])
            ->to($user->email)
            ->emailFormat('html')
            ->subject(sprintf('Forgot Password link  %s', $user->username))
            ->viewVars([
            'username'=> $user->username,
            'useremail'=>$user->email,
            'passkey' => $user->pass_key,
            'userid' => $user->id
            ])
    
            // the template file you will use in this emial
            ->template('forgotPasswordEmail') // By default template with same name as method name is used.
            // the layout .ctp file you will use in this email
            ->layout('customLayout');
        }
    }
    ?>
    

    Template \ Email \ html \ forgot_password_email.ctp

    <?php
    use Cake\Routing\Router;
    ?>
    
    <p>Your username is:  <?=$username?> </p>
    
    <p>Click on the link below to Reset Your Password.</p>
    
    <?php $resetUrl = Router::url(['controller' => 'users', 'action' => 'reset/' . $passkey]);?>
    
    <p><a href="http://localhost<?php echo $resetUrl; ?>">Click here to Reset Your Password</a></p>
    
    ?>
    
    
    
    
      <?php
            $url = Router::url(['controller' => 'users', 'action' => 'reset'], true) . '/' . $passkey; // Im just generate this link in the view and pass
                            $status = $this->Users->updateAll(array('Users.pass_key' => $passkey,'timeout' => $timeout), array('Users.id' => $user->id));
                         if($status){
    
            var_dump($url);exit;  I get the exact pass_key but I dont know how to pass the url to forgot_password_email.ctp . I just know how to pass the using this [$user] 
    
    
     var_dump($user->pass_key);exit; will retrieve the previous transaction not current pass_key
    
            .............
        ?>
    

1 个答案:

答案 0 :(得分:1)

您的问题是您正在生成密钥并将其保存到数据库,但不会更新$user对象,因此当您将其传递给视图时,它仍然具有旧版本。

试试这个:

$this->Users->patchEntity($user, [
    'pass_key' => Text::uuid(), // create key 
    'timeout' => time() + DAY,
]);
if ($this->Users->save($user)) {

在此,我们使用新值更新$user实体,以便它们适用于视图,并使用save函数代替updateAll

另外,在您看来,您应该更改为以下内容:

$resetUrl = Router::url(['controller' => 'users', 'action' => 'reset', $passkey]);