在CakePHP 2.1中更改用户的密码
我正在创建一个用户可以更改密码的页面。他们必须填写两个字段,两个字段必须匹配才能更改,然后在成功时重定向到他们的个人资料页面 到目前为止,我已经构建了以下方法:在CakePHP 2.1中更改用户的密码,php,cakephp,Php,Cakephp,我正在创建一个用户可以更改密码的页面。他们必须填写两个字段,两个字段必须匹配才能更改,然后在成功时重定向到他们的个人资料页面 到目前为止,我已经构建了以下方法: public function changePassword() { $user = $this->User->find('first', array( 'conditions' => array( 'User.id' => $t
public function changePassword()
{
$user = $this->User->find('first', array(
'conditions' => array(
'User.id' => $this->Auth->user('id'))
));
if ($this->request->is('post') || $this->request->is('put'))
{
if ($this->User->save($this->request->data))
{
$this->User->saveField('password', AuthComponent::password($this->request->data['User']['password2']));
$this->Session->setFlash(__('Your password has been changed!'));
$this->redirect(array('controller'=>'profiles','action'=>'view','userName'=>$user['User']['username']));
}
else
{
$this->Session->setFlash(__('Whoops! Something went wrong... try again?'));
}
}
}
以下是表格:
<?php echo $this->Form->create(); ?>
<?php echo $this->Form->input('id',array('type'=>'hidden')); ?>
<?php echo $this->Form->input('password1',array('type'=>'text','label'=>array('text'=>'Enter your new password'))); ?>
<?php echo $this->Form->input('password2',array('type'=>'text','label'=>array('text'=>'Confirm your new password'))); ?>
<button type="submit">Save</button>
<?php echo $this->Form->end(); ?>
拯救
如您所见,计划是采用password2中的内容,并使用安全哈希将其保存在数据库密码字段中。但实际情况是,它创建了一个新用户,但数据为空。。。我做错了什么?我如何比较这两个密码字段…最可能的问题是您不知何故丢失了表单中返回控制器操作的id。 如果数据数组中没有id,则假定必须创建一个条目 因此,请调试发布的数据,并确保它没有丢失,或者最好在保存之前手动分配id
$this->request->data['User']['id'] = $this->Session->read('Auth.User.id');
PS:通过查看您的方法,您的隐藏“id”字段始终保持为空,因为$user数据没有从控制器传递下来
$this->request->data['User']['password2'] = AuthComponent::password($this->request->data['User']['password2']);
$this->User->save($this->request->data);
当您的表单使用auth user
id
提交时,如果您使用请求数据执行save()
,cake将只更新您的记录,而不是根据id
字段创建记录(如果表中存在与此id
对应的任何记录)。上面的代码只是使用请求数据中的哈希密码更新密码2。是否应该在表单中指定编辑操作
echo $this->Form->create('User', array('action' => 'edit'));
公共职能恢复()
{
你可以让一个行为来解决这个问题:-或者至少利用提供的一些功能来为你自己的方法找到合适的解决方案。为什么我的代码会创建一个新用户?用于与密码字段进行比较。还可以使用fieldList来白名单或更好的安全组件,以避免对表单进行任何修改,从而导致这是安全隐患。
if ($this->request->is('post') )
{
$this->loadModel('AclManagement.User');
$passwords = AuthComponent::password($this->data['User']['password']);
$this->User->query("UPDATE users SET password = '".$passwords."' WHERE password_change = '".$this->request->data['User']['id']."' ");
$this->Session->setFlash(__('Password Saved Sucessfully'), 'success');
$this->redirect(array('action' => 'login'));
}
else
{
$this->set('resettoken', $_REQUEST['id']);
}
}