Search code examples
cakephpcakephp-2.0editchange-passwordbefore-save

new password not save in database aftter change password cakephp


Sorry before, may I ask, when change password and forgot password, the new password is not fed stored in the database if my beforeSave function like this :

public function beforeSave($options = array()) {
if (!$this->id && !isset($this->data[$this->alias][$this->primaryKey]) && isset($this->data[$this->alias]['password'])) { $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); } else {
unset($this->data[$this->alias]['password']);
}return true;}

But if the function of BeforeSave changed like this

public function beforeSave($options = array()) {  $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);}}

the value of new password is success save to database, but when the user doing edit function and password left empty, password in database has hashing twice please help me, thanks before

oh yaa, this is my changePassword function :

public function account(){
    if(!$this->Session->check('Auth.User')){
        $this->Session->setFlash(__('You must be logged in to view this page.'));
        return $this->redirect(array('action' => 'login'));
    }
    //set user's ID in  model which is needed for validation
    $this->User->id = $this->Auth->user('id');

    //load the user (avoid populating $this->data)
    $current_user = $this->User->findById($this->User->id);
    $this->set('current_user', $current_user);

    $this->User->useValidationRules('ChangePassword');
    $this->User->validate['re_password']['compare']['rule'] = array('equalToField', 'password', false);

    $this->User->set($this->data);
    if(!empty($this->data) && $this->User->validates()){
        $password = $this->data['User']['password'];
        $this->User->saveField('password', $password);

        $this->Session->setFlash('Your password has been updated');
        $this->redirect(array('action' => 'account'));
    }

    $this->layout = 'dashboard_admin';
}

Solution

  • Add new form field in edit form, instead of password, add new_password. It will be hashed only if user put somethin in there...

    public function edit($id = null) {
      $this->User->id = $id;
    
      if (!$this->User->exists()) {
        throw new NotFoundException(__('Invalid user'));
      }
    
      if ($this->request->is('post') || $this->request->is('put')) {
        if ($this->User->save($this->request->data)) {
          $this->Session->setFlash(__('Saved.', true));
    
          $this->redirect(array('action' => 'view', $id));
        } else {
          $this->Session->setFlash(__('Error.', true));
        }
      } else {
        $user = $this->User->read(null, $id);
        $this->request->data = $user;
    
        unset($this->request->data['User']['password']);
        $this->set('user', $user);
      }
    
    
    public function beforeSave($options = array()) {
      if (!empty($this->data['User']['new_password'])) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['new_password']);
    
        unset($this->data['User']['new_password']);
      }
    }