src/Controller/RegistrationController.php line 95

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Repository\ArrayJsonRepository;
  6. use App\Repository\ConfigurationRepository;
  7. use App\Repository\UserRepository;
  8. use App\Security\EmailVerifier;
  9. use App\Security\UserAuthenticator;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  20. class RegistrationController extends AbstractController
  21. {
  22.     private EmailVerifier $emailVerifier;
  23.     public function __construct(EmailVerifier $emailVerifier)
  24.     {
  25.         $this->emailVerifier $emailVerifier;
  26.     }
  27.     #[Route('/register'name'app_register')]
  28.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorUserAuthenticator $authenticatorEntityManagerInterface $entityManagerConfigurationRepository $configurationRepositoryArrayJsonRepository $arrayJsonRepositoryUserRepository $userRepository): Response
  29.     {
  30.         $dynamicLinkArray $arrayJsonRepository->findOneBy(['name' => 'dynamic_link']);
  31.         if ($dynamicLinkArray == null) {
  32.             $dynamicLink null;
  33.         } else {
  34.             $dynamicLink $dynamicLinkArray->getContent() ?? null;
  35.         }
  36.         $skeleton $arrayJsonRepository->findOneBy(['name' => 'skeleton_template']);
  37.         $array $skeleton->getContent();
  38.         if (!isset($array['background_mode'])) {
  39.             $array['background_mode'] = true;
  40.         }
  41.         $backgroundMode $array['background_mode'];
  42.         $config $configurationRepository->findOneBy([]);
  43.         $user = new User();
  44.         $form $this->createForm(RegistrationFormType::class, $user);
  45.         $form->handleRequest($request);
  46.         if ($form->isSubmitted() && $form->isValid()) {
  47.             $isFirstUser false;
  48.             if ($userRepository->count([]) === 0) {
  49.                 $isFirstUser true;
  50.                 $user->setRoles(['ROLE_ADMIN_SUP']);
  51.             }
  52.             // encode the plain password
  53.             $user->setPassword(
  54.                 $userPasswordHasher->hashPassword(
  55.                     $user,
  56.                     $form->get('plainPassword')->getData()
  57.                 )
  58.             );
  59.             $entityManager->persist($user);
  60.             $entityManager->flush();
  61.             // generate a signed url and email it to the user
  62.             $this->emailVerifier->sendEmailConfirmation(
  63.                 'app_verify_email',
  64.                 $user,
  65.                 (new TemplatedEmail())
  66.                     // ->from(new Address('no-reply@impulse-web-design', 'Impulse Web Design'))
  67.                     ->from($config->getEmailSender())
  68.                     ->to($user->getEmail())
  69.                     ->subject('Confirmez votre inscription')
  70.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  71.             );
  72.             // do anything else you need here, like send an email
  73.             return $userAuthenticator->authenticateUser(
  74.                 $user,
  75.                 $authenticator,
  76.                 $request
  77.             );
  78.         }
  79.         return $this->render('registration/register.html.twig', [
  80.             'registrationForm' => $form->createView(),
  81.             'dynamicLink' => $dynamicLink,
  82.             'backgroundmode' => $backgroundMode
  83.         ]);
  84.     }
  85.     #[Route('/verify/email'name'app_verify_email')]
  86.     public function verifyUserEmail(Request $requestTranslatorInterface $translator): Response
  87.     {
  88.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  89.         // validate email confirmation link, sets User::isVerified=true and persists
  90.         try {
  91.             $this->emailVerifier->handleEmailConfirmation($request$this->getUser());
  92.         } catch (VerifyEmailExceptionInterface $exception) {
  93.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  94.             return $this->redirectToRoute('app_register');
  95.         }
  96.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  97.         $this->addFlash('success''Your email address has been verified.');
  98.         return $this->redirectToRoute('app_home');
  99.     }
  100. }