1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yiicod\auth\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\InvalidParamException; |
7
|
|
|
use yii\base\NotSupportedException; |
8
|
|
|
use yii\behaviors\TimestampBehavior; |
9
|
|
|
use yii\db\ActiveRecord; |
10
|
|
|
use yii\web\IdentityInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* User model |
14
|
|
|
* |
15
|
|
|
* @property int $id |
16
|
|
|
* @property string $username |
17
|
|
|
* @property string $password_hash |
18
|
|
|
* @property string $password_reset_token |
19
|
|
|
* @property string $email |
20
|
|
|
* @property string $auth_key |
21
|
|
|
* @property int $role |
22
|
|
|
* @property int $status |
23
|
|
|
* @property int $updated_at |
24
|
|
|
* @property string $password write-only password |
25
|
|
|
*/ |
26
|
|
|
class UserModel extends ActiveRecord implements IdentityInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public static function tableName() |
32
|
|
|
{ |
33
|
|
|
return 'user'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
public function rules() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public static function findIdentity($id) |
49
|
|
|
{ |
50
|
|
|
return static::find() |
51
|
|
|
->where(['id' => $id]) |
52
|
|
|
->identity() |
53
|
|
|
->one(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
60
|
|
|
{ |
61
|
|
|
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Finds user by username |
66
|
|
|
* |
67
|
|
|
* @param string $username |
68
|
|
|
* |
69
|
|
|
* @return static|null |
70
|
|
|
*/ |
71
|
|
|
public static function findByUsername($username) |
72
|
|
|
{ |
73
|
|
|
return static::find() |
74
|
|
|
->where([static::attributesMap()['fieldLogin'] => $username]) |
75
|
|
|
->byUsername() |
76
|
|
|
->one(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Finds user by password reset token |
81
|
|
|
* |
82
|
|
|
* @param string $token password reset token |
83
|
|
|
* |
84
|
|
|
* @return static|null |
85
|
|
|
*/ |
86
|
|
|
public static function findByPasswordResetToken($token) |
87
|
|
|
{ |
88
|
|
|
if (false === static::isPasswordResetTokenValid($token)) { |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return static::find() |
93
|
|
|
->where([static::attributesMap()['fieldPasswordResetToken'] => $token]) |
94
|
|
|
->byPasswordResetToken() |
95
|
|
|
->one(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Finds out if password reset token is valid |
100
|
|
|
* |
101
|
|
|
* @param string $token password reset token |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public static function isPasswordResetTokenValid($token) |
106
|
|
|
{ |
107
|
|
|
if (empty($token)) { |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
$timestamp = (int)substr($token, strrpos($token, '_') + 1); |
111
|
|
|
$expire = Yii::$app->params['user.passwordResetTokenExpire']; |
112
|
|
|
|
113
|
|
|
return $timestamp + $expire >= time(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public static function find() |
120
|
|
|
{ |
121
|
|
|
/** @var UserQueryInterface $userQueryclass */ |
122
|
|
|
$userQueryclass = Yii::$app->get('auth')->modelMap['userQuery']['class']; |
123
|
|
|
|
124
|
|
|
return new $userQueryclass(get_called_class()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
public function getId() |
131
|
|
|
{ |
132
|
|
|
return $this->getPrimaryKey(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getAuthKey() |
139
|
|
|
{ |
140
|
|
|
return $this->auth_key; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $authKey |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function validateAuthKey($authKey) |
149
|
|
|
{ |
150
|
|
|
return $this->getAuthKey() === $authKey; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Validates password |
155
|
|
|
* |
156
|
|
|
* @param string $password password to validate |
157
|
|
|
* |
158
|
|
|
* @return bool if password provided is valid for current user |
159
|
|
|
*/ |
160
|
|
|
public function validatePassword($password) |
161
|
|
|
{ |
162
|
|
|
try { |
163
|
|
|
return Yii::$app->security->validatePassword( |
164
|
|
|
$password, $this->password |
165
|
|
|
); |
166
|
|
|
} catch (InvalidParamException $e) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Generates password hash from password and sets it to the model |
173
|
|
|
* |
174
|
|
|
* @param string $password |
175
|
|
|
*/ |
176
|
|
|
public function generatePassword($password) |
177
|
|
|
{ |
178
|
|
|
$this->password = Yii::$app->security->generatePasswordHash($password); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Generates "remember me" authentication key |
183
|
|
|
*/ |
184
|
|
|
public function generateAuthKey() |
185
|
|
|
{ |
186
|
|
|
$this->auth_key = Yii::$app->security->generateRandomString(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Generates new password reset token |
191
|
|
|
*/ |
192
|
|
|
public function generatePasswordResetToken() |
193
|
|
|
{ |
194
|
|
|
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Removes password reset token |
199
|
|
|
*/ |
200
|
|
|
public function removePasswordResetToken() |
201
|
|
|
{ |
202
|
|
|
$this->password_reset_token = null; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public static function attributesMap() |
206
|
|
|
{ |
207
|
|
|
return [ |
208
|
|
|
'fieldLogin' => 'email', //requred |
209
|
|
|
'fieldEmail' => 'email', //requred |
210
|
|
|
'fieldPassword' => 'password_hash', //requred |
211
|
|
|
'fieldAuthKey' => 'auth_key', |
212
|
|
|
'fieldUsername' => 'username', |
213
|
|
|
'fieldStatus' => 'status', |
214
|
|
|
'fieldPasswordResetToken' => 'password_reset_token', //requred |
215
|
|
|
'fieldCreatedDate' => 'created_date', //or null |
216
|
|
|
'fieldUpdatedDate' => 'updated_date', //or null |
217
|
|
|
]; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function behaviors() |
221
|
|
|
{ |
222
|
|
|
return [ |
223
|
|
|
'attributesMapBehavior' => [ |
224
|
|
|
'class' => '\yiicod\base\models\behaviors\AttributesMapBehavior', |
225
|
|
|
'attributesMap' => static::attributesMap(), |
226
|
|
|
], |
227
|
|
|
'timestampBehavior' => [ |
228
|
|
|
'class' => TimestampBehavior::className(), |
229
|
|
|
'createdAtAttribute' => static::attributesMap()['fieldCreatedDate'], |
230
|
|
|
'updatedAtAttribute' => static::attributesMap()['fieldUpdatedDate'], |
231
|
|
|
], |
232
|
|
|
]; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|