1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models\entity; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\NotSupportedException; |
7
|
|
|
use yii\db\ActiveRecord; |
8
|
|
|
use yii\web\IdentityInterface; |
9
|
|
|
use yii\behaviors\TimestampBehavior; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This is the model class for table "{{%user}}". |
13
|
|
|
* |
14
|
|
|
* @property integer $id |
15
|
|
|
* @property string $username |
16
|
|
|
* @property string $email |
17
|
|
|
* @property string $password |
18
|
|
|
* @property string $password_reset_token |
19
|
|
|
* @property string $email_confirm_token |
20
|
|
|
* @property string $auth_key |
21
|
|
|
* @property string $date_confirm |
22
|
|
|
* @property string $date_create |
23
|
|
|
* @property string $date_update |
24
|
|
|
* @property string $date_login |
25
|
|
|
* @property integer $ip |
26
|
|
|
* @property string $role_name |
27
|
|
|
* @property integer $status |
28
|
|
|
* |
29
|
|
|
* @property AuthItem $role |
30
|
|
|
* @property UserProfile $profile |
31
|
|
|
* @property UserProvider[] $providers |
32
|
|
|
*/ |
33
|
|
|
class User extends \yii\db\ActiveRecord implements IdentityInterface |
34
|
|
|
{ |
35
|
|
|
const STATUS_DELETED = 0; |
36
|
|
|
const STATUS_ACTIVE = 1; |
37
|
|
|
const STATUS_BLOCKED = 2; |
38
|
|
|
|
39
|
|
|
const ROLE_SUPERUSER = 'SuperUser'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
public $passwordNew; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
111 |
|
public static function tableName() |
50
|
|
|
{ |
51
|
111 |
|
return '{{%user}}'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
43 |
|
public function attributeLabels() |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
43 |
|
'id' => Yii::t('app', 'ID'), |
61
|
43 |
|
'username' => Yii::t('app', 'Username'), |
62
|
43 |
|
'email' => Yii::t('app', 'Email'), |
63
|
43 |
|
'password' => Yii::t('app', 'Password'), |
64
|
43 |
|
'date_create' => Yii::t('app', 'Date create'), |
65
|
43 |
|
'date_update' => Yii::t('app', 'Date update'), |
66
|
43 |
|
'date_login' => Yii::t('app', 'Last login'), |
67
|
43 |
|
'ip' => Yii::t('app', 'IP'), |
68
|
43 |
|
'role_name' => Yii::t('app', 'Role'), |
69
|
43 |
|
'status' => Yii::t('app', 'Status'), |
70
|
|
|
|
71
|
43 |
|
'passwordNew' => Yii::t('app', 'New password'), |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
74 |
|
public function behaviors() |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
[ |
82
|
74 |
|
'class' => TimestampBehavior::class, |
83
|
74 |
|
'createdAtAttribute' => 'date_create', |
84
|
74 |
|
'updatedAtAttribute' => 'date_update', |
85
|
74 |
|
'value' => new \yii\db\Expression('NOW()'), |
86
|
|
|
], |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
* @codeCoverageIgnore |
93
|
|
|
*/ |
94
|
|
|
public function events() |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete', |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
9 |
|
public function transactions() |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
9 |
|
'create' => self::OP_ALL, |
105
|
9 |
|
'update' => self::OP_ALL, |
106
|
9 |
|
'delete' => self::OP_ALL, |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return \yii\db\ActiveQuery |
112
|
|
|
*/ |
113
|
9 |
|
public function getProfile() |
114
|
|
|
{ |
115
|
9 |
|
return $this->hasOne(UserProfile::class, ['user_id' => 'id']); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param array $attributes |
120
|
|
|
*/ |
121
|
3 |
|
public function setProfile($attributes = []) |
122
|
|
|
{ |
123
|
3 |
|
return $this->populateRelation('profile', new UserProfile($attributes)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return \yii\db\ActiveQuery |
128
|
|
|
*/ |
129
|
10 |
|
public function getProviders() |
130
|
|
|
{ |
131
|
10 |
|
return $this->hasMany(UserProvider::class, ['user_id' => 'id']); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param array $attributes |
136
|
|
|
*/ |
137
|
|
|
public function setProviders($attributes = []) |
138
|
|
|
{ |
139
|
|
|
return $this->populateRelation('providers', [new UserProvider($attributes)]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return \yii\db\ActiveQuery |
144
|
|
|
*/ |
145
|
10 |
|
public function getRole() |
146
|
|
|
{ |
147
|
10 |
|
return $this->hasOne(AuthItem::class, ['name' => 'role_name']); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @inheritdoc |
152
|
|
|
* @return \query\UserQuery the active query used by this AR class. |
153
|
|
|
*/ |
154
|
80 |
|
public static function find() |
155
|
|
|
{ |
156
|
80 |
|
return new \app\models\query\UserQuery(get_called_class()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @inheritdoc |
161
|
|
|
*/ |
162
|
9 |
|
public function beforeSave($insert) |
163
|
|
|
{ |
164
|
9 |
|
if (parent::beforeSave($insert)) { |
165
|
9 |
|
if ($insert) { |
166
|
3 |
|
$this->generateAuthKey(); |
167
|
3 |
|
if (!Yii::$app instanceof \yii\console\Application) { |
168
|
3 |
|
$this->ip = ip2long(Yii::$app->request->getUserIP()); |
169
|
|
|
} |
170
|
|
|
|
171
|
3 |
|
if ($this->profile === null) { |
172
|
|
|
$this->setProfile(); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
9 |
|
if (!empty($this->passwordNew)) { |
177
|
1 |
|
$this->setPassword($this->passwordNew); |
178
|
|
|
} |
179
|
|
|
|
180
|
9 |
|
return true; |
181
|
|
|
} // @codeCoverageIgnore |
182
|
|
|
|
183
|
|
|
return false; // @codeCoverageIgnore |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritdoc |
188
|
|
|
* @property \entity\UserProfile $profile |
189
|
|
|
* @property \entity\UserProvider $providers |
190
|
|
|
*/ |
191
|
9 |
|
public function afterSave($insert, $changedAttributes) |
192
|
|
|
{ |
193
|
9 |
|
parent::afterSave($insert, $changedAttributes); |
194
|
|
|
|
195
|
9 |
|
if ($this->profile !== null) { |
196
|
7 |
|
$this->link('profile', $this->profile); |
197
|
|
|
} |
198
|
|
|
|
199
|
9 |
|
if ($this->providers !== null && count($this->providers)) { |
200
|
|
|
foreach ($this->providers as $provider) { |
201
|
|
|
if ($provider) { |
202
|
|
|
$this->link('providers', $provider); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
9 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @inheritdoc |
210
|
|
|
* @param boolean $runValidation |
211
|
|
|
* @param array $attributeNames |
212
|
|
|
* @return boolean |
213
|
|
|
*/ |
214
|
|
|
public function save($runValidation = true, $attributeNames = null) |
215
|
|
|
{ |
216
|
9 |
|
return $this->getDb()->transaction(function () use ($runValidation, $attributeNames) { |
217
|
9 |
|
return parent::save($runValidation, $attributeNames); |
218
|
9 |
|
}); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get all statuses |
223
|
|
|
* |
224
|
|
|
* @param string[] |
225
|
|
|
*/ |
226
|
6 |
|
public static function getStatuses(): array |
227
|
|
|
{ |
228
|
|
|
return [ |
229
|
6 |
|
self::STATUS_DELETED => Yii::t('app', 'Deleted'), |
230
|
6 |
|
self::STATUS_BLOCKED => Yii::t('app', 'Locked'), |
231
|
6 |
|
self::STATUS_ACTIVE => Yii::t('app', 'Active'), |
232
|
|
|
]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Get statuse name |
237
|
|
|
* |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
6 |
|
public function getStatusName(): string |
241
|
|
|
{ |
242
|
6 |
|
$statuses = self::getStatuses(); |
243
|
6 |
|
return isset($statuses[$this->status]) ? $statuses[$this->status] : ''; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Is it deleted? |
248
|
|
|
* |
249
|
|
|
* @param bool |
250
|
|
|
*/ |
251
|
|
|
public function isDeleted(): bool |
252
|
|
|
{ |
253
|
|
|
return $this->status == self::STATUS_DELETED; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Is it blocked? |
258
|
|
|
* |
259
|
|
|
* @param bool |
260
|
|
|
*/ |
261
|
6 |
|
public function isBlocked(): bool |
262
|
|
|
{ |
263
|
6 |
|
return $this->status == self::STATUS_BLOCKED; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Is it active? |
268
|
|
|
* |
269
|
|
|
* @param bool |
270
|
|
|
*/ |
271
|
30 |
|
public function isActive(): bool |
272
|
|
|
{ |
273
|
30 |
|
return $this->status == self::STATUS_ACTIVE; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Is it confirmed? |
278
|
|
|
* |
279
|
|
|
* @param bool |
280
|
|
|
*/ |
281
|
10 |
|
public function isConfirmed(): bool |
282
|
|
|
{ |
283
|
10 |
|
return strtotime($this->date_confirm) > 0; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Is SuperUser? |
288
|
|
|
* |
289
|
|
|
* @return bool |
290
|
|
|
*/ |
291
|
15 |
|
public function isSuperUser(): bool |
292
|
|
|
{ |
293
|
15 |
|
return $this->role_name === self::ROLE_SUPERUSER; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Set confirmed |
298
|
|
|
*/ |
299
|
2 |
|
public function setConfirmed(): void |
300
|
|
|
{ |
301
|
2 |
|
$this->email_confirm_token = null; |
302
|
2 |
|
$this->date_confirm = new \yii\db\Expression('NOW()'); |
303
|
2 |
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get status description |
307
|
|
|
* |
308
|
|
|
* @return string |
309
|
|
|
*/ |
310
|
8 |
|
public function getStatusDescription(): string |
311
|
|
|
{ |
312
|
8 |
|
if ($this->status == self::STATUS_BLOCKED) { |
313
|
4 |
|
return Yii::t('app', 'Your account has been suspended'); |
314
|
4 |
|
} elseif ($this->status == self::STATUS_DELETED) { |
315
|
4 |
|
return Yii::t('app', 'Your account has been deleted'); |
316
|
|
|
} |
317
|
|
|
return Yii::t('app', 'Your account is activated'); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @inheritdoc |
322
|
|
|
*/ |
323
|
28 |
|
public function getId() |
324
|
|
|
{ |
325
|
28 |
|
return $this->id; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @inheritdoc |
330
|
|
|
*/ |
331
|
|
|
public function getAuthKey() |
332
|
|
|
{ |
333
|
|
|
return $this->auth_key; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @inheritdoc |
338
|
|
|
*/ |
339
|
|
|
public function validateAuthKey($authKey) |
340
|
|
|
{ |
341
|
|
|
return $this->getAuthKey() === $authKey; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Generates "remember me" authentication key |
346
|
|
|
*/ |
347
|
3 |
|
public function generateAuthKey() |
348
|
|
|
{ |
349
|
3 |
|
$this->auth_key = Yii::$app->security->generateRandomString(); |
350
|
3 |
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Validates password |
354
|
|
|
* |
355
|
|
|
* @param string $password password to validate |
356
|
|
|
* @return boolean if password provided is valid for current user |
357
|
|
|
*/ |
358
|
22 |
|
public function validatePassword(string $password): bool |
359
|
|
|
{ |
360
|
22 |
|
if (empty($this->password)) { |
361
|
|
|
return false; |
362
|
|
|
} |
363
|
|
|
|
364
|
22 |
|
return Yii::$app->security->validatePassword($password, $this->password); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Set a new password |
369
|
|
|
* |
370
|
|
|
* @param string $password |
371
|
|
|
*/ |
372
|
6 |
|
public function setPassword(string $password): void |
373
|
|
|
{ |
374
|
6 |
|
$this->password = Yii::$app->security->generatePasswordHash($password); |
375
|
6 |
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Set new password reset token |
379
|
|
|
* |
380
|
|
|
* @param string $token |
381
|
|
|
*/ |
382
|
1 |
|
public function setPasswordResetToken(string $token): void |
383
|
|
|
{ |
384
|
1 |
|
$this->password_reset_token = $token; |
385
|
1 |
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Removes password reset token |
389
|
|
|
*/ |
390
|
2 |
|
public function removePasswordResetToken(): void |
391
|
|
|
{ |
392
|
2 |
|
$this->password_reset_token = null; |
393
|
2 |
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Set new confirm email token |
397
|
|
|
* |
398
|
|
|
* @param string $token |
399
|
|
|
*/ |
400
|
4 |
|
public function setEmailConfirmToken(string $token): void |
401
|
|
|
{ |
402
|
4 |
|
$this->email_confirm_token = $token; |
403
|
4 |
|
$this->date_confirm = null; |
404
|
4 |
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @inheritdoc |
408
|
|
|
*/ |
409
|
20 |
|
public static function findIdentity($id) |
410
|
|
|
{ |
411
|
20 |
|
return static::findOne([$id]); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* @inheritdoc |
416
|
|
|
*/ |
417
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
418
|
|
|
{ |
419
|
|
|
throw new NotSupportedException('findIdentityByAccessToken is not implemented.'); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Update date login |
424
|
|
|
*/ |
425
|
12 |
|
public function updateDateLogin(): void |
426
|
|
|
{ |
427
|
12 |
|
$this->updateAttributes([ |
428
|
12 |
|
'date_login' => new \yii\db\Expression('NOW()'), |
429
|
12 |
|
'ip' => ip2long(Yii::$app->request->getUserIP()) |
430
|
|
|
]); |
431
|
12 |
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* @return bool |
435
|
|
|
*/ |
436
|
|
|
public function beforeDelete() |
437
|
|
|
{ |
438
|
|
|
Yii::$app->authManager->revokeAll($this->id); |
439
|
|
|
|
440
|
|
|
if ($this->profile !== null) { |
441
|
|
|
$this->profile->delete(); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
return true; |
445
|
|
|
} |
446
|
|
|
} |
447
|
|
|
|