custom/plugins/NgsShopRestrict/src/Subscriber/GenericPageSubscriber.php line 221

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Ngs\ShopRestrict\Subscriber;
  3. use Ngs\ShopRestrict\Core\Struct\AllowedRouteStruct;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. class GenericPageSubscriber implements EventSubscriberInterface
  14. {
  15.     const PREFIX_STOREFRONT 'frontend';
  16.     const NOT_FOUND_MESSAGE 'This page is not allowed';
  17.     const ROUTE_QUICKVIEW_MINIMAL 'widgets.quickview.minimal';
  18.     const ROUTE_CMS_PAGE 'frontend.cms.page';
  19.     private $routeList = [
  20.         'account' => [
  21.             'frontend.account.order.page',
  22.             'frontend.account.order.cancel',
  23.             'frontend.account.order.single.page',
  24.             'frontend.account.edit-order.page',
  25.             'frontend.account.edit-order.change-payment-method',
  26.             'frontend.account.edit-order.update-order',
  27.             'frontend.account.payment.page',
  28.             'frontend.account.payment.save',
  29.             'frontend.account.home.page',
  30.             'frontend.account.profile.page',
  31.             'frontend.account.profile.save',
  32.             'frontend.account.profile.email.save',
  33.             'frontend.account.profile.password.save',
  34.             'frontend.account.profile.delete',
  35.             'frontend.account.address.page',
  36.             'frontend.account.address.create.page',
  37.             'frontend.account.address.edit.page',
  38.             'frontend.account.address.set-default-address',
  39.             'frontend.account.address.delete',
  40.             'frontend.account.address.create',
  41.             'frontend.account.address.edit.save',
  42.             'frontend.account.addressbook',
  43.             'frontend.account.login.page',
  44.             'frontend.account.guest.login.page',
  45.             'frontend.account.logout.page',
  46.             'frontend.account.login',
  47.             'frontend.account.recover.page',
  48.             'frontend.account.recover.request',
  49.             'frontend.account.recover.password.page',
  50.             'frontend.account.recover.password.reset',
  51.             'frontend.account.order.single.document',
  52.             'frontend.account.order.single.download',
  53.             'frontend.account.newsletter',
  54.             'frontend.account.register.page',
  55.             'frontend.account.customer-group-registration.page',
  56.             'frontend.account.register.save',
  57.             'frontend.account.register.mail',
  58.             'frontend.well-known.change-password',
  59.             'frontend.newsletter.subscribe',
  60.             'frontend.form.newsletter.register.handle',
  61.             'frontend.checkout.register.page',
  62.         ],
  63.         'checkout' => [
  64.             'frontend.checkout.line-item.delete',
  65.             'frontend.checkout.line-items.delete',
  66.             'frontend.checkout.promotion.add',
  67.             'frontend.checkout.line-item.change-quantity',
  68.             'frontend.checkout.line-items.update',
  69.             'frontend.checkout.product.add-by-number',
  70.             'frontend.checkout.line-item.add',
  71.             'frontend.checkout.cart.page',
  72.             'frontend.checkout.cart.json',
  73.             'frontend.checkout.confirm.page',
  74.             'frontend.checkout.finish.page',
  75.             'frontend.checkout.finish.order',
  76.             'frontend.checkout.info',
  77.             'frontend.checkout.configure',
  78.             'frontend.checkout.switch-language',
  79.             'frontend.checkout.register.page',
  80.             'frontend.cart.offcanvas',
  81.             //payment routes
  82.             'frontend.account.payment.page',
  83.             'frontend.account.payment.save',
  84.             //address routes
  85.             'frontend.account.address.page',
  86.             'frontend.account.address.create.page',
  87.             'frontend.account.address.edit.page',
  88.             'frontend.account.address.set-default-address',
  89.             'frontend.account.address.delete',
  90.             'frontend.account.address.create',
  91.             'frontend.account.address.edit.save',
  92.             //account order
  93.             'frontend.account.order.page',
  94.             'frontend.account.order.cancel',
  95.             'frontend.account.order.single.page',
  96.             'frontend.account.edit-order.page',
  97.             'frontend.account.edit-order.change-payment-method',
  98.             'frontend.account.edit-order.update-order',
  99.             //account payment
  100.             'frontend.account.payment.page',
  101.             'frontend.account.payment.save',
  102.         ],
  103.         'wishlist' => [
  104.             'frontend.wishlist.page',
  105.             'frontend.wishlist.guestPage.pagelet',
  106.             'frontend.wishlist.product.list',
  107.             'frontend.wishlist.product.delete',
  108.             'frontend.wishlist.product.add',
  109.             'frontend.wishlist.product.remove',
  110.             'frontend.wishlist.add.after.login',
  111.             'frontend.wishlist.product.merge',
  112.             'frontend.wishlist.product.merge.pagelet',
  113.         ],
  114.         'cms' => [
  115.             'frontend.cms.page',
  116.             'frontend.cms.buybox.switch',
  117.         ],
  118.         'legal_pages' => [
  119.             'frontend.cms.page',
  120.         ],
  121.         'sorting_and_filter' => [
  122.             'frontend.cms.navigation.page',
  123.             'frontend.cms.navigation.filter',
  124.         ],
  125.         'search' => [
  126.             'frontend.search.page',
  127.             'frontend.search.suggest',
  128.         ],
  129.         'review' => [
  130.             'frontend.detail.review.save',
  131.             'frontend.product.reviews',
  132.         ],
  133.         'support' => [
  134.             'frontend.form.contact.send',
  135.         ],
  136.         'newsletter' => [
  137.             'frontend.newsletter.subscribe',
  138.             'frontend.form.newsletter.register.handle',
  139.             'frontend.account.newsletter',
  140.         ],
  141.         //TODO other routes
  142. //        'other' => [
  143. //            'frontend.captcha.basic-captcha.load',
  144. //            'frontend.captcha.basic-captcha.validate',
  145. //            'frontend.country.country.data',
  146. //            'frontend.landing.page',
  147. //            'frontend.maintenance.page',
  148. //            'frontend.maintenance.singlepage',
  149. //            'frontend.navigation.page',
  150. //            'frontend.menu.offcanvas',
  151. //            'frontend.script_endpoint',
  152. //        ],
  153.         'cookie' => [
  154.             'frontend.cookie.offcanvas',
  155.             'frontend.cookie.permission',
  156.         ],
  157.         'product_detail' => [
  158.             'frontend.detail.page',
  159.             'frontend.detail.switch',
  160.             'widgets.quickview.minimal'
  161.         ],
  162.         'description' => [],
  163.         'frontend.home.page',
  164.         'frontend.sitemap.xml',
  165.     ];
  166.     private SystemConfigService $systemConfigService;
  167.     private $entityRepository;
  168.     /**
  169.      * @param SystemConfigService $systemConfigService
  170.      */
  171.     public function __construct(SystemConfigService $systemConfigServiceEntityRepository $entityRepository)
  172.     {
  173.         $this->systemConfigService $systemConfigService;
  174.         $this->entityRepository $entityRepository;
  175.     }
  176.     /**
  177.      * @return string[]
  178.      */
  179.     public static function getSubscribedEvents(): array
  180.     {
  181.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  182.         return [
  183.             GenericPageLoadedEvent::class => 'allowedRoutes',
  184.             KernelEvents::REQUEST => 'requestEvent',
  185.         ];
  186.     }
  187.     /**
  188.      * @param GenericPageLoadedEvent $event
  189.      * @return void
  190.      */
  191.     public function allowedRoutes(GenericPageLoadedEvent $event)
  192.     {
  193.         $notAllowedArray $this->getNotAllowedRoutes();
  194.         $struct = new AllowedRouteStruct();
  195.         $struct->setRoutes($notAllowedArray);
  196.         $entityData $this->getNotAllowedPages();
  197.         $notAllowedPages array_filter($entityData, function ($item) {
  198.             return $item === false;
  199.         });
  200.         $struct->setPages($notAllowedPages);
  201.         $event->getPage()->addExtension('notAllowedRoutes'$struct);
  202.         $event->getPage()->addExtension('notAllowedPages'$struct);
  203.     }
  204.     /**
  205.      * @param RequestEvent $event
  206.      * @return void
  207.      */
  208.     public function requestEvent(RequestEvent $event)
  209.     {
  210.         $route $event->getRequest()->attributes->get('_route');
  211.         if (!$this->isStoreFront($route)) {
  212.             return;
  213.         }
  214.         $notAllowedArray $this->getNotAllowedRoutes();
  215.         if (!in_array($route$notAllowedArray)) {
  216.             return;
  217.         }
  218.         if ($route == self::ROUTE_CMS_PAGE) {
  219.             $routeParamId $event->getRequest()->attributes->get('_route_params')['id'];
  220.             if (!($routeParamId == $this->systemConfigService->get('core.basicInformation.privacyPage')
  221.                 || $routeParamId == $this->systemConfigService->get('core.basicInformation.tosPage')
  222.                 || $routeParamId == $this->systemConfigService->get('core.basicInformation.imprintPage')
  223.                 || $routeParamId == $this->systemConfigService->get('core.basicInformation.shippingPaymentInfoPage')
  224.                 || $routeParamId == $this->systemConfigService->get('core.basicInformation.revocationPage'))
  225.             ) {
  226.                 return;
  227.             }
  228.         }
  229.         throw new NotFoundHttpException(self::NOT_FOUND_MESSAGE);
  230.     }
  231.     /**
  232.      * @param $route
  233.      * @return bool
  234.      */
  235.     public function isStoreFront($route)
  236.     {
  237.         if ($route) {
  238.             if (explode('.'$route)[0] == self::PREFIX_STOREFRONT || $route === self::ROUTE_QUICKVIEW_MINIMAL) {
  239.                 return true;
  240.             }
  241.         }
  242.         return false;
  243.     }
  244.     /**
  245.      * @return array
  246.      */
  247.     public function getNotAllowedRoutes()
  248.     {
  249. //        $configs = $this->systemConfigService->all()['NgsShopRestrict']['config']; TODO data from config
  250.         $notAllowedArray = [];
  251.         $configs $this->getEntityData();
  252.         $struct = new AllowedRouteStruct();
  253.         $struct->setPages($configs);
  254.         array_filter($configs, function ($item$key) use (&$notAllowedArray) {
  255.             if ($item === false) {
  256.                 $notAllowedArray array_merge($notAllowedArray$this->routeList[$key]);
  257.             }
  258.         }, ARRAY_FILTER_USE_BOTH);
  259.         return $notAllowedArray;
  260.     }
  261.     /**
  262.      * @return array
  263.      */
  264.     public function getNotAllowedPages()
  265.     {
  266.         return $this->getEntityData();
  267.     }
  268.     /**
  269.      * @return array
  270.      */
  271.     public function getEntityData()
  272.     {
  273.         $restrictedPages $this->entityRepository->search((new Criteria()), Context::createDefaultContext())->getEntities();
  274.         
  275.         $configs = [];
  276.         foreach ($restrictedPages->getElements() as $element) {
  277.             $configs[$element->title] = $element->is_allowed;
  278.         }
  279.         return $configs;
  280.     }
  281. }