app/Plugin/CustomerGroup/EventListener/ProductListener.php line 59

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of CustomerGroupPrice
  4.  *
  5.  * Copyright(c) Akira Kurozumi <info@a-zumi.net>
  6.  *
  7.  * https://a-zumi.net
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\CustomerGroup\EventListener;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Entity\Category;
  15. use Eccube\Entity\Page;
  16. use Eccube\Entity\Product;
  17. use Plugin\CustomerGroup\Security\Authorization\Voter\CategoryVoter;
  18. use Plugin\CustomerGroup\Security\Authorization\Voter\PageVoter;
  19. use Plugin\CustomerGroup\Security\Authorization\Voter\ProductVoter;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  23. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  24. use Symfony\Component\HttpKernel\KernelEvents;
  25. use Symfony\Component\Security\Core\Security;
  26. class ProductListener implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var Security
  30.      */
  31.     private $security;
  32.     /**
  33.      * @var EntityManagerInterface
  34.      */
  35.     private $entityManager;
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             KernelEvents::REQUEST => 'onKernelRequest',
  40.         ];
  41.     }
  42.     public function __construct(
  43.         Security $security,
  44.         EntityManagerInterface $entityManager
  45.     )
  46.     {
  47.         $this->security $security;
  48.         $this->entityManager $entityManager;
  49.     }
  50.     public function onKernelRequest(GetResponseEvent $event)
  51.     {
  52.         if (false === $event->isMasterRequest()) {
  53.             return;
  54.         }
  55.         switch ($event->getRequest()->attributes->get('_route')) {
  56.             case 'product_list':
  57.                 if ($category $this->findCategory($event->getRequest())) {
  58.                     if (false === $this->security->isGranted(CategoryVoter::VIEW$category)) {
  59.                         throw new AccessDeniedHttpException();
  60.                     }
  61.                 }
  62.                 break;
  63.             case 'product_detail':
  64.                 if ($product $this->findProduct($event->getRequest())) {
  65.                     if (false === $this->security->isGranted(ProductVoter::VIEW$product)) {
  66.                         throw new AccessDeniedHttpException();
  67.                     }
  68.                 }
  69.                 break;
  70.             case 'user_data':
  71.                 if ($page $this->findPage($event->getRequest())) {
  72.                     if (false === $this->security->isGranted(PageVoter::VIEW$page)) {
  73.                         throw new AccessDeniedHttpException();
  74.                     }
  75.                 }
  76.         }
  77.     }
  78.     /**
  79.      * @param Request $request
  80.      * @return Category|null
  81.      */
  82.     protected function findCategory(Request $request): ?Category
  83.     {
  84.         // 数字以外だとpsglでエラーが発生するので
  85.         if (!preg_match('/^\d+$/'$request->query->get('category_id'), $matches)) {
  86.             return null;
  87.         }
  88.         return $this->entityManager->getRepository(Category::class)->find($matches[0]);
  89.     }
  90.     /**
  91.      * @param Request $request
  92.      * @return Product|null
  93.      */
  94.     protected function findProduct(Request $request): ?Product
  95.     {
  96.         if (!$id $request->get('id')) {
  97.             return null;
  98.         }
  99.         return $this->entityManager->getRepository(Product::class)->find($id);
  100.     }
  101.     protected function findPage(Request $request): ?Page
  102.     {
  103.         if (!$route $request->get('route')) {
  104.             return null;
  105.         }
  106.         return $this->entityManager->getRepository(Page::class)->findOneBy([
  107.             'url' => $route,
  108.             'edit_type' => Page::EDIT_TYPE_USER
  109.         ]);
  110.     }
  111. }