src/Security/AppAuthenticator.php line 20

  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\RememberMeBadge;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
  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 AppAuthenticator extends AbstractLoginFormAuthenticator
  18. {
  19.     use TargetPathTrait;
  20.     final public const LOGIN_ROUTE 'login';
  21.     public function __construct(private readonly UrlGeneratorInterface $urlGenerator,private readonly Security $security,private readonly CustomUserProvider $userProvider)
  22.     {
  23.         
  24.     }
  25.     public function authenticate(Request $request): Passport
  26.     {
  27.         $username $request->request->get('username''');
  28.         $password $request->request->get('password''');
  29.         $request->getSession()->set(Security::LAST_USERNAME$username);
  30.         return new Passport(
  31.             new UserBadge($username, [$this->userProvider'loadUserByIdentifier']),
  32.             new CustomCredentials(
  33.                 function ($credentials$user) {
  34.                     return $this->userProvider->validatePassword($user$credentials);
  35.                 },
  36.                 $password
  37.             ),
  38.             [
  39.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  40.             ],
  41.             [
  42.                 new RememberMeBadge(),
  43.             ]
  44.         );
  45.     }
  46.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  47.     {
  48.         // For example:
  49.         $currentUser $this->security->getUser();
  50.         if(in_array("ROLE_VENDOR",$currentUser->getRoles())){
  51.             return new RedirectResponse($this->urlGenerator->generate('shop_vendor'));
  52.         }
  53.         elseif(in_array("ROLE_STORE",$currentUser->getRoles())){
  54.             return new RedirectResponse($this->urlGenerator->generate('shop_store'));
  55.         }
  56.         elseif(in_array("ROLE_BACKOFFICE_AGENT",$currentUser->getRoles())){
  57.             return new RedirectResponse($this->urlGenerator->generate('shop_agent'));
  58.         }
  59.         elseif(in_array("ROLE_SHIPPING_AGENT",$currentUser->getRoles())){
  60.             return new RedirectResponse($this->urlGenerator->generate('shop_shipping_agent'));
  61.         }
  62.         elseif(in_array("ROLE_ADMIN",$currentUser->getRoles())){
  63.             return new RedirectResponse($this->urlGenerator->generate('admin'));
  64.         }
  65.         else{
  66.             return new RedirectResponse("/");
  67.         }
  68.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  69.             return new RedirectResponse($targetPath);
  70.         }
  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. }