User.php
2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace app\modules\admin\models;
use Yii;
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
public $password_repeat;
public static function tableName()
{
return 'user';
}
public function rules()
{
return [
[['username', 'password'], 'required'],
[['password_repeat'], 'required', 'on'=>'save'],
[['password_repeat'], 'password_repeat', 'on'=>'save'],
[['username'], 'is_username', 'on'=>'save'],
];
}
public function attributeLabels()
{
return [
'username'=>'Логин',
'password'=>'Пароль',
'password_repeat'=>'Повторить пароль',
];
}
public function password_repeat($attribute){
if($this->password != $this->password_repeat)
$this->addError('password_repeat','Не правильный повтор пароля.');
}
public function is_username($attribute)
{
if(User::find()
//->where( ['username' => $this->username],['id!='.$_GET['id']] )
->where('username = :username and id != :id', [':username' => $this->username, ':id' => $this->id])
->exists())
$this->addError('username','Такой пользователь уже есть.');
}
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
// установка роли пользователя
$auth = Yii::$app->authManager;
$role = $auth->getRole('admin');
if (!$insert) {
$auth->revokeAll($this->id);
}
$auth->assign($role, $this->id);
}
public function afterDelete(){
parent::afterDelete();
$auth = Yii::$app->authManager;
$auth->revokeAll($this->id);
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne($id);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->authKey;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return $this->password === $password;
}
}