src/Base/EventSubscriber/ExecutorSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Base\EventSubscriber;
  4. use App\Base\Component\GraphQL\Parser;
  5. use App\Base\Model\GraphQLObject;
  6. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\ExecutorEvents;
  7. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\ExecutorEvent;
  8. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\ExecutorResultEvent;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ExecutorSubscriber implements EventSubscriberInterface
  12. {
  13.     protected ContainerBagInterface $params;
  14.     public function __construct(ContainerBagInterface $params)
  15.     {
  16.         $this->params $params;
  17.     }
  18.     /**
  19.      * @return string[]
  20.      */
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             ExecutorEvents::PRE_EXECUTE => 'onPreExecute',
  25.             ExecutorEvents::POST_EXECUTE => 'onPostExecute',
  26.         ];
  27.     }
  28.     public function onPreExecute(ExecutorEvent $event): void
  29.     {
  30.         $graphQLObject Parser::createFromQuery($event->getQuery());
  31.         if ($graphQLObject->getOperationType() === GraphQLObject::OPERATION_TYPE_QUERY) {
  32.             $queryName $graphQLObject->getOperationName();
  33.             if (in_array($queryName$this->params->get('app.pimcore.graphql.skipPermissionQueryList'))) {
  34.                 /** @var \Pimcore\Bundle\DataHubBundle\Configuration $configuration */
  35.                 $configuration $event->getContext()['configuration'];
  36.                 $configurationList $configuration->getConfiguration();
  37.                 $configurationList['security']['skipPermissionCheck'] = true;
  38.                 $configuration->setConfiguration($configurationList);
  39.             }
  40.         }
  41.     }
  42.     /**
  43.      * @param \Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\ExecutorResultEvent $event
  44.      */
  45.     public function onPostExecute(ExecutorResultEvent $event): void
  46.     {
  47.         $result $event->getResult();
  48.         $result->data['status'] = true;
  49.         if(!empty($result->errors)){
  50.             $error current($result->errors);
  51.             $result->data['status'] = false;
  52.             $result->data['message'] = $error->getMessage();
  53.             $result->errors = [];
  54.         }
  55.         $event->setResult($result);
  56.     }
  57. }