src/Base/EventSubscriber/PermissionSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Base\EventSubscriber;
  4. use App\Base\Component\Permission;
  5. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\ListingEvents;
  6. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\ListingEvent;
  7. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\PermissionEvent;
  8. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\PermissionEvents;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. class PermissionSubscriber implements EventSubscriberInterface
  13. {
  14.     /** @var TokenStorageInterface */
  15.     private TokenStorageInterface $tokenStorage;
  16.     /**
  17.      * @param TokenStorageInterface $tokenStorage
  18.      */
  19.     public function __construct(
  20.         TokenStorageInterface $tokenStorage
  21.     ) {
  22.         $this->tokenStorage $tokenStorage;
  23.     }
  24.     /**
  25.      * @return string[]
  26.      */
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             PermissionEvents::PRE_CHECK => 'onPermissionCheck',
  31.             ListingEvents::PRE_LOAD => 'onListingPreLoad',
  32.         ];
  33.     }
  34.     /**
  35.      * @param PermissionEvent $event
  36.      */
  37.     public function onPermissionCheck(PermissionEvent $event)
  38.     {
  39.         $token $this->tokenStorage->getToken();
  40.         $user $token->getUser();
  41.         if ($user instanceof UserInterface) {
  42.             if ($event->getType() === 'update') {
  43.                 $operation 'save';
  44.             } else if ($event->getType() === 'read') {
  45.                 $operation 'view';
  46.             } else {
  47.                 $operation $event->getType();
  48.             }
  49.             $event->setIsGranted($event->getElement()->isAllowed($operation));
  50.         } else {
  51.             $event->setIsGranted(false);
  52.         }
  53.     }
  54.     /**
  55.      * @param ListingEvent $event
  56.      * @return void
  57.      */
  58.     public function onListingPreLoad(ListingEvent $event)
  59.     {
  60.         $listing $event->getListing();
  61.         $token $this->tokenStorage->getToken();
  62.         $user $token->getUser();
  63.         if ($user instanceof UserInterface) {
  64.             if (!$user->getUser()->isAdmin()) {
  65.                 $userIds = [intval($user->getId())];
  66.                 if (is_array($user->getUser()->getRoles()) && count($user->getUser()->getRoles())) {
  67.                     $userIds array_merge($userIdsarray_map('intval'$user->getUser()->getRoles()));
  68.                 }
  69.                 $listing->setCondition(
  70.                     $listing->getCondition() .
  71.                     " AND ((oo_id IN (
  72.                                SELECT uwo.cid
  73.                                FROM users_workspaces_object uwo
  74.                                 WHERE uwo.userId in (" implode(','$userIds) . ") and uwo.list=1
  75.                                 )
  76.                                 )
  77.                                OR (oo_id IN (
  78.                                SELECT o.o_id
  79.                                FROM objects o
  80.                                inner join objects op on op.o_id=o.o_parentId
  81.                                inner join users_workspaces_object uwo on uwo.cid=op.o_parentId
  82.                                 WHERE uwo.userId in (" implode(','$userIds) . ") and uwo.list=1
  83.                                 )
  84.                                 )
  85.                                 ) "
  86.                 );
  87.             }
  88.         } else {
  89.             $listing->setCondition(' 1=2 ');
  90.         }
  91.         $event->setListing($listing);
  92.     }
  93. }