vendor/pimcore/portal-engine/src/EventSubscriber/SecuritySubscriber.php line 139

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under following license:
  6.  * - Pimcore Commercial License (PCL)
  7.  *
  8.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  9.  *  @license    http://www.pimcore.org/license     PCL
  10.  */
  11. namespace Pimcore\Bundle\PortalEngineBundle\EventSubscriber;
  12. use Pimcore\Bundle\PortalEngineBundle\Enum\Permission;
  13. use Pimcore\Bundle\PortalEngineBundle\Service\PortalConfig\PortalConfigService;
  14. use Pimcore\Bundle\PortalEngineBundle\Service\PublicShare\PublicShareService;
  15. use Pimcore\Bundle\PortalEngineBundle\Service\Security\SecurityService;
  16. use Pimcore\Controller\FrontendController;
  17. use Pimcore\Event\AssetEvents;
  18. use Pimcore\Event\DataObjectEvents;
  19. use Pimcore\Event\Model\AssetEvent;
  20. use Pimcore\Event\Model\DataObjectEvent;
  21. use Pimcore\Tool;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Component\HttpFoundation\RequestStack;
  24. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  25. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  26. use Symfony\Component\Security\Core\Security;
  27. /**
  28.  * Class IndexUpdateListener
  29.  *
  30.  * @package Pimcore\Bundle\PortalEngineBundle\EventListener
  31.  */
  32. class SecuritySubscriber implements EventSubscriberInterface
  33. {
  34.     /**
  35.      * @var PortalConfigService
  36.      */
  37.     protected $portalConfigService;
  38.     /**
  39.      * @var Security
  40.      */
  41.     protected $security;
  42.     /**
  43.      * @var SecurityService
  44.      */
  45.     protected $securityService;
  46.     /**
  47.      * @var RequestStack
  48.      */
  49.     protected $requestStack;
  50.     /**
  51.      * @var PublicShareService
  52.      */
  53.     protected $publicShareService;
  54.     protected $publicRoutes = [
  55.         'pimcore_portalengine_auth_login',
  56.         'pimcore_portalengine_auth_oidc',
  57.         'pimcore_portalengine_auth_recover_password',
  58.         'pimcore_portalengine_public_share_public_list',
  59.         'pimcore_portalengine_rest_api_translation_load_catalogue',
  60.         'pimcore_directedit_downloadfile',
  61.         'pimcore_directedit_renotifybrowser',
  62.         'pimcore_directedit_uploadfile',
  63.         'pimcore_directedit_client_askactivity',
  64.     ];
  65.     protected $customPublicRoutes = [];
  66.     protected $publicShareHashRoutes = [
  67.         'pimcore_portalengine_public_share_public_asset_detail',
  68.         'pimcore_portalengine_public_share_public_object_detail',
  69.         'pimcore_portalengine_rest_api_public_share_asset_list',
  70.         'pimcore_portalengine_rest_api_public_share_asset_list_filters',
  71.         'pimcore_portalengine_rest_api_public_share_asset_detail',
  72.         'pimcore_portalengine_rest_api_public_share_asset_detail_results_list',
  73.         'pimcore_portalengine_rest_api_public_share_data_object_list',
  74.         'pimcore_portalengine_rest_api_public_share_data_object_list_filters',
  75.         'pimcore_portalengine_rest_api_public_share_data_object_detail',
  76.         'pimcore_portalengine_rest_api_public_share_data_object_detail_results_list',
  77.         'pimcore_portalengine_rest_api_public_share_download_download_types',
  78.         'pimcore_portalengine_rest_api_batch_task_list',
  79.         'pimcore_portalengine_rest_api_batch_task_delete',
  80.         'pimcore_portalengine_rest_api_batch_task_process_notification_action',
  81.         'pimcore_portalengine_rest_api_asset_download',
  82.         'pimcore_portalengine_rest_api_download_trigger_download',
  83.         'pimcore_portalengine_rest_api_download_get_estimation_result',
  84.         'pimcore_portalengine_rest_api_download_multi_download_trigger_download_estimation',
  85.         'pimcore_portalengine_rest_api_download_single_download',
  86.         'pimcore_portalengine_rest_api_download_do_single_download',
  87.         'pimcore_portalengine_rest_api_public_share_trigger_download_estimation',
  88.         'pimcore_portalengine_rest_api_public_share_detail_actions',
  89.         'pimcore_portalengine_rest_api_translation_valid_languages',
  90.         'pimcore_portalengine_rest_api_asset_metadata_layout',
  91.     ];
  92.     /**
  93.      * @param PortalConfigService $portalConfigService
  94.      * @param Security $security
  95.      * @param SecurityService $securityService
  96.      * @param RequestStack $requestStack
  97.      * @param PublicShareService $publicShareService
  98.      * @param array $customPublicRoutes
  99.      */
  100.     public function __construct(PortalConfigService $portalConfigServiceSecurity $securitySecurityService $securityServiceRequestStack $requestStackPublicShareService $publicShareService, array $customPublicRoutes)
  101.     {
  102.         $this->portalConfigService $portalConfigService;
  103.         $this->security $security;
  104.         $this->securityService $securityService;
  105.         $this->requestStack $requestStack;
  106.         $this->publicShareService $publicShareService;
  107.         $this->customPublicRoutes $customPublicRoutes;
  108.     }
  109.     /**
  110.      * @return array
  111.      */
  112.     public static function getSubscribedEvents()
  113.     {
  114.         return [
  115.             ControllerEvent::class => ['onKernelController'19],
  116.             DataObjectEvents::PRE_UPDATE => 'onPreUpdate',
  117.             AssetEvents::PRE_UPDATE => 'onPreUpdate',
  118.         ];
  119.     }
  120.     /**
  121.      * @param ControllerEvent $controllerEvent
  122.      */
  123.     public function onKernelController(ControllerEvent $controllerEvent)
  124.     {
  125.         if (!$controllerEvent->isMasterRequest()) {
  126.             return;
  127.         }
  128.         if (!$this->portalConfigService->isPortalEngineSite()) {
  129.             return;
  130.         }
  131.         if (!Tool::isFrontend()) {
  132.             return;
  133.         }
  134.         if (!$controllerEvent->getController()[0] instanceof FrontendController) {
  135.             return;
  136.         }
  137.         $route $controllerEvent->getRequest()->attributes->get('_route');
  138.         $isPublicRoute in_array(
  139.             $route,
  140.             array_merge($this->publicRoutes$this->customPublicRoutes)
  141.         );
  142.         $request $controllerEvent->getRequest();
  143.         if (in_array($route$this->publicShareHashRoutes) && $request->get('publicShareHash')) {
  144.             $publicShare $this->publicShareService->validateByHash($request->get('publicShareHash'));
  145.             $this->publicShareService->setUpPublicShare($publicShare);
  146.         }
  147.         if (!$isPublicRoute && !$this->security->isGranted(Permission::PORTAL_ACCESS)) {
  148.             throw new AuthenticationException('invalid login');
  149.         }
  150.     }
  151.     /**
  152.      * @param DataObjectEvent|AssetEvent $event
  153.      */
  154.     public function onPreUpdate($event)
  155.     {
  156.         if ($this->requestStack->getMasterRequest() && !$this->portalConfigService->isPortalEngineSite()) {
  157.             return;
  158.         }
  159.         if (!$portalUser $this->securityService->getPortalUser()) {
  160.             return;
  161.         }
  162.         $event->getElement()->setUserModification($this->securityService->getPimcoreUserId());
  163.     }
  164. }