src/Controller/AuthController.php line 98

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ForumUser;
  4. use App\Entity\Profile;
  5. use App\Entity\Society;
  6. use App\Entity\User;
  7. use App\Form\FreelanceRegisterFormType;
  8. use App\Form\FreelanceRegisterMobileFormType;
  9. use App\Form\RegistrationFormType;
  10. use App\Form\ResetPasswordRequestFormType;
  11. use App\Form\SocietyRegisterFormType;
  12. use App\Security\AppAuthenticator;
  13. use App\Security\EmailVerifier;
  14. use App\Service\Forum\ForumUserService;
  15. use App\Service\Upload\AvatarUploader;
  16. use App\Service\Upload\PDFUploader;
  17. use DateTime;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Exception;
  20. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  21. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Symfony\Component\Form\FormError;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Symfony\Component\Mime\Address;
  27. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. use Symfony\Component\Security\Core\User\UserInterface;
  30. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  31. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  32. class AuthController extends AbstractController
  33. {
  34.     /**
  35.      * @var EmailVerifier
  36.      */
  37.     private EmailVerifier $emailVerifier;
  38.     public function __construct(EmailVerifier $emailVerifier)
  39.     {
  40.         $this->emailVerifier $emailVerifier;
  41.     }
  42.     public function register(
  43.         Request                     $request,
  44.         UserPasswordHasherInterface $userPasswordHasher,
  45.         EntityManagerInterface      $entityManager,
  46.         UserAuthenticatorInterface  $authenticator,
  47.         AppAuthenticator            $formAuthenticator
  48.     ): ?Response
  49.     {
  50.         if ($this->getUser()) {
  51.             $user $this->getUser();
  52.             $route $user->isAdmin() ? 'admin_dashboard' 'homepage';
  53.             return $this->redirectToRoute($route);
  54.         }
  55.         $user = new User();
  56.         $user->setCreatedAt(new DateTime());
  57.         $form $this->createForm(RegistrationFormType::class, $user);
  58.         $form->handleRequest($request);
  59.         if ($form->isSubmitted() && $form->isValid()) {
  60.             $form['email']->addError(new FormError('Veuillez saisir un email valide'));
  61.             $user->setPassword($userPasswordHasher->hashPassword($user,
  62.                 $form->get('plainPassword')->getData()
  63.             ));
  64.             $role $user->isFreelance() ? "ROLE_FREELANCE" "ROLE_SOCIETY";
  65.             $user->setRoles(["ROLE_USER"$role]);
  66.             $entityManager->persist($user);
  67.             $entityManager->flush();
  68.             $this->sendConfirmation($user);
  69.             $this->addFlash('email_validation_send'true);
  70.             return $authenticator->authenticateUser($user$formAuthenticator$request);
  71.         }
  72.         return $this->render('auth/register.html.twig', ['registrationForm' => $form->createView()]);
  73.     }
  74.     /**
  75.      * @Route("/register-jobseeker", name="register_freelance")
  76.      * @param Request $request
  77.      * @param UserPasswordHasherInterface $userPasswordHasher
  78.      * @param EntityManagerInterface $entityManager
  79.      * @param UserAuthenticatorInterface $authenticator
  80.      * @param AppAuthenticator $formAuthenticator
  81.      * @param PDFUploader $pdfUploader
  82.      * @return Response|null
  83.      */
  84.     public function registerFreelance(
  85.         Request                     $request,
  86.         UserPasswordHasherInterface $userPasswordHasher,
  87.         EntityManagerInterface      $entityManager,
  88.         UserAuthenticatorInterface  $authenticator,
  89.         AppAuthenticator            $formAuthenticator,
  90.         PDFUploader                 $pdfUploader,
  91.         ForumUserService            $forumUserService
  92.     ): ?Response
  93.     {
  94.         $user = new User();
  95.         $user->setCreatedAt(new DateTime());
  96.         $user->setRoles(['ROLE_FREELANCE']);
  97.         $user->setType('freelance');
  98.         $user->setPhone('');
  99.         $form $this->createForm(FreelanceRegisterFormType::class, $user, [
  100.             'forumPseudo' => $forumUserService->getNextForumUserID()
  101.         ]);
  102.         $form->handleRequest($request);
  103.         $isErrorEmail false;
  104.         if ($form->isSubmitted() && $form->isValid()) {
  105.             $user->setPassword($userPasswordHasher->hashPassword($user,
  106.                 $form->get('plainPassword')->getData()
  107.             ));
  108.             $user->setIsVerified(true);
  109.             // SET PROFILE
  110.             $profile $user->getUniqueProfile();
  111.             $profile->setTitle($form->get('title')->getData());
  112.             $profile->setName($form->get('name')->getData());
  113.             $profile->setCivility($form->get('civility')->getData());
  114.             $profile->setFirstname($form->get('firstname')->getData());
  115.             $pdfFile $form->get('cv_file')->getData();
  116.             if (isset($pdfFile) && is_object($pdfFile)) {
  117.                 $pdfSavedFile $pdfUploader->upload($pdfFile);
  118.                 if ($pdfSavedFile) {
  119.                     $profile->setCvname($pdfSavedFile['name']);
  120.                     $profile->setCvFile($pdfSavedFile['path']);
  121.                 }
  122.             }
  123.             $entityManager->persist($user);
  124.             $entityManager->persist($profile);
  125.             $entityManager->flush();
  126.             // SET PSEUDO
  127.             $userForum = new ForumUser();
  128.             $userForum->setAlias($form->get('forumPseudo')->getData());
  129.             $userForum->setUser($user);
  130.             $entityManager->persist($userForum);
  131.             $entityManager->flush();
  132.             $profile->updateSlug();
  133.             $entityManager->persist($profile);
  134.             $entityManager->flush();
  135.             try {
  136.                 /*$this->sendConfirmation($user);
  137.                 $this->addFlash('email_validation_send', true);*/
  138.                 return $authenticator->authenticateUser($user$formAuthenticator$request);
  139.             } catch (Exception $e) {
  140.                 return $authenticator->authenticateUser($user$formAuthenticator$request);
  141.             }
  142.         }
  143.         return $this->render('auth/register_freelance_new.html.twig', [
  144.             'form' => $form->createView(),
  145.             'is_error_mail' => $isErrorEmail
  146.         ]);
  147.     }
  148.     /**
  149.      * @Route("/register-employer", name="register_society")
  150.      */
  151.     public function registerSociety(
  152.         Request                     $request,
  153.         UserPasswordHasherInterface $userPasswordHasher,
  154.         EntityManagerInterface      $entityManager,
  155.         UserAuthenticatorInterface  $authenticator,
  156.         AppAuthenticator            $formAuthenticator
  157.     ): ?Response
  158.     {
  159.         $user = new User();
  160.         $form $this->createForm(SocietyRegisterFormType::class, $user);
  161.         $form->handleRequest($request);
  162.         if ($form->isSubmitted() && $form->isValid()) {
  163.             $user->setCreatedAt(new DateTime());
  164.             $user->setRoles(['ROLE_SOCIETY']);
  165.             $user->setType('society');
  166.             $user->setPassword($userPasswordHasher->hashPassword($user,
  167.                 $form->get('plainPassword')->getData()
  168.             ));
  169.             $society = new Society();
  170.             $society->setName($form->get('name')->getData());
  171.             $society->setCa(0);
  172.             $society->setAdress('');
  173.             $society->setVille('');
  174.             $society->setDescription('');
  175.             $society->setPhone($form->get('phone')->getData());
  176.             $user->setPhone($form->get('phone')->getData());
  177.             $society->setUser($user);
  178.             $entityManager->persist($user);
  179.             $entityManager->persist($society);
  180.             $entityManager->flush();
  181.             $society->updateSlug();
  182.             $entityManager->persist($society);
  183.             $entityManager->flush();
  184.             /* @changeLog 2022-11-03 [FIX] (Anthony) Mise en place d'une redirection après inscription compte entreprise */
  185.             $this->addFlash('success''Your company account has been successfully created.');
  186.             return $this->redirectToRoute('login');
  187.         }
  188.         return $this->render('auth/register_society_new.html.twig', [
  189.             'form' => $form->createView(),
  190.         ]);
  191.     }
  192.     /**
  193.      * @Route("/verify/resend", name="resend_verification")
  194.      */
  195.     public function resendVerificationEmail(Request $request): Response
  196.     {
  197.         $this->sendConfirmation($this->getUser());
  198.         return $this->redirectToRoute('dashboard');
  199.     }
  200.     private function sendConfirmation(UserInterface $user)
  201.     {
  202.         $this->emailVerifier->sendEmailConfirmation('verify_email'$user,
  203.             (new TemplatedEmail())
  204.                 ->to($user->getEmail())->subject('Verify your mail')
  205.                 ->htmlTemplate('emails/confirmation_email.html.twig')
  206.         );
  207.     }
  208.     /**
  209.      * @Route("/verify/email", name="verify_email")
  210.      */
  211.     public function verifyUserEmail(Request $request): Response
  212.     {
  213.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  214.         // validate email confirmation link, sets User::isVerified=true and persists
  215.         try {
  216.             $this->emailVerifier->handleEmailConfirmation($request$this->getUser());
  217.         } catch (VerifyEmailExceptionInterface $exception) {
  218.             $this->addFlash('verify_email_error'$exception->getReason());
  219.             return $this->redirectToRoute('register_freelance');
  220.         }
  221.         // return $this->redirectToRoute('dashboard');
  222.         return $this->redirectToRoute('complete_resume_profile');
  223.     }
  224.     /**
  225.      * @Route("/login", name="login")
  226.      */
  227.     public function login(AuthenticationUtils $authenticationUtils): Response
  228.     {
  229.         if ($this->getUser()) {
  230.             $user $this->getUser();
  231.             $route $user->isAdmin() ? 'admin_dashboard' 'homepage';
  232.             return $this->redirectToRoute($route);
  233.         }
  234.         $resetForm $this->createForm(ResetPasswordRequestFormType::class);
  235.         // get the login error if there is one
  236.         $error $authenticationUtils->getLastAuthenticationError();
  237.         // last username entered by the user
  238.         $lastUsername $authenticationUtils->getLastUsername();
  239.         return $this->render('auth/login.html.twig', [
  240.             'last_username' => $lastUsername,
  241.             'error' => $error,
  242.             'resetForm' => $resetForm->createView(),
  243.         ]);
  244.     }
  245.     /**
  246.      * @Route("/logout", name="logout")
  247.      */
  248.     public function logout(): Response
  249.     {
  250.         return $this->redirectToRoute('homepage');
  251.     }
  252.     /**
  253.      * @Route("/register-jobseeker/mobile", name="register_freelance_mobile")
  254.      * @param Request $request
  255.      * @param UserPasswordHasherInterface $userPasswordHasher
  256.      * @param EntityManagerInterface $entityManager
  257.      * @param UserAuthenticatorInterface $authenticator
  258.      * @param AppAuthenticator $formAuthenticator
  259.      * @param PDFUploader $pdfUploader
  260.      * @return Response|null
  261.      */
  262.     public function registerFreelanceMobile(
  263.         Request                     $request,
  264.         UserPasswordHasherInterface $userPasswordHasher,
  265.         EntityManagerInterface      $entityManager,
  266.         UserAuthenticatorInterface  $authenticator,
  267.         AppAuthenticator            $formAuthenticator
  268.     ): ?Response
  269.     {
  270.         $user = new User();
  271.         $user->setCreatedAt(new DateTime());
  272.         $user->setRoles(['ROLE_FREELANCE']);
  273.         $user->setType('freelance');
  274.         $user->setPhone('');
  275.         $form $this->createForm(FreelanceRegisterMobileFormType::class, $user);
  276.         $form->handleRequest($request);
  277.         $isErrorEmail false;
  278.         if ($form->isSubmitted() && $form->isValid()) {
  279.             $user->setPassword($userPasswordHasher->hashPassword($user,
  280.                 $form->get('plainPassword')->getData()
  281.             ));
  282.             $user->setIsVerified(true);
  283.             // SET PROFILE
  284.             $profile $user->getUniqueProfile();
  285.             $profile->setTitle($form->get('title')->getData());
  286.             $profile->setName($form->get('name')->getData());
  287.             $profile->setCivility($form->get('civility')->getData());
  288.             $profile->setFirstname($form->get('firstname')->getData());
  289.             $profile->setPhone($form->get('phone')->getData());
  290.             $entityManager->persist($user);
  291.             $entityManager->persist($profile);
  292.             $entityManager->flush();
  293.             $profile->updateSlug();
  294.             $entityManager->persist($profile);
  295.             $entityManager->flush();
  296.             try {
  297.                 /*$this->sendConfirmation($user);
  298.                 $this->addFlash('email_validation_send', true);*/
  299.                 return $authenticator->authenticateUser($user$formAuthenticator$request);
  300.             } catch (Exception $e) {
  301.                 return $authenticator->authenticateUser($user$formAuthenticator$request);
  302.             }
  303.         }
  304.         return $this->render('auth/register_freelance_mobile.html.twig', [
  305.             'form' => $form->createView(),
  306.             'is_error_mail' => $isErrorEmail
  307.         ]);
  308.     }
  309. }