|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yii2mod\user\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\NotSupportedException; |
|
7
|
|
|
use yii\db\ActiveRecord; |
|
8
|
|
|
use yii\web\IdentityInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class BaseUserModel |
|
12
|
|
|
* |
|
13
|
|
|
* @property integer $id |
|
14
|
|
|
* @property string $username |
|
15
|
|
|
* @property string $passwordHash |
|
16
|
|
|
* @property string $passwordResetToken |
|
17
|
|
|
* @property string $email |
|
18
|
|
|
* @property string $authKey |
|
19
|
|
|
* @property integer $status |
|
20
|
|
|
* @property integer $createdAt |
|
21
|
|
|
* @property integer $updatedAt |
|
22
|
|
|
* @property integer $lastLogin |
|
23
|
|
|
* @property string $password write-only password |
|
24
|
|
|
* |
|
25
|
|
|
* @property BaseUserDetailsModel $userDetails |
|
26
|
|
|
*/ |
|
27
|
|
|
class BaseUserModel extends ActiveRecord implements IdentityInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Deleted Status |
|
31
|
|
|
*/ |
|
32
|
|
|
const STATUS_DELETED = 0; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Active Status |
|
36
|
|
|
*/ |
|
37
|
|
|
const STATUS_ACTIVE = 1; |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritdoc |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function tableName() |
|
44
|
|
|
{ |
|
45
|
|
|
return '{{%User}}'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
*/ |
|
52
|
|
|
public function behaviors() |
|
53
|
|
|
{ |
|
54
|
|
|
return [ |
|
55
|
|
|
'timestamp' => [ |
|
56
|
|
|
'class' => 'yii\behaviors\TimestampBehavior', |
|
57
|
|
|
'createdAtAttribute' => 'createdAt', |
|
58
|
|
|
'updatedAtAttribute' => 'updatedAt' |
|
59
|
|
|
] |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns the validation rules for attributes. |
|
65
|
|
|
* |
|
66
|
|
|
* Validation rules are used by [[validate()]] to check if attribute values are valid. |
|
67
|
|
|
* Child classes may override this method to declare different validation rules. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function rules() |
|
70
|
|
|
{ |
|
71
|
|
|
return [ |
|
72
|
|
|
['status', 'default', 'value' => self::STATUS_ACTIVE], |
|
73
|
|
|
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]], |
|
74
|
|
|
[['lastLogin'], 'integer', 'integerOnly' => true], |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return \yii\db\ActiveQuery |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getUserDetails() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->hasOne(BaseUserDetailsModel::className(), ['userId' => 'id']); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function findIdentity($id) |
|
90
|
|
|
{ |
|
91
|
|
|
return static::findOne($id); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritdoc |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
|
98
|
|
|
{ |
|
99
|
|
|
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Finds user (with active status) by username |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $username |
|
106
|
|
|
* |
|
107
|
|
|
* @return static|null |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function findByUsername($username) |
|
110
|
|
|
{ |
|
111
|
|
|
return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Finds user by email |
|
116
|
|
|
* |
|
117
|
|
|
* @param $email |
|
118
|
|
|
* @return null|static |
|
119
|
|
|
*/ |
|
120
|
|
|
public static function findByEmail($email) |
|
121
|
|
|
{ |
|
122
|
|
|
return static::findOne(['email' => $email]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Finds user by password reset token |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $token password reset token |
|
129
|
|
|
* @return static|null |
|
130
|
|
|
*/ |
|
131
|
|
|
public static function findByPasswordResetToken($token) |
|
132
|
|
|
{ |
|
133
|
|
|
if (!static::isPasswordResetTokenValid($token)) { |
|
134
|
|
|
return null; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return static::findOne([ |
|
138
|
|
|
'passwordResetToken' => $token, |
|
139
|
|
|
'status' => self::STATUS_ACTIVE, |
|
140
|
|
|
]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Finds out if password reset token is valid |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $token password reset token |
|
147
|
|
|
* @return boolean |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function isPasswordResetTokenValid($token) |
|
150
|
|
|
{ |
|
151
|
|
|
if (empty($token)) { |
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
$timestamp = (int) substr($token, strrpos($token, '_') + 1); |
|
155
|
|
|
$expire = Yii::$app->params['user.passwordResetTokenExpire']; |
|
156
|
|
|
|
|
157
|
|
|
return $timestamp + $expire >= time(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @inheritdoc |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getId() |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->getPrimaryKey(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @inheritdoc |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getAuthKey() |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->authKey; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @inheritdoc |
|
178
|
|
|
*/ |
|
179
|
|
|
public function validateAuthKey($authKey) |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->getAuthKey() === $authKey; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Validates password |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $password password to validate |
|
188
|
|
|
* |
|
189
|
|
|
* @return boolean if password provided is valid for current user |
|
190
|
|
|
*/ |
|
191
|
|
|
public function validatePassword($password) |
|
192
|
|
|
{ |
|
193
|
|
|
return Yii::$app->getSecurity()->validatePassword($password, $this->passwordHash); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Generates password hash from password and sets it to the model |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $password |
|
200
|
|
|
*/ |
|
201
|
|
|
public function setPassword($password) |
|
202
|
|
|
{ |
|
203
|
|
|
$this->passwordHash = Yii::$app->getSecurity()->generatePasswordHash($password); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Generates "remember me" authentication key |
|
208
|
|
|
*/ |
|
209
|
|
|
public function generateAuthKey() |
|
210
|
|
|
{ |
|
211
|
|
|
$this->authKey = Yii::$app->getSecurity()->generateRandomString(); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Generates new password reset token |
|
216
|
|
|
*/ |
|
217
|
|
|
public function generatePasswordResetToken() |
|
218
|
|
|
{ |
|
219
|
|
|
$this->passwordResetToken = Yii::$app->getSecurity()->generateRandomString() . '_' . time(); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Removes password reset token |
|
224
|
|
|
*/ |
|
225
|
|
|
public function removePasswordResetToken() |
|
226
|
|
|
{ |
|
227
|
|
|
$this->passwordResetToken = null; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Update last login |
|
232
|
|
|
*/ |
|
233
|
|
|
public function updateLastLogin() |
|
234
|
|
|
{ |
|
235
|
|
|
$this->updateAttributes(['lastLogin' => time()]); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|