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