1 | <?php |
||
58 | trait PasswordTrait |
||
59 | { |
||
60 | /** |
||
61 | * User password. |
||
62 | * |
||
63 | * The property is only used to update the {@link $password_hash} property when the |
||
64 | * record is saved. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected function set_password($password) |
||
69 | { |
||
70 | if (!$password) |
||
71 | { |
||
72 | $this->password_hash = null; |
||
73 | |||
74 | return; |
||
75 | } |
||
76 | |||
77 | $this->password_hash = $this->hash_password($password); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * User password hash. |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | private $password_hash; |
||
86 | |||
87 | /** |
||
88 | * Checks if the password hash is a legacy hash, and not a hash created by |
||
89 | * the {@link \password_hash()} function. |
||
90 | * |
||
91 | * @return bool|null `true` if the password hash is a legacy hash, `false` if the password |
||
92 | * hash was created by the {@link \password_hash()} function, and `null` if the password hash |
||
93 | * is empty. |
||
94 | */ |
||
95 | protected function get_has_legacy_password_hash() |
||
96 | { |
||
97 | if (!$this->password_hash) |
||
98 | { |
||
99 | return null; |
||
100 | } |
||
101 | |||
102 | return $this->password_hash[0] != '$'; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Hashes a password. |
||
107 | * |
||
108 | * @param string $password |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | static public function hash_password($password) |
||
116 | |||
117 | /** |
||
118 | * Compares a password to the user's password hash. |
||
119 | * |
||
120 | * @param string $password |
||
121 | * |
||
122 | * @return bool `true` if the hashed password matches the user's password hash, |
||
123 | * `false` otherwise. |
||
124 | */ |
||
125 | public function verify_password($password) |
||
145 | } |
||
146 |