vendor/shopware/core/Framework/Api/EventListener/Authentication/SalesChannelAuthenticationListener.php line 51

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\EventListener\Authentication;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Api\Util\AccessKeyHelper;
  5. use Shopware\Core\Framework\Routing\Exception\SalesChannelNotFoundException;
  6. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  7. use Shopware\Core\Framework\Routing\RouteScopeCheckTrait;
  8. use Shopware\Core\Framework\Routing\RouteScopeRegistry;
  9. use Shopware\Core\Framework\Routing\StoreApiRouteScope;
  10. use Shopware\Core\Framework\Uuid\Uuid;
  11. use Shopware\Core\PlatformRequest;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  14. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. class SalesChannelAuthenticationListener implements EventSubscriberInterface
  17. {
  18.     use RouteScopeCheckTrait;
  19.     /**
  20.      * @var Connection
  21.      */
  22.     private $connection;
  23.     /**
  24.      * @var RouteScopeRegistry
  25.      */
  26.     private $routeScopeRegistry;
  27.     /**
  28.      * @internal
  29.      */
  30.     public function __construct(
  31.         Connection $connection,
  32.         RouteScopeRegistry $routeScopeRegistry
  33.     ) {
  34.         $this->connection $connection;
  35.         $this->routeScopeRegistry $routeScopeRegistry;
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             KernelEvents::CONTROLLER => ['validateRequest'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_PRIORITY_AUTH_VALIDATE],
  41.         ];
  42.     }
  43.     public function validateRequest(ControllerEvent $event): void
  44.     {
  45.         $request $event->getRequest();
  46.         if (!$request->attributes->get('auth_required'true)) {
  47.             return;
  48.         }
  49.         if (!$this->isRequestScoped($requestStoreApiRouteScope::class)) {
  50.             return;
  51.         }
  52.         if (!$request->headers->has(PlatformRequest::HEADER_ACCESS_KEY)) {
  53.             throw new UnauthorizedHttpException('header'sprintf('Header "%s" is required.'PlatformRequest::HEADER_ACCESS_KEY));
  54.         }
  55.         $accessKey $request->headers->get(PlatformRequest::HEADER_ACCESS_KEY);
  56.         $origin AccessKeyHelper::getOrigin($accessKey);
  57.         if ($origin !== 'sales-channel') {
  58.             throw new SalesChannelNotFoundException();
  59.         }
  60.         $salesChannelId $this->getSalesChannelId($accessKey);
  61.         $request->attributes->set(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID$salesChannelId);
  62.     }
  63.     protected function getScopeRegistry(): RouteScopeRegistry
  64.     {
  65.         return $this->routeScopeRegistry;
  66.     }
  67.     private function getSalesChannelId(string $accessKey): string
  68.     {
  69.         $builder $this->connection->createQueryBuilder();
  70.         $salesChannelId $builder->select(['sales_channel.id'])
  71.             ->from('sales_channel')
  72.             ->where('sales_channel.access_key = :accessKey')
  73.             ->setParameter('accessKey'$accessKey)
  74.             ->execute()
  75.             ->fetchColumn();
  76.         if (!$salesChannelId) {
  77.             throw new SalesChannelNotFoundException();
  78.         }
  79.         return Uuid::fromBytesToHex($salesChannelId);
  80.     }
  81. }