app/Plugin/MembersOnlyUserData4/Event.php line106

Open in your IDE?
  1. <?php
  2. namespace Plugin\MembersOnlyUserData4;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\KernelEvents;
  5. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  6. use Eccube\Event\TemplateEvent;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Eccube\Common\EccubeConfig;
  12. use Eccube\Repository\PageRepository;
  13. use Eccube\Entity\Page;
  14. class Event implements EventSubscriberInterface
  15. {
  16.     /**
  17.      *
  18.      * @var type 
  19.      */
  20.     private $container;
  21.     
  22.     /**
  23.      * @var AuthorizationCheckerInterface
  24.      */
  25.     private $authorizationChecker;
  26.     /**
  27.      *
  28.      * @var RequestStack 
  29.      */
  30.     private $requestStack;
  31.     
  32.     /**
  33.      *
  34.      * @var EccubeConfig 
  35.      */
  36.     private $eccubeConfig;
  37.     
  38.     /**
  39.      *
  40.      * @var PageRepository
  41.      */
  42.     private $pageRepository;
  43.     public function __construct(
  44.         ContainerInterface $container,
  45.         AuthorizationCheckerInterface $authorizationChecker
  46.         RequestStack $requestStack
  47.         EccubeConfig $eccubeConfig,
  48.         PageRepository $pageRepository
  49.     )
  50.     {
  51.         $this->container $container;
  52.         $this->authorizationChecker $authorizationChecker;
  53.         $this->requestStack $requestStack;
  54.         $this->eccubeConfig $eccubeConfig;
  55.         $this->pageRepository $pageRepository;
  56.     }
  57.     
  58.     /**
  59.      * @return array
  60.      */
  61.     public static function getSubscribedEvents()
  62.     {
  63.         return [
  64.             KernelEvents::RESPONSE => 'onKernelResponse',
  65.             'Mypage/login.twig' => 'onTemplateMypageLogin',
  66.         ];
  67.     }
  68.     
  69.     public function onKernelResponse(FilterResponseEvent $event)
  70.     {
  71.         $request $this->requestStack->getCurrentRequest();
  72.         $adminRoute $this->eccubeConfig->get('eccube_admin_route');
  73.         $path rawurldecode($request->getPathInfo());
  74.         if (preg_match("/^\/({$adminRoute})/i"$path)) {
  75.             return;
  76.         }        
  77.         if ($route $request->get("route")) {
  78.             $Page $this->pageRepository->findOneBy(
  79.                 [
  80.                     'url' => $route,
  81.                     'edit_type' => Page::EDIT_TYPE_USER,
  82.                 ]
  83.             );
  84.             if ($Page->isPlgMembersOnlyUserData4()) {
  85.                 if ($this->authorizationChecker->isGranted('ROLE_USER'))
  86.                     return;
  87.                 $this->container->get('session')->getFlashBag()->set('members_only_user_data4.mypage.login.message'trans(
  88.                     'members_only_user_data4.mypage.login.message', [
  89.                     'PageName' => $Page->getName()]));
  90.                 $event->setResponse(
  91.                     new RedirectResponse($this->container->get('router')->generate('mypage_login'))
  92.                 );
  93.             }
  94.         }
  95.     }
  96.     
  97.     public function onTemplateMypageLogin(TemplateEvent $event)
  98.     {
  99.         if ($this->container->get('session')->getFlashBag()->has('members_only_user_data4.mypage.login.message')) {
  100.             $event->addSnippet('@MembersOnlyUserData4/default/Mypage/login_message.twig');
  101.         }
  102.     }
  103. }