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