src/Security/AppAuthenticator.php line 21

  1. <?php
  2. namespace App\Security;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  17. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  18. class AppAuthenticator extends AbstractLoginFormAuthenticator
  19. {
  20.     use TargetPathTrait;
  21.     public const LOGIN_ROUTE 'app_login';
  22.     private UrlGeneratorInterface $urlGenerator;
  23.     private AuthorizationCheckerInterface $authorization;
  24.     private RouterInterface $router;
  25.     public function __construct(
  26.         UrlGeneratorInterface $urlGenerator,
  27.         AuthorizationCheckerInterface $authorization,
  28.         RouterInterface $router
  29.     ) {
  30.         $this->urlGenerator $urlGenerator;
  31.         $this->authorization $authorization;
  32.         $this->router $router;
  33.     }
  34.     public function supports(Request $request): bool
  35.     {
  36.         return $request->getPathInfo() === '/connexion' && $request->isMethod('POST');
  37.     }
  38.     public function authenticate(Request $request): Passport
  39.     {
  40.         $email $request->request->get('email''');
  41.         $password $request->request->get('password''');
  42.         $csrfToken $request->request->get('_csrf_token');
  43.         $request->getSession()->set(Security::LAST_USERNAME$email);
  44.         return new Passport(
  45.             new UserBadge($email),
  46.             new PasswordCredentials($password),
  47.             [
  48.                 new CsrfTokenBadge('authenticate'$csrfToken),
  49.                 new RememberMeBadge(),
  50.             ]
  51.         );
  52.     }
  53.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  54.     {
  55.         // 1. Vérifier le target_path envoyé par le formulaire
  56.         $targetPathPost $request->request->get('_target_path');
  57.         if ($targetPathPost) {
  58.             return new RedirectResponse(urldecode($targetPathPost));
  59.         }
  60.         // 2. Vérifier redirect dans l'URL
  61.         $redirect $request->query->get('redirect');
  62.         if ($redirect) {
  63.             return new RedirectResponse($redirect);
  64.         }
  65.         // 3. Vérifier targetPath stocké dans la session
  66.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  67.             return new RedirectResponse($targetPath);
  68.         }
  69.         // 4. Redirection selon rôle
  70.         if ($this->authorization->isGranted('ROLE_ADMIN')) {
  71.             return new RedirectResponse($this->router->generate('app_dashboard'));
  72.         }
  73.         if ($this->authorization->isGranted('ROLE_ENT') || $this->authorization->isGranted('ROLE_JURY')) {
  74.             return new RedirectResponse($this->router->generate('app_dashboard'));
  75.         }
  76.         if ($this->authorization->isGranted('ROLE_CANDIDAT')) {
  77.             return new RedirectResponse($this->router->generate('app_candidature_index'));
  78.         }
  79.         return new RedirectResponse($this->router->generate('app_home'));
  80.     }
  81.     protected function getLoginUrl(Request $request): string
  82.     {
  83.         $redirect $request->query->get('redirect');
  84.         if ($redirect) {
  85.             return $this->urlGenerator->generate(self::LOGIN_ROUTE, ['redirect' => $redirect]);
  86.         }
  87.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  88.     }
  89. }