|
10 | 10 |
|
11 | 11 | namespace Bolt\Extension\Bolt\JsonApi;
|
12 | 12 |
|
| 13 | +use Bolt\Controller\Zone; |
13 | 14 | use Bolt\Extension\Bolt\JsonApi\Controllers\ContentController;
|
14 | 15 | use Bolt\Extension\Bolt\JsonApi\Exception\ApiException;
|
| 16 | +use Bolt\Extension\Bolt\JsonApi\Exception\FrontendDisabledException; |
15 | 17 | use Bolt\Extension\Bolt\JsonApi\Provider\APIProvider;
|
16 | 18 | use Bolt\Extension\Bolt\JsonApi\Response\ApiErrorResponse;
|
17 | 19 | use Bolt\Extension\SimpleExtension;
|
18 | 20 | use Symfony\Component\HttpFoundation\Response;
|
| 21 | +use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
19 | 22 | use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
| 23 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
20 | 24 | use Symfony\Component\HttpKernel\KernelEvents;
|
21 | 25 |
|
22 | 26 | /**
|
@@ -63,11 +67,42 @@ public static function getSubscribedEvents()
|
63 | 67 | KernelEvents::EXCEPTION => [
|
64 | 68 | ['error', 515],
|
65 | 69 | ],
|
| 70 | + KernelEvents::CONTROLLER => [ |
| 71 | + ['disableFrontend', 10] |
| 72 | + ] |
66 | 73 | ];
|
67 | 74 |
|
68 | 75 | return $parentEvents + $localEvents;
|
69 | 76 | }
|
70 | 77 |
|
| 78 | + public function disableFrontend(FilterControllerEvent $event) |
| 79 | + { |
| 80 | + $request = $event->getRequest(); |
| 81 | + |
| 82 | + $container = $this->getContainer(); |
| 83 | + |
| 84 | + $routeName = $request->get('_route'); |
| 85 | + |
| 86 | + //Check if request is NOT to frontend |
| 87 | + if (! Zone::isFrontend($request)) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + //Check if we should disable frontend based upon the configuration |
| 92 | + if (! $container['jsonapi.config']->isDisableFrontend()) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + //Only disable frontend routes, don't disable json routes |
| 97 | + if (strpos($routeName, 'jsonapi') === false) { |
| 98 | + $event->setController( |
| 99 | + function() { |
| 100 | + throw new HttpException(Response::HTTP_FORBIDDEN, "Front-end is disabled by JSON API extension."); |
| 101 | + } |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + |
71 | 106 | /**
|
72 | 107 | * Listener to handle all exceptions thrown of type ApiException. It converts
|
73 | 108 | * the exception into an ApiErrorResponse.
|
|
0 commit comments