custom/plugins/NetiNextAccessManager/src/Subscriber/LoginSubscriber.php line 95

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextAccessManager\Subscriber;
  4. use NetInventors\NetiNextAccessManager\Constants\ConfigConstants;
  5. use NetInventors\NetiNextAccessManager\Extension\Content\Customer\Aggregate\CustomerAttribute\CustomerAttributeEntity;
  6. use NetInventors\NetiNextAccessManager\Service\PluginConfig;
  7. use Shopware\Core\Checkout\Customer\CustomerEntity;
  8. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\Routing\RouterInterface;
  18. class LoginSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var bool
  22.      */
  23.     private $allowlogin;
  24.     /**
  25.      * @var bool
  26.      */
  27.     private $afterLogin false;
  28.     /**
  29.      * @var RouterInterface
  30.      */
  31.     protected $router;
  32.     /**
  33.      * @var EntityRepositoryInterface
  34.      */
  35.     protected $customerRepository;
  36.     /**
  37.      * @var PluginConfig
  38.      */
  39.     protected $pluginConfig;
  40.     /**
  41.      * LoginSubscriber constructor.
  42.      *
  43.      * @param RouterInterface           $router
  44.      * @param EntityRepositoryInterface $customerRepository
  45.      * @param PluginConfig              $pluginConfig
  46.      */
  47.     public function __construct(
  48.         RouterInterface $router,
  49.         EntityRepositoryInterface $customerRepository,
  50.         PluginConfig $pluginConfig
  51.     ) {
  52.         $this->router             $router;
  53.         $this->customerRepository $customerRepository;
  54.         $this->pluginConfig       $pluginConfig;
  55.     }
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             CustomerLoginEvent::class => 'onCustomerLogin',
  60.             KernelEvents::RESPONSE    => 'onResponse',
  61.         ];
  62.     }
  63.     /**
  64.      * @param CustomerLoginEvent $event
  65.      */
  66.     public function onCustomerLogin(CustomerLoginEvent $event): void
  67.     {
  68.         if (!$this->pluginConfig->isActive()) {
  69.             return;
  70.         }
  71.         $customerId $event->getCustomer()->getId();
  72.         //The customer from the event has a not updated Attribute so we have to get an updated Customer
  73.         $customer $this->getUpdatedCustomer($customerId$event->getContext());
  74.         /** @var CustomerAttributeEntity $amAttribute */
  75.         $amAttribute $customer->getExtension('netiAccessManagerCustomerAttribute');
  76.         $this->allowlogin $amAttribute->isActivated();
  77.         $this->afterLogin true;
  78.     }
  79.     public function onResponse(ResponseEvent $event): void
  80.     {
  81.         if (!$this->pluginConfig->isActive()) {
  82.             return;
  83.         }
  84.         if (false === $this->allowlogin) {
  85.             $response = new RedirectResponse(
  86.                 $this->router->generate('frontend.access_manager.logout')
  87.             );
  88.             $event->setResponse($response);
  89.         }
  90.         if ($this->pluginConfig->getLoginRedirectType() === ConfigConstants::CONFIG_REDIRECT_TYPE_DEFAULT) {
  91.             return;
  92.         }
  93.         if ($this->pluginConfig->getLoginRedirectType() === ConfigConstants::CONFIG_REDIRECT_TYPE_REFERER) {
  94.             if ($this->afterLogin === true) {
  95.                 $redirect $event->getRequest()->getSession()->get('neti-access-manager-referer');
  96.                 if ($redirect['route'] === 'frontend.account.logout.page') {
  97.                     return;
  98.                 }
  99.                 $response = new RedirectResponse(
  100.                     $this->router->generate($redirect['route'], $redirect['routeParams'])
  101.                 );
  102.                 $event->setResponse($response);
  103.                 return;
  104.             }
  105.             $referer['route']       = $event->getRequest()->attributes->get('_route');
  106.             $referer['routeParams'] = $event->getRequest()->attributes->get('_route_params');
  107.             if ($referer['route'] !== 'frontend.account.login.page' && $referer['route'] !== 'frontend.checkout.info') {
  108.                 $event->getRequest()->getSession()->set('neti-access-manager-referer'$referer);
  109.             }
  110.         }
  111.         if ($this->afterLogin === false) {
  112.             return;
  113.         }
  114.         if (
  115.             $this->pluginConfig->getLoginRedirectType() === ConfigConstants::CONFIG_REDIRECT_TYPE_CATEGORY
  116.             && $this->pluginConfig->getLoginRedirectCategory() !== ''
  117.         ) {
  118.             $response = new RedirectResponse(
  119.                 $this->router->generate(
  120.                     'frontend.navigation.page',
  121.                     [ 'navigationId' => $this->pluginConfig->getLoginRedirectCategory() ]
  122.                 )
  123.             );
  124.             $event->setResponse($response);
  125.             return;
  126.         }
  127.         if (
  128.             $this->pluginConfig->getLoginRedirectType() === ConfigConstants::CONFIG_REDIRECT_TYPE_LANDING_PAGE
  129.             && $this->pluginConfig->getLoginRedirectLandingPage() !== ''
  130.         ) {
  131.             $response = new RedirectResponse(
  132.                 $this->router->generate(
  133.                     'frontend.landing.page',
  134.                     [ 'landingPageId' => $this->pluginConfig->getLoginRedirectLandingPage() ]
  135.                 )
  136.             );
  137.             $event->setResponse($response);
  138.         }
  139.     }
  140.     /**
  141.      * @param string  $customerId
  142.      * @param Context $context
  143.      *
  144.      * @return CustomerEntity
  145.      */
  146.     private function getUpdatedCustomer(string $customerIdContext $context): CustomerEntity
  147.     {
  148.         $criteria = new Criteria();
  149.         $criteria->addFilter(new EqualsFilter('id'$customerId));
  150.         return $this->customerRepository->search($criteria$context)->first();
  151.     }
  152. }