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