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