1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.me/ |
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
10
|
|
|
* @license https://vistart.me/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace rhosocial\base\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 1.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 integer if $passwordHashStrategy equals 'crypt', this value statically |
70
|
|
|
* equals 60. |
71
|
|
|
*/ |
72
|
|
|
public $passwordHashAttributeLength = 60; |
73
|
|
|
private $passwordHashRules = []; |
74
|
|
|
private $passwordResetTokenRules = []; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return the empty password specialty. |
78
|
|
|
* NOTE: PLEASE SPECIFY YOUR OWN EMPTY PASSWORD SPECIALTY. |
79
|
|
|
* - The length of specialty should be greater than 18. |
80
|
|
|
* - Uppercase and lowercase letters, punctuation marks, numbers, and underscores are required. |
81
|
|
|
* @return string The string regarded as empty password. |
82
|
|
|
*/ |
83
|
5 |
|
protected function getEmptyPasswordSpecialty() |
84
|
|
|
{ |
85
|
5 |
|
return 'Rrvl-7}cXt_<iAx[5s'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get rules of password hash. |
90
|
|
|
* @return array password hash rules. |
91
|
|
|
*/ |
92
|
82 |
|
public function getPasswordHashRules() |
93
|
|
|
{ |
94
|
82 |
|
if (empty($this->passwordHashRules) || !is_array($this->passwordHashRules)) { |
95
|
80 |
|
$this->passwordHashRules = [ |
96
|
80 |
|
[[$this->passwordHashAttribute], 'string', 'max' => $this->passwordHashAttributeLength], |
97
|
|
|
]; |
98
|
80 |
|
} |
99
|
82 |
|
return $this->passwordHashRules; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set rules of password hash. |
104
|
|
|
* @param array $rules password hash rules. |
105
|
|
|
*/ |
106
|
2 |
|
public function setPasswordHashRules($rules) |
107
|
|
|
{ |
108
|
2 |
|
if (!empty($rules) && is_array($rules)) { |
109
|
2 |
|
$this->passwordHashRules = $rules; |
110
|
2 |
|
} |
111
|
2 |
|
} |
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
|
82 |
|
public function getPasswordResetTokenRules() |
119
|
|
|
{ |
120
|
82 |
|
if (!is_string($this->passwordResetTokenAttribute)) { |
121
|
|
|
return []; |
122
|
|
|
} |
123
|
82 |
|
if (empty($this->passwordResetTokenRules) || !is_array($this->passwordResetTokenRules)) { |
124
|
82 |
|
$this->passwordResetTokenRules = [ |
125
|
82 |
|
[[$this->passwordResetTokenAttribute], 'string', 'length' => 40], |
126
|
82 |
|
[[$this->passwordResetTokenAttribute], 'unique'], |
127
|
|
|
]; |
128
|
82 |
|
} |
129
|
82 |
|
return $this->passwordResetTokenRules; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set the rules associated with password reset token attribute. |
134
|
|
|
* @param mixed $rules |
135
|
|
|
*/ |
136
|
1 |
|
public function setPasswordResetTokenRules($rules) |
137
|
|
|
{ |
138
|
1 |
|
if (!empty($rules) && is_array($rules)) { |
139
|
|
|
$this->passwordResetTokenRules = $rules; |
140
|
|
|
} |
141
|
1 |
|
} |
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
|
11 |
|
public function generatePasswordHash($password) |
169
|
|
|
{ |
170
|
11 |
|
return Yii::$app->security->generatePasswordHash((string)$password, $this->passwordCost); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Verifies a password against a hash. |
175
|
|
|
* @param string $password The password to verify. |
176
|
|
|
* @return boolean whether the password is correct. |
177
|
|
|
*/ |
178
|
7 |
|
public function validatePassword($password) |
179
|
|
|
{ |
180
|
7 |
|
$phAttribute = $this->passwordHashAttribute; |
181
|
7 |
|
$result = Yii::$app->security->validatePassword($password, $this->$phAttribute); |
182
|
7 |
|
if ($result) { |
183
|
6 |
|
$this->trigger(static::$eventValidatePasswordSucceeded); |
|
|
|
|
184
|
6 |
|
return $result; |
185
|
|
|
} |
186
|
2 |
|
$this->trigger(static::$eventValidatePasswordFailed); |
|
|
|
|
187
|
2 |
|
return $result; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set new password. |
192
|
|
|
* @param string $password the new password to be set. |
193
|
|
|
*/ |
194
|
11 |
|
public function setPassword($password = null) |
195
|
|
|
{ |
196
|
11 |
|
if (empty($password)) { |
197
|
2 |
|
$password = $this->getEmptyPasswordSpecialty(); |
198
|
2 |
|
} |
199
|
11 |
|
$phAttribute = $this->passwordHashAttribute; |
200
|
11 |
|
$this->$phAttribute = $this->generatePasswordHash($password); |
201
|
11 |
|
$this->trigger(static::$eventAfterSetPassword); |
|
|
|
|
202
|
11 |
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* |
206
|
|
|
*/ |
207
|
2 |
|
public function setEmptyPassword() |
208
|
|
|
{ |
209
|
2 |
|
$this->password = $this->getEmptyPasswordSpecialty(); |
210
|
2 |
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* |
214
|
|
|
* @return boolean |
215
|
|
|
*/ |
216
|
3 |
|
public function getIsEmptyPassword() |
217
|
|
|
{ |
218
|
3 |
|
return (!is_string($this->passwordHashAttribute) || empty($this->passwordHashAttribute)) ? true : $this->validatePassword($this->getEmptyPasswordSpecialty()); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Apply for new password. |
223
|
|
|
* If this model is new one, false will be given, and no events will be triggered. |
224
|
|
|
* If password reset feature is not enabled, `$eventNewPasswordAppliedFor` |
225
|
|
|
* will be triggered and return true directly. |
226
|
|
|
* Otherwise, the new password reset token will be regenerated and saved. Then |
227
|
|
|
* trigger the `$eventNewPasswordAppliedFor` and |
228
|
|
|
* `$eventPasswordResetTokenGenerated` events and return true. |
229
|
|
|
* @return boolean |
230
|
|
|
*/ |
231
|
1 |
|
public function applyForNewPassword() |
232
|
|
|
{ |
233
|
1 |
|
if ($this->isNewRecord) { |
|
|
|
|
234
|
1 |
|
return false; |
235
|
|
|
} |
236
|
1 |
|
if (!is_string($this->passwordResetTokenAttribute)) { |
237
|
|
|
$this->trigger(static::$eventNewPasswordAppliedFor); |
|
|
|
|
238
|
|
|
return true; |
239
|
|
|
} |
240
|
1 |
|
$prtAttribute = $this->passwordResetTokenAttribute; |
241
|
1 |
|
$this->$prtAttribute = static::generatePasswordResetToken(); |
242
|
1 |
|
if (!$this->save()) { |
|
|
|
|
243
|
|
|
$this->trigger(static::$eventResetPasswordFailed); |
|
|
|
|
244
|
|
|
return false; |
245
|
|
|
} |
246
|
1 |
|
$this->trigger(static::$eventNewPasswordAppliedFor); |
|
|
|
|
247
|
1 |
|
$this->trigger(static::$eventPasswordResetTokenGenerated); |
|
|
|
|
248
|
1 |
|
return true; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Reset password with password reset token. |
253
|
|
|
* It will validate password reset token, before reseting password. |
254
|
|
|
* @param string $password |
255
|
|
|
* @param string $token |
256
|
|
|
* @return boolean whether reset password successfully or not. |
257
|
|
|
*/ |
258
|
|
|
public function resetPassword($password, $token) |
259
|
|
|
{ |
260
|
|
|
if (!$this->validatePasswordResetToken($token)) { |
261
|
|
|
return false; |
262
|
|
|
} |
263
|
|
|
$this->trigger(static::$eventBeforeResetPassword); |
|
|
|
|
264
|
|
|
$this->password = $password; |
265
|
|
|
if (is_string($this->passwordResetTokenAttribute)) { |
266
|
|
|
$prtAttribute = $this->passwordResetTokenAttribute; |
267
|
|
|
$this->$prtAttribute = ''; |
268
|
|
|
} |
269
|
|
|
if (!$this->save()) { |
|
|
|
|
270
|
|
|
$this->trigger(static::$eventResetPasswordFailed); |
|
|
|
|
271
|
|
|
return false; |
272
|
|
|
} |
273
|
|
|
$this->trigger(static::$eventAfterResetPassword); |
|
|
|
|
274
|
|
|
return true; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Generate password reset token. |
279
|
|
|
* @return string |
280
|
|
|
*/ |
281
|
1 |
|
public static function generatePasswordResetToken() |
282
|
|
|
{ |
283
|
1 |
|
return sha1(Yii::$app->security->generateRandomString()); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* The event triggered after new password set. |
288
|
|
|
* The auth key and access token should be regenerated if new password has applied. |
289
|
|
|
* @param ModelEvent $event |
290
|
|
|
*/ |
291
|
7 |
|
public function onAfterSetNewPassword($event) |
292
|
|
|
{ |
293
|
7 |
|
$this->onInitAuthKey($event); |
|
|
|
|
294
|
7 |
|
$this->onInitAccessToken($event); |
|
|
|
|
295
|
7 |
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Validate whether the $token is the valid password reset token. |
299
|
|
|
* If password reset feature is not enabled, true will be given. |
300
|
|
|
* @param string $token |
301
|
|
|
* @return boolean whether the token is correct. |
302
|
|
|
*/ |
303
|
|
|
protected function validatePasswordResetToken($token) |
304
|
|
|
{ |
305
|
|
|
if (!is_string($this->passwordResetTokenAttribute)) { |
306
|
|
|
return true; |
307
|
|
|
} |
308
|
|
|
$prtAttribute = $this->passwordResetTokenAttribute; |
309
|
|
|
return $this->$prtAttribute === $token; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Initialize password reset token attribute. |
314
|
|
|
* @param ModelEvent $event |
315
|
|
|
*/ |
316
|
87 |
|
public function onInitPasswordResetToken($event) |
317
|
|
|
{ |
318
|
87 |
|
$sender = $event->sender; |
319
|
87 |
|
if (!is_string($sender->passwordResetTokenAttribute)) { |
320
|
|
|
return; |
321
|
|
|
} |
322
|
87 |
|
$prtAttribute = $sender->passwordResetTokenAttribute; |
323
|
87 |
|
$sender->$prtAttribute = ''; |
324
|
87 |
|
} |
325
|
|
|
} |
326
|
|
|
|
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
Idable
provides a methodequalsId
that 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.