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 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() |
|
87 | |||
88 | /** |
||
89 | * Get rules of password hash. |
||
90 | * @return array password hash rules. |
||
91 | */ |
||
92 | 82 | public function getPasswordHashRules() |
|
101 | |||
102 | /** |
||
103 | * Set rules of password hash. |
||
104 | * @param array $rules password hash rules. |
||
105 | */ |
||
106 | 2 | 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 | 82 | public function getPasswordResetTokenRules() |
|
131 | |||
132 | /** |
||
133 | * Set the rules associated with password reset token attribute. |
||
134 | * @param mixed $rules |
||
135 | */ |
||
136 | 1 | 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 | 11 | public function generatePasswordHash($password) |
|
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) |
|
189 | |||
190 | /** |
||
191 | * Set new password. |
||
192 | * @param string $password the new password to be set. |
||
193 | */ |
||
194 | 11 | public function setPassword($password = null) |
|
203 | |||
204 | /** |
||
205 | * |
||
206 | */ |
||
207 | 2 | public function setEmptyPassword() |
|
211 | |||
212 | /** |
||
213 | * |
||
214 | * @return boolean |
||
215 | */ |
||
216 | 3 | public function getIsEmptyPassword() |
|
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() |
|
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) |
||
276 | |||
277 | /** |
||
278 | * Generate password reset token. |
||
279 | * @return string |
||
280 | */ |
||
281 | 1 | public static function generatePasswordResetToken() |
|
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) |
|
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) |
||
311 | |||
312 | /** |
||
313 | * Initialize password reset token attribute. |
||
314 | * @param ModelEvent $event |
||
315 | */ |
||
316 | 87 | public function onInitPasswordResetToken($event) |
|
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.