merged EmailVerifier

This commit is contained in:
tracer 2022-11-01 16:34:47 +01:00
parent d83ed5b659
commit b1ce69cc47
1 changed files with 43 additions and 3 deletions

View File

@ -26,6 +26,7 @@ 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
{ {
@ -33,7 +34,9 @@ class SecurityController extends AbstractController
public function __construct(private readonly EmailVerifier $emailVerifier, public function __construct(private readonly EmailVerifier $emailVerifier,
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 +91,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 +127,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 +287,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();
}
} }