Informer
Informer

Reputation: 205

Hashed password overwriting when updating data

Did you workin with user data when u extend Your class from ActiveRecord? I have problem when i want to update user. There is PasswordHash attribute. I use function beforeSave where i hashed password on create but i dont know that i should use this in update action?

public function beforeSave($insert)
{
    if (parent::beforeSave($insert)) {
        if($this->isNewRecord) {
            $this->created_at = time();
            $this->status = self::DEFAULT_STATUS;
            $this->generateAuthKey();
            $this->setPassword($this->PasswordHash);
            $this->RulesAccept=1;
            return true;
        } else {
            $this->setPassword($this->PasswordHash);
            return true;
        }
    } 
}

When I update user in input i can see dotted at hashed password but when i change password it not hashed this password. I modified my code but the password is constantly overwritten. How I should work with passwordInput and password hashed in ActiveRecord?

Upvotes: 0

Views: 45

Answers (1)

Shaig Khaligli
Shaig Khaligli

Reputation: 5505

When I need to hash password in Yii2 I use this code:

 $password="YOUR PASSWORD";
 $hash = Yii::$app->getSecurity()->generatePasswordHash($password);
 die($hash);

Upvotes: 0

Related Questions