diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php index 22137cb..448d76b 100644 --- a/src/Controller/SecurityController.php +++ b/src/Controller/SecurityController.php @@ -7,7 +7,6 @@ use App\Form\ChangePasswordFormType; use App\Form\RegistrationFormType; use App\Form\ResetPasswordRequestFormType; use App\Repository\UserRepository; -use App\Security\EmailVerifier; use Doctrine\ORM\EntityManagerInterface; use Exception; use Symfony\Bridge\Twig\Mime\TemplatedEmail; @@ -26,14 +25,16 @@ use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait; use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface; use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface; use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface; +use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface; class SecurityController extends AbstractController { use ResetPasswordControllerTrait; - public function __construct(private readonly EmailVerifier $emailVerifier, - private readonly ResetPasswordHelperInterface $resetPasswordHelper, - private readonly EntityManagerInterface $entityManager + public function __construct(private readonly ResetPasswordHelperInterface $resetPasswordHelper, + private readonly EntityManagerInterface $entityManager, + private readonly VerifyEmailHelperInterface $verifyEmailHelper, + private readonly MailerInterface $mailer, ) { // empty body @@ -88,7 +89,7 @@ class SecurityController extends AbstractController $entityManager->flush(); // generate a signed url and email it to the user - $this->emailVerifier->sendEmailConfirmation(verifyEmailRouteName: 'security_verify_email', user: $user, + $this->sendEmailConfirmation(verifyEmailRouteName: 'security_verify_email', user: $user, email: (new TemplatedEmail()) ->from(new Address(address: 'info@24unix.net', name: '24unix.net')) ->to($user->getEmail()) @@ -124,7 +125,7 @@ class SecurityController extends AbstractController // validate email confirmation link, sets User::isVerified=true and persists try { - $this->emailVerifier->handleEmailConfirmation(request: $request, user: $user); + $this->handleEmailConfirmation(request: $request, user: $user); } catch (VerifyEmailExceptionInterface $exception) { $this->addFlash(type: 'verify_email_error', message: $translator->trans(id: $exception->getReason(), parameters: [], domain: 'VerifyEmailBundle')); @@ -284,4 +285,41 @@ class SecurityController extends AbstractController return $this->redirectToRoute(route: 'security_recovery_mail_sent'); } + + + public function sendEmailConfirmation(string $verifyEmailRouteName, User /* UserInterface */ $user, TemplatedEmail $email): void + { + $signatureComponents = $this->verifyEmailHelper->generateSignature( + routeName: $verifyEmailRouteName, + userId: $user->getId(), + userEmail: $user->getEmail(), + extraParams: ['id' => $user->getId()] + ); + + $context = $email->getContext(); + $context['signedUrl'] = $signatureComponents->getSignedUrl(); + $context['expiresAtMessageKey'] = $signatureComponents->getExpirationMessageKey(); + $context['expiresAtMessageData'] = $signatureComponents->getExpirationMessageData(); + + $email->context(context: $context); + + try { + $this->mailer->send(message: $email); + } catch (TransportExceptionInterface $e) { + die($e->getMessage()); + } + } + + /** + * @throws VerifyEmailExceptionInterface + */ + public function handleEmailConfirmation(Request $request, User /*UserInterface*/ $user): void + { + $this->verifyEmailHelper->validateEmailConfirmation(signedUrl: $request->getUri(), userId: $user->getId(), userEmail: $user->getEmail()); + + $user->setIsVerified(isVerified: true); + + $this->entityManager->persist(entity: $user); + $this->entityManager->flush(); + } } diff --git a/src/Repository/QuotesRepository.php b/src/Repository/QuotesRepository.php index c84ca94..1e3b7a3 100644 --- a/src/Repository/QuotesRepository.php +++ b/src/Repository/QuotesRepository.php @@ -38,7 +38,7 @@ class QuotesRepository extends ServiceEntityRepository } } - public function findOneRandom(): ?QueryBuilder + public function findOneRandom(): ?Quotes { try { $idLimits = $this->createQueryBuilder(alias: 'q') diff --git a/src/Security/EmailVerifier.php b/src/Security/EmailVerifier.php deleted file mode 100644 index 1d45afc..0000000 --- a/src/Security/EmailVerifier.php +++ /dev/null @@ -1,54 +0,0 @@ -verifyEmailHelper->generateSignature( - routeName: $verifyEmailRouteName, - userId: $user->getId(), - userEmail: $user->getEmail(), - extraParams: ['id' => $user->getId()] - ); - - $context = $email->getContext(); - $context['signedUrl'] = $signatureComponents->getSignedUrl(); - $context['expiresAtMessageKey'] = $signatureComponents->getExpirationMessageKey(); - $context['expiresAtMessageData'] = $signatureComponents->getExpirationMessageData(); - - $email->context(context: $context); - - $this->mailer->send(message: $email); - } - - /** - * @throws VerifyEmailExceptionInterface - */ - public function handleEmailConfirmation(Request $request, User /*UserInterface*/ $user): void - { - $this->verifyEmailHelper->validateEmailConfirmation(signedUrl: $request->getUri(), userId: $user->getId(), userEmail: $user->getEmail()); - - $user->setIsVerified(isVerified: true); - - $this->entityManager->persist(entity: $user); - $this->entityManager->flush(); - } -}