2012-07-30 105 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