upstream V3 development https://www.gnusocial.rocks/v3
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

60 lignes
1.9 KiB

  1. <?php
  2. declare(strict_types = 1);
  3. namespace App\Security;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  10. use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
  11. class EmailVerifier
  12. {
  13. private $verifyEmailHelper;
  14. private $mailer;
  15. private $entityManager;
  16. public function __construct(VerifyEmailHelperInterface $helper, MailerInterface $mailer, EntityManagerInterface $manager)
  17. {
  18. $this->verifyEmailHelper = $helper;
  19. $this->mailer = $mailer;
  20. $this->entityManager = $manager;
  21. }
  22. public function sendEmailConfirmation(string $verifyEmailRouteName, UserInterface $user, TemplatedEmail $email): void
  23. {
  24. $signatureComponents = $this->verifyEmailHelper->generateSignature(
  25. $verifyEmailRouteName,
  26. $user->getId(),
  27. $user->getOutgoingEmail(),
  28. ['id' => $user->getId()],
  29. );
  30. $context = $email->getContext();
  31. $context['signedUrl'] = $signatureComponents->getSignedUrl();
  32. $context['expiresAtMessageKey'] = $signatureComponents->getExpirationMessageKey();
  33. $context['expiresAtMessageData'] = $signatureComponents->getExpirationMessageData();
  34. $email->context($context);
  35. $this->mailer->send($email);
  36. }
  37. /**
  38. * @throws VerifyEmailExceptionInterface
  39. */
  40. public function handleEmailConfirmation(Request $request, UserInterface $user): void
  41. {
  42. $this->verifyEmailHelper->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getOutgoingEmail());
  43. $user->setIsVerified(true);
  44. $this->entityManager->persist($user);
  45. $this->entityManager->flush();
  46. }
  47. }