2011-10-27 92 views
0

我在CakePHP 1.3應用程序中具有以下功能,它需要一個令牌並允許用戶更改其密碼。一切似乎工作正常,但密碼實際上並沒有改變:/任何想法是什麼問題?CakePHP密碼重置不更改密碼

function admin_changepassword ($token = null) 
    { 
     // If has a token or form has been submitted 
     if (!empty($token) || !(empty($this->data))) 
     { 
      $user = $this->User->find('first',array("MD5(User.email + '".Configure::read('Security.salt')."')"=>$token)); 

      if (empty($user)) 
      { 
       $this->redirect(array('admin'=>false,'controller'=>'pages','action'=>'display','home')); 
       $this->Session->setFlash('Invalid token'); 
      } 
      else 
      { 
       $this->set('user',$user); 
       if (!empty($this->data['User']['password'])) 
       { 
        $user['User']['password'] = $this->data['User']['password']; 
        $this->User->save($this->data); 
        $this->Session->setFlash('Your password has been changed! Please log in.'); 
        $this->redirect(array('admin'=>true,'controller' => 'users', 'action' => 'login')); 
       } 
      } 
     } 
     else 
     { 
      $this->redirect(array('admin'=>false,'controller'=>'home','action'=>'display','home')); 
      $this->Session->setFlash('No token'); 
     } 
    } 
+0

你問你的數據庫湊每個用戶的每一個密碼與查詢,潛在的一個巨大的無用的動作比較。 – deceze

+0

你能否進一步解釋這一點,因爲我不明白,並且密碼沒有被更改的原因? – Cameron

+0

您正在查詢WHERE MD5(密碼...)='somestring''。這意味着數據庫需要在每一行執行'MD5(password ...)',以便將它與'somestring'進行比較。無論如何,這個標記應該是什麼?我不認爲這是你的問題。 *你的問題究竟是什麼?它有什麼作用? – deceze

回答

1

就我個人而言,我會這樣做;但我假設你的用戶實際上是使用auth組件或類似組件登錄的。未測試;但邏輯看起來應該對我有用。

<?php 
function admin_changepassword ($token = NULL) { 
    // bail out early if there is no token set, and always set the flash before redirecting. 
    if($token==NULL) { 
     $this->Session->setFlash('No token'); 
     $this->redirect(array('admin'=>false,'controller'=>'home','action'=>'display','home')); 
    } 

    // this is an admin action; the user already be logged in right? 
    $this->User->id = $this->Auth->user('id'); 
    $user_password = $this->User->field('password'); 

    // does the token match the hashed password, and did they enter a new password? 
    if($token==md5($user_password . Configure::read('Security.salt')) && !empty($this->data['User']['password'])) { 
     $this->User->saveField('password', $this->data['User']['password']);  
     $this->Session->setFlash('Your password has been changed! Please log in.'); 
     $this->redirect(array('admin'=>true,'controller' => 'users', 'action' => 'login')); 
    } 

    // somethings gone wrong/password was not updated 
    $this->Session->setFlash('Your password was not changed.'); 
    $this->redirect(array('admin'=>false,'controller'=>'home','action'=>'display','home')); 
} 
+0

沒有用戶不會因爲他們忘記密碼而更改密碼而無法登錄!用戶是管理員! – Cameron

+0

但是我的代碼肯定知道我是什麼用戶,因爲我在視圖上顯示了一個Hello電子郵件地址消息,所以它知道它只是不會更改密碼:/ – Cameron

+0

也''更改密碼'與'忘記密碼'不同。如果僅出於語義原因,可能想要重新命名您的方法。更改意味着用戶已登錄並且希望將其密碼更改爲新的內容;因此我假設你的用戶已經登錄。 – Ross

1

確定一些測試後,您的問題是我最初在我的評論中建議的。

您沒有設置用戶的id,所以:

$this->User->save($this->data);

沒有更新的密碼,它是增加一個新行到數據庫。

您需要指定要更新的用戶的ID。

$user = $this->User->find('first',array("MD5(User.email + 
          '".Configure::read('Security.salt')."')"=>$token)); 

// this line is redundant 
$user['User']['password'] = $this->data['User']['password']; 


$this->User->id = $user['User']['id']; // set user id 
$this->User->save($this->data); // save it 

如果您檢查users表,我懷疑你會發現有很多與「變」的密碼爲空的記錄。我的測試與蛋糕手冊一致。

0

這是一個古老的線程,但它可能會幫助別人

function changePwd(){ 
    $this->User->id = $this->data->['User']['id']; //assuming this is set 
    //check if the password fields are empty 
    //check if the password fields match (password and confirm password one) 

    //convert password 
    $this->request->data['User']['password'] = $this->Auth->password($this->data['User']['password']); 

    //saveField worked for me 
    if($this->User->saveField('password',$this->request->data['User']['password'])){ 
     $this->Session->setFlash('Password changed successfully.','flashSuccess'); 
    }else{ .... 
}