@@ -13,163 +13,163 @@ |
||
| 13 | 13 | |
| 14 | 14 | trait Password |
| 15 | 15 | { |
| 16 | - /** @var string The password hash. */ |
|
| 17 | - protected $password; |
|
| 18 | - |
|
| 19 | - /** @var string|null The password reset token. */ |
|
| 20 | - protected $passwordResetToken; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * this method is required to be called in the constructor for each class that uses this trait. |
|
| 24 | - * It adds the fields necessary for the passwords struct to the table definition |
|
| 25 | - */ |
|
| 26 | - protected function initPassword() |
|
| 27 | - { |
|
| 28 | - $this->extendTableDefinition(TRAIT_PASSWORD_FIELD_PASSWORD, [ |
|
| 29 | - 'value' => &$this->password, |
|
| 30 | - 'validate' => [$this, 'validatePassword'], |
|
| 31 | - 'type' => 'VARCHAR', |
|
| 32 | - 'length' => 1024, |
|
| 33 | - 'properties' => null |
|
| 34 | - ]); |
|
| 35 | - |
|
| 36 | - $this->extendTableDefinition(TRAIT_PASSWORD_FIELD_PASSWORD_RESET_TOKEN, [ |
|
| 37 | - 'value' => &$this->passwordResetToken, |
|
| 38 | - 'validate' => null, |
|
| 39 | - 'default' => 0, |
|
| 40 | - 'type' => 'VARCHAR', |
|
| 41 | - 'length' => 1024 |
|
| 42 | - ]); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Returns whether the users password has been set |
|
| 48 | - * @return boolean true if the user has a password |
|
| 49 | - */ |
|
| 50 | - public function hasPasswordBeenSet() |
|
| 51 | - { |
|
| 52 | - return $this->password !== null; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Returns true if the credentials are correct. |
|
| 57 | - * |
|
| 58 | - * @param string $password |
|
| 59 | - * @return boolean true if the credentials are correct |
|
| 60 | - */ |
|
| 61 | - public function isPassword($password) |
|
| 62 | - { |
|
| 63 | - if (!$this->hasPasswordBeenSet()) |
|
| 64 | - { |
|
| 65 | - throw new ActiveRecordTraitException("Password field has not been set"); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - if (!password_verify($password, $this->password)) { |
|
| 69 | - return false; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - if (password_needs_rehash($this->password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH])) { |
|
| 73 | - $this->setPassword($password)->sync(); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - return true; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - public function validatePassword($password) { |
|
| 80 | - if (strlen($password) < TRAIT_PASSWORD_MIN_LENGTH) { |
|
| 81 | - $message = sprintf('\'Password\' must be atleast %s characters long. %s characters provied.', TRAIT_PASSWORD_MIN_LENGTH, strlen($password)); |
|
| 82 | - return [false, $message]; |
|
| 83 | - } |
|
| 84 | - return [true, '']; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Set the password. |
|
| 89 | - * |
|
| 90 | - * @param string $password |
|
| 91 | - * @return $this |
|
| 92 | - * @throws \Exception |
|
| 93 | - */ |
|
| 94 | - public function setPassword($password) |
|
| 95 | - { |
|
| 96 | - [$status, $error] = $this->validatePassword($password); |
|
| 97 | - if (!$status) { |
|
| 98 | - throw new ActiveRecordTraitException($error); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $passwordHash = \password_hash($password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH]); |
|
| 102 | - |
|
| 103 | - if ($passwordHash === false) { |
|
| 104 | - throw new ActiveRecordTraitException('\'Password\' hash failed.'); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $this->password = $passwordHash; |
|
| 108 | - |
|
| 109 | - return $this; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @return string The Hash of the password |
|
| 114 | - */ |
|
| 115 | - public function getPasswordHash() |
|
| 116 | - { |
|
| 117 | - return $this->password; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Returns the currently set password token for the entity, or null if not set |
|
| 122 | - * @return string|null The password reset token |
|
| 123 | - */ |
|
| 124 | - public function getPasswordResetToken() |
|
| 125 | - { |
|
| 126 | - return $this->passwordResetToken; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Generates a new password reset token for the user |
|
| 131 | - */ |
|
| 132 | - public function generatePasswordResetToken() |
|
| 133 | - { |
|
| 134 | - $this->passwordResetToken = md5(uniqid(mt_rand(), true)); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Clears the current password reset token |
|
| 139 | - */ |
|
| 140 | - public function clearPasswordResetToken() |
|
| 141 | - { |
|
| 142 | - $this->passwordResetToken = null; |
|
| 143 | - } |
|
| 16 | + /** @var string The password hash. */ |
|
| 17 | + protected $password; |
|
| 18 | + |
|
| 19 | + /** @var string|null The password reset token. */ |
|
| 20 | + protected $passwordResetToken; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * this method is required to be called in the constructor for each class that uses this trait. |
|
| 24 | + * It adds the fields necessary for the passwords struct to the table definition |
|
| 25 | + */ |
|
| 26 | + protected function initPassword() |
|
| 27 | + { |
|
| 28 | + $this->extendTableDefinition(TRAIT_PASSWORD_FIELD_PASSWORD, [ |
|
| 29 | + 'value' => &$this->password, |
|
| 30 | + 'validate' => [$this, 'validatePassword'], |
|
| 31 | + 'type' => 'VARCHAR', |
|
| 32 | + 'length' => 1024, |
|
| 33 | + 'properties' => null |
|
| 34 | + ]); |
|
| 35 | + |
|
| 36 | + $this->extendTableDefinition(TRAIT_PASSWORD_FIELD_PASSWORD_RESET_TOKEN, [ |
|
| 37 | + 'value' => &$this->passwordResetToken, |
|
| 38 | + 'validate' => null, |
|
| 39 | + 'default' => 0, |
|
| 40 | + 'type' => 'VARCHAR', |
|
| 41 | + 'length' => 1024 |
|
| 42 | + ]); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Returns whether the users password has been set |
|
| 48 | + * @return boolean true if the user has a password |
|
| 49 | + */ |
|
| 50 | + public function hasPasswordBeenSet() |
|
| 51 | + { |
|
| 52 | + return $this->password !== null; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Returns true if the credentials are correct. |
|
| 57 | + * |
|
| 58 | + * @param string $password |
|
| 59 | + * @return boolean true if the credentials are correct |
|
| 60 | + */ |
|
| 61 | + public function isPassword($password) |
|
| 62 | + { |
|
| 63 | + if (!$this->hasPasswordBeenSet()) |
|
| 64 | + { |
|
| 65 | + throw new ActiveRecordTraitException("Password field has not been set"); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + if (!password_verify($password, $this->password)) { |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + if (password_needs_rehash($this->password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH])) { |
|
| 73 | + $this->setPassword($password)->sync(); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + return true; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + public function validatePassword($password) { |
|
| 80 | + if (strlen($password) < TRAIT_PASSWORD_MIN_LENGTH) { |
|
| 81 | + $message = sprintf('\'Password\' must be atleast %s characters long. %s characters provied.', TRAIT_PASSWORD_MIN_LENGTH, strlen($password)); |
|
| 82 | + return [false, $message]; |
|
| 83 | + } |
|
| 84 | + return [true, '']; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Set the password. |
|
| 89 | + * |
|
| 90 | + * @param string $password |
|
| 91 | + * @return $this |
|
| 92 | + * @throws \Exception |
|
| 93 | + */ |
|
| 94 | + public function setPassword($password) |
|
| 95 | + { |
|
| 96 | + [$status, $error] = $this->validatePassword($password); |
|
| 97 | + if (!$status) { |
|
| 98 | + throw new ActiveRecordTraitException($error); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $passwordHash = \password_hash($password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH]); |
|
| 102 | + |
|
| 103 | + if ($passwordHash === false) { |
|
| 104 | + throw new ActiveRecordTraitException('\'Password\' hash failed.'); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $this->password = $passwordHash; |
|
| 108 | + |
|
| 109 | + return $this; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @return string The Hash of the password |
|
| 114 | + */ |
|
| 115 | + public function getPasswordHash() |
|
| 116 | + { |
|
| 117 | + return $this->password; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Returns the currently set password token for the entity, or null if not set |
|
| 122 | + * @return string|null The password reset token |
|
| 123 | + */ |
|
| 124 | + public function getPasswordResetToken() |
|
| 125 | + { |
|
| 126 | + return $this->passwordResetToken; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Generates a new password reset token for the user |
|
| 131 | + */ |
|
| 132 | + public function generatePasswordResetToken() |
|
| 133 | + { |
|
| 134 | + $this->passwordResetToken = md5(uniqid(mt_rand(), true)); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Clears the current password reset token |
|
| 139 | + */ |
|
| 140 | + public function clearPasswordResetToken() |
|
| 141 | + { |
|
| 142 | + $this->passwordResetToken = null; |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * @return void |
|
| 147 | - */ |
|
| 148 | - abstract protected function extendTableDefinition($columnName, $definition); |
|
| 145 | + /** |
|
| 146 | + * @return void |
|
| 147 | + */ |
|
| 148 | + abstract protected function extendTableDefinition($columnName, $definition); |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * @return void |
|
| 152 | - */ |
|
| 153 | - abstract protected function registerSearchHook($columnName, $fn); |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * @return void |
|
| 157 | - */ |
|
| 158 | - abstract protected function registerDeleteHook($columnName, $fn); |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @return void |
|
| 162 | - */ |
|
| 163 | - abstract protected function registerUpdateHook($columnName, $fn); |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * @return void |
|
| 167 | - */ |
|
| 168 | - abstract protected function registerReadHook($columnName, $fn); |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @return void |
|
| 172 | - */ |
|
| 173 | - abstract protected function registerCreateHook($columnName, $fn); |
|
| 150 | + /** |
|
| 151 | + * @return void |
|
| 152 | + */ |
|
| 153 | + abstract protected function registerSearchHook($columnName, $fn); |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * @return void |
|
| 157 | + */ |
|
| 158 | + abstract protected function registerDeleteHook($columnName, $fn); |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @return void |
|
| 162 | + */ |
|
| 163 | + abstract protected function registerUpdateHook($columnName, $fn); |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * @return void |
|
| 167 | + */ |
|
| 168 | + abstract protected function registerReadHook($columnName, $fn); |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @return void |
|
| 172 | + */ |
|
| 173 | + abstract protected function registerCreateHook($columnName, $fn); |
|
| 174 | 174 | |
| 175 | 175 | } |
| 176 | 176 | \ No newline at end of file |