src/Security/AppAuthenticator.php line 27

  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 AppAuthenticator extends AbstractLoginFormAuthenticator
  16. {
  17.     use TargetPathTrait;
  18.     final public const LOGIN_ROUTE = 'login';
  19.     public function __construct(private readonly UrlGeneratorInterface $urlGenerator,Security $security)
  20.     {
  21.         $this->security = $security;
  22.     }
  23.     public function authenticate(Request $request): Passport
  24.     {
  25.         $username = $request->request->get('username', '');
  26.         $request->getSession()->set(Security::LAST_USERNAME, $username);
  27.         return new Passport(
  28.             new UserBadge($username),
  29.             new PasswordCredentials($request->request->get('password', '')),
  30.             [
  31.                 new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),
  32.             ]
  33.         );
  34.     }
  35.     public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
  36.     {
  37.         // For example:
  38.         $currentUser = $this->security->getUser();
  39.         if(in_array("ROLE_VENDOR",$currentUser->getRoles())){
  40.             return new RedirectResponse($this->urlGenerator->generate('shop_vendor'));
  41.         }
  42.         elseif(in_array("ROLE_STORE",$currentUser->getRoles())){
  43.             return new RedirectResponse($this->urlGenerator->generate('shop_store'));
  44.         }
  45.         elseif(in_array("ROLE_BACKOFFICE_AGENT",$currentUser->getRoles())){
  46.             return new RedirectResponse($this->urlGenerator->generate('shop_agent'));
  47.         }
  48.         elseif(in_array("ROLE_SHIPPING_AGENT",$currentUser->getRoles())){
  49.             return new RedirectResponse($this->urlGenerator->generate('shop_shipping_agent'));
  50.         }
  51.         elseif(in_array("ROLE_ADMIN",$currentUser->getRoles())){
  52.             return new RedirectResponse($this->urlGenerator->generate('admin'));
  53.         }
  54.         else{
  55.             return new RedirectResponse("/");
  56.         }
  57.         if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
  58.             return new RedirectResponse($targetPath);
  59.         }
  60.         //throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
  61.     }
  62.     protected function getLoginUrl(Request $request): string
  63.     {
  64.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  65.     }
  66. }