Luis Martinez
Luis Martinez

Reputation: 67

CakePHP 3- Validation for changing password don't work

I am making a app with cakephp 3, now i am tring to make a function that allow users change their passwords. The problem is that the validation of the password doesn't work. I don't know if i am doing correctly.

Here is the chage_password.ctp file:

<div class="users form large-9 medium-9 columns">
<?= $this->Form->create() ?>
<fieldset>
    <legend><?= __('Actualice su contraseña') ?></legend>
    <?= $this->Form->input('password1',['type'=>'password' ,'label'=>'Ingrese Contraseña']) ?>
    <?= $this->Form->input('password2',['type' => 'password' , 'label'=>'Reescriba su Contraseña'])?>
</fieldset>
<?= $this->Form->button(__('Agregar')) ?>
<?= $this->Form->end() ?>

Here is the changePassword function in the UsersControler.php:

public function changePassword($id)
{        
    $user_data=$this->Users->get($id);
    if (!empty($this->request->data)) {
        $user = $this->Users->patchEntity($user_data, [
                    'password' => $this->request->data['password1']                    
                    ],
                    ['validate' => 'password']
                );
        $time = Time::now();
        $user->set('fecha_cambio_password',$time);
        if ($this->Users->save($user)) {
            $this->Flash->success('Contraseña Actualizada');
            $this->redirect('/users/login');
        } else {
            debug($user);die;
            $this->Flash->error('No se pudo actualizar la contraseña!');
        }
    }
}

And finally the validation in UsersTable.php:

public function validationPassword(Validator $validator)
{
    $validator
        ->add('password1', [
            'length' => [
                'rule' => ['minLength', 6],
                'message' => 'El largo minimo es 6',
            ]
        ])
        ->add('password1',[
            'match'=>[
                'rule'=> ['compareWith','password2'],
                'message'=>'Los campos no coinciden',
            ]
        ])
        ->notEmpty('password1');
    $validator
        ->add('password2', [
            'length' => [
                'rule' => ['minLength', 6],
                'message' => 'El largo minimo es 6',
            ]
        ])
        ->add('password2',[
            'match'=>[
                'rule'=> ['compareWith','password1'],
                'message'=>'Los campos no coinciden',
            ]
        ])
        ->notEmpty('password2');

        return $validator;
}

Upvotes: 0

Views: 908

Answers (1)

VladoM
VladoM

Reputation: 64

I found the solution!

You need to pass the password fields in the 2nd argument of the patchEntity() method

        $user = $this->Users->patchEntity($user, [
                'old_password'  => $this->request->data['old_password'],
                'password'      => $this->request->data['password1'],
                'password1'     => $this->request->data['password1'],
                'password2'     => $this->request->data['password2']
            ],
            ['validate' => 'password']
        );

In order to check the old password you need to modify your current validator as follows:

public function validationPassword(Validator $validator )
{

    $validator
        ->add('old_password','custom',[
            'rule'=>  function($value, $context){
                $user = $this->get($context['data']['id']);
                if ($user) {
                    if ((new DefaultPasswordHasher)->check($value, $user->password)) {
                        return true;
                    }
                }
                return false;
            },
            'message'=>'The old password is not correct!',
        ])
        ->notEmpty('old_password');

    $validator
        ->add('password1', [
            'length' => [
                'rule' => ['minLength', 6],
                'message' => 'Min value is 6',
            ]
        ])
        ->add('password1',[
            'match'=>[
                'rule'=> ['compareWith','password2'],
                'message'=>'Los campos no coinciden',
            ]
        ])
        ->notEmpty('password1');
    $validator
        ->add('password2', [
            'length' => [
                'rule' => ['minLength', 6],
                'message' => 'El largo minimo es 6',
            ]
        ])
        ->add('password2',[
            'match'=>[
                'rule'=> ['compareWith','password1'],
                'message'=>'Los campos no coinciden',
            ]
        ])
        ->notEmpty('password2');

    return $validator;
}

Cheers!

Upvotes: 4

Related Questions