src/Security/LoginAuthenticator.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Enum\Role;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  16. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  17. class LoginAuthenticator extends AbstractLoginFormAuthenticator
  18. {
  19.     use TargetPathTrait;
  20.     public const LOGIN_ROUTE 'app_login';
  21.     private UrlGeneratorInterface $urlGenerator;
  22.     private RouterInterface $router;
  23.     /**
  24.      * @param UrlGeneratorInterface $urlGenerator
  25.      * @param RouterInterface $router
  26.      */
  27.     public function __construct(UrlGeneratorInterface $urlGeneratorRouterInterface $router)
  28.     {
  29.         $this->urlGenerator $urlGenerator;
  30.         $this->router $router;
  31.     }
  32.     public function authenticate(Request $request): Passport
  33.     {
  34.         $email $request->request->get('email''');
  35.         $request->getSession()->set(Security::LAST_USERNAME$email);
  36.         return new Passport(
  37.             new UserBadge($email),
  38.             new PasswordCredentials($request->request->get('password''')),
  39.             [
  40.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  41.             ]
  42.         );
  43.     }
  44.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): Response
  45.     {
  46.         $user $token->getUser();
  47.         try {
  48.             $roles $user->getRoles();
  49.             if (in_array(Role::ROLE_ADMIN->getSymfonyRole(), $rolestrue)) {
  50.                 return new RedirectResponse($this->router->generate('app_panel_dashboard'));
  51.             }
  52.             if (in_array(Role::ROLE_SALES_REP->getSymfonyRole(), $rolestrue)) {
  53.                 return new RedirectResponse($this->router->generate('seller_dashboard'));
  54.             }
  55.             if (in_array(Role::ROLE_ACCOUNTING->getSymfonyRole(), $rolestrue)) {
  56.                 return new RedirectResponse($this->router->generate('accounting_dashboard'));
  57.             }
  58.             return new RedirectResponse($this->router->generate('app_panel_dashboard'));
  59.         } catch (\Exception $e) {
  60.             return new RedirectResponse($this->router->generate('login'));
  61.         }
  62.     }
  63. //    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
  64. //    {
  65. ////        if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
  66. ////            return new RedirectResponse($targetPath);
  67. ////        }
  68. //
  69. //        // For example:
  70. //         return new RedirectResponse($this->urlGenerator->generate('app_panel_dashboard'));
  71. ////        throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
  72. //    }
  73.     protected function getLoginUrl(Request $request): string
  74.     {
  75.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  76.     }
  77. }