77 lines
2.0 KiB
PHP
77 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace backend\models;
|
|
|
|
use Yii;
|
|
use common\models\User;
|
|
|
|
class UserUpdate extends User {
|
|
public $password_plain;
|
|
public $password_repeat;
|
|
public $selected_accounts = [];
|
|
|
|
/**
|
|
* @inheritdoc
|
|
* @formatter:off
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['username','email'], 'required' ],
|
|
['email' ,'email' ],
|
|
['email' ,'unique' , 'targetClass' => User::className(), 'targetAttribute' => 'email'],
|
|
['username' ,'unique', 'targetClass' => User::className(), 'targetAttribute' => 'username'],
|
|
[['password_plain' ,'password_repeat'] ,'string','min' =>6 ],
|
|
[['password_repeat'] ,'validatePasswordRepeat' ],
|
|
['selected_accounts',function ($attribute, $params) {
|
|
if (!is_array($this->$attribute)) {
|
|
$this->addError($attribute, 'Invalid array');
|
|
}
|
|
}
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @formatter:on
|
|
*/
|
|
public function validatePasswordRepeat($attribute, $params) {
|
|
if (! $this->hasErrors ()) {
|
|
if ( !empty($this->password_plain) || !empty($this->password_repeat) ){
|
|
if ($this->password_plain != $this->password_repeat) {
|
|
$this->addError ( $attribute, Yii::t ( 'app', 'Jelszó és jelszó újra nem egyezik!' ) );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public function attributeLabels() {
|
|
return [
|
|
|
|
'email' => 'E-mail',
|
|
'username' => 'Felhasználónév',
|
|
'created_at' => 'Létrehozás dátuma',
|
|
'password_plain' => Yii::t ( 'app', 'Jelszó' ),
|
|
'password_repeat' => Yii::t ( 'app', 'Jelszó újra' )
|
|
]
|
|
;
|
|
}
|
|
public function beforeSave($insert) {
|
|
if (parent::beforeSave ( $insert )) {
|
|
if (! $insert) {
|
|
if ( !empty( $this->password_plain ) ) {
|
|
$this->setPassword($this->password_plain);
|
|
return true;
|
|
}
|
|
}
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
public function afterSave($insert, $changedAttributes) {
|
|
parent::afterSave ( $insert, $changedAttributes );
|
|
// $am = Yii::$app->authManager;
|
|
// $role = $am->getRole('admin');
|
|
// Yii::$app->authManager->assign($role, $this->id);
|
|
}
|
|
} |