<?phpdeclare(strict_types=1);namespace App\Base\EventSubscriber;use App\Base\Component\Permission;use Pimcore\Bundle\DataHubBundle\Event\GraphQL\ListingEvents;use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\ListingEvent;use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\PermissionEvent;use Pimcore\Bundle\DataHubBundle\Event\GraphQL\PermissionEvents;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;use Symfony\Component\Security\Core\User\UserInterface;class PermissionSubscriber implements EventSubscriberInterface{ /** @var TokenStorageInterface */ private TokenStorageInterface $tokenStorage; /** * @param TokenStorageInterface $tokenStorage */ public function __construct( TokenStorageInterface $tokenStorage ) { $this->tokenStorage = $tokenStorage; } /** * @return string[] */ public static function getSubscribedEvents() { return [ PermissionEvents::PRE_CHECK => 'onPermissionCheck', ListingEvents::PRE_LOAD => 'onListingPreLoad', ]; } /** * @param PermissionEvent $event */ public function onPermissionCheck(PermissionEvent $event) { $token = $this->tokenStorage->getToken(); $user = $token->getUser(); if ($user instanceof UserInterface) { if ($event->getType() === 'update') { $operation = 'save'; } else if ($event->getType() === 'read') { $operation = 'view'; } else { $operation = $event->getType(); } $event->setIsGranted($event->getElement()->isAllowed($operation)); } else { $event->setIsGranted(false); } } /** * @param ListingEvent $event * @return void */ public function onListingPreLoad(ListingEvent $event) { $listing = $event->getListing(); $token = $this->tokenStorage->getToken(); $user = $token->getUser(); if ($user instanceof UserInterface) { if (!$user->getUser()->isAdmin()) { $userIds = [intval($user->getId())]; if (is_array($user->getUser()->getRoles()) && count($user->getUser()->getRoles())) { $userIds = array_merge($userIds, array_map('intval', $user->getUser()->getRoles())); } $listing->setCondition( $listing->getCondition() . " AND ((oo_id IN ( SELECT uwo.cid FROM users_workspaces_object uwo WHERE uwo.userId in (" . implode(',', $userIds) . ") and uwo.list=1 ) ) OR (oo_id IN ( SELECT o.o_id FROM objects o inner join objects op on op.o_id=o.o_parentId inner join users_workspaces_object uwo on uwo.cid=op.o_parentId WHERE uwo.userId in (" . implode(',', $userIds) . ") and uwo.list=1 ) ) ) " ); } } else { $listing->setCondition(' 1=2 '); } $event->setListing($listing); }}