|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Domain\Identity\Service; |
| 6 | + |
| 7 | +use DateTime; |
| 8 | +use PhpList\Core\Domain\Identity\Model\AdminPasswordRequest; |
| 9 | +use PhpList\Core\Domain\Identity\Model\Administrator; |
| 10 | +use PhpList\Core\Domain\Identity\Repository\AdminPasswordRequestRepository; |
| 11 | +use PhpList\Core\Domain\Identity\Repository\AdministratorRepository; |
| 12 | +use PhpList\Core\Domain\Messaging\Message\PasswordResetMessage; |
| 13 | +use PhpList\Core\Security\HashGenerator; |
| 14 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 15 | +use Symfony\Component\Messenger\MessageBusInterface; |
| 16 | + |
| 17 | +class PasswordManager |
| 18 | +{ |
| 19 | + private const TOKEN_EXPIRY = '+24 hours'; |
| 20 | + |
| 21 | + private AdminPasswordRequestRepository $passwordRequestRepository; |
| 22 | + private AdministratorRepository $administratorRepository; |
| 23 | + private HashGenerator $hashGenerator; |
| 24 | + private MessageBusInterface $messageBus; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + AdminPasswordRequestRepository $passwordRequestRepository, |
| 28 | + AdministratorRepository $administratorRepository, |
| 29 | + HashGenerator $hashGenerator, |
| 30 | + MessageBusInterface $messageBus |
| 31 | + ) { |
| 32 | + $this->passwordRequestRepository = $passwordRequestRepository; |
| 33 | + $this->administratorRepository = $administratorRepository; |
| 34 | + $this->hashGenerator = $hashGenerator; |
| 35 | + $this->messageBus = $messageBus; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Generates a password reset token for the administrator with the given email. |
| 40 | + * Returns the token that should be sent to the user via email. |
| 41 | + * |
| 42 | + * @param string $email The email of the administrator |
| 43 | + * @return string The generated token |
| 44 | + * @throws NotFoundHttpException If no administrator with the given email exists |
| 45 | + */ |
| 46 | + public function generatePasswordResetToken(string $email): string |
| 47 | + { |
| 48 | + $administrator = $this->administratorRepository->findOneBy(['email' => $email]); |
| 49 | + if ($administrator === null) { |
| 50 | + throw new NotFoundHttpException('Administrator not found', null, 1500567100); |
| 51 | + } |
| 52 | + |
| 53 | + $existingRequests = $this->passwordRequestRepository->findByAdmin($administrator); |
| 54 | + foreach ($existingRequests as $request) { |
| 55 | + $this->passwordRequestRepository->remove($request); |
| 56 | + } |
| 57 | + |
| 58 | + $token = md5(random_bytes(256)); |
| 59 | + |
| 60 | + $expiryDate = new DateTime(self::TOKEN_EXPIRY); |
| 61 | + $passwordRequest = new AdminPasswordRequest(date: $expiryDate, admin: $administrator, keyValue: $token); |
| 62 | + |
| 63 | + $this->passwordRequestRepository->save($passwordRequest); |
| 64 | + |
| 65 | + $message = new PasswordResetMessage(email: $email, token: $token); |
| 66 | + $this->messageBus->dispatch($message); |
| 67 | + |
| 68 | + return $token; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Validates a password reset token. |
| 73 | + * Returns the administrator if the token is valid, null otherwise. |
| 74 | + * |
| 75 | + * @param string $token The token to validate |
| 76 | + * @return Administrator|null The administrator if the token is valid, null otherwise |
| 77 | + */ |
| 78 | + public function validatePasswordResetToken(string $token): ?Administrator |
| 79 | + { |
| 80 | + $passwordRequest = $this->passwordRequestRepository->findOneByToken($token); |
| 81 | + if ($passwordRequest === null) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + $now = new DateTime(); |
| 86 | + if ($now >= $passwordRequest->getDate()) { |
| 87 | + $this->passwordRequestRepository->remove($passwordRequest); |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + return $passwordRequest->getAdmin(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Updates the password for the administrator with the given token. |
| 96 | + * Returns true if the password was updated successfully, false otherwise. |
| 97 | + * |
| 98 | + * @param string $token The password reset token |
| 99 | + * @param string $newPassword The new password |
| 100 | + * @return bool True if the password was updated successfully, false otherwise |
| 101 | + */ |
| 102 | + public function updatePasswordWithToken(string $token, string $newPassword): bool |
| 103 | + { |
| 104 | + $administrator = $this->validatePasswordResetToken($token); |
| 105 | + if ($administrator === null) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + $passwordHash = $this->hashGenerator->createPasswordHash($newPassword); |
| 110 | + $administrator->setPasswordHash($passwordHash); |
| 111 | + $this->administratorRepository->save($administrator); |
| 112 | + |
| 113 | + $passwordRequest = $this->passwordRequestRepository->findOneByToken($token); |
| 114 | + $this->passwordRequestRepository->remove($passwordRequest); |
| 115 | + |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Cleans up expired password reset requests. |
| 121 | + */ |
| 122 | + public function cleanupExpiredTokens(): void |
| 123 | + { |
| 124 | + $now = new DateTime(); |
| 125 | + $allRequests = $this->passwordRequestRepository->findAll(); |
| 126 | + foreach ($allRequests as $request) { |
| 127 | + if ($now >= $request->getDate()) { |
| 128 | + $this->passwordRequestRepository->remove($request); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments