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 yii\helpers\Html; |
10
|
|
|
use yii\helpers\ArrayHelper; |
11
|
|
|
use modules\users\traits\ModuleTrait; |
12
|
|
|
use modules\users\Module; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class BaseUser |
16
|
|
|
* @package modules\users\models |
17
|
|
|
* |
18
|
|
|
* This is the model class for table "{{%users}}". |
19
|
|
|
* |
20
|
|
|
* @property int $id ID |
21
|
|
|
* @property string $username Username |
22
|
|
|
* @property string $auth_key Authorization Key |
23
|
|
|
* @property string $password_hash Hash Password |
24
|
|
|
* @property string $password_reset_token Password Token |
25
|
|
|
* @property string $email_confirm_token Email Confirm Token |
26
|
|
|
* @property string $email Email |
27
|
|
|
* @property int|string $status Status |
28
|
|
|
* @property int $last_visit Last Visit |
29
|
|
|
* @property int $created_at Created |
30
|
|
|
* @property int $updated_at Updated |
31
|
|
|
* @property string $first_name First Name |
32
|
|
|
* @property string $last_name Last Name |
33
|
|
|
* @property int $registration_type Type Registration |
34
|
|
|
* @property string statusLabelName Status name in label |
35
|
|
|
* @property array statusesArray Array statuses |
36
|
|
|
* @property string statusName Name status |
37
|
|
|
* @property int|string registrationType Type registered |
38
|
|
|
*/ |
39
|
|
|
class BaseUser extends ActiveRecord implements IdentityInterface |
40
|
|
|
{ |
41
|
|
|
use ModuleTrait; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Length password symbols min |
45
|
|
|
*/ |
46
|
|
|
const LENGTH_STRING_PASSWORD_MIN = 6; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Length password symbols max |
50
|
|
|
*/ |
51
|
|
|
const LENGTH_STRING_PASSWORD_MAX = 16; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Users Statuses |
55
|
|
|
*/ |
56
|
|
|
const STATUS_BLOCKED = 0; |
57
|
|
|
const STATUS_ACTIVE = 1; |
58
|
|
|
const STATUS_WAIT = 2; |
59
|
|
|
const STATUS_DELETED = 3; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Type of registration |
63
|
|
|
*/ |
64
|
|
|
const TYPE_REGISTRATION_SYSTEM = 0; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public static function tableName() |
71
|
|
|
{ |
72
|
|
|
return '{{%users}}'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function behaviors() |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
'timestamp' => [ |
83
|
|
|
'class' => TimestampBehavior::class, |
84
|
|
|
], |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function rules() |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
['username', 'required'], |
96
|
|
|
['username', 'match', 'pattern' => '#^[\w_-]+$#i'], |
97
|
|
|
['username', 'unique', 'targetClass' => self::class, 'message' => Module::t('module', 'This username is already taken.')], |
98
|
|
|
['username', 'string', 'min' => 2, 'max' => 255], |
99
|
|
|
|
100
|
|
|
['email', 'required'], |
101
|
|
|
['email', 'email'], |
102
|
|
|
['email', 'unique', 'targetClass' => self::class, 'message' => Module::t('module', 'This email is already taken.')], |
103
|
|
|
['email', 'string', 'max' => 255], |
104
|
|
|
|
105
|
|
|
['first_name', 'string', 'max' => 45], |
106
|
|
|
['last_name', 'string', 'max' => 45], |
107
|
|
|
|
108
|
|
|
['registration_type', 'safe'], |
109
|
|
|
|
110
|
|
|
['status', 'integer'], |
111
|
|
|
['status', 'default', 'value' => self::STATUS_WAIT], |
112
|
|
|
['status', 'in', 'range' => array_keys(self::getStatusesArray())], |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritdoc |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function attributeLabels() |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
'id' => 'ID', |
124
|
|
|
'created_at' => Module::t('module', 'Created'), |
125
|
|
|
'updated_at' => Module::t('module', 'Updated'), |
126
|
|
|
'last_visit' => Module::t('module', 'Last Visit'), |
127
|
|
|
'username' => Module::t('module', 'Username'), |
128
|
|
|
'email' => Module::t('module', 'Email'), |
129
|
|
|
'auth_key' => Module::t('module', 'Auth Key'), |
130
|
|
|
'status' => Module::t('module', 'Status'), |
131
|
|
|
'first_name' => Module::t('module', 'First Name'), |
132
|
|
|
'last_name' => Module::t('module', 'Last Name'), |
133
|
|
|
'registration_type' => Module::t('module', 'Registration Type'), |
134
|
|
|
'email_confirm_token' => Module::t('module', 'Email Confirm Token'), |
135
|
|
|
'password_reset_token' => Module::t('module', 'Password Reset Token'), |
136
|
|
|
'password_hash' => Module::t('module', 'Password Hash'), |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param int|string $id |
142
|
|
|
* @return IdentityInterface |
143
|
|
|
*/ |
144
|
|
|
public static function findIdentity($id) |
145
|
|
|
{ |
146
|
|
|
/** @var IdentityInterface $result */ |
147
|
|
|
$result = static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); |
148
|
|
|
return $result; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param mixed $token |
153
|
|
|
* @param null|mixed $type |
154
|
|
|
* @return IdentityInterface |
155
|
|
|
*/ |
156
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
157
|
|
|
{ |
158
|
|
|
/** @var IdentityInterface $result */ |
159
|
|
|
$result = static::findOne(['auth_key' => $token, 'status' => self::STATUS_ACTIVE]); |
160
|
|
|
return $result; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return string|integer |
165
|
|
|
*/ |
166
|
|
|
public function getId() |
167
|
|
|
{ |
168
|
|
|
return $this->id; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return string current users auth key |
173
|
|
|
*/ |
174
|
|
|
public function getAuthKey() |
175
|
|
|
{ |
176
|
|
|
return $this->auth_key; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string|mixed $authKey |
181
|
|
|
* @return boolean if auth key is valid for current users |
182
|
|
|
*/ |
183
|
|
|
public function validateAuthKey($authKey) |
184
|
|
|
{ |
185
|
|
|
return $this->getAuthKey() === $authKey; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Finds users by username |
190
|
|
|
* |
191
|
|
|
* @param string $username |
192
|
|
|
* @return static|null |
193
|
|
|
*/ |
194
|
|
|
public static function findByUsername($username) |
195
|
|
|
{ |
196
|
|
|
return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Generates "remember me" authentication key |
201
|
|
|
*/ |
202
|
|
|
public function generateAuthKey() |
203
|
|
|
{ |
204
|
|
|
$this->auth_key = Yii::$app->security->generateRandomString(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Actions before saving |
209
|
|
|
* |
210
|
|
|
* @param bool $insert |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
|
|
public function beforeSave($insert) |
214
|
|
|
{ |
215
|
|
|
if (parent::beforeSave($insert)) { |
216
|
|
|
if ($insert) { |
217
|
|
|
$this->generateAuthKey(); |
218
|
|
|
} |
219
|
|
|
return true; |
220
|
|
|
} |
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
public static function getStatusesArray() |
228
|
|
|
{ |
229
|
|
|
return [ |
230
|
|
|
self::STATUS_BLOCKED => Module::t('module', 'Blocked'), |
231
|
|
|
self::STATUS_ACTIVE => Module::t('module', 'Active'), |
232
|
|
|
self::STATUS_WAIT => Module::t('module', 'Wait'), |
233
|
|
|
self::STATUS_DELETED => Module::t('module', 'Deleted'), |
234
|
|
|
]; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return mixed |
239
|
|
|
*/ |
240
|
|
|
public function getStatusName() |
241
|
|
|
{ |
242
|
|
|
return ArrayHelper::getValue(self::getStatusesArray(), $this->status); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Return <span class="label label-success">Active</span> |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
public function getStatusLabelName() |
250
|
|
|
{ |
251
|
|
|
$name = ArrayHelper::getValue(self::getLabelsArray(), $this->status); |
252
|
|
|
return Html::tag('span', $this->getStatusName(), ['class' => 'label label-' . $name]); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @return array |
257
|
|
|
*/ |
258
|
|
|
public static function getLabelsArray() |
259
|
|
|
{ |
260
|
|
|
return [ |
261
|
|
|
self::STATUS_BLOCKED => 'default', |
262
|
|
|
self::STATUS_ACTIVE => 'success', |
263
|
|
|
self::STATUS_WAIT => 'warning', |
264
|
|
|
self::STATUS_DELETED => 'danger', |
265
|
|
|
]; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Type of registration |
270
|
|
|
* How the user is created |
271
|
|
|
* If the system registration type is registered by itself, |
272
|
|
|
* if it is created from the admin area, |
273
|
|
|
* then the login type that created the account |
274
|
|
|
* |
275
|
|
|
* @return mixed|string |
276
|
|
|
*/ |
277
|
|
|
public function getRegistrationType() |
278
|
|
|
{ |
279
|
|
|
if ($this->registration_type > 0) { |
280
|
|
|
if (($model = User::findOne($this->registration_type)) !== null) { |
281
|
|
|
return $model->username; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
return $this->getRegistrationTypeName(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Returns the registration type string |
289
|
|
|
* @return mixed |
290
|
|
|
*/ |
291
|
|
|
public function getRegistrationTypeName() |
292
|
|
|
{ |
293
|
|
|
return ArrayHelper::getValue(self::getRegistrationTypesArray(), $this->registration_type); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Returns an array of log types |
298
|
|
|
* @return array |
299
|
|
|
*/ |
300
|
|
|
public static function getRegistrationTypesArray() |
301
|
|
|
{ |
302
|
|
|
return [ |
303
|
|
|
self::TYPE_REGISTRATION_SYSTEM => Module::t('module', 'System'), |
304
|
|
|
]; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|