vendor/pimcore/data-hub/src/Controller/WebserviceController.php line 41

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace Pimcore\Bundle\DataHubBundle\Controller;
  15. use GraphQL\Error\Debug;
  16. use GraphQL\Error\Warning;
  17. use GraphQL\GraphQL;
  18. use Pimcore\Bundle\DataHubBundle\Configuration;
  19. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\ExecutorEvents;
  20. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\ExecutorEvent;
  21. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\ExecutorResultEvent;
  22. use Pimcore\Bundle\DataHubBundle\GraphQL\ClassTypeDefinitions;
  23. use Pimcore\Bundle\DataHubBundle\GraphQL\Mutation\MutationType;
  24. use Pimcore\Bundle\DataHubBundle\GraphQL\Query\QueryType;
  25. use Pimcore\Bundle\DataHubBundle\GraphQL\Service;
  26. use Pimcore\Bundle\DataHubBundle\PimcoreDataHubBundle;
  27. use Pimcore\Cache\Runtime;
  28. use Pimcore\Controller\FrontendController;
  29. use Pimcore\Localization\LocaleServiceInterface;
  30. use Pimcore\Logger;
  31. use Pimcore\Model\Factory;
  32. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  33. use Symfony\Component\HttpFoundation\JsonResponse;
  34. use Symfony\Component\HttpFoundation\Request;
  35. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  36. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  37. class WebserviceController extends FrontendController
  38. {
  39.     /**
  40.      * @var EventDispatcherInterface
  41.      */
  42.     private $eventDispatcher;
  43.     /**
  44.      * @param EventDispatcherInterface $eventDispatcher
  45.      */
  46.     public function __construct(EventDispatcherInterface $eventDispatcher)
  47.     {
  48.         $this->eventDispatcher $eventDispatcher;
  49.     }
  50.     /**
  51.      * @param Service $service
  52.      * @param LocaleServiceInterface $localeService
  53.      * @param Factory $modelFactory
  54.      * @param Request $request
  55.      *
  56.      * @return JsonResponse
  57.      *
  58.      * @throws \Exception
  59.      */
  60.     public function webonyxAction(Service $serviceLocaleServiceInterface $localeServiceFactory $modelFactoryRequest $request)
  61.     {
  62.         $clientname $request->get('clientname');
  63.         $configuration Configuration::getByName($clientname);
  64.         if (!$configuration || !$configuration->isActive()) {
  65.             throw new NotFoundHttpException('No active configuration found for ' $clientname);
  66.         }
  67.         $this->performSecurityCheck($request$configuration);
  68.         // context info, will be passed on to all resolver function
  69.         $context = ['clientname' => $clientname'configuration' => $configuration];
  70.         $config $this->container->getParameter('pimcore_data_hub');
  71.         if (isset($config['graphql']) && isset($config['graphql']['not_allowed_policy'])) {
  72.             PimcoreDataHubBundle::setNotAllowedPolicy($config['graphql']['not_allowed_policy']);
  73.         }
  74.         Runtime::set('datahub_context'$context);
  75.         ClassTypeDefinitions::build($service$context);
  76.         $queryType = new QueryType($service$localeService$modelFactory$this->eventDispatcher, [], $context);
  77.         $mutationType = new MutationType($service$localeService$modelFactory$this->eventDispatcher, [], $context);
  78.         try {
  79.             $schemaConfig = [
  80.                 'query' => $queryType
  81.             ];
  82.             if (!$mutationType->isEmpty()) {
  83.                 $schemaConfig['mutation'] = $mutationType;
  84.             }
  85.             $schema = new \GraphQL\Type\Schema(
  86.                 $schemaConfig
  87.             );
  88.         } catch (\Exception $e) {
  89.             Warning::enable(false);
  90.             $schema = new \GraphQL\Type\Schema(
  91.                 [
  92.                     'query' => $queryType,
  93.                     'mutation' => $mutationType
  94.                 ]
  95.             );
  96.             $schema->assertValid();
  97.             Logger::error($e);
  98.             throw $e;
  99.         }
  100.         $input json_decode($request->getContent(), true);
  101.         $query $input['query'];
  102.         $variableValues = isset($input['variables']) ? $input['variables'] : null;
  103.         try {
  104.             $rootValue = [];
  105.             $validators null;
  106.             if ($request->get('novalidate')) {
  107.                 // disable all validators except the listed ones
  108.                 $validators = [
  109. //                    new NoUndefinedVariables()
  110.                 ];
  111.             }
  112.             $event = new ExecutorEvent(
  113.                 $request,
  114.                 $query,
  115.                 $schema,
  116.                 $context);
  117.             $this->eventDispatcher->dispatch(ExecutorEvents::PRE_EXECUTE$event);
  118.             $result GraphQL::executeQuery(
  119.                 $event->getSchema(),
  120.                 $event->getQuery(),
  121.                 $rootValue,
  122.                 $event->getContext(),
  123.                 $variableValues,
  124.                 null,
  125.                 null,
  126.                 $validators
  127.             );
  128.             $exResult = new ExecutorResultEvent($request$result);
  129.             $this->eventDispatcher->dispatch(ExecutorEvents::POST_EXECUTE,
  130.                 $exResult);
  131.             $result $exResult->getResult();
  132.             if (PIMCORE_DEBUG) {
  133.                 $debug Debug::INCLUDE_DEBUG_MESSAGE Debug::INCLUDE_TRACE Debug::RETHROW_INTERNAL_EXCEPTIONS;
  134.                 $output $result->toArray($debug);
  135.             } else {
  136.                 $output $result->toArray(false);
  137.             }
  138.         } catch (\Exception $e) {
  139.             $output = [
  140.                 'errors' => [
  141.                     [
  142.                         'message' => $e->getMessage(),
  143.                     ],
  144.                 ],
  145.             ];
  146.         }
  147.         $origin '*';
  148.         if (!empty($_SERVER['HTTP_ORIGIN'])) {
  149.             $origin $_SERVER['HTTP_ORIGIN'];
  150.         }
  151.         $response = new JsonResponse($output);
  152.         $response->headers->set('Access-Control-Allow-Origin'$origin);
  153.         $response->headers->set('Access-Control-Allow-Credentials''true');
  154.         $response->headers->set('Access-Control-Allow-Methods''GET, POST, OPTIONS');
  155.         $response->headers->set('Access-Control-Allow-Headers''authorization, Origin, Content-Type, X-Auth-Token');
  156.         return $response;
  157.     }
  158.     /**
  159.      * @param Request $request
  160.      * @param Configuration $configuration
  161.      *
  162.      * @return void
  163.      *
  164.      * @throws AccessDeniedHttpException
  165.      */
  166.     protected function performSecurityCheck(Request $requestConfiguration $configuration): void
  167.     {
  168.         $securityConfig $configuration->getSecurityConfig();
  169.         if ($securityConfig['method'] === 'datahub_apikey') {
  170.             $apiKey $request->get('apikey');
  171.             if ($apiKey === $securityConfig['apikey']) {
  172.                 return;
  173.             }
  174.         }
  175.         throw new AccessDeniedHttpException('Permission denied, apikey not valid');
  176.     }
  177. }