src/Controller/Security/SecurityController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  3. use App\Entity\User;
  4. use App\Form\UserRegistrationType;
  5. use App\Security\CustomAuthenticator;
  6. use MercurySeries\FlashyBundle\FlashyNotifier;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
  13. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  14. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. use Symfony\Component\Serializer\SerializerInterface;
  17. class SecurityController extends AbstractController
  18. {
  19.     public const LAST_EMAIL 'app_login_form_last_email';
  20.     /**
  21.      * @var FlashyNotifier
  22.      */
  23.     private $flashy;
  24.     public function __construct(FlashyNotifier $flashyContainerInterface $container)
  25.     {
  26.         $this->flashy $flashy;
  27.         $this->container $container;
  28.     }
  29.     /**
  30.      * @Route("/login", name="security_login")
  31.      * @param AuthenticationUtils $authenticationUtils
  32.      * @return Response
  33.      */
  34.     public function login(AuthenticationUtils $authenticationUtils): Response
  35.     {
  36.         /** @var AuthorizationChecker $authorization_checker */
  37.         $authorization_checker $this->get('security.authorization_checker');
  38.         if ($authorization_checker->isGranted('IS_AUTHENTICATED_FULLY'))
  39.         {
  40.             return $this->redirectToRoute('home');
  41.         }
  42.         // get the login error if there is one
  43.         $error $authenticationUtils->getLastAuthenticationError();
  44.         // last username entered by the user
  45.         $lastUsername $authenticationUtils->getLastUsername();
  46.         return $this->render('security/login.html.twig', [
  47.             'title' => 'Connectez vous',
  48.             'last_username' => $lastUsername,
  49.             'error' => $error
  50.         ]);
  51.     }
  52.     /**
  53.      * @Route("/register", name="security_register")
  54.      * @param Request $request
  55.      * @param UserPasswordEncoderInterface $encoder
  56.      * @param GuardAuthenticatorHandler $guardHandler
  57.      * @param CustomAuthenticator $authenticator
  58.      * @return Response
  59.      */
  60.     public function registration(
  61.         Request $request,
  62.         UserPasswordEncoderInterface $encoder,
  63.         GuardAuthenticatorHandler $guardHandler,
  64.         CustomAuthenticator $authenticator
  65.     ): Response
  66.     {
  67.         if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY'))
  68.         {
  69.             return $this->redirectToRoute('home');
  70.         }
  71.         $user = new User();
  72.         $form $this->createForm(UserRegistrationType::class, $user);
  73.         $form->handleRequest($request);
  74.         if($form->isSubmitted() && $form->isValid()){
  75.             $domaine $form->get('domaines')->getData()->getRole();
  76.             $password $encoder->encodePassword($user$user->getPassword());
  77.             $user->setPassword($password);
  78.             $user->setEnabled(true);
  79.             $user->setRoles([$domaine]);
  80.             $em $this->getDoctrine()->getManager();
  81.             $em->persist($user);
  82.             $em->flush();
  83.             $this->flashy->success('Votre inscription a été prise en compte.');
  84.             //On connecte automatiquement l'utilisateur
  85.             return $guardHandler
  86.                 ->authenticateUserAndHandleSuccess($user$request$authenticator'main');
  87.             //return $this->redirectToRoute('security_login');
  88.         }
  89.         return $this->render('security/registration.html.twig', [
  90.             'form' => $form->createView(),
  91.             'last' => $user
  92.         ]);
  93.     }
  94.     /**
  95.      * @Route("/logout", name="security_logout")
  96.      */
  97.     public function logout()
  98.     {
  99.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  100.     }
  101. }