Bonjour,

j'ai ajouté des formulaires pour changer le mot de passe dans la méthode edit mais ça ne sauvegarde pas le nouveau mot de passe voici le code que j ai ajouté:

src/controller/usersController.php


function change_password() {
        if (!empty($this->data)) {
            if ($this->User->save($this->data)) {
                $this->Session->setFlash('Password has been changed.');
                // call $this->redirect() here
            } else {
                $this->Session->setFlash('Password could not be changed.');
            }
        } else {
            $this->data = $this->User->findById($this->Auth->user('id'));
        }
    }
    template/users/edit/ctp

   <h1>Modifier vos informations</h1>
<?php

//debug($user);
echo $this->Form->create($user);
echo $this->Form->input('first_name');
echo $this->Form->input('last_name');
echo $this->Form->input('email');
echo $this->Form->input('current_password');
echo $this->Form->input('password1');
echo $this->Form->input('password2');
echo $this->Form->button(__('Maj'));
echo $this->Form->end();
?>

si vous pouvez m'aider je serai reconnaissante

3 réponses


Je ne vois pas de champs "password", tu gère ça au niveau de ton model ?

Houdette
Auteur

oui exactement voici le code :
Model/Table/UsersTable.php

<?
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Auth\DigestAuthenticate;
use Cake\Event\Event;

class UsersTable extends Table
{

    public function validationDefault(Validator $validator)
    {
    return $validator
    ->notEmpty('first_name', "A first_name is necessary")
    ->notEmpty('last_name', 'A last_name is necessary')
    ->notEmpty('email', 'An email is necessary')
    ->notEmpty('password', 'A password is necessary');

    }
    public function beforeSave(Event $event)
    {
        $entity = $event->data['entity'];

        // Make a password for digest auth.
        $entity->digest_hash = DigestAuthenticate::password(
            $entity->email,
            $entity->plain_password,
            env('SERVER_NAME')
        );
        return true;
        if (isset($this->data[$this->alias]['password1'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password1']);
        }
        return true;

    }

    public function checkCurrentPassword($data) {
        $this->id = AuthComponent::user('id');
        $password = $this->field('password');
        return(AuthComponent::password($data['current_password']) == $password);
    }

}

La validation foire selon moi.