4

I'm building a page where a user can change their password. They must fill two fields and both must match before it will be changed and then they will be redirected to their profile page on success.

So far I have built the following method:

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?'));
        }
    }

}

and this is the form:

<?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(); ?>

So as you can see the plan is to take what it says in password2 and save it in the database password field using the security hashing. But what happens is it creates a new user instead but with blank data... What am I doing wrong here? And how do I compare the two password fields...

4

4 回答 4

1

最可能的问题是您以某种方式将表单中的 id 丢失回控制器操作。如果数据数组 cake 中不存在 id,则假定它必须创建一个条目。

因此调试您发布的数据并确保它没有丢失或更好地在保存之前手动分配 id。

$this->request->data['User']['id'] = $this->Session->read('Auth.User.id');

PS:从您的方法来看,您隐藏的“id”字段始终为空,因为 $user 数据没有从控制器传递下来!

于 2012-04-29T16:57:38.693 回答
1

公共功能恢复(){

    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']);  
    }
}
于 2013-06-01T05:54:01.317 回答
0
$this->request->data['User']['password2'] = AuthComponent::password($this->request->data['User']['password2']);

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

As your form submit with auth user id so, if you execute save() with request data, cake will simply update your record, instead of create due to id field(if any record exists against this idin the table). Above code just update the password2 with the Hash password within the request data.

于 2012-04-30T08:14:19.683 回答
0

您不应该在表单中指定操作编辑吗?

    echo $this->Form->create('User', array('action' => 'edit'));
于 2012-11-29T18:59:55.490 回答