1 | <?php |
||
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() |
|
101 | |||
102 | /** |
||
103 | * Set rules of password hash. |
||
104 | * @param array $rules password hash rules. |
||
105 | */ |
||
106 | public function setPasswordHashRules($rules) |
||
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() |
||
131 | |||
132 | /** |
||
133 | * Set the rules associated with password reset token attribute. |
||
134 | * @param mixed $rules |
||
135 | */ |
||
136 | 3 | public function setPasswordResetTokenRules($rules) |
|
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) |
||
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) |
|
190 | |||
191 | /** |
||
192 | * Set new password. |
||
193 | * @param string $password the new password to be set. |
||
194 | */ |
||
195 | 49 | public function setPassword($password) |
|
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() |
|
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) |
|
257 | |||
258 | /** |
||
259 | * Generate password reset token. |
||
260 | * @return string |
||
261 | */ |
||
262 | 1 | public static function generatePasswordResetToken() |
|
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) |
|
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) |
|
292 | |||
293 | /** |
||
294 | * Initialize password reset token attribute. |
||
295 | * @param ModelEvent $event |
||
296 | */ |
||
297 | 60 | public function onInitPasswordResetToken($event) |
|
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
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.