custom/plugins/b2bsellerscore/src/Components/B2bPlatform/Subscriber/RequestSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace B2bSellersCore\Components\B2bPlatform\Subscriber;
  3. use B2bSellersCore\Components\B2bPlatform\Exception\B2bPlatformContextException;
  4. use B2bSellersCore\Components\Employee\Exception\InsufficientEmployeePermissionException;
  5. use B2bSellersCore\Components\Framework\Routing\Annotation\B2bPlatformContextRequired;
  6. use B2bSellersCore\Components\Framework\Routing\Annotation\B2bPlatformPermission;
  7. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. class RequestSubscriber implements EventSubscriberInterface
  13. {
  14.     private RequestStack $requestStack;
  15.     public function __construct(RequestStack $requestStack)
  16.     {
  17.         $this->requestStack $requestStack;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             SalesChannelContextResolvedEvent::class => 'checkB2bPlatformAnnotations'
  23.         ];
  24.     }
  25.     public function checkB2bPlatformAnnotations(SalesChannelContextResolvedEvent $event)
  26.     {
  27.         $this->validateB2bPlatformContextRequired($this->getRequest(), $event->getSalesChannelContext());
  28.         $this->validateB2bPlatformPermission($this->getRequest(), $event->getSalesChannelContext());
  29.     }
  30.     private function validateB2bPlatformContextRequired(Request $requestSalesChannelContext $context): void
  31.     {
  32.         /** @var B2bPlatformContextRequired|null $b2bPlatformContextRequired */
  33.         $b2bPlatformContextRequired $request->attributes->get(B2bPlatformContextRequired::ATTRIBUTE_B2B_PlATFORM_CONTEXT_REQUIRED);
  34.         if ($b2bPlatformContextRequired === null) {
  35.             return;
  36.         }
  37.         if ($b2bPlatformContextRequired->hasB2bPlatformContext($context)) {
  38.             return;
  39.         }
  40.         throw new B2bPlatformContextException();
  41.     }
  42.     private function validateB2bPlatformPermission(Request $requestSalesChannelContext $context): void
  43.     {
  44.         /** @var B2bPlatformPermission|null $b2bPlatformPermission */
  45.         $b2bPlatformPermission $request->attributes->get(B2bPlatformPermission::ATTRIBUTE_B2B_PlATFORM_PERMISSION);
  46.         if ($b2bPlatformPermission === null) {
  47.             return;
  48.         }
  49.         if ($b2bPlatformPermission->hasB2bPlatformPermission($context)) {
  50.             return;
  51.         }
  52.         throw new InsufficientEmployeePermissionException();
  53.     }
  54.     private function getRequest(): Request
  55.     {
  56.         return $this->requestStack->getCurrentRequest();
  57.     }
  58. }