1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace modules\user\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\ArrayHelper; |
7
|
|
|
use modules\user\models\query\UserQuery; |
8
|
|
|
use modules\user\traits\ModuleTrait; |
9
|
|
|
use modules\user\Module; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class User |
13
|
|
|
* @package modules\user\models |
14
|
|
|
* |
15
|
|
|
* This is the model class for table "{{%user}}". |
16
|
|
|
* |
17
|
|
|
* @property int $id ID |
18
|
|
|
* @property string $username Username |
19
|
|
|
* @property string $auth_key Authorization Key |
20
|
|
|
* @property string $password_hash Hash Password |
21
|
|
|
* @property string $password_reset_token Password Token |
22
|
|
|
* @property string $email_confirm_token Email Confirm Token |
23
|
|
|
* @property string $email Email |
24
|
|
|
* @property int|string $status Status |
25
|
|
|
* @property int $last_visit Last Visit |
26
|
|
|
* @property int $created_at Created |
27
|
|
|
* @property int $updated_at Updated |
28
|
|
|
* @property string $first_name First Name |
29
|
|
|
* @property string $last_name Last Name |
30
|
|
|
* @property int $registration_type Type Registration |
31
|
|
|
* @property string statusLabelName Status name in label |
32
|
|
|
* @property array statusesArray Array statuses |
33
|
|
|
* @property string statusName Name status |
34
|
|
|
* @property int|string registrationType Type registered |
35
|
|
|
*/ |
36
|
|
|
class User extends BaseUser |
37
|
|
|
{ |
38
|
|
|
use ModuleTrait; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return object|\yii\db\ActiveQuery |
42
|
|
|
* @throws \yii\base\InvalidConfigException |
43
|
|
|
*/ |
44
|
|
|
public static function find() |
45
|
|
|
{ |
46
|
|
|
return Yii::createObject(UserQuery::class, [get_called_class()]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Finds user by email |
51
|
|
|
* |
52
|
|
|
* @param string $email |
53
|
|
|
* @return static|null |
54
|
|
|
*/ |
55
|
|
|
public static function findByUsernameEmail($email) |
56
|
|
|
{ |
57
|
|
|
return static::findOne(['email' => $email, 'status' => self::STATUS_ACTIVE]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Finds user by username or email |
62
|
|
|
* |
63
|
|
|
* @param string $string |
64
|
|
|
* @return static|null |
65
|
|
|
* @throws \yii\base\InvalidConfigException |
66
|
|
|
*/ |
67
|
|
|
public static function findByUsernameOrEmail($string) |
68
|
|
|
{ |
69
|
|
|
return static::find() |
70
|
|
|
->where(['or', ['username' => $string], ['email' => $string]]) |
71
|
|
|
->andWhere(['status' => self::STATUS_ACTIVE]) |
72
|
|
|
->one(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Generates password hash from password and sets it to the model |
77
|
|
|
* |
78
|
|
|
* @param string $password |
79
|
|
|
* @throws \yii\base\Exception |
80
|
|
|
*/ |
81
|
|
|
public function setPassword($password) |
82
|
|
|
{ |
83
|
|
|
$this->password_hash = Yii::$app->security->generatePasswordHash($password); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Generates new password reset token |
88
|
|
|
*/ |
89
|
|
|
public function generatePasswordResetToken() |
90
|
|
|
{ |
91
|
|
|
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Removes password reset token |
96
|
|
|
*/ |
97
|
|
|
public function removePasswordResetToken() |
98
|
|
|
{ |
99
|
|
|
$this->password_reset_token = null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Removes email confirmation token |
104
|
|
|
*/ |
105
|
|
|
public function removeEmailConfirmToken() |
106
|
|
|
{ |
107
|
|
|
$this->email_confirm_token = null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Finds user by password reset token |
112
|
|
|
* |
113
|
|
|
* @param mixed $token password reset token |
114
|
|
|
* @return static|null |
115
|
|
|
*/ |
116
|
|
|
public static function findByPasswordResetToken($token) |
117
|
|
|
{ |
118
|
|
|
if (!static::isPasswordResetTokenValid($token)) { |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
return static::findOne([ |
122
|
|
|
'password_reset_token' => $token, |
123
|
|
|
'status' => self::STATUS_ACTIVE, |
124
|
|
|
]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param mixed $email_confirm_token |
129
|
|
|
* @return bool|null|static |
130
|
|
|
*/ |
131
|
|
|
public static function findByEmailConfirmToken($email_confirm_token) |
132
|
|
|
{ |
133
|
|
|
return static::findOne([ |
134
|
|
|
'email_confirm_token' => $email_confirm_token, |
135
|
|
|
'status' => self::STATUS_WAIT |
136
|
|
|
]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Generates email confirmation token |
141
|
|
|
*/ |
142
|
|
|
public function generateEmailConfirmToken() |
143
|
|
|
{ |
144
|
|
|
$this->email_confirm_token = Yii::$app->security->generateRandomString(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Validates password |
149
|
|
|
* |
150
|
|
|
* @param string $password password to validate |
151
|
|
|
* @return bool if password provided is valid for current user |
152
|
|
|
*/ |
153
|
|
|
public function validatePassword($password) |
154
|
|
|
{ |
155
|
|
|
return Yii::$app->security->validatePassword($password, $this->password_hash); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Type of registration |
160
|
|
|
* How the user is created |
161
|
|
|
* If the system registration type is registered by itself, |
162
|
|
|
* if it is created from the admin area, |
163
|
|
|
* then the login type that created the account |
164
|
|
|
* |
165
|
|
|
* @return mixed|string |
166
|
|
|
*/ |
167
|
|
|
public function getRegistrationType() |
168
|
|
|
{ |
169
|
|
|
if ($this->registration_type > 0) { |
170
|
|
|
if (($model = User::findOne($this->registration_type)) !== null) { |
171
|
|
|
return $model->username; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
return $this->getRegistrationTypeName(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns the registration type string |
179
|
|
|
* @return mixed |
180
|
|
|
*/ |
181
|
|
|
public function getRegistrationTypeName() |
182
|
|
|
{ |
183
|
|
|
return ArrayHelper::getValue(self::getRegistrationTypesArray(), $this->registration_type); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns an array of log types |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
public static function getRegistrationTypesArray() |
191
|
|
|
{ |
192
|
|
|
return [ |
193
|
|
|
self::TYPE_REGISTRATION_SYSTEM => Module::t('module', 'System'), |
194
|
|
|
]; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
public function isDeleted() |
201
|
|
|
{ |
202
|
|
|
return $this->status === self::STATUS_DELETED; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Finds out if password reset token is valid |
207
|
|
|
* |
208
|
|
|
* @param mixed $token password reset token |
209
|
|
|
* @return boolean |
210
|
|
|
*/ |
211
|
|
|
public static function isPasswordResetTokenValid($token) |
212
|
|
|
{ |
213
|
|
|
if (empty($token)) { |
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
$expire = Yii::$app->params['user.passwordResetTokenExpire']; |
217
|
|
|
$parts = explode('_', $token); |
218
|
|
|
$timestamp = (int)end($parts); |
219
|
|
|
return $timestamp + $expire >= time(); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|