2014-09-29 156 views
0

我刚开始使用CakePHP并喜欢使用它!我创建了一个登录系统和注册系统,但是我们真的在“忘记密码”部分挣扎。CakePHP 2.4忘记密码

我想用一个tokenhash和有效期在用户数据库,以便它不能被滥用,用户需要输入用户名和电子邮件获得通过电子邮件发送给他们提供一个新产生的tokenhash

有一个激活链接这里有很多教程,但我发现其中大部分都适用于第一部分,例如通过电子邮件发送激活链接/重置令牌和计时器,但似乎都无法更改密码。

请帮助我,或者从网上的工作教程或适用上述要求的解决方案。

在此先感谢 史蒂夫

+1

内change_password.html HTTP ://github.com/cakedc/users使用它或查看代码,如果你想自己做。该插件涵盖了你想要的。 – burzum 2014-09-29 19:26:59

回答

2

下面我写,我写我的项目的一个代码,这可能会帮助你。

1-我创建了一个新表,其中包含每个用户的唯一标记。

表名称: - user_password_resets

列:userclient_id,令牌

2-电子邮件模板名称为: - /webroot/template/change_password.html

public function login_send() { 
     $this->isLoggedIn(); //Check if the user is logged in 
     if($this->request->is('post')) { #if the form is submitted 
     $login = $this->data['User']['login']; 
     $conditions = array('User.login'=>$login); 
     if($this->User->hasAny($conditions)) { 
      $users = $this->User->find('first', array('conditions'=>$conditions)); 
      #Generate the token 
      $token = md5(uniqid(rand(),true)); 
      #Save token and other details in user_password_reset_links table 
      $users = $this->User->find('first', array('conditions'=>array('User.login'=>$login))); 
      $my_name = $users['User']['first_name']; 
      $reset_links = array(); 
      $reset_links['UserPasswordReset']['userclient_id'] = $users['User']['client_id']; 
      $reset_links['UserPasswordReset']['token'] = $token; 

      $conditions = array('UserPasswordReset.userclient_id'=>$users['User']['client_id']); 
      if($this->UserPasswordReset->hasAny($conditions)) { 
       $user_id = $users['User']['client_id']; 
       $this->UserPasswordReset->updateAll(array('UserPasswordReset.token'=>"'$token'"), array("UserPasswordReset.userclient_id"=>"$user_id"));  
      } else { 
       $this->UserPasswordReset->create(); 
       $this->UserPasswordReset->save($reset_links); 
      } 
      $password_reset_link = BASE_URL."users/reset_password/$token"; 

      #Send Welcome Email 
      $mailContent = file_get_contents(BASE_URL . "templates/change_password.html"); 
      $rootlink = BASE_URL; 
      $arrMail = array(
       "{NICK}" => ucfirst($my_name), 
       "{rootlink}" => BASE_URL, 
       "{SITE_TITLE}" => SITE_TITLE, 
       "{PASSWORD_RESET_LINK}"=>$password_reset_link 
       ); 

      $mails = explode(',', $users['User']['email']);  
      $msg = @str_replace(array_keys($arrMail), array_values($arrMail), $mailContent); 
      $data = array(); 
      $data['to'] = @$mails[0]; 
      $data['body'] = $msg; 
      $data['subject'] = SITE_TITLE.'- Reset Password.'; 
      $this->send_mail($data); 

      $this->Session->setFlash('A password reset link has been sent to the email address.', 'default', array('class'=>'successMsg')); 
      $this->redirect(array('controller'=>'users', 'action'=>'login')); 
      exit; 
     } else { 
      $this->Session->setFlash('The Username entered is not registered with Captain Marketing.', 'default', array('class'=>'errorMsg')); 
      $this->redirect(array('controller'=>'users', 'action'=>'login_send')); 
      exit; 
     } 
    } 
    $this->set('title_for_layout', '-Send password reset link'); 
    } 
+0

嗨,感谢Dinesh,我喜欢这个脚本,你的reset_password视图的外观/功能是如何工作的?这是我正在努力的主要位,我喜欢你是如何做到这一点的 – 2014-09-30 09:18:53

+0

我正在生成一个唯一的标记,并将其与user_id一起保存在一个新表中,并向客户端发送重置密码标记电子邮件。当客户端点击重置密码令牌和用户来到我的站点时,我正在检查令牌创建日期(如果未过期),新密码将被保存,否则会通知用户令牌已过期。再试一次 – 2014-09-30 10:19:01

+0

嗯好的,这是有道理的抱歉,我没有正确读取代码:)我会尽快测试。谢谢 – 2014-09-30 11:07:51