src/Controller/SecurityController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. class SecurityController extends AbstractController
  13. {
  14.     private TokenStorageInterface $tokenStorage;
  15.     private RouterInterface $router;
  16.     private TranslatorInterface $translator;
  17.     public function __construct(
  18.         TokenStorageInterface $tokenStorage,
  19.         RouterInterface $router,
  20.         TranslatorInterface $translator
  21.     ) {
  22.         $this->tokenStorage $tokenStorage;
  23.         $this->router $router;
  24.         $this->translator $translator;
  25.     }
  26.     #[Route('/login'name'login')]
  27.     public function index(AuthenticationUtils $authenticationUtils): Response
  28.     {
  29.         $error $authenticationUtils->getLastAuthenticationError();
  30.         $lastUsername $authenticationUtils->getLastUsername();
  31.         return $this->render('login/index.html.twig', [
  32.             'last_username' => $lastUsername,
  33.             'error' => $error
  34.         ]);
  35.     }
  36.     #[Route('/logout'name'logout'methods: ['GET'])]
  37.     public function logout(Request $request)
  38.     {
  39.         $request->getSession()->invalidate();
  40.         $this->tokenStorage->setToken();
  41.         $this->redirectToRoute('login');
  42.         // throw new \Exception('Don\'t forget to activate logout in security.yaml');
  43.     }
  44.     #[Route('/access_denied'name'access_denied'methods: ['GET'])]
  45.     public function accessDeniedHandler(): Response
  46.     {
  47.         return $this->render('error/access_denied.html.twig', [
  48.             'response' => 'Vous n’êtes pas autorisé à accéder à cette page',
  49.         ]);
  50.     }
  51.     #[Route('/change_locale/{locale}'name'change_locale'methods: ['GET'])]
  52.     public function changeLocale(string $localeRequest $request): Response
  53.     {
  54.         $host $request->headers->get('host');
  55.         $referer $request->headers->get('referer');
  56.         $route substr($refererstrpos($referer$host) + strlen($host));
  57.         $baseUrl $this->router->getContext()->getBaseUrl();
  58.         if ($baseUrl) {
  59.             $route substr($routestrpos($route$baseUrl) + strlen($baseUrl));
  60.         }
  61.         $route $this->router->matchRequest(Request::create($route));
  62.         $request->getSession()->set('_locale'$locale);
  63.         $request->setLocale($locale);
  64.         $parameters = ['_locale' => $locale];
  65.         $parameters = (count($route) > 3) ? array_merge($parametersarray_slice($route3)) : $parameters;
  66.         $routePath $this->router->generate($route['_route'], $parameters);
  67.         return new RedirectResponse($routePath);
  68.     }
  69. }