src/Security/LoginAuthenticator.php line 18

Open in your IDE?
  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\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  10. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  11. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  14. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  15. class LoginAuthenticator extends AbstractLoginFormAuthenticator
  16. {
  17.     use TargetPathTrait;
  18.     public const LOGIN_ROUTE 'app_login';
  19.     public function __construct(private UrlGeneratorInterface $urlGenerator)
  20.     {
  21.     }
  22.     public function authenticate(Request $request): Passport
  23.     {
  24.         $email $request->request->get('email''');
  25.         $request->getSession()->set(Security::LAST_USERNAME$email);
  26.         return new Passport(
  27.             new UserBadge($email),
  28.             new PasswordCredentials($request->request->get('password''')),
  29.             [
  30.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),            ]
  31.         );
  32.     }
  33.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  34.     {
  35.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  36.             $targetPathUrl = (string) parse_url($targetPathPHP_URL_PATH);
  37.             $blockedTargets = [
  38.                 '/produits/produitsepuises',
  39.                 '/new_produits/produits_epuises',
  40.                 '/api',
  41.             ];
  42.             $isBlockedTarget false;
  43.             foreach ($blockedTargets as $blockedTarget) {
  44.                 if ($targetPathUrl === $blockedTarget || str_starts_with($targetPathUrl$blockedTarget '/')) {
  45.                     $isBlockedTarget true;
  46.                     break;
  47.                 }
  48.             }
  49.             if (!$isBlockedTarget) {
  50.                 return new RedirectResponse($targetPath);
  51.             }
  52.             $this->removeTargetPath($request->getSession(), $firewallName);
  53.         }
  54.         $roles $token->getUser()->getRoles();
  55.         if (
  56.             in_array('ROLE_ADMIN'$roles) ||
  57.             in_array('ROLE_ULTRATEX'$roles) ||
  58.             in_array('ROLE_ULTRACONFECTION'$roles) ||
  59.             in_array('ROLE_GESTION_STOCK'$roles) ||
  60.             in_array('ROLE_RH'$roles)
  61.         )  {
  62.             return new RedirectResponse($this->urlGenerator->generate('app_home'));
  63.         }
  64.         else{
  65.             return new RedirectResponse($this->urlGenerator->generate('app_login'));
  66.         }
  67. //        return new RedirectResponse($this->urlGenerator->generate('app_login'));
  68.     }
  69.     protected function getLoginUrl(Request $request): string
  70.     {
  71.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  72.     }
  73. }