Skip to content
Snippets Groups Projects
Select Git revision
  • 07ed81324a168c9eeea5d48c194ef04155cc17a0
  • main default
  • 35-cgu
  • 34-peertube-support
  • 27-add-autoplay-to-iframe
  • 33-bug-on-youtube-embed-urls
  • RC-Rekall-v1.1-fix_lpo
  • tuleap-140-go-back-to-my-capsules-page-when-i-m-on-capsule-preview-page
  • RC-Rekall-v1.2-fix10
  • RC-Rekall-v1.2-fix9
  • RC-Rekall-v1.2-fix8
  • RC-Rekall-v1.2-fix7
  • RC-Rekall-v1.2-fix6
  • RC-Rekall-v1.2-fix5
  • RC-Rekall-v1.2-fix4
  • RC-Rekall-v1.2-fix3
  • RC-Rekall-v1.2-fix2
  • RC-Rekall-v1.2-fix1
  • RC-Rekall-v1.1-fix-3
  • RC-Rekall-v1.1-fix-2
  • RC-Rekall-v1.1-fix-1
  • RC-Rekall-v1.1-delivered
  • preprod20220209-1535
23 results

EmailVerifier.php

Blame
  • EmailVerifier.php 2.01 KiB
    <?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 SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
    use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
    
    class EmailVerifier
    {
        private VerifyEmailHelperInterface $verifyEmailHelper;
        private MailerInterface $mailer;
        private EntityManagerInterface $entityManager;
    
        public function __construct(
            VerifyEmailHelperInterface $helper,
            MailerInterface $mailer,
            EntityManagerInterface $manager
        ) {
            $this->verifyEmailHelper = $helper;
            $this->mailer = $mailer;
            $this->entityManager = $manager;
        }
    
        public function sendEmailConfirmation(
            string $verifyEmailRouteName,
            int $user_id,
            string $email_address,
            TemplatedEmail $email
        ): void {
            $signatureComponents = $this->verifyEmailHelper->generateSignature(
                $verifyEmailRouteName,
                (string) $user_id,
                $email_address,
                ['id' => $user_id]
            );
    
            $context = $email->getContext();
            $context['signedUrl'] = $signatureComponents->getSignedUrl();
            $context['expiresAtMessageKey'] = $signatureComponents->getExpirationMessageKey();
            $context['expiresAtMessageData'] = $signatureComponents->getExpirationMessageData();
    
            $email->context($context);
    
            $this->mailer->send($email);
        }
    
        /**
         * @throws VerifyEmailExceptionInterface
         */
        public function handleEmailConfirmation(
            Request $request,
            User $user
        ): void {
            $this->verifyEmailHelper->validateEmailConfirmation(
                $request->getUri(),
                (string) $user->getId(),
                $user->getEmail()
            );
    
            $user->setIsVerified(true);
    
            $this->entityManager->persist($user);
            $this->entityManager->flush();
        }
    }