vendor/dachcom-digital/members/src/MembersBundle/Manager/RestrictionManager.php line 138

Open in your IDE?
  1. <?php
  2. namespace MembersBundle\Manager;
  3. use MembersBundle\Adapter\Group\GroupInterface;
  4. use MembersBundle\Adapter\User\UserInterface;
  5. use MembersBundle\Configuration\Configuration;
  6. use MembersBundle\Restriction\ElementRestriction;
  7. use MembersBundle\Restriction\Restriction;
  8. use Pimcore\Model\Asset;
  9. use Pimcore\Model\Document;
  10. use Pimcore\Model\DataObject;
  11. use Pimcore\Model\Element\ElementInterface;
  12. use Pimcore\Tool;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. class RestrictionManager implements RestrictionManagerInterface
  15. {
  16.     public const PROTECTED_ASSET_FOLDER 'restricted-assets';
  17.     public const RESTRICTION_STATE_LOGGED_IN 'members.restriction.logged_in';
  18.     public const RESTRICTION_STATE_NOT_LOGGED_IN 'members.restriction.not_logged_in';
  19.     public const RESTRICTION_SECTION_ALLOWED 'members.restriction.allowed';
  20.     public const RESTRICTION_SECTION_NOT_ALLOWED 'members.restriction.not_allowed';
  21.     public const RESTRICTION_SECTION_REFUSED 'members.restriction.refused';
  22.     public const REQUEST_RESTRICTION_STORAGE 'members.restriction.store';
  23.     protected Configuration $configuration;
  24.     protected TokenStorageInterface $tokenStorage;
  25.     public function __construct(Configuration $configurationTokenStorageInterface $tokenStorage)
  26.     {
  27.         $this->configuration $configuration;
  28.         $this->tokenStorage $tokenStorage;
  29.     }
  30.     public function getElementRestrictedGroups(ElementInterface $element): array
  31.     {
  32.         $restriction false;
  33.         $groups[] = 'default';
  34.         if ($element instanceof Document) {
  35.             $restriction $this->getRestrictionElement($element'page');
  36.         } elseif ($element instanceof DataObject\Concrete) {
  37.             $restriction $this->getRestrictionElement($element'object');
  38.         } elseif ($element instanceof Asset) {
  39.             $restriction $this->getRestrictionElement($element'asset');
  40.         }
  41.         if (!$restriction instanceof Restriction) {
  42.             return $groups;
  43.         }
  44.         $groups = [];
  45.         if (is_array($restriction->getRelatedGroups())) {
  46.             $groups $restriction->getRelatedGroups();
  47.         }
  48.         return $groups;
  49.     }
  50.     public function getElementRestrictionStatus(ElementInterface $element): ElementRestriction
  51.     {
  52.         $user $this->getUser();
  53.         $elementRestriction = new ElementRestriction();
  54.         $restriction null;
  55.         if ($element instanceof Document) {
  56.             $restriction $this->getRestrictionElement($element'page');
  57.         } elseif ($element instanceof DataObject\Concrete) {
  58.             $restriction $this->getRestrictionElement($element'object');
  59.         } elseif ($element instanceof Asset) {
  60.             $restriction $this->getRestrictionElement($element'asset');
  61.         }
  62.         if ($user instanceof UserInterface) {
  63.             $elementRestriction->setState(self::RESTRICTION_STATE_LOGGED_IN);
  64.         }
  65.         if ($restriction === null) {
  66.             if ($element instanceof Asset) {
  67.                 //protect asset if element is in restricted area with no added restriction group.
  68.                 $elementRestriction->setSection($this->isFrontendRequestByAdmin() || !$this->elementIsInProtectedStorageFolder($element)
  69.                     ? self::RESTRICTION_SECTION_ALLOWED
  70.                     self::RESTRICTION_SECTION_NOT_ALLOWED
  71.                 );
  72.             } else {
  73.                 $elementRestriction->setSection(self::RESTRICTION_SECTION_ALLOWED);
  74.             }
  75.             return $elementRestriction;
  76.         }
  77.         if (is_array($restriction->getRelatedGroups())) {
  78.             $elementRestriction->setRestrictionGroups($restriction->getRelatedGroups());
  79.         }
  80.         //check if user is not logged in.
  81.         if (!$user instanceof UserInterface) {
  82.             return $elementRestriction;
  83.         }
  84.         return $elementRestriction->setSection($this->filterAllowedSectionToUser($user->getGroups(), $restriction->getRelatedGroups()));
  85.     }
  86.     private function filterAllowedSectionToUser(array $userGroups, array $elementGroups): string
  87.     {
  88.         $sectionStatus self::RESTRICTION_SECTION_NOT_ALLOWED;
  89.         if (!empty($elementGroups)) {
  90.             $allowedGroups = [];
  91.             /** @var GroupInterface $group */
  92.             foreach ($userGroups as $group) {
  93.                 $allowedGroups[] = $group->getId();
  94.             }
  95.             $intersectResult array_intersect($elementGroups$allowedGroups);
  96.             if (count($intersectResult) > 0) {
  97.                 $sectionStatus self::RESTRICTION_SECTION_ALLOWED;
  98.             }
  99.         }
  100.         return $sectionStatus;
  101.     }
  102.     private function getRestrictionElement(ElementInterface $elementstring $cType 'page'): ?Restriction
  103.     {
  104.         $restriction null;
  105.         if ($this->isFrontendRequestByAdmin()) {
  106.             return null;
  107.         }
  108.         try {
  109.             if ($cType === 'page') {
  110.                 $restriction Restriction::getByTargetId($element->getId(), $cType);
  111.             } elseif ($cType === 'asset') {
  112.                 $restriction Restriction::getByTargetId($element->getId(), $cType);
  113.             } else {
  114.                 $restrictionConfig $this->configuration->getConfig('restriction');
  115.                 $allowedTypes $restrictionConfig['allowed_objects'];
  116.                 if ($element instanceof DataObject\Concrete && in_array($element->getClass()?->getName(), $allowedTypestrue)) {
  117.                     $restriction Restriction::getByTargetId($element->getId(), $cType);
  118.                 }
  119.             }
  120.         } catch (\Exception $e) {
  121.             // fail silently
  122.         }
  123.         return $restriction;
  124.     }
  125.     public function elementIsInProtectedStorageFolder(ElementInterface $element): bool
  126.     {
  127.         if (!$element instanceof Asset) {
  128.             return false;
  129.         }
  130.         return $this->pathIsInProtectedStorageFolder($element->getPath());
  131.     }
  132.     public function pathIsInProtectedStorageFolder(string $path): bool
  133.     {
  134.         return str_contains($pathself::PROTECTED_ASSET_FOLDER);
  135.     }
  136.     public function isFrontendRequestByAdmin(): bool
  137.     {
  138.         return Tool::isFrontendRequestByAdmin();
  139.     }
  140.     public function getUser(): ?UserInterface
  141.     {
  142.         $token $this->tokenStorage->getToken();
  143.         if (is_null($token)) {
  144.             return null;
  145.         }
  146.         $user $token->getUser();
  147.         return $user instanceof UserInterface $user null;
  148.     }
  149. }