merge_email_verifier #2

Merged
tracer merged 4 commits from merge_email_verifier into master 2022-11-01 16:36:51 +01:00
3 changed files with 45 additions and 61 deletions

View File

@ -7,7 +7,6 @@ use App\Form\ChangePasswordFormType;
use App\Form\RegistrationFormType; use App\Form\RegistrationFormType;
use App\Form\ResetPasswordRequestFormType; use App\Form\ResetPasswordRequestFormType;
use App\Repository\UserRepository; use App\Repository\UserRepository;
use App\Security\EmailVerifier;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Exception; use Exception;
use Symfony\Bridge\Twig\Mime\TemplatedEmail; 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\Exception\ResetPasswordExceptionInterface;
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface; use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface; use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
class SecurityController extends AbstractController class SecurityController extends AbstractController
{ {
use ResetPasswordControllerTrait; use ResetPasswordControllerTrait;
public function __construct(private readonly EmailVerifier $emailVerifier, public function __construct(private readonly ResetPasswordHelperInterface $resetPasswordHelper,
private readonly ResetPasswordHelperInterface $resetPasswordHelper, private readonly EntityManagerInterface $entityManager,
private readonly EntityManagerInterface $entityManager private readonly VerifyEmailHelperInterface $verifyEmailHelper,
private readonly MailerInterface $mailer,
) )
{ {
// empty body // empty body
@ -88,7 +89,7 @@ class SecurityController extends AbstractController
$entityManager->flush(); $entityManager->flush();
// generate a signed url and email it to the user // 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()) email: (new TemplatedEmail())
->from(new Address(address: 'info@24unix.net', name: '24unix.net')) ->from(new Address(address: 'info@24unix.net', name: '24unix.net'))
->to($user->getEmail()) ->to($user->getEmail())
@ -124,7 +125,7 @@ class SecurityController extends AbstractController
// validate email confirmation link, sets User::isVerified=true and persists // validate email confirmation link, sets User::isVerified=true and persists
try { try {
$this->emailVerifier->handleEmailConfirmation(request: $request, user: $user); $this->handleEmailConfirmation(request: $request, user: $user);
} catch (VerifyEmailExceptionInterface $exception) { } catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash(type: 'verify_email_error', message: $translator->trans(id: $exception->getReason(), parameters: [], domain: 'VerifyEmailBundle')); $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'); 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();
}
} }

View File

@ -38,7 +38,7 @@ class QuotesRepository extends ServiceEntityRepository
} }
} }
public function findOneRandom(): ?QueryBuilder public function findOneRandom(): ?Quotes
{ {
try { try {
$idLimits = $this->createQueryBuilder(alias: 'q') $idLimits = $this->createQueryBuilder(alias: 'q')

View File

@ -1,54 +0,0 @@
<?php
namespace App\Security;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
class EmailVerifier
{
public function __construct(
private readonly VerifyEmailHelperInterface $verifyEmailHelper,
private readonly MailerInterface $mailer,
private readonly EntityManagerInterface $entityManager
) {
}
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);
$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();
}
}