It is necessary that the admin had the ability to add new users through the admin area.
How, then, to set the password and password_hash ?
It is necessary that the admin had the ability to add new users through the admin area.
How, then, to set the password and password_hash ?
In Yii2, there is a function for this to set a password.
public function setPassword($password) { $this->password_hash = Yii::$app->security->generatePasswordHash($password); } To verify password during authorization
public function validatePassword($password) { return Yii::$app->security->validatePassword($password, $this->password_hash); } Write a console controller to work with the User model.
I created two form models for this (UserCreateForm, UserUpdateForm). That in one that in another, identical fields (attributes) to fill. In the first model, I checked the $ password attribute for filling (rules = [['password', 'required']]). In the second model, I removed the verification of the field with the password for filling and by saving saved the recording of the new password into the model, if the field was not filled (so as not to knock down the old password).
Also in the second model it is necessary to transfer the $ id of the user being edited in order to get his data. This was done through __construct ().
The principle is almost the same as in the SignupForm model, which was created by default in yii2 advanced.
Source: https://ru.stackoverflow.com/questions/454489/
All Articles