2020-07-25 02:06:55 +00:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2020-07-25 02:06:55 +00:00
|
|
|
namespace App\Security;
|
|
|
|
|
2021-11-16 14:48:18 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-07-25 02:06:55 +00:00
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-07-29 17:26:14 +00:00
|
|
|
use Symfony\Component\Mailer\MailerInterface;
|
2021-11-16 14:48:18 +00:00
|
|
|
use Symfony\Component\Security\Core\User\UserInterface;
|
2020-07-25 02:06:55 +00:00
|
|
|
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
|
|
|
|
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
|
|
|
|
|
2021-11-16 14:48:18 +00:00
|
|
|
class EmailVerifier
|
2020-07-25 02:06:55 +00:00
|
|
|
{
|
2021-11-16 14:48:18 +00:00
|
|
|
private $verifyEmailHelper;
|
|
|
|
private $mailer;
|
|
|
|
private $entityManager;
|
2021-07-29 17:26:14 +00:00
|
|
|
|
2021-11-16 14:48:18 +00:00
|
|
|
public function __construct(VerifyEmailHelperInterface $helper, MailerInterface $mailer, EntityManagerInterface $manager)
|
2021-07-29 17:26:14 +00:00
|
|
|
{
|
2021-11-16 14:48:18 +00:00
|
|
|
$this->verifyEmailHelper = $helper;
|
|
|
|
$this->mailer = $mailer;
|
|
|
|
$this->entityManager = $manager;
|
2021-07-29 17:26:14 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 14:48:18 +00:00
|
|
|
public function sendEmailConfirmation(string $verifyEmailRouteName, UserInterface $user, TemplatedEmail $email): void
|
2020-07-25 02:06:55 +00:00
|
|
|
{
|
2021-11-16 14:48:18 +00:00
|
|
|
$signatureComponents = $this->verifyEmailHelper->generateSignature(
|
|
|
|
$verifyEmailRouteName,
|
2020-07-25 02:06:55 +00:00
|
|
|
$user->getId(),
|
2021-10-10 09:26:18 +01:00
|
|
|
$user->getOutgoingEmail(),
|
2021-11-16 14:48:18 +00:00
|
|
|
['id' => $user->getId()],
|
2020-07-25 02:06:55 +00:00
|
|
|
);
|
|
|
|
|
2021-11-16 14:48:18 +00:00
|
|
|
$context = $email->getContext();
|
|
|
|
$context['signedUrl'] = $signatureComponents->getSignedUrl();
|
|
|
|
$context['expiresAtMessageKey'] = $signatureComponents->getExpirationMessageKey();
|
|
|
|
$context['expiresAtMessageData'] = $signatureComponents->getExpirationMessageData();
|
2020-07-25 02:06:55 +00:00
|
|
|
|
|
|
|
$email->context($context);
|
|
|
|
|
2021-11-16 14:48:18 +00:00
|
|
|
$this->mailer->send($email);
|
2020-07-25 02:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws VerifyEmailExceptionInterface
|
|
|
|
*/
|
2021-11-16 14:48:18 +00:00
|
|
|
public function handleEmailConfirmation(Request $request, UserInterface $user): void
|
2020-07-25 02:06:55 +00:00
|
|
|
{
|
2021-11-16 14:48:18 +00:00
|
|
|
$this->verifyEmailHelper->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getOutgoingEmail());
|
2021-07-29 17:26:14 +00:00
|
|
|
|
2021-11-16 14:48:18 +00:00
|
|
|
$user->setIsVerified(true);
|
2021-07-29 17:26:14 +00:00
|
|
|
|
2021-11-16 14:48:18 +00:00
|
|
|
$this->entityManager->persist($user);
|
|
|
|
$this->entityManager->flush();
|
2021-07-29 17:26:14 +00:00
|
|
|
}
|
2020-07-25 02:06:55 +00:00
|
|
|
}
|