|
3 | 3 | namespace HiEvents\Services\Application\Handlers\Auth; |
4 | 4 |
|
5 | 5 | use HiEvents\DomainObjects\UserDomainObject; |
| 6 | +use HiEvents\Exceptions\PasswordInvalidException; |
6 | 7 | use HiEvents\Mail\User\ResetPasswordSuccess; |
7 | 8 | use HiEvents\Repository\Interfaces\PasswordResetTokenRepositoryInterface; |
8 | 9 | use HiEvents\Repository\Interfaces\UserRepositoryInterface; |
9 | 10 | use HiEvents\Services\Application\Handlers\Auth\DTO\ResetPasswordDTO; |
10 | 11 | use HiEvents\Services\Domain\Auth\ResetPasswordTokenValidateService; |
| 12 | +use Illuminate\Contracts\Mail\Mailer; |
11 | 13 | use Illuminate\Database\DatabaseManager; |
12 | 14 | use Illuminate\Hashing\HashManager; |
13 | | -use Illuminate\Mail\Mailer; |
14 | 15 | use Psr\Log\LoggerInterface; |
15 | 16 | use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
16 | 17 | use Throwable; |
@@ -38,13 +39,22 @@ public function handle(ResetPasswordDTO $resetPasswordData): void |
38 | 39 | $resetToken = $this->passwordTokenValidateService->validateAndFetchToken($resetPasswordData->token); |
39 | 40 | $user = $this->validateUser($resetToken->getEmail()); |
40 | 41 |
|
| 42 | + if ($this->checkNewPasswordIsOldPassword($user, $resetPasswordData->password)) { |
| 43 | + throw new PasswordInvalidException(__('New password must be different from the old password.')); |
| 44 | + } |
| 45 | + |
41 | 46 | $this->resetUserPassword($user->getId(), $resetPasswordData->password); |
42 | 47 | $this->deleteResetToken($resetToken->getEmail()); |
43 | 48 | $this->logResetPasswordSuccess($user); |
44 | 49 | $this->sendResetPasswordEmail($user); |
45 | 50 | }); |
46 | 51 | } |
47 | 52 |
|
| 53 | + private function checkNewPasswordIsOldPassword(UserDomainObject $user, string $newPassword): bool |
| 54 | + { |
| 55 | + return $this->hashManager->check($newPassword, $user->getPassword()); |
| 56 | + } |
| 57 | + |
48 | 58 | private function validateUser(string $email): UserDomainObject |
49 | 59 | { |
50 | 60 | $user = $this->userRepository->findFirstWhere(['email' => $email]); |
@@ -72,7 +82,7 @@ private function deleteResetToken(string $email): void |
72 | 82 | $this->passwordResetTokenRepository->deleteWhere(['email' => $email]); |
73 | 83 | } |
74 | 84 |
|
75 | | - private function logResetPasswordSuccess($user): void |
| 85 | + private function logResetPasswordSuccess(UserDomainObject $user): void |
76 | 86 | { |
77 | 87 | $this->logger->info('Password reset successfully', [ |
78 | 88 | 'user_id' => $user->getId(), |
|
0 commit comments