vendor/dachcom-digital/members/src/MembersBundle/EventListener/TreeListener.php line 96

Open in your IDE?
  1. <?php
  2. namespace MembersBundle\EventListener;
  3. use MembersBundle\Configuration\Configuration;
  4. use MembersBundle\Manager\RestrictionManager;
  5. use MembersBundle\Restriction\Restriction;
  6. use MembersBundle\Security\RestrictionUri;
  7. use Pimcore\Event\AdminEvents;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\EventDispatcher\GenericEvent;
  10. class TreeListener implements EventSubscriberInterface
  11. {
  12.     public function __construct(
  13.         protected Configuration $configuration,
  14.         protected RestrictionManager $restrictionManager
  15.     )
  16.     {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         $defaultEvents = [
  21.             AdminEvents::OBJECT_TREE_GET_CHILDREN_BY_ID_PRE_SEND_DATA => ['handleObjectTree'0]
  22.         ];
  23.         if (defined('\Pimcore\Event\AdminEvents::ASSET_TREE_GET_CHILDREN_BY_ID_PRE_SEND_DATA')) {
  24.             $defaultEvents[AdminEvents::ASSET_TREE_GET_CHILDREN_BY_ID_PRE_SEND_DATA] = ['handleAssetTree'0];
  25.         }
  26.         if (defined('\Pimcore\Event\AdminEvents::DOCUMENT_TREE_GET_CHILDREN_BY_ID_PRE_SEND_DATA')) {
  27.             $defaultEvents[AdminEvents::DOCUMENT_TREE_GET_CHILDREN_BY_ID_PRE_SEND_DATA] = ['handleDocumentTree'0];
  28.         }
  29.         return $defaultEvents;
  30.     }
  31.     public function handleObjectTree(GenericEvent $event): void
  32.     {
  33.         $objects $event->getArgument('objects');
  34.         $restrictionConfig $this->configuration->getConfig('restriction');
  35.         $allowedTypes $restrictionConfig['allowed_objects'];
  36.         foreach ($objects as &$object) {
  37.             if (!isset($object['className'])) {
  38.                 continue;
  39.             }
  40.             if (!in_array($object['className'], $allowedTypestrue)) {
  41.                 continue;
  42.             }
  43.             $restriction $this->getRestriction($object['id'], 'object');
  44.             if (!$restriction instanceof Restriction) {
  45.                 continue;
  46.             }
  47.             $lockClass $restriction->isInherited() ? 'members-locked-inherit' 'members-locked-main';
  48.             $currentClass $object['cls'] ?? '';
  49.             $object['cls'] = implode(' ', [$currentClass'members-locked'$lockClass]);
  50.         }
  51.         $event->setArgument('objects'$objects);
  52.     }
  53.     public function handleAssetTree(GenericEvent $event): void
  54.     {
  55.         $assets $event->getArgument('assets');
  56.         foreach ($assets as &$asset) {
  57.             if (!isset($asset['basePath'])) {
  58.                 continue;
  59.             }
  60.             if ($this->restrictionManager->pathIsInProtectedStorageFolder($asset['basePath']) === false) {
  61.                 continue;
  62.             }
  63.             $restriction $this->getRestriction($asset['id'], 'asset');
  64.             if (!$restriction instanceof Restriction) {
  65.                 continue;
  66.             }
  67.             $lockClass $restriction->isInherited() ? 'members-locked-inherit' 'members-locked-main';
  68.             $currentClass $asset['cls'] ?? '';
  69.             $asset['cls'] = implode(' ', [$currentClass'members-locked'$lockClass]);
  70.         }
  71.         $event->setArgument('assets'$assets);
  72.     }
  73.     public function handleDocumentTree(GenericEvent $event): void
  74.     {
  75.         $documents $event->getArgument('documents');
  76.         foreach ($documents as &$document) {
  77.             $restriction $this->getRestriction($document['id'], 'page');
  78.             if (!$restriction instanceof Restriction) {
  79.                 continue;
  80.             }
  81.             $lockClass $restriction->isInherited() ? 'members-locked-inherit' 'members-locked-main';
  82.             $currentClass $document['cls'] ?? '';
  83.             $document['cls'] = implode(' ', [$currentClass'members-locked'$lockClass]);
  84.         }
  85.         $event->setArgument('documents'$documents);
  86.     }
  87.     private function getRestriction(int $idstring $type): ?Restriction
  88.     {
  89.         try {
  90.             $restriction Restriction::getByTargetId($id$type);
  91.         } catch (\Exception $e) {
  92.             return null;
  93.         }
  94.         return $restriction;
  95.     }
  96. }