Password edited twice when editing - CakePHP

I have a custom edit view. When people access this view, it has a hashed password in the password block.

If you click "Save", it (obviously) again hashes the password because of this in my user model.

public function beforeSave($options = array()) {
    if(!empty($this->data['User']['password'])) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    }
    return true;
}

But I do not want it to hash twice (because it means the password has changed). I changed the type of editing and added it array('value' => '','autocomplete'=>'off')to the password field. Now, when I save it, it saves an empty row in the database. I thought this was stopping him from doing this with an instruction if(!empty($this->data['User']['password']))in a function beforeSave.

How can I prevent double password hashing?

+5
source share
5 answers

. if beforeSave:

public function beforeSave($options = array()) {
    if(!empty($this->data['User']['password'])) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    }
    return true;
}

public function beforeSave($options = array()) {
    if(!empty($this->data['User']['password'])) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    } else {
        unset($this->data['User']['password']);
    }
    return true;
}
+5

:

, , . ( ), . , xyz, , - , , xyz; , .

, , xyz, html abc; (abc), , .

:

public function beforeSave($options = array()) {

    if (isset($this->data[$this->alias]['password'])) {

        if(isset($this->data[$this->alias]['id'])) {
            $id = $this->data[$this->alias]['id'];
            $user = $this->findById($id);
        } else {
            $id = false;
        }

        if(!$id || $this->data[$this->alias]['password'] != $user['User']['password']) {
            $passwordHasher = new SimplePasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
                $this->data[$this->alias]['password']
            );
        }

    }

    return true;
}

,

+3

(, $this->Form->password('pwd'))

public function beforeSave($options = array()) {
    if (!empty($this->data[$this->alias]['pwd'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['pwd']);
 }
    return true;
}

/ . , .

+1

, . , .

, 3

  • ( , )

:

  • ,
  • hash
  • hash
  • , ...

, , , , .

0
public function beforeSave($options = array()) {
    if(AuthComponent::user('id') == null)
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
}
0

All Articles