src/Controller/RegistrationController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Ringer;
  4. use App\Entity\User;
  5. use App\Form\RegistrationFormType;
  6. use App\Service\LoggerService;
  7. use App\Security\EmailVerifier;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Exception;
  10. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Mime\Address;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  19. class RegistrationController extends AbstractController
  20. {
  21.     public function __construct(
  22.         private EmailVerifier $emailVerifier,
  23.         private LoggerService $logger
  24.     ) {
  25.     }
  26.     /**
  27.      * @Route(
  28.      *      {
  29.      *          "es": "/{_locale}/register",
  30.      *          "ca": "/{_locale}/register",
  31.      *          "eu": "/{_locale}/register",
  32.      *      },
  33.      *      name="app_register",
  34.      *      requirements={"_locale": "%app.supported_locales%"}
  35.      * )
  36.      */
  37.     public function register(
  38.         Request $request,
  39.         UserPasswordHasherInterface $userPasswordHasher,
  40.         EntityManagerInterface $entityManager,
  41.         TranslatorInterface $translator
  42.     ): Response {
  43.         if ($this->getUser()) {
  44.             return $this->redirectToRoute('app_actAs');
  45.         }
  46.         $user = new User();
  47.         $form $this->createForm(RegistrationFormType::class, $user);
  48.         $form->handleRequest($request);
  49.         if ($form->isSubmitted() && $form->isValid()) {
  50.             // encode the plain password
  51.             $user->setPassword(
  52.                 $userPasswordHasher->hashPassword(
  53.                     $user,
  54.                     $form->get('plainPassword')->getData()
  55.                 )
  56.             );
  57.             $user->setHashAux("");
  58.             try {
  59.                 $entityManager->persist($user);
  60.                 $entityManager->flush();
  61.                 $this->logger->info('usuario registrado:', ['Name' => $user->getName()]);
  62.             } catch (Exception $e) {
  63.                 dd($e);
  64.             }
  65.             $ringer $entityManager->getRepository(Ringer::class)->findOneBy(['identityDocument' => $user->getDni()]);
  66.             if (isset($ringer)) {
  67.                 $ringer->setUser($user);
  68.                 $entityManager->persist($ringer);
  69.                 $entityManager->flush();
  70.             }
  71.             $office $entityManager->getRepository(User::class)->findOffice($user->getInstitution()->getId());
  72.             $sender_email $this->getParameter('sender_email');
  73.             $subject $translator->trans('email.ringer_platform_record');
  74.             if (isset($office[0])) {
  75.                 $office $office[0];
  76.                 // generate a signed url and email it to the user
  77.                 $this->emailVerifier->sendEmailConfirmation(
  78.                     'app_verify_email',
  79.                     $user,
  80.                     (new TemplatedEmail())
  81.                         ->from(new Address(
  82.                             $sender_email,
  83.                             $translator->trans('login.platform')
  84.                         ))
  85.                         ->to($office->getEmail())
  86.                         ->subject($subject)
  87.                         ->htmlTemplate('registration/confirmation_email.html.twig')
  88.                         ->context([
  89.                             'user' => $user,
  90.                         ])
  91.                 );
  92.             }
  93.             // send the email
  94.             $this->emailVerifier->sendEmailConfirmation(
  95.                 'app_verify_email',
  96.                 $user,
  97.                 (new TemplatedEmail())
  98.                     ->from(new Address(
  99.                         $sender_email,
  100.                         $translator->trans('login.platform')
  101.                     ))
  102.                     ->to($user->getEmail())
  103.                     ->subject($subject)
  104.                     ->htmlTemplate('registration/notify.html.twig')
  105.                     ->context([
  106.                         'user' => $user,
  107.                     ])
  108.             );
  109.             return $this->redirectToRoute('app_waiting_verify');
  110.         }
  111.         return $this->render('registration/register.html.twig', [
  112.             'registrationForm' => $form->createView(),
  113.         ]);
  114.     }
  115.     /**
  116.      * @Route("/verify/email", name="app_verify_email")
  117.      */
  118.     public function verifyUserEmail(Request $requestTranslatorInterface $translator): Response
  119.     {
  120.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  121.         // validate email confirmation link, sets User::isVerified=true and persists
  122.         try {
  123.             $this->emailVerifier->handleEmailConfirmation($request$this->getUser());
  124.         } catch (VerifyEmailExceptionInterface $exception) {
  125.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  126.             return $this->redirectToRoute('app_register');
  127.         }
  128.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  129.         $this->addFlash('success''Your email address has been verified.');
  130.         // ! TODO , verificar si el dd se ejecuta en algún sitio,
  131.         // ! creo que la variable $user no existe
  132.         // ! creo que este código no se ejecuta nunca
  133.         // dd("User verification", $request, $this->getUser());
  134.         // $this->logger->info('Usuario verificado', ['Id' => $user->getId()]);
  135.         $this->logger->info('Usuario verificado - method: verifyUserEmail', ['Id' => 'desconocido']);
  136.         return $this->redirectToRoute('app_register');
  137.     }
  138. }