src/Security/UserAuthenticator.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\Security\Core\Security;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  14. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  16. //Connexion qui prend en compte la hiérarchie des roles 
  17. class UserAuthenticator extends AbstractLoginFormAuthenticator
  18. {
  19.     use TargetPathTrait;
  20.     public const LOGIN_ROUTE 'app_login';
  21.     //Appel du service security pour les rôles
  22.     private Security $security;
  23.     public function __construct(private UrlGeneratorInterface $urlGeneratorSecurity $security)
  24.     {
  25.         //INJECTION du service security et urlGenerator dans le constructeur 
  26.         $this->security $security;
  27.         $this->urlGenerator $urlGenerator;
  28.     }
  29.     public function authenticate(Request $request): Passport
  30.     {
  31.         $email $request->request->get('email''');
  32.         $request->getSession()->set(Security::LAST_USERNAME$email);
  33.         return new Passport(
  34.             new UserBadge($email),
  35.             new PasswordCredentials($request->request->get('password''')),
  36.             [
  37.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  38.             ]
  39.         );
  40.     }
  41.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  42.     {
  43.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  44.             return new RedirectResponse($targetPath);
  45.         }
  46.         // Vérifies directement avec la méthode isGranted() du composant Security
  47.         /* ROLE_SUPER_ADMIN et ROLE_ADMIN sont rediriger respectivement avec leur controlleur 
  48.         menant à la même vue back_admin/index.html.twig */
  49.         
  50.         if ($this->security->isGranted("ROLE_SUPER_ADMIN")) {
  51.             return new RedirectResponse($this->urlGenerator->generate('app_accueil'));
  52.         }
  53.         elseif ($this->security->isGranted("ROLE_ADMIN")) {
  54.             return new RedirectResponse($this->urlGenerator->generate('app_accueil'));
  55.         }
  56.         //Sinon redirige le ROLE_USER vers la page test app_register
  57.         
  58.         return new RedirectResponse($this->urlGenerator->generate('app_accueil'));
  59.     }
  60.         /* For example:
  61.            return new RedirectResponse($this->urlGenerator->generate('app_login'));
  62.            throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
  63.         }*/
  64.     protected function getLoginUrl(Request $request): string
  65.     {
  66.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  67.     }
  68. }