fix user update screen in backend

This commit is contained in:
Schneider Roland 2023-01-02 16:36:36 +01:00
parent ccc50457d3
commit c191ce6c7d

View File

@ -11,7 +11,7 @@ class UserUpdate extends User {
public $selected_accounts = []; public $selected_accounts = [];
public $role; public $role;
/** /**
* @inheritdoc * @inheritdoc
* @formatter:off * @formatter:off
@ -21,10 +21,12 @@ class UserUpdate extends User {
return [ return [
[['username','email'], 'required' ], [['username','email'], 'required' ],
['email' ,'email' ], ['email' ,'email' ],
['email' ,'unique' , 'targetClass' => User::className(), 'targetAttribute' => 'email'], // ['email' ,'unique' , 'targetClass' => User::className(), 'targetAttribute' => 'email'],
['username' ,'unique', 'targetClass' => User::className(), 'targetAttribute' => 'username'], // ['username' ,'unique', 'targetClass' => User::className(), 'targetAttribute' => 'username'],
[['password_plain' ,'password_repeat'] ,'string','min' =>6 ], [['password_plain' ,'password_repeat'] ,'string','min' =>6 ],
[['password_repeat'] ,'validatePasswordRepeat' ], [['password_repeat'] ,'validatePasswordRepeat' ],
[['username'] ,'validateUsername' ],
[['email'] ,'validateEmail' ],
['selected_accounts',function ($attribute, $params) { ['selected_accounts',function ($attribute, $params) {
if (!is_array($this->$attribute)) { if (!is_array($this->$attribute)) {
$this->addError($attribute, 'Invalid array'); $this->addError($attribute, 'Invalid array');
@ -33,13 +35,13 @@ class UserUpdate extends User {
], ],
[['role'], 'required'], [['role'], 'required'],
[['role'], 'string', 'max' => 20], [['role'], 'string', 'max' => 20],
['status', 'default', 'value' => self::STATUS_ACTIVE], ['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]], ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
]; ];
} }
/** /**
* @formatter:on * @formatter:on
*/ */
@ -52,15 +54,37 @@ class UserUpdate extends User {
} }
} }
} }
public function validateEmail($attribute, $params) {
if (! $this->hasErrors ()) {
if ( !empty($this->email) ){
$user = User::findOne(['email' => $this->email]);
if ( isset($user) && $user->id != $this->id){
$this->addError ( $attribute, "Az email már használatban van (".$user->username.")");
}
}
}
}
public function validateUsername($attribute, $params) {
if (! $this->hasErrors ()) {
if ( !empty($this->email) ){
$user = User::findOne(['username' => $this->username]);
if ( isset($user) && $user->id != $this->id){
$this->addError ( $attribute, "A felhasználónév már használatban van (".$user->username.")");
}
}
}
}
public function attributeLabels() { public function attributeLabels() {
return [ return [
'status' => 'Státusz', 'status' => 'Státusz',
'email' => 'E-mail', 'email' => 'E-mail',
'username' => 'Felhasználónév', 'username' => 'Felhasználónév',
'created_at' => 'Létrehozás dátuma', 'created_at' => 'Létrehozás dátuma',
'password_plain' => Yii::t ( 'app', 'Jelszó' ), 'password_plain' => Yii::t ( 'app', 'Jelszó' ),
'password_repeat' => Yii::t ( 'app', 'Jelszó újra' ) 'password_repeat' => Yii::t ( 'app', 'Jelszó újra' )
] ]
; ;
} }
@ -78,7 +102,7 @@ class UserUpdate extends User {
} }
} }
public function afterSave($insert, $changedAttributes){ public function afterSave($insert, $changedAttributes){
parent::afterSave($insert, $changedAttributes); parent::afterSave($insert, $changedAttributes);
$am = Yii::$app->authManager; $am = Yii::$app->authManager;
$am->revokeAll($this->id); $am->revokeAll($this->id);
$role = $am->getRole($this->role); $role = $am->getRole($this->role);