-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathExplorerEventSubscriber.php
More file actions
50 lines (39 loc) · 1.54 KB
/
ExplorerEventSubscriber.php
File metadata and controls
50 lines (39 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Drupal\graphql\EventSubscriber;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ExplorerEventSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = ['bypassValidation'];
return $events;
}
/**
* Add the bypass_validation url parameter to graphql admin routes.
*
* @param RequestEvent $event
*/
public function bypassValidation(RequestEvent $event) {
$route = \Drupal::routeMatch()->getRouteName();
// If a bypass_validation param is already set, skip.
if ($event->getRequest()->get('bypass_validation')) {
return;
}
// Only bypass validation for these two routes.
if ($route === 'graphql.explorer' || $route === 'graphql.voyager') {
/** @var \Drupal\graphql\Entity\Server $server */
$server = $event->getRequest()->get('graphql_server');
$url = $event->getRequest()->getUri();
// Get the bypass_validation_token from the server settings.
$bypass_validation_token = $server->get('bypass_validation_token');
// Add the bypass_validation parameter to the current url.
$url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?') . 'bypass_validation=' . $bypass_validation_token;
// Redirect to the new url.
$event->setResponse(new RedirectResponse($url));
}
}
}