src/Controller/RegistrationController.php line 29

Open in your IDE?
  1. <?php
  2. // src/Controller/RegistrationController.php
  3. namespace App\Controller;
  4. use App\Entity\AppUser;
  5. use App\Form\RegistrationFormType;
  6. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  15. use App\Security\LoginPersonnelFormAuthenticator;
  16. /**
  17.  * @Route("/{_locale}/register")
  18.  */
  19. class RegistrationController extends AbstractController
  20. {
  21.     
  22.     /**
  23.      * @Route("/", name="registration_register")
  24.      * @return \Symfony\Component\HttpFoundation\Response
  25.     */
  26.     public function register(Request $requestUserPasswordHasherInterface $passwordHasherUserAuthenticatorInterface $userAuthenticatorLoginPersonnelFormAuthenticator $authenticator)
  27.     {
  28.         // ... e.g. get the user data from a registration form
  29.         $user = new AppUser;
  30.         $plaintextPassword '';
  31.         $em $this->getDoctrine()
  32.                     ->getManager();
  33.         $error_message '';
  34.         $form $this->createForm(RegistrationFormType::class, $user);
  35.         if($request->getMethod() == 'POST'){
  36.              $form->handleRequest($request);
  37.             if($form->isValid()){
  38.                 $data $form->getData();
  39.                 
  40.                 $userExist $em->getRepository(AppUser::class)
  41.                                 ->findOneByEmail($data->getEmail());
  42.                 if($userExist != null){
  43.                     $error_message 'Email adresse déjà utilisé';
  44.                 }else{
  45.                      $userExist $em->getRepository(AppUser::class)
  46.                                 ->findOneByUsername($data->getUsername());
  47.                      if($userExist != null){
  48.                         $error_message 'Nom d\'utilisateur adresse déjà utilisé';
  49.                      } else{
  50.                             $plaintextPassword '';
  51.                             // hash the password (based on the security.yaml config for the $user class)
  52.                             $hashedPassword $passwordHasher->hashPassword($user$data->getPlainPassword());
  53.                             $user->setPassword($hashedPassword);
  54.                             $em->persist($user);
  55.                             $em->flush();
  56.                             return $userAuthenticator->authenticateUser(
  57.                                         $user,
  58.                                         $authenticator// Pass your specific authenticator instance
  59.                                         $request
  60.                                     );
  61.                             
  62.                             return $this->redirectToRoute('registration_confirmed', array('username' => $user->getUsername()));
  63.                     }
  64.                 }
  65.                     
  66.             }
  67.         }
  68.         
  69.         return $this->render('registration/register.html.twig', [
  70.         
  71.             'form' => $form->createView(),
  72.             'error_message' => $error_message
  73.         ]);
  74.     }
  75.     /**
  76.      * @Route("/registration-confirmed/{username}", name="registration_confirmed")
  77.      * @return \Symfony\Component\HttpFoundation\Response
  78.     */
  79.     public function registrationConfirmedAction(Request $requestUserPasswordHasherInterface $passwordHasherAppUser $user)
  80.     {
  81.         // ... e.g. get the user data from a registration form
  82.         
  83.         
  84.         return $this->render('registration/confirmed.html.twig', [
  85.         
  86.             'user' => $user
  87.         ]);
  88.     }
  89.     
  90. }