custom/plugins/CioSalesChannelSwitcher/src/Subscriber/HeaderSalesChannelSelectorSubscriber.php line 83

Open in your IDE?
  1. <?php
  2. namespace CioSalesChannelSwitcher\Subscriber;
  3. use CioCustomerPermissionGroups\Service\CustomerPermissionService;
  4. use CioSalesChannelSwitcher\Service\SwitchSalesChannelService;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  7. use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  13. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  14. use Shopware\Storefront\Event\StorefrontRenderEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Session\Session;
  18. class HeaderSalesChannelSelectorSubscriber implements EventSubscriberInterface
  19. {
  20.     private EntityRepositoryInterface $salesChannelRepository;
  21.     private CustomerPermissionService $customerPermissionService;
  22.     private SwitchSalesChannelService $switchSalesChannelService;
  23.     private Session $session;
  24.     public function __construct(EntityRepositoryInterface $salesChannelRepositoryCustomerPermissionService $customerPermissionServiceSwitchSalesChannelService $switchSalesChannelServiceSession $session)
  25.     {
  26.         $this->salesChannelRepository $salesChannelRepository;
  27.         $this->customerPermissionService $customerPermissionService;
  28.         $this->switchSalesChannelService $switchSalesChannelService;
  29.         $this->session $session;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             StorefrontRenderEvent::class => 'onStorefrontRender',
  35.             CustomerLoginEvent::class => 'onCustomerLoginEvent',
  36.             CustomerLogoutEvent::class => 'onCustomerLogoutEvent'
  37.         ];
  38.     }
  39.     public function onStorefrontRender(StorefrontRenderEvent $event)
  40.     {
  41.         $customer $event->getSalesChannelContext()->getCustomer();
  42.         if ($customer) {
  43.             $salesChannels $this->getAllowedSalesChannels($customer$event->getSalesChannelContext()->getContext());
  44.             $event->setParameter('salesChannels'$salesChannels);
  45.             $event->setParameter('currentSalesChannelId'$event->getSalesChannelContext()->getSalesChannel()->getId());
  46.         }
  47.     }
  48.     public function onCustomerLoginEvent(CustomerLoginEvent $event)
  49.     {
  50.         /** @var string[] $allowedSalesChannelIds */
  51.         $allowedSalesChannelIds array_map(function (SalesChannelEntity $salesChannelEntity) {
  52.             return $salesChannelEntity->getId();
  53.         }, $this->getAllowedSalesChannels($event->getCustomer(), $event->getSalesChannelContext()->getContext()));
  54.         if ($this->session->has('cio_master_login_current') && $this->session->has('cio_master_login_sales_channel_id')) {
  55.             $this->session->remove('cio_master_login_current');
  56.             $this->session->save();
  57.             (new RedirectResponse($this->switchSalesChannelService->getSalesChannelDomainEntityAndLogIn($this->session->get('cio_master_login_sales_channel_id'), $event->getSalesChannelId(), $event->getSalesChannelContext())))->send();
  58.         }
  59.         // user can't access the current sales channel
  60.         if (!in_array($event->getSalesChannelId(), $allowedSalesChannelIds)) {
  61.             // if user has access to other sales channels, then login to the first allowed
  62.             if (count($allowedSalesChannelIds) > 0) {
  63.                 if (!$this->session->get('ignore_sales_channel_switch_during_login')) {
  64.                     (new RedirectResponse($this->switchSalesChannelService->getSalesChannelDomainEntityAndLogIn($allowedSalesChannelIds[array_key_first($allowedSalesChannelIds)], $event->getSalesChannelId(), $event->getSalesChannelContext())))->send();
  65.                 }
  66.             }
  67.         }
  68.     }
  69.     public function onCustomerLogoutEvent(CustomerLogoutEvent $event)
  70.     {
  71.         $this->session->set('ignore_sales_channel_switch_during_login'false);
  72.     }
  73.     protected function getAllowedSalesChannels(CustomerEntity $customerContext $context): array
  74.     {
  75.         $salesChannels = [];
  76.         $salesChannelEntities $this->salesChannelRepository
  77.             ->search((new Criteria())->addFilter(new NotFilter(NotFilter::CONNECTION_AND, [new EqualsFilter('name''Headless')])), $context)
  78.             ->getEntities();
  79.         foreach ($salesChannelEntities as $salesChannelEntity) {
  80.             /** @var $salesChannelEntity SalesChannelEntity */
  81.             foreach ($this->customerPermissionService->getCustomerPermissionGroups($customer$context) as $customerPermissionGroup) {
  82.                 if ($customerPermissionGroup->getName() == $salesChannelEntity->getName()) {
  83.                     $salesChannels[] = $salesChannelEntity;
  84.                 }
  85.             }
  86.         }
  87.         return $salesChannels;
  88.     }
  89. }