2014-09-29 153 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