src/Controller/Auth/RegistrationController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Auth;
  3. use App\Entity\User;
  4. use App\Form\AddAgentType;
  5. use App\Security\EmailVerifier;
  6. use App\Form\RegistrationFormType;
  7. use App\Security\LoginAuthenticator;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use App\Entity\Utils\AccountVerification;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use App\Message\Auth\Registration\ConfirmAccount;
  14. use App\Repository\Department\DepartmentRepository;
  15. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  18. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  19. class RegistrationController extends AbstractController
  20. {
  21.     private $emailVerifier;
  22.     protected $entityManager;
  23.     protected $departmentRepository;
  24.     public function __construct(EmailVerifier $emailVerifier,EntityManagerInterface $entityManagerDepartmentRepository $departmentRepository)
  25.     {
  26.         $this->emailVerifier $emailVerifier;
  27.         $this->entityManager $entityManager;
  28.         $this->departmentRepository $departmentRepository;
  29.     }
  30.     /**
  31.      * @Route("/register", name="app.register")
  32.      */
  33.     public function register(Request $requestUserPasswordHasherInterface $passwordEncoderGuardAuthenticatorHandler $guardHandlerLoginAuthenticator $authenticator): Response
  34.     {
  35.         $user = new User();
  36.         if ($request->get("id"))
  37.             $form $this->createForm(AddAgentType::class, $user);
  38.         else
  39.             $form $this->createForm(RegistrationFormType::class, $user);
  40.         
  41.         $form->handleRequest($request);
  42.         $department $request->get("id");
  43.         $dpt $this->departmentRepository->findOneBy(["slug" => $department]);
  44.         if ($form->isSubmitted() && $form->isValid()) {
  45.             $user->setPassword(
  46.                 $passwordEncoder->hashPassword(
  47.                     $user,
  48.                     strtolower($form->get('firstname')->getData())
  49.                 )
  50.             )
  51.             ->setUsername(uniqid())->setAccountVerification(new AccountVerification());
  52.             if ($department) {
  53.                 $user->setDepartment($dpt)
  54.                 ->setCompany($dpt->getCompany())
  55.                 ->setRoles(["ROLE_USER"])
  56.                 ->setAccountVerification(
  57.                     (new AccountVerification())
  58.                         ->setAccountVerified(true)
  59.                         ->setEmailVerified(true)
  60.                         ->setEmailVerifiedAt(new \DateTime())
  61.                 );
  62.                 $this->addFlash('notice',$user->getNames(). '  a été enregistré avec succès !');
  63.             }
  64.             //affecter le membre
  65.             $user->setParent($this->getUser());
  66.             //
  67.             $this->entityManager->persist($user);
  68.             $this->entityManager->flush();
  69.             $this->dispatchMessage(new ConfirmAccount($user->getSlug()));
  70.             if ($department)
  71.                 return $this->redirectToRoute('docs.executive.dpt',['slug' => $department]);
  72.             return $guardHandler->authenticateUserAndHandleSuccess(
  73.                 $user,
  74.                 $request,
  75.                 $authenticator,
  76.                 'main'
  77.             );
  78.         }
  79.         return $this->render('auth/registration/register.html.twig', [
  80.             'registrationForm'  => $form->createView(),
  81.             'id'                => $request->get("id"),
  82.         ]);
  83.     }
  84.     /**
  85.      * @Route("/verify/email", name="app_verify_email")
  86.      */
  87.     public function verifyUserEmail(Request $request): Response
  88.     {
  89.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  90.         // validate email confirmation link, sets User::isVerified=true and persists
  91.         try {
  92.             $this->emailVerifier->handleEmailConfirmation($request$this->getUser());
  93.         } catch (VerifyEmailExceptionInterface $exception) {
  94.             $this->addFlash('verify_email_error'$exception->getReason());
  95.             return $this->redirectToRoute('app.register');
  96.         }
  97.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  98.         $this->addFlash('success''Your email address has been verified.');
  99.         return $this->redirectToRoute('app.register');
  100.     }
  101. }