src/Security/Http/Authenticator/WebAuthenticator.php line 47

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XCart\Security\Http\Authenticator;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  12. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  18. use XCart\Security\DTO\WebUser;
  19. use XLite\Core\Auth;
  20. use XLite\Core\Database;
  21. use XLite\Model\Profile;
  22. final class WebAuthenticator extends AbstractAuthenticator
  23. {
  24.     private \XLite\Model\Repo\Profile $profileRepository;
  25.     public function __construct()
  26.     {
  27.         $this->profileRepository Database::getRepo(Profile::class);
  28.     }
  29.     public function supports(Request $request): bool
  30.     {
  31.         return $request->query->get('target') !== 'login';
  32.     }
  33.     public function authenticate(Request $request): Passport
  34.     {
  35.         $user Auth::getInstance()->getProfile() ?? new Profile();
  36.         return new SelfValidatingPassport(
  37.             new UserBadge(
  38.                 $user->getLogin(),
  39.                 function (string $userIdentifier): UserInterface {
  40.                     $profile $this->profileRepository->findByLogin($userIdentifier);
  41.                     if ($profile === null) {
  42.                         $e = new UserNotFoundException(sprintf('User with "%s" not found.'$userIdentifier));
  43.                         $e->setUserIdentifier($userIdentifier);
  44.                         throw $e;
  45.                     }
  46.                     return new WebUser($profile);
  47.                 }
  48.             )
  49.         );
  50.     }
  51.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  52.     {
  53.         return null;
  54.     }
  55.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  56.     {
  57.         return $request->getRequestUri() === '/admin/' && $request->getMethod() !== Request::METHOD_POST
  58.             ? new RedirectResponse(
  59.                 \XLite::getInstance()->getShopURL('admin/?target=login')
  60.             )
  61.             : null;
  62.     }
  63. }