app/Plugin/DisableNonMember42/Event.php line 65

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of DisableNonMember42
  4.  * Copyright(c) U-Mebius Inc. All Rights Reserved.
  5.  * https://umebius.com/
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. namespace Plugin\DisableNonMember42;
  10. use Eccube\Controller\NonMemberShoppingController;
  11. use Eccube\Event\EccubeEvents;
  12. use Eccube\Event\EventArgs;
  13. use Eccube\Event\TemplateEvent;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  18. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use Symfony\Component\Routing\RouterInterface;
  21. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  22. class Event implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var ContainerInterface
  26.      */
  27.     private $container;
  28.     /**
  29.      * @var AuthorizationCheckerInterface
  30.      */
  31.     protected $authorizationChecker;
  32.     /**
  33.      * @var RouterInterface
  34.      */
  35.     private $router;
  36.     public function __construct(
  37.         ContainerInterface $container,
  38.         AuthorizationCheckerInterface $authorizationChecker,
  39.         RouterInterface $router
  40.     ) {
  41.         $this->container $container;
  42.         $this->authorizationChecker $authorizationChecker;
  43.         $this->router $router;
  44.     }
  45.     /**
  46.      * @return array
  47.      */
  48.     public static function getSubscribedEvents()
  49.     {
  50.         return [
  51.             KernelEvents::CONTROLLER => 'onKernelController',
  52.             EccubeEvents::FRONT_CART_BUYSTEP_COMPLETE => 'onFrontCartBuystepComplete',
  53.             'Shopping/login.twig' => 'onShoppingLoginTwigRender',
  54.         ];
  55.     }
  56.     public function onKernelController(ControllerEvent $event)
  57.     {
  58.         $controllers $event->getController();
  59.         if (!is_array($controllers)) {
  60.             return;
  61.         }
  62.         foreach ($controllers as $controller) {
  63.             if ($controller instanceof NonMemberShoppingController) {
  64.                 $event->setController(function () {
  65.                     return new RedirectResponse($this->router->generate('entry'));
  66.                 });
  67.                 break;
  68.             }
  69.         }
  70.     }
  71.     public function onFrontCartBuystepComplete(EventArgs $eventArgs)
  72.     {
  73.         // IS_AUTHENTICATED_REMEMBEREDの場合はShoppingControllerからリダイレクトされてくるのでROLE_USERにしておく
  74.         if (!$this->authorizationChecker->isGranted('ROLE_USER')) {
  75.             $response = new RedirectResponse($this->container->get('router')->generate('shopping_login'));
  76.             $eventArgs->setResponse($response);
  77.         }
  78.     }
  79.     public function onShoppingLoginTwigRender(TemplateEvent $event)
  80.     {
  81.         $event->addAsset('@DisableNonMember42/shopping_login_css.twig');
  82.         $event->addSnippet('@DisableNonMember42/shopping_login.twig');
  83.     }
  84. }