2012-07-22 50 views
0

我有一個用戶配置文件更新頁面,該頁面預先使用從$ this-> request-> data收集的用戶信息填充文本字段。一切正常,除了密碼字段預先填充了數據庫中似乎是加密密碼的內容,所以在保存時,驗證失敗,因爲密碼超過了允許的字符數,即使它存儲了,它也會然後用加密的相同密碼替換用戶的實際密碼,如果有意義的話。cakePHP預先填充密碼字段並更新

什麼是最好的解決方法?

控制器:

public function profile() { 
    if($this->request->is('post') || $this->request->is('put')) { 
     if($this->Auth->user("id") == $this->request->data['User']['id']) { 
      $this->request->data['User']['user_status_id'] = $this->Auth->user("user_status_id"); 
      $this->request->data['User']['user_type_id'] = $this->Auth->user("user_type_id"); 
      if($this->User->save($this->request->data)) { 
       $this->Session->setFlash('Your profile has been updated','default',array('class'=>'success')); 
      } else { 
       $this->Session->setFlash("An error has occured updating your profile."); 
      } 
     } else { 
      $this->Session->setFlash("Unable to save this result as there is an ID mismatch."); 
     } 
    } else { 
     $this->request->data = $this->User->read(null,$this->Auth->user("id")); 
    } 
} 

查看:

<h1>Profile</h1> 
<h3>Update your profile</h3> 
<div class="left"> 
<div class="formContainer"> 
<?php 
    echo $this->Form->create('User'); 
     echo $this->Form->input('username',array("id" => "profileUsername")); 
     echo $this->Form->input('password',array("id" => "profilePassword")); 
     echo $this->Form->input('company'); 
     echo $this->Form->input('first_name'); 
     echo $this->Form->input('last_name'); 
     echo $this->Form->input('telephone'); 
     echo $this->Form->input('fax'); 
     echo $this->Form->input('id',array('type' => 'hidden')); 
    echo $this->Form->end(__('Update')); 
    ?> 
</div> 
</div> 

回答

-1
$this->Form->input('password',array('value'=>null)); 
2

你的問題是,如果你存儲你的密碼,你應該(用單向散列),你無法將其反轉爲顯示純文本版本,因此您可以簡單地將密碼數據設置爲空(將密碼字段留空):

$this->request->data['User']['password'] = NULL; 

然後你有兩種選擇:

  1. 要求用戶的密碼更新他們的數據(一個不錯的安全措施)

  2. 只有更新的用戶密碼,當用戶已經進入了一個新的密碼

第二個選項可能會破壞您的驗證,如果您已設置密碼驗證規則allowEmpty => false,在這情況下,你可以使用多個驗證組,您可以通過以下任一(在CakePHP的2.x的所有工作)multivalidateable behaviour做,multiple validation sets

0

閱讀和使用本行爲: http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp

密碼是一個由於哈希的技術細節,通票。

基本上,你永遠不會將密碼散列傳遞迴視圖。始終顯示空白字段(就像您應該在www中幾乎所有其他網站上看到的一樣)。

然後你忽略併發布空的字段。只有當密碼字段剛剛設置,你實際上觸發驗證。

0
<?php echo $this->Form->input('password', array('label' => false,'type'=>'password','required'=>'false')); ?> 

輸出

<input name="data[User][password]" type="password" id="UserPassword"> 

要小心!僅在編輯部分使用此項