vendor/dachcom-digital/members/src/MembersBundle/EventListener/AssetFrontendPathListener.php line 77

Open in your IDE?
  1. <?php
  2. namespace MembersBundle\EventListener;
  3. use MembersBundle\Configuration\Configuration;
  4. use MembersBundle\Security\RestrictionUri;
  5. use Pimcore\Event\FrontendEvents;
  6. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  7. use Pimcore\Model\Asset;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\EventDispatcher\GenericEvent;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  13. class AssetFrontendPathListener implements EventSubscriberInterface
  14. {
  15.     public function __construct(
  16.         protected Configuration $configuration,
  17.         protected RequestStack $requestStack,
  18.         protected PimcoreContextResolver $contextResolver,
  19.         protected RestrictionUri $restrictionUri
  20.     ) {
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             FrontendEvents::ASSET_PATH                     => 'checkAssetPath',
  26.             FrontendEvents::ASSET_VIDEO_THUMBNAIL          => 'checkVideoThumbnailPath',
  27.             FrontendEvents::ASSET_VIDEO_IMAGE_THUMBNAIL    => 'checkVideoImageThumbnailPath',
  28.             FrontendEvents::ASSET_DOCUMENT_IMAGE_THUMBNAIL => 'checkDocumentImageThumbnailPath',
  29.             FrontendEvents::ASSET_IMAGE_THUMBNAIL          => 'checkImageThumbnailPath',
  30.         ];
  31.     }
  32.     public function checkAssetPath(GenericEvent $event): void
  33.     {
  34.         if ($this->contextMatches() === false) {
  35.             return;
  36.         }
  37.         if (!$event->getSubject() instanceof Asset) {
  38.             return;
  39.         }
  40.         $this->checkAsset($event$event->getSubject());
  41.     }
  42.     public function checkVideoThumbnailPath(GenericEvent $event): void
  43.     {
  44.         if ($this->contextMatches() === false) {
  45.             return;
  46.         }
  47.         if (!$event->getSubject() instanceof Asset) {
  48.             return;
  49.         }
  50.         $this->checkAsset($event$event->getSubject());
  51.     }
  52.     public function checkVideoImageThumbnailPath(GenericEvent $event): void
  53.     {
  54.         if ($this->contextMatches() === false) {
  55.             return;
  56.         }
  57.         $thumbnail $event->getSubject();
  58.         if (!$thumbnail instanceof Asset\Video\ImageThumbnail) {
  59.             return;
  60.         }
  61.         $this->checkAsset($event$thumbnail->getAsset());
  62.     }
  63.     public function checkDocumentImageThumbnailPath(GenericEvent $event): void
  64.     {
  65.         if ($this->contextMatches() === false) {
  66.             return;
  67.         }
  68.         $thumbnail $event->getSubject();
  69.         if (!$thumbnail instanceof Asset\Document\ImageThumbnail) {
  70.             return;
  71.         }
  72.         $this->checkAsset($event$thumbnail->getAsset());
  73.     }
  74.     public function checkImageThumbnailPath(GenericEvent $event): void
  75.     {
  76.         if ($this->contextMatches() === false) {
  77.             return;
  78.         }
  79.         $thumbnail $event->getSubject();
  80.         if (!$thumbnail instanceof Asset\Image\Thumbnail) {
  81.             return;
  82.         }
  83.         $this->checkAsset($event$thumbnail->getAsset());
  84.     }
  85.     private function checkAsset(GenericEvent $eventAsset $asset): void
  86.     {
  87.         if (!$event->hasArgument('frontendPath')) {
  88.             return;
  89.         }
  90.         $publicAssetPath $this->restrictionUri->generatePublicAssetUrl($asset$event->getArgument('frontendPath'));
  91.         if ($publicAssetPath === null) {
  92.             return;
  93.         }
  94.         $event->setArgument('frontendPath'$publicAssetPath);
  95.     }
  96.     private function contextMatches(): bool
  97.     {
  98.         $restrictionConfig $this->configuration->getConfig('restriction');
  99.         if ($restrictionConfig['enabled'] === false) {
  100.             return false;
  101.         }
  102.         if ($restrictionConfig['enable_public_asset_path_protection'] === false) {
  103.             return false;
  104.         }
  105.         if (!$this->requestStack->getMainRequest() instanceof Request) {
  106.             return false;
  107.         }
  108.         if (!$this->contextResolver->matchesPimcoreContext($this->requestStack->getMainRequest(), PimcoreContextResolver::CONTEXT_DEFAULT)) {
  109.             return false;
  110.         }
  111.         return true;
  112.     }
  113. }