1 | <?php |
||
26 | trait PasswordTrait |
||
27 | { |
||
28 | |||
29 | public static $eventAfterSetPassword = "afterSetPassword"; |
||
30 | public static $eventBeforeValidatePassword = "beforeValidatePassword"; |
||
31 | public static $eventValidatePasswordSucceeded = "validatePasswordSucceeded"; |
||
32 | public static $eventValidatePasswordFailed = "validatePasswordFailed"; |
||
33 | public static $eventBeforeResetPassword = "beforeResetPassword"; |
||
34 | public static $eventAfterResetPassword = "afterResetPassword"; |
||
35 | public static $eventResetPasswordFailed = "resetPasswordFailed"; |
||
36 | public static $eventNewPasswordAppliedFor = "newPasswordAppliedFor"; |
||
37 | public static $eventPasswordResetTokenGenerated = "passwordResetTokenGenerated"; |
||
38 | |||
39 | /** |
||
40 | * @var string The name of attribute used for storing password hash. |
||
41 | */ |
||
42 | public $passwordHashAttribute = 'pass_hash'; |
||
43 | |||
44 | /** |
||
45 | * @var string The name of attribute used for storing password reset token. |
||
46 | * If you do not want to provide password reset feature, please set `false`. |
||
47 | */ |
||
48 | public $passwordResetTokenAttribute = 'password_reset_token'; |
||
49 | |||
50 | /** |
||
51 | * @var integer Cost parameter used by the Blowfish hash algorithm. |
||
52 | */ |
||
53 | public $passwordCost = 13; |
||
54 | |||
55 | /** |
||
56 | * @var string strategy, which should be used to generate password hash. |
||
57 | * Available strategies: |
||
58 | * - 'password_hash' - use of PHP `password_hash()` function with PASSWORD_DEFAULT algorithm. |
||
59 | * This option is recommended, but it requires PHP version >= 5.5.0 |
||
60 | * - 'crypt' - use PHP `crypt()` function. |
||
61 | */ |
||
62 | public $passwordHashStrategy = 'crypt'; |
||
63 | |||
64 | /** |
||
65 | * @var integer if $passwordHashStrategy equals 'crypt', this value statically |
||
66 | * equals 60. |
||
67 | */ |
||
68 | public $passwordHashAttributeLength = 60; |
||
69 | private $passwordHashRules = []; |
||
70 | private $passwordResetTokenRules = []; |
||
71 | |||
72 | /** |
||
73 | * Get rules of password hash. |
||
74 | * @return array password hash rules. |
||
75 | */ |
||
76 | 1 | public function getPasswordHashRules() |
|
88 | |||
89 | /** |
||
90 | * Set rules of password hash. |
||
91 | * @param array $rules password hash rules. |
||
92 | */ |
||
93 | public function setPasswordHashRules($rules) |
||
99 | |||
100 | /** |
||
101 | * Get the rules associated with password reset token attribute. |
||
102 | * If password reset feature is not enabled, the empty array will be given. |
||
103 | * @return mixed |
||
104 | */ |
||
105 | public function getPasswordResetTokenRules() |
||
118 | |||
119 | /** |
||
120 | * Set the rules associated with password reset token attribute. |
||
121 | * @param mixed $rules |
||
122 | */ |
||
123 | public function setPasswordResetTokenRules($rules) |
||
129 | |||
130 | /** |
||
131 | * Generates a secure hash from a password and a random salt. |
||
132 | * |
||
133 | * The generated hash can be stored in database. |
||
134 | * Later when a password needs to be validated, the hash can be fetched and passed |
||
135 | * to [[validatePassword()]]. For example, |
||
136 | * |
||
137 | * ~~~ |
||
138 | * // generates the hash (usually done during user registration or when the password is changed) |
||
139 | * $hash = Yii::$app->getSecurity()->generatePasswordHash($password); |
||
140 | * // ...save $hash in database... |
||
141 | * |
||
142 | * // during login, validate if the password entered is correct using $hash fetched from database |
||
143 | * if (Yii::$app->getSecurity()->validatePassword($password, $hash) { |
||
144 | * // password is good |
||
145 | * } else { |
||
146 | * // password is bad |
||
147 | * } |
||
148 | * ~~~ |
||
149 | * |
||
150 | * @param string $password The password to be hashed. |
||
151 | * @return string The password hash string. When [[passwordHashStrategy]] is set to 'crypt', |
||
152 | * the output is always 60 ASCII characters, when set to 'password_hash' the output length |
||
153 | * might increase in future versions of PHP (http://php.net/manual/en/function.password-hash.php) |
||
154 | */ |
||
155 | public function generatePasswordHash($password) |
||
160 | |||
161 | /** |
||
162 | * Verifies a password against a hash. |
||
163 | * @param string $password The password to verify. |
||
164 | * @return boolean whether the password is correct. |
||
165 | */ |
||
166 | 1 | public function validatePassword($password) |
|
177 | |||
178 | /** |
||
179 | * Set new password. |
||
180 | * @param string $password the new password to be set. |
||
181 | */ |
||
182 | 28 | public function setPassword($password) |
|
188 | |||
189 | /** |
||
190 | * Apply for new password. |
||
191 | * If this model is new one, false will be given, and no events will be triggered. |
||
192 | * If password reset feature is not enabled, `$eventNewPasswordAppliedFor` |
||
193 | * will be triggered and return true directly. |
||
194 | * Otherwise, the new password reset token will be regenerated and saved. Then |
||
195 | * trigger the `$eventNewPasswordAppliedFor` and |
||
196 | * `$eventPasswordResetTokenGenerated` events and return true. |
||
197 | * @return boolean |
||
198 | */ |
||
199 | 1 | public function applyForNewPassword() |
|
218 | |||
219 | /** |
||
220 | * Reset password with password reset token. |
||
221 | * It will validate password reset token, before reseting password. |
||
222 | * @param string $password |
||
223 | * @param string $token |
||
224 | * @return boolean whether reset password successfully or not. |
||
225 | */ |
||
226 | 1 | public function resetPassword($password, $token) |
|
244 | |||
245 | /** |
||
246 | * Generate password reset token. |
||
247 | * @return string |
||
248 | */ |
||
249 | 1 | public static function generatePasswordResetToken() |
|
253 | |||
254 | /** |
||
255 | * The event triggered after new password set. |
||
256 | * The auth key and access token should be regenerated if new password has applied. |
||
257 | * @param \yii\base\Event $event |
||
258 | */ |
||
259 | 3 | public function onAfterSetNewPassword($event) |
|
264 | |||
265 | /** |
||
266 | * Validate whether the $token is the valid password reset token. |
||
267 | * If password reset feature is not enabled, true will be given. |
||
268 | * @param string $token |
||
269 | * @return boolean whether the token is correct. |
||
270 | */ |
||
271 | 1 | protected function validatePasswordResetToken($token) |
|
279 | |||
280 | /** |
||
281 | * Initialize password reset token attribute. |
||
282 | * @param \yii\base\ModelEvent $event |
||
283 | */ |
||
284 | 38 | public function onInitPasswordResetToken($event) |
|
293 | } |
||
294 |
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.