src/Controller/SecurityController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Builder\MailerBuilder;
  4. use App\Form\User\CreateAccountType;
  5. use Exception;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use Symfony\Component\Security\Http\Event\LogoutEvent;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. class SecurityController extends AbstractController
  17. {
  18.     private MailerBuilder $mailerBuilder;
  19.     public function __construct(MailerBuilder $mailerBuilderTranslatorInterface $translatorRequestStack $requestStackTokenStorageInterface $tokenStorageEventDispatcherInterface $eventDispatcher)
  20.     {
  21.         $this->mailerBuilder $mailerBuilder;
  22.         $this->translator $translator;
  23.         $this->requestStack $requestStack;
  24.         $this->tokenStorage $tokenStorage;
  25.         $this->eventDispatcher $eventDispatcher;
  26.     }
  27.     /**
  28.      * @Route("/login", name="app_login")
  29.      */
  30.     public function login(AuthenticationUtils $authenticationUtilsRequest $request): Response
  31.     {
  32.         if ($this->getUser()) {
  33.             if (!$this->getUser()->getIsActive()) {
  34.                 $this->addFlash('error'"Votre compte est inactif, veuillez prendre contact avec l'administrateur.");
  35.                 $this->forceLogout();
  36.                 return $this->redirectToRoute('login');
  37.             } else {
  38.                 return $this->redirectToRoute('home');
  39.             }
  40.         } else {
  41.             // get the login error if there is one
  42.             $error $authenticationUtils->getLastAuthenticationError();
  43.             // last username entered by the user
  44.             $lastUsername $authenticationUtils->getLastUsername();
  45.             $formCreateAccount $this->createForm(CreateAccountType::class)->handleRequest($request);
  46.             return $this->render(
  47.                 'security/login.html.twig',
  48.                 [
  49.                     'last_username' => $lastUsername,
  50.                     'error' => $error,
  51.                     'formCreateAccount' => $formCreateAccount->createView(),
  52.                 ]
  53.             );
  54.         }
  55.     }
  56.     /**
  57.      * @Route("/logout", name="app_logout")
  58.      */
  59.     public function logout()
  60.     {
  61.         throw new Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
  62.     }
  63.     public function forceLogout(): void
  64.     {
  65.         $logoutEvent = new LogoutEvent($this->requestStack->getCurrentRequest(), $this->tokenStorage->getToken());
  66.         $this->eventDispatcher->dispatch($logoutEvent);
  67.         $this->tokenStorage->setToken(null);
  68.         $response = new Response();
  69.         $response->headers->clearCookie('REMEMBERME');
  70.         $response->send();
  71.     }
  72. }