2012-07-30 108 views
0

我正在使用cakephp 2.1,我試圖通過用戶在忘記密碼請求時將收到的鏈接更改用戶的密碼。Cakephp密碼更改不起作用

鏈接看起來是這樣的

../myApp/users/change_password/1

我傳遞的用戶ID的鏈接。即如上所述1。

的觀點即,change_password.ctp是如下

<?php echo $this->Form->create('User', array('controller' => 'users', 'action' => 'change_password', 'class' => 'well')); ?> 
<?php echo $this->Form->input('User.id',array('value' => $this->params['pass'][0],'type'=>'hidden')); ?> 
<?php echo $this->Form->label('password', 'Password', array('class' => 'control-label')); ?> 
<?php echo $this->Form->password('password', array('class' => 'span3', 'type' => 'password')); ?> 
<?php echo $this->Form->error('password', null , array('wrap' => 'span', 'class' => 'help-inline')); ?> 
<?php echo $this->Form->submit('Change Password', array('class' => 'btn')); ?> 
<?php echo $this->Form->end(); ?> 

而且控制器如下

public function change_password() { 
if($this->request->is('post')) {         
    if ($this->User->save($this->request->data)) { 
     $this->Session->setFlash('Password has been changed.', 'default/flash_success'); 
     $this->redirect(array('controller' => 'movies', 'action' => 'index')); 
     } else { 
    $this->Session->setFlash('Password could not be changed.', 'default/flash_error'); 
    $this->redirect(array('controller' => 'movies', 'action' => 'index')); 
    } 
} 

}

但我不能夠保存密碼。

回答

0

所以,如果我只是在URL中更改用戶ID,我可以更改其他用戶的密碼?

/myApp/users/change_password/2 這根本不安全。你應該重新考慮其他方法。

但是,您的問題的答案是: 您可能無法更改密碼,因爲數據未驗證。 您是否在User.php模型中設置了驗證規則?如果是,則必須在保存用戶之前取消驗證規則。 實施例:unset($this->User->validate['username']);

有關模型驗證的更多信息,請閱讀documentation