src/Security/LoginFormAuthenticator.php line 19

  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. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  16. class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
  17. {
  18.     use TargetPathTrait;
  19.     public const LOGIN_ROUTE 'app_login';
  20.     protected $authorizationChecker;
  21.     public function __construct(private UrlGeneratorInterface $urlGeneratorAuthorizationCheckerInterface $authorizationChecker)
  22.     {
  23.         $this->authorizationChecker $authorizationChecker;
  24.     }
  25.     public function authenticate(Request $request): Passport
  26.     {
  27.         $email $request->request->get('email''');
  28.         $request->getSession()->set(Security::LAST_USERNAME$email);
  29.         return new Passport(
  30.             new UserBadge($email),
  31.             new PasswordCredentials($request->request->get('password''')),
  32.             [
  33.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  34.             ]
  35.         );
  36.     }
  37.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  38.     {
  39.         // if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
  40.         //     return new RedirectResponse($targetPath);
  41.         // }
  42.         if(true === $this->authorizationChecker->isGranted('ROLE_ADMIN'))
  43.         {
  44.             
  45.             return new RedirectResponse($this->urlGenerator->generate('app_sales_invoice_index'));
  46.         }
  47.         else if(true === $this->authorizationChecker->isGranted('ROLE_CS'))
  48.         {
  49.             
  50.             return new RedirectResponse($this->urlGenerator->generate('app_shipment_index'));
  51.         }
  52.         else if(true === $this->authorizationChecker->isGranted('ROLE_SALES'))
  53.         {
  54.             
  55.             return new RedirectResponse($this->urlGenerator->generate('app_sales_offer_index'));
  56.         }
  57.         else if(true === $this->authorizationChecker->isGranted('ROLE_ACCOUNTS'))
  58.         {
  59.             
  60.             return new RedirectResponse($this->urlGenerator->generate('app_receive_voucher_index'));
  61.         }
  62.         else if(true === $this->authorizationChecker->isGranted('ROLE_CUSTOMER'))
  63.         {
  64.             
  65.             return new RedirectResponse($this->urlGenerator->generate('app_customer_shipment_index'));
  66.         }
  67.         else
  68.         {
  69.             
  70.             return new RedirectResponse($this->urlGenerator->generate('app_shipment_booking_index'));
  71.         }
  72.  
  73.         // For example:
  74.          
  75.         //throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
  76.     }
  77.     protected function getLoginUrl(Request $request): string
  78.     {
  79.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  80.     }
  81. }