src/Controller/RegistrationController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Form\SuperAdminRegistrationFormType;
  6. use App\Repository\UserRepository;
  7. use App\Security\UserAuthenticator;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\Form\FormInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mailer\MailerInterface;
  14. use Symfony\Component\Mime\Email;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  18. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  19. use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
  20. class RegistrationController extends AbstractController
  21. {
  22.     #[Route('/register'name'app_register')]
  23.     public function register(MailerInterface $mailerRequest $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorVerifyEmailHelperInterface $verifyEmailHelperUserAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  24.     {
  25.         $user = new User();
  26.         $form $this->createForm(RegistrationFormType::class, $user);
  27.         $form->handleRequest($request);
  28.         if ($form->isSubmitted() && $form->isValid()) {
  29.             if(($form->get('role')->getData()) == 'ROLE_CANDIDAT') {
  30.                 return $this->createUser($mailer$user$userPasswordHasher$form$entityManager$userAuthenticator$verifyEmailHelper$authenticator$requestfalsefalsetrue);
  31.             }
  32.             elseif (($form->get('role')->getData()) == 'ROLE_ADMIN_STRUCTURE') {
  33.                 return $this->createUser($mailer$user$userPasswordHasher$form$entityManager$userAuthenticator$verifyEmailHelper$authenticator$requestfalsetruefalse);
  34.             }
  35.         }
  36.         return $this->render('registration/register.html.twig', [
  37.             'registrationForm' => $form->createView(),
  38.         ]);
  39.     }
  40.     #[Route('/superAdminRegister'name'app_admin_register')]
  41.     public function superAdminRegister(MailerInterface $mailerRequest $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorVerifyEmailHelperInterface $verifyEmailHelperUserAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  42.     {
  43.         $user = new User();
  44.         $form $this->createForm(SuperAdminRegistrationFormType::class, $user);
  45.         $form->handleRequest($request);
  46.         if ($form->isSubmitted() && $form->isValid()) {
  47.             return $this->createUser($mailer$user$userPasswordHasher$form$entityManager$userAuthenticator$verifyEmailHelper$authenticator$requesttruefalsefalse);
  48.         }
  49.         return $this->render('registration/superAdminRegister.html.twig', [
  50.             'registrationForm' => $form->createView(),
  51.         ]);
  52.     }
  53.     /**
  54.      * @param User $user
  55.      * @param UserPasswordHasherInterface $userPasswordHasher
  56.      * @param FormInterface $form
  57.      * @param EntityManagerInterface $entityManager
  58.      * @param UserAuthenticatorInterface $userAuthenticator
  59.      * @param UserAuthenticator $authenticator
  60.      * @param Request $request
  61.      * @return Response|null
  62.      */
  63.     public function createUser(MailerInterface $mailerUser $userUserPasswordHasherInterface $userPasswordHasherFormInterface $formEntityManagerInterface $entityManagerUserAuthenticatorInterface $userAuthenticatorVerifyEmailHelperInterface $verifyEmailHelperUserAuthenticator $authenticatorRequest $requestbool $isSuperAdminbool $isAdminStructbool $isCandidat): ?Response
  64.     {
  65.         $user->setPassword(
  66.             $userPasswordHasher->hashPassword(
  67.                 $user,
  68.                 $form->get('password')->getData(),
  69.             )
  70.         );
  71.         $role = array($form->get('role')->getData());
  72.         $user->setRoles($role);
  73.         $nom $form->get('lastName')->getData();
  74.         $prenom $form->get('firstName')->getData();
  75.         $structure $form->get('nomStructure')->getData();
  76.         $user->setLastName($nom);
  77.         $user->setFirstName($prenom);
  78.         if ($isAdminStruct && !$isCandidat){
  79.             $user->addStructureUser($structure);
  80.         }
  81.         $entityManager->persist($user);
  82.         $entityManager->flush();
  83.         // do anything else you need here, like send an email
  84. //        $signatureComponents = $verifyEmailHelper->generateSignature(
  85. //            'app_verify_email',
  86. //            $user->getId(),
  87. //            $user->getEmail(),
  88. //            ['id' => $user->getId()]
  89. //        );
  90. //
  91. //        $email = (new Email())
  92. //            ->from('MonAdresseTest@example.com')
  93. //            ->to($user->getEmail())
  94. //            ->subject('Vérification de votre adresse mail')
  95. //            ->text("Finaliser votre inscription en cliquant sur ce lien : {$signatureComponents->getSignedUrl()}");
  96. //
  97. //        $mailer->send($email);
  98.         if (!$isSuperAdmin)
  99.         {
  100.             return $userAuthenticator->authenticateUser// à supprimer pour la vérification par mail
  101.                 $user,
  102.                 $authenticator,
  103.                 $request
  104.             );
  105. //            return $this->redirectToRoute('app_home');
  106.         }
  107.         return $this->redirectToRoute('app_alluser');
  108.     }
  109.     #[Route('/verifyEmail',name'app_verify_email')]
  110.     public function verifyUserEmail(Request $requestVerifyEmailHelperInterface $verifyEmailHelperUserRepository $userRepositoryEntityManagerInterface $entityManager)
  111.     {
  112.         $user $userRepository->find($request->query->get('id'));
  113.         if (!$user) {
  114.             throw $this->createNotFoundException();
  115.         }
  116.         try {
  117.             $verifyEmailHelper->validateEmailConfirmation(
  118.                 $request->getUri(),
  119.                 $user->getId(),
  120.                 $user->getEmail(),
  121.             );
  122.         } catch (VerifyEmailExceptionInterface $e) {
  123.             $this->addFlash('error'$e->getReason());
  124.             return $this->redirectToRoute('app_register');
  125.         }
  126.         $user->setIsVerified(true);
  127.         $entityManager->flush();
  128.         $this->addFlash('success''Le compte a été vérifié avec succès, vous pouvez désormais vous connecter !');
  129.         return $this->redirectToRoute('app_login');
  130.     }
  131. }