mirror of
https://github.com/delight-im/PHP-Auth.git
synced 2025-08-04 07:07:25 +02:00
Re-implement 'changePassword' method using two existing methods
Make use of 'reconfirmPassword' and 'changePasswordWithoutOldPassword'
This commit is contained in:
43
src/Auth.php
43
src/Auth.php
@@ -592,49 +592,20 @@ final class Auth extends UserManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Changes the (currently logged-in) user's password
|
* Changes the currently signed-in user's password while requiring the old password for verification
|
||||||
*
|
*
|
||||||
* @param string $oldPassword the old password to verify account ownership
|
* @param string $oldPassword the old password to verify account ownership
|
||||||
* @param string $newPassword the new password that should be used
|
* @param string $newPassword the new password that should be set
|
||||||
* @throws NotLoggedInException if the user is not currently logged in
|
* @throws NotLoggedInException if the user is not currently signed in
|
||||||
* @throws InvalidPasswordException if either the old password was wrong or the new password was invalid
|
* @throws InvalidPasswordException if either the old password has been wrong or the desired new one has been invalid
|
||||||
* @throws AuthError if an internal problem occurred (do *not* catch)
|
* @throws AuthError if an internal problem occurred (do *not* catch)
|
||||||
*/
|
*/
|
||||||
public function changePassword($oldPassword, $newPassword) {
|
public function changePassword($oldPassword, $newPassword) {
|
||||||
if ($this->isLoggedIn()) {
|
if ($this->reconfirmPassword($oldPassword)) {
|
||||||
$oldPassword = self::validatePassword($oldPassword);
|
$this->changePasswordWithoutOldPassword($newPassword);
|
||||||
$newPassword = self::validatePassword($newPassword);
|
|
||||||
|
|
||||||
$userId = $this->getUserId();
|
|
||||||
|
|
||||||
try {
|
|
||||||
$passwordInDatabase = $this->db->selectValue(
|
|
||||||
'SELECT password FROM ' . $this->dbTablePrefix . 'users WHERE id = ?',
|
|
||||||
[ $userId ]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
catch (Error $e) {
|
|
||||||
throw new DatabaseError();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($passwordInDatabase)) {
|
|
||||||
if (password_verify($oldPassword, $passwordInDatabase)) {
|
|
||||||
// update the password in the database
|
|
||||||
$this->updatePassword($userId, $newPassword);
|
|
||||||
|
|
||||||
// delete any remaining remember directives
|
|
||||||
$this->deleteRememberDirective($userId);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new InvalidPasswordException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new NotLoggedInException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new NotLoggedInException();
|
throw new InvalidPasswordException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user