vendor/store.shopware.com/swagb2bplatform/components/StoreFrontAuthentication/Framework/AuthenticationService.php line 48

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\StoreFrontAuthentication\Framework;
  3. use RuntimeException;
  4. use Shopware\B2B\Common\IdValue;
  5. use Shopware\B2B\Common\Repository\NotFoundException;
  6. class AuthenticationService
  7. {
  8.     private AuthStorageAdapterInterface $authStorageAdapter;
  9.     private StoreFrontAuthenticationRepository $authenticationRepository;
  10.     private IdentityChainIdentityLoader $identityChainRepository;
  11.     private LoginContextService $loginContextService;
  12.     public function __construct(
  13.         AuthStorageAdapterInterface $authStorageAdapter,
  14.         StoreFrontAuthenticationRepository $authenticationRepository,
  15.         IdentityChainIdentityLoader $identityChainRepository,
  16.         LoginContextService $loginContextService
  17.     ) {
  18.         $this->authStorageAdapter $authStorageAdapter;
  19.         $this->authenticationRepository $authenticationRepository;
  20.         $this->identityChainRepository $identityChainRepository;
  21.         $this->loginContextService $loginContextService;
  22.     }
  23.     public function getIdentity(): Identity
  24.     {
  25.         if (!$this->isAuthenticated()) {
  26.             throw new NotAuthenticatedException('Not authenticated, can not provide a valid identity.');
  27.         }
  28.         return $this->authStorageAdapter->getIdentity();
  29.     }
  30.     public function isAuthenticated(): bool
  31.     {
  32.         return $this->authStorageAdapter->isAuthenticated();
  33.     }
  34.     public function isB2b(): bool
  35.     {
  36.         try {
  37.             $this->getIdentity();
  38.         } catch (NotFoundException NotAuthenticatedException NoIdentitySetException RuntimeException $e) {
  39.             return false;
  40.         }
  41.         return true;
  42.     }
  43.     public function is($className): bool
  44.     {
  45.         if (!$this->isB2b()) {
  46.             return false;
  47.         }
  48.         return $this->getIdentity() instanceof $className;
  49.     }
  50.     public function getIdentityByAuthId(IdValue $authId): Identity
  51.     {
  52.         $auth $this->authenticationRepository
  53.             ->fetchAuthenticationById($authId);
  54.         return $this->identityChainRepository
  55.             ->fetchIdentityByAuthentication($auth$this->loginContextService);
  56.     }
  57. }