1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace modules\users\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\behaviors\TimestampBehavior; |
7
|
|
|
use yii\db\ActiveRecord; |
8
|
|
|
use yii\web\IdentityInterface; |
9
|
|
|
use modules\users\traits\ModuleTrait; |
10
|
|
|
use modules\users\Module; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class IdentityUser |
14
|
|
|
* @package modules\users\models |
15
|
|
|
* |
16
|
|
|
* This is the model class for table "{{%users}}". |
17
|
|
|
* |
18
|
|
|
* @property int $id ID |
19
|
|
|
* @property string $username Username |
20
|
|
|
* @property string $auth_key Authorization Key |
21
|
|
|
* @property string $password_hash Hash Password |
22
|
|
|
* @property string $password_reset_token Password Token |
23
|
|
|
* @property string $email_confirm_token Email Confirm Token |
24
|
|
|
* @property string $email Email |
25
|
|
|
* @property int|string $status Status |
26
|
|
|
* @property int $last_visit Last Visit |
27
|
|
|
* @property int $created_at Created |
28
|
|
|
* @property int $updated_at Updated |
29
|
|
|
* @property string $first_name First Name |
30
|
|
|
* @property string $last_name Last Name |
31
|
|
|
* @property int $registration_type Type Registration |
32
|
|
|
* @property array statusesArray Array statuses |
33
|
|
|
*/ |
34
|
|
|
class IdentityUser extends ActiveRecord implements IdentityInterface |
35
|
|
|
{ |
36
|
|
|
use ModuleTrait; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Length password symbols min |
40
|
|
|
*/ |
41
|
|
|
const LENGTH_STRING_PASSWORD_MIN = 6; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Length password symbols max |
45
|
|
|
*/ |
46
|
|
|
const LENGTH_STRING_PASSWORD_MAX = 16; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Users Statuses |
50
|
|
|
*/ |
51
|
|
|
const STATUS_BLOCKED = 0; |
52
|
|
|
const STATUS_ACTIVE = 1; |
53
|
|
|
const STATUS_WAIT = 2; |
54
|
|
|
const STATUS_DELETED = 3; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Type of registration |
58
|
|
|
*/ |
59
|
|
|
const TYPE_REGISTRATION_SYSTEM = 0; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public static function tableName() |
66
|
|
|
{ |
67
|
|
|
return '{{%users}}'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function behaviors() |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
|
|
'timestamp' => [ |
78
|
|
|
'class' => TimestampBehavior::class, |
79
|
|
|
], |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function rules() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
['username', 'required'], |
91
|
|
|
['username', 'match', 'pattern' => '#^[\w_-]+$#i'], |
92
|
|
|
['username', 'unique', 'targetClass' => self::class, 'message' => Module::t('module', 'This username is already taken.')], |
93
|
|
|
['username', 'string', 'min' => 2, 'max' => 255], |
94
|
|
|
|
95
|
|
|
['email', 'required'], |
96
|
|
|
['email', 'email'], |
97
|
|
|
['email', 'unique', 'targetClass' => self::class, 'message' => Module::t('module', 'This email is already taken.')], |
98
|
|
|
['email', 'string', 'max' => 255], |
99
|
|
|
|
100
|
|
|
['first_name', 'string', 'max' => 45], |
101
|
|
|
['last_name', 'string', 'max' => 45], |
102
|
|
|
|
103
|
|
|
['registration_type', 'safe'], |
104
|
|
|
|
105
|
|
|
['status', 'integer'], |
106
|
|
|
['status', 'default', 'value' => self::STATUS_WAIT], |
107
|
|
|
['status', 'in', 'range' => array_keys(self::getStatusesArray())], |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritdoc |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function attributeLabels() |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
'id' => 'ID', |
119
|
|
|
'created_at' => Module::t('module', 'Created'), |
120
|
|
|
'updated_at' => Module::t('module', 'Updated'), |
121
|
|
|
'last_visit' => Module::t('module', 'Last Visit'), |
122
|
|
|
'username' => Module::t('module', 'Username'), |
123
|
|
|
'email' => Module::t('module', 'Email'), |
124
|
|
|
'auth_key' => Module::t('module', 'Auth Key'), |
125
|
|
|
'status' => Module::t('module', 'Status'), |
126
|
|
|
'first_name' => Module::t('module', 'First Name'), |
127
|
|
|
'last_name' => Module::t('module', 'Last Name'), |
128
|
|
|
'registration_type' => Module::t('module', 'Registration Type'), |
129
|
|
|
'email_confirm_token' => Module::t('module', 'Email Confirm Token'), |
130
|
|
|
'password_reset_token' => Module::t('module', 'Password Reset Token'), |
131
|
|
|
'password_hash' => Module::t('module', 'Password Hash'), |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param int|string $id |
137
|
|
|
* @return IdentityInterface |
138
|
|
|
*/ |
139
|
|
|
public static function findIdentity($id) |
140
|
|
|
{ |
141
|
|
|
/** @var IdentityInterface $result */ |
142
|
|
|
$result = static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); |
143
|
|
|
return $result; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param mixed $token |
148
|
|
|
* @param null|mixed $type |
149
|
|
|
* @return IdentityInterface |
150
|
|
|
*/ |
151
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
152
|
|
|
{ |
153
|
|
|
/** @var IdentityInterface $result */ |
154
|
|
|
$result = static::findOne(['auth_key' => $token, 'status' => self::STATUS_ACTIVE]); |
155
|
|
|
return $result; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return string|integer |
160
|
|
|
*/ |
161
|
|
|
public function getId() |
162
|
|
|
{ |
163
|
|
|
return $this->id; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return string current users auth key |
168
|
|
|
*/ |
169
|
|
|
public function getAuthKey() |
170
|
|
|
{ |
171
|
|
|
return $this->auth_key; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string|mixed $authKey |
176
|
|
|
* @return boolean if auth key is valid for current users |
177
|
|
|
*/ |
178
|
|
|
public function validateAuthKey($authKey) |
179
|
|
|
{ |
180
|
|
|
return $this->getAuthKey() === $authKey; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
public static function getStatusesArray() |
187
|
|
|
{ |
188
|
|
|
return [ |
189
|
|
|
self::STATUS_BLOCKED => Module::t('module', 'Blocked'), |
190
|
|
|
self::STATUS_ACTIVE => Module::t('module', 'Active'), |
191
|
|
|
self::STATUS_WAIT => Module::t('module', 'Wait'), |
192
|
|
|
self::STATUS_DELETED => Module::t('module', 'Deleted'), |
193
|
|
|
]; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|