|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
|
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
|
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
|
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
|
8
|
|
|
* @link https://vistart.name/ |
|
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
|
10
|
|
|
* @license https://vistart.name/license/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
|
14
|
|
|
|
|
15
|
|
|
use Yii; |
|
16
|
|
|
use yii\base\ModelEvent; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* User features concerning password. |
|
20
|
|
|
* |
|
21
|
|
|
* Notice! Please DO NOT change password throughout modifying `pass_hash` property, |
|
22
|
|
|
* use `setPassword()` magic property instead! |
|
23
|
|
|
* |
|
24
|
|
|
* Set or directly reset password: |
|
25
|
|
|
* ```php |
|
26
|
|
|
* $this->password = '<new password>'; // 'afterSetPassword' event will be triggered. |
|
27
|
|
|
* $this->save(); |
|
28
|
|
|
* ``` |
|
29
|
|
|
* |
|
30
|
|
|
* @property-write string $password New password to be set. |
|
31
|
|
|
* @property array $passwordHashRules |
|
32
|
|
|
* @property array $passwordResetTokenRules |
|
33
|
|
|
* @property array $rules |
|
34
|
|
|
* @version 2.0 |
|
35
|
|
|
* @author vistart <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
trait PasswordTrait |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
public static $eventAfterSetPassword = "afterSetPassword"; |
|
41
|
|
|
public static $eventBeforeValidatePassword = "beforeValidatePassword"; |
|
42
|
|
|
public static $eventValidatePasswordSucceeded = "validatePasswordSucceeded"; |
|
43
|
|
|
public static $eventValidatePasswordFailed = "validatePasswordFailed"; |
|
44
|
|
|
public static $eventBeforeResetPassword = "beforeResetPassword"; |
|
45
|
|
|
public static $eventAfterResetPassword = "afterResetPassword"; |
|
46
|
|
|
public static $eventResetPasswordFailed = "resetPasswordFailed"; |
|
47
|
|
|
public static $eventNewPasswordAppliedFor = "newPasswordAppliedFor"; |
|
48
|
|
|
public static $eventPasswordResetTokenGenerated = "passwordResetTokenGenerated"; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string The name of attribute used for storing password hash. |
|
52
|
|
|
* We strongly recommend you not to change `pass_hash` property directly, |
|
53
|
|
|
* please use setPassword() magic property instead. |
|
54
|
|
|
*/ |
|
55
|
|
|
public $passwordHashAttribute = 'pass_hash'; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var string The name of attribute used for storing password reset token. |
|
59
|
|
|
* If you do not want to provide password reset feature, please set `false`. |
|
60
|
|
|
*/ |
|
61
|
|
|
public $passwordResetTokenAttribute = 'password_reset_token'; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var integer Cost parameter used by the Blowfish hash algorithm. |
|
65
|
|
|
*/ |
|
66
|
|
|
public $passwordCost = 13; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var string strategy, which should be used to generate password hash. |
|
70
|
|
|
* Available strategies: |
|
71
|
|
|
* - 'password_hash' - use of PHP `password_hash()` function with PASSWORD_DEFAULT algorithm. |
|
72
|
|
|
* This option is recommended, but it requires PHP version >= 5.5.0 |
|
73
|
|
|
* - 'crypt' - use PHP `crypt()` function. |
|
74
|
|
|
*/ |
|
75
|
|
|
public $passwordHashStrategy = 'crypt'; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var integer if $passwordHashStrategy equals 'crypt', this value statically |
|
79
|
|
|
* equals 60. |
|
80
|
|
|
*/ |
|
81
|
|
|
public $passwordHashAttributeLength = 60; |
|
82
|
|
|
private $passwordHashRules = []; |
|
83
|
|
|
private $passwordResetTokenRules = []; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get rules of password hash. |
|
87
|
|
|
* @return array password hash rules. |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function getPasswordHashRules() |
|
90
|
|
|
{ |
|
91
|
1 |
|
if ($this->passwordHashStrategy == 'crypt') { |
|
92
|
1 |
|
$this->passwordHashAttributeLength = 60; |
|
93
|
1 |
|
} |
|
94
|
1 |
|
if (empty($this->passwordHashRules) || !is_array($this->passwordHashRules)) { |
|
95
|
1 |
|
$this->passwordHashRules = [ |
|
96
|
1 |
|
[[$this->passwordHashAttribute], 'string', 'max' => $this->passwordHashAttributeLength], |
|
97
|
|
|
]; |
|
98
|
1 |
|
} |
|
99
|
1 |
|
return $this->passwordHashRules; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Set rules of password hash. |
|
104
|
|
|
* @param array $rules password hash rules. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setPasswordHashRules($rules) |
|
107
|
|
|
{ |
|
108
|
|
|
if (!empty($rules) && is_array($rules)) { |
|
109
|
|
|
$this->passwordHashRules = $rules; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get the rules associated with password reset token attribute. |
|
115
|
|
|
* If password reset feature is not enabled, the empty array will be given. |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getPasswordResetTokenRules() |
|
119
|
|
|
{ |
|
120
|
|
|
if (!is_string($this->passwordResetTokenAttribute)) { |
|
121
|
|
|
return []; |
|
122
|
|
|
} |
|
123
|
|
|
if (empty($this->passwordResetTokenRules) || !is_array($this->passwordResetTokenRules)) { |
|
124
|
|
|
$this->passwordResetTokenRules = [ |
|
125
|
|
|
[[$this->passwordResetTokenAttribute], 'string', 'length' => 40], |
|
126
|
|
|
[[$this->passwordResetTokenAttribute], 'unique'], |
|
127
|
|
|
]; |
|
128
|
|
|
} |
|
129
|
|
|
return $this->passwordResetTokenRules; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set the rules associated with password reset token attribute. |
|
134
|
|
|
* @param mixed $rules |
|
135
|
|
|
*/ |
|
136
|
3 |
|
public function setPasswordResetTokenRules($rules) |
|
137
|
|
|
{ |
|
138
|
|
|
if (!empty($rules) && is_array($rules)) { |
|
139
|
|
|
$this->passwordResetTokenRules = $rules; |
|
140
|
3 |
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Generates a secure hash from a password and a random salt. |
|
145
|
|
|
* |
|
146
|
|
|
* The generated hash can be stored in database. |
|
147
|
|
|
* Later when a password needs to be validated, the hash can be fetched and passed |
|
148
|
|
|
* to [[validatePassword()]]. For example, |
|
149
|
|
|
* |
|
150
|
|
|
* ~~~ |
|
151
|
|
|
* // generates the hash (usually done during user registration or when the password is changed) |
|
152
|
|
|
* $hash = Yii::$app->getSecurity()->generatePasswordHash($password); |
|
153
|
|
|
* // ...save $hash in database... |
|
154
|
|
|
* |
|
155
|
|
|
* // during login, validate if the password entered is correct using $hash fetched from database |
|
156
|
|
|
* if (Yii::$app->getSecurity()->validatePassword($password, $hash) { |
|
157
|
|
|
* // password is good |
|
158
|
|
|
* } else { |
|
159
|
|
|
* // password is bad |
|
160
|
|
|
* } |
|
161
|
|
|
* ~~~ |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $password The password to be hashed. |
|
164
|
|
|
* @return string The password hash string. When [[passwordHashStrategy]] is set to 'crypt', |
|
165
|
|
|
* the output is always 60 ASCII characters, when set to 'password_hash' the output length |
|
166
|
|
|
* might increase in future versions of PHP (http://php.net/manual/en/function.password-hash.php) |
|
167
|
|
|
*/ |
|
168
|
|
|
public function generatePasswordHash($password) |
|
169
|
|
|
{ |
|
170
|
|
|
Yii::$app->security->passwordHashStrategy = $this->passwordHashStrategy; |
|
171
|
|
|
return Yii::$app->security->generatePasswordHash((string)$password, $this->passwordCost); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Verifies a password against a hash. |
|
176
|
|
|
* @param string $password The password to verify. |
|
177
|
|
|
* @return boolean whether the password is correct. |
|
178
|
|
|
*/ |
|
179
|
1 |
|
public function validatePassword($password) |
|
180
|
|
|
{ |
|
181
|
1 |
|
$phAttribute = $this->passwordHashAttribute; |
|
182
|
1 |
|
$result = Yii::$app->security->validatePassword($password, $this->$phAttribute); |
|
183
|
1 |
|
if ($result) { |
|
184
|
1 |
|
$this->trigger(static::$eventValidatePasswordSucceeded); |
|
|
|
|
|
|
185
|
1 |
|
return $result; |
|
186
|
|
|
} |
|
187
|
|
|
$this->trigger(static::$eventValidatePasswordFailed); |
|
|
|
|
|
|
188
|
|
|
return $result; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Set new password. |
|
193
|
|
|
* @param string $password the new password to be set. |
|
194
|
|
|
*/ |
|
195
|
49 |
|
public function setPassword($password) |
|
196
|
|
|
{ |
|
197
|
49 |
|
$phAttribute = $this->passwordHashAttribute; |
|
198
|
49 |
|
$this->$phAttribute = Yii::$app->security->generatePasswordHash($password); |
|
199
|
49 |
|
$this->trigger(static::$eventAfterSetPassword); |
|
|
|
|
|
|
200
|
49 |
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Apply for new password. |
|
204
|
|
|
* If this model is new one, false will be given, and no events will be triggered. |
|
205
|
|
|
* If password reset feature is not enabled, `$eventNewPasswordAppliedFor` |
|
206
|
|
|
* will be triggered and return true directly. |
|
207
|
|
|
* Otherwise, the new password reset token will be regenerated and saved. Then |
|
208
|
|
|
* trigger the `$eventNewPasswordAppliedFor` and |
|
209
|
|
|
* `$eventPasswordResetTokenGenerated` events and return true. |
|
210
|
|
|
* @return boolean |
|
211
|
|
|
*/ |
|
212
|
1 |
|
public function applyForNewPassword() |
|
213
|
|
|
{ |
|
214
|
1 |
|
if ($this->isNewRecord) { |
|
|
|
|
|
|
215
|
|
|
return false; |
|
216
|
|
|
} |
|
217
|
1 |
|
if (!is_string($this->passwordResetTokenAttribute)) { |
|
218
|
|
|
$this->trigger(static::$eventNewPasswordAppliedFor); |
|
|
|
|
|
|
219
|
|
|
return true; |
|
220
|
|
|
} |
|
221
|
1 |
|
$prtAttribute = $this->passwordResetTokenAttribute; |
|
222
|
1 |
|
$this->$prtAttribute = static::generatePasswordResetToken(); |
|
223
|
1 |
|
if (!$this->save()) { |
|
|
|
|
|
|
224
|
|
|
$this->trigger(static::$eventResetPasswordFailed); |
|
|
|
|
|
|
225
|
|
|
return false; |
|
226
|
|
|
} |
|
227
|
1 |
|
$this->trigger(static::$eventNewPasswordAppliedFor); |
|
|
|
|
|
|
228
|
1 |
|
$this->trigger(static::$eventPasswordResetTokenGenerated); |
|
|
|
|
|
|
229
|
1 |
|
return true; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Reset password with password reset token. |
|
234
|
|
|
* It will validate password reset token, before reseting password. |
|
235
|
|
|
* @param string $password |
|
236
|
|
|
* @param string $token |
|
237
|
|
|
* @return boolean whether reset password successfully or not. |
|
238
|
|
|
*/ |
|
239
|
1 |
|
public function resetPassword($password, $token) |
|
240
|
|
|
{ |
|
241
|
1 |
|
if (!$this->validatePasswordResetToken($token)) { |
|
242
|
|
|
return false; |
|
243
|
|
|
} |
|
244
|
1 |
|
$this->trigger(static::$eventBeforeResetPassword); |
|
|
|
|
|
|
245
|
1 |
|
$this->password = $password; |
|
246
|
1 |
|
if (is_string($this->passwordResetTokenAttribute)) { |
|
247
|
1 |
|
$prtAttribute = $this->passwordResetTokenAttribute; |
|
248
|
1 |
|
$this->$prtAttribute = ''; |
|
249
|
1 |
|
} |
|
250
|
1 |
|
if (!$this->save()) { |
|
|
|
|
|
|
251
|
|
|
$this->trigger(static::$eventResetPasswordFailed); |
|
|
|
|
|
|
252
|
|
|
return false; |
|
253
|
|
|
} |
|
254
|
1 |
|
$this->trigger(static::$eventAfterResetPassword); |
|
|
|
|
|
|
255
|
1 |
|
return true; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Generate password reset token. |
|
260
|
|
|
* @return string |
|
261
|
|
|
*/ |
|
262
|
1 |
|
public static function generatePasswordResetToken() |
|
263
|
|
|
{ |
|
264
|
1 |
|
return sha1(Yii::$app->security->generateRandomString()); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* The event triggered after new password set. |
|
269
|
|
|
* The auth key and access token should be regenerated if new password has applied. |
|
270
|
|
|
* @param ModelEvent $event |
|
271
|
|
|
*/ |
|
272
|
3 |
|
public function onAfterSetNewPassword($event) |
|
273
|
|
|
{ |
|
274
|
3 |
|
$this->onInitAuthKey($event); |
|
|
|
|
|
|
275
|
3 |
|
$this->onInitAccessToken($event); |
|
|
|
|
|
|
276
|
3 |
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Validate whether the $token is the valid password reset token. |
|
280
|
|
|
* If password reset feature is not enabled, true will be given. |
|
281
|
|
|
* @param string $token |
|
282
|
|
|
* @return boolean whether the token is correct. |
|
283
|
|
|
*/ |
|
284
|
1 |
|
protected function validatePasswordResetToken($token) |
|
285
|
|
|
{ |
|
286
|
1 |
|
if (!is_string($this->passwordResetTokenAttribute)) { |
|
287
|
|
|
return true; |
|
288
|
|
|
} |
|
289
|
1 |
|
$prtAttribute = $this->passwordResetTokenAttribute; |
|
290
|
1 |
|
return $this->$prtAttribute === $token; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Initialize password reset token attribute. |
|
295
|
|
|
* @param ModelEvent $event |
|
296
|
|
|
*/ |
|
297
|
60 |
|
public function onInitPasswordResetToken($event) |
|
298
|
|
|
{ |
|
299
|
60 |
|
$sender = $event->sender; |
|
300
|
60 |
|
if (!is_string($sender->passwordResetTokenAttribute)) { |
|
301
|
|
|
return; |
|
302
|
|
|
} |
|
303
|
60 |
|
$prtAttribute = $sender->passwordResetTokenAttribute; |
|
304
|
60 |
|
$sender->$prtAttribute = ''; |
|
305
|
60 |
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.