src/Security/UserAuthenticator.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Security\User;
  4. use App\Service\Api\ApiServicePortalService;
  5. use App\Service\Interfaces\ApiInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  15. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  17. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  18. class UserAuthenticator extends AbstractLoginFormAuthenticator
  19. {
  20.     use TargetPathTrait;
  21.     public const LOGIN_ROUTE 'login';
  22.     private UrlGeneratorInterface $urlGenerator;
  23.     private ApiServicePortalService $apiServicePortalService;
  24.     public function __construct(UrlGeneratorInterface $urlGeneratorApiServicePortalService $apiServicePortalService)
  25.     {
  26.         $this->urlGenerator $urlGenerator;
  27.         $this->apiServicePortalService $apiServicePortalService;
  28.     }
  29.     public function authenticate(Request $request): Passport
  30.     {
  31.         $username $request->request->get('username''');
  32.         $password $request->request->get('password''');
  33.         $request->getSession()->set(Security::LAST_USERNAME$username);
  34.         $resource sprintf('user/check-credentials/%s/%s'$usernameUser::ROLE_PROVIDER);
  35.         $options = [
  36.             ApiInterface::HEADERS => ['user-password' => $password]
  37.         ];
  38.         $response $this->apiServicePortalService->apiRequest(
  39.             ApiInterface::GET,
  40.             $resource,
  41.             [],
  42.             $options
  43.         );
  44.         if (ApiInterface::API_SUCCESS_CODE_200 !== $response[ApiInterface::RESPONSE_CODE]) {
  45.             $msg 'Fallo en la conexión';
  46.             if (isset($response[ApiInterface::RESPONSE_STATUS][ApiInterface::RESPONSE_STATUS_MESSAGE])) {
  47.                 $msg $response[ApiInterface::RESPONSE_STATUS][ApiInterface::RESPONSE_STATUS_MESSAGE];
  48.             }
  49.             throw new CustomUserMessageAuthenticationException(
  50.                 $msg
  51.             );
  52.         }
  53.         $userUuid $response[ApiInterface::RESPONSE_DATA]['user']['uuid'];
  54.         /* Using callback function in UserBadge
  55.            Not call to src/Security/UserProvider.php::loadUserByIdentifier
  56.          */
  57.         return new SelfValidatingPassport(
  58.             new UserBadge($userUuid, function ($userUuid) use ($response) {
  59.                 $userName $response[ApiInterface::RESPONSE_DATA]['user']['username'];
  60.                 $userEmail $response[ApiInterface::RESPONSE_DATA]['user']['email'];
  61.                 return (new User())
  62.                     ->setUsername($userName)
  63.                     ->setUuid($userUuid)
  64.                     ->setEmail($userEmail)
  65.                     ->setRoles([User::ROLE_PROVIDER]);
  66.             })
  67.         );
  68.     }
  69.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  70.     {
  71.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  72.             return new RedirectResponse($targetPath);
  73.         }
  74.         // For example:
  75.         return new RedirectResponse($this->urlGenerator->generate('work_order_list'));
  76.     }
  77.     protected function getLoginUrl(Request $request): string
  78.     {
  79.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  80.     }
  81. }