From 78bcc5d7b7ad6c25895d1bb121da7a3fd9179547 Mon Sep 17 00:00:00 2001 From: Aaron Valandra Date: Fri, 30 Mar 2018 15:36:16 -0500 Subject: [PATCH 1/4] Added the ability to disable the frontend through a configuration value and just show a blank page. --- config/config.yml.dist | 3 +++ src/Config/Config.php | 28 +++++++++++++++++++++++++++- src/JSONAPIExtension.php | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/config/config.yml.dist b/config/config.yml.dist index d642350..2d4d684 100644 --- a/config/config.yml.dist +++ b/config/config.yml.dist @@ -50,3 +50,6 @@ date-iso-8601: true # - 0 for no options (recommended for production) # - 128 for pretty print (recommended for testing/development) jsonoptions: 0 + +# Set this to true to disable frontend completely and display an empty page +disablefrontend: false \ No newline at end of file diff --git a/src/Config/Config.php b/src/Config/Config.php index e9b204d..0381614 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -62,12 +62,17 @@ class Config */ private $jsonOptions; + /** + * @var boolean + */ + private $disableFrontend; + public function __construct($config, Application $app) { if (isset($config['base'])) { $this->base = $config['base']; } - + $this->setBasePath($app['paths']['hosturl'] . $this->base); $this->setContentTypes($config['contenttypes']); $this->setReplacements($config['replacements']); @@ -77,6 +82,7 @@ public function __construct($config, Application $app) $this->setDateIso($config['date-iso-8601']); $this->setHeaders($config['headers']); $this->setJsonOptions($config['jsonoptions']); + $this->setDisableFrontend($config['disablefrontend']); } /** @@ -322,4 +328,24 @@ public function getSort($contentType) return ''; } + + /** + * @return bool + */ + public function isDisableFrontend() + { + return $this->disableFrontend; + } + + /** + * @param bool $disableFrontend + * + * @return Config + */ + public function setDisableFrontend($disableFrontend) + { + $this->disableFrontend = $disableFrontend; + + return $this; + } } diff --git a/src/JSONAPIExtension.php b/src/JSONAPIExtension.php index 9ec2868..1c583da 100644 --- a/src/JSONAPIExtension.php +++ b/src/JSONAPIExtension.php @@ -10,12 +10,14 @@ namespace Bolt\Extension\Bolt\JsonApi; +use Bolt\Controller\Zone; use Bolt\Extension\Bolt\JsonApi\Controllers\ContentController; use Bolt\Extension\Bolt\JsonApi\Exception\ApiException; use Bolt\Extension\Bolt\JsonApi\Provider\APIProvider; use Bolt\Extension\Bolt\JsonApi\Response\ApiErrorResponse; use Bolt\Extension\SimpleExtension; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Event\FilterControllerEvent; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\KernelEvents; @@ -63,11 +65,41 @@ public static function getSubscribedEvents() KernelEvents::EXCEPTION => [ ['error', 515], ], + KernelEvents::CONTROLLER => [ + ['disableFrontend', -1025] + ] ]; return $parentEvents + $localEvents; } + public function disableFrontend(FilterControllerEvent $event) + { + $request = $event->getRequest(); + //$response = $event->getResponse(); + + $container = $this->getContainer(); + + //Get route name + $routeName = $request->get('_route'); + + //Check if request is to frontend + if (Zone::isFrontend($request)) { + //Check if we should disable frontend based upon the configuration + if ($container['jsonapi.config']->isDisableFrontend()) { + //Only disable frontend routes, don't disable json routes + if (strpos($routeName, 'jsonapi') === false) { + $event->stopPropagation(); + $event->setController( + function() { + return new Response('', 200); + } + ); + } + } + } + } + /** * Listener to handle all exceptions thrown of type ApiException. It converts * the exception into an ApiErrorResponse. From bdd0fe40e08bf9cee0490f439f638e959c151c80 Mon Sep 17 00:00:00 2001 From: Aaron Valandra Date: Wed, 4 Apr 2018 13:11:06 -0500 Subject: [PATCH 2/4] Changed the height of the priority to something much lower than what it was beforehand. --- src/JSONAPIExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JSONAPIExtension.php b/src/JSONAPIExtension.php index 1c583da..4bd1685 100644 --- a/src/JSONAPIExtension.php +++ b/src/JSONAPIExtension.php @@ -66,7 +66,7 @@ public static function getSubscribedEvents() ['error', 515], ], KernelEvents::CONTROLLER => [ - ['disableFrontend', -1025] + ['disableFrontend', 10] ] ]; From 905f18dff4c71440d0b168e03e23e312b78ee2e7 Mon Sep 17 00:00:00 2001 From: Aaron Valandra Date: Wed, 4 Apr 2018 13:58:37 -0500 Subject: [PATCH 3/4] Removed nesting and returned HttpException instead of displaying nothing. --- src/JSONAPIExtension.php | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/JSONAPIExtension.php b/src/JSONAPIExtension.php index 4bd1685..3ddbe17 100644 --- a/src/JSONAPIExtension.php +++ b/src/JSONAPIExtension.php @@ -13,12 +13,14 @@ use Bolt\Controller\Zone; use Bolt\Extension\Bolt\JsonApi\Controllers\ContentController; use Bolt\Extension\Bolt\JsonApi\Exception\ApiException; +use Bolt\Extension\Bolt\JsonApi\Exception\FrontendDisabledException; use Bolt\Extension\Bolt\JsonApi\Provider\APIProvider; use Bolt\Extension\Bolt\JsonApi\Response\ApiErrorResponse; use Bolt\Extension\SimpleExtension; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\FilterControllerEvent; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; +use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\KernelEvents; /** @@ -76,27 +78,28 @@ public static function getSubscribedEvents() public function disableFrontend(FilterControllerEvent $event) { $request = $event->getRequest(); - //$response = $event->getResponse(); $container = $this->getContainer(); - //Get route name $routeName = $request->get('_route'); - //Check if request is to frontend - if (Zone::isFrontend($request)) { - //Check if we should disable frontend based upon the configuration - if ($container['jsonapi.config']->isDisableFrontend()) { - //Only disable frontend routes, don't disable json routes - if (strpos($routeName, 'jsonapi') === false) { - $event->stopPropagation(); - $event->setController( - function() { - return new Response('', 200); - } - ); + //Check if request is NOT to frontend + if (! Zone::isFrontend($request)) { + return; + } + + //Check if we should disable frontend based upon the configuration + if (! $container['jsonapi.config']->isDisableFrontend()) { + return; + } + + //Only disable frontend routes, don't disable json routes + if (strpos($routeName, 'jsonapi') === false) { + $event->setController( + function() { + throw new HttpException(Response::HTTP_FORBIDDEN, "Front-end is disabled by JSON API extension."); } - } + ); } } From 02efebff14e70f9884c8d2f3a6212e4e41858922 Mon Sep 17 00:00:00 2001 From: Aaron Valandra Date: Fri, 20 Apr 2018 09:18:12 -0500 Subject: [PATCH 4/4] Added check to see if configuration value isset, otherwise default to false. --- src/Config/Config.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Config/Config.php b/src/Config/Config.php index 0381614..c727ad8 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -82,7 +82,9 @@ public function __construct($config, Application $app) $this->setDateIso($config['date-iso-8601']); $this->setHeaders($config['headers']); $this->setJsonOptions($config['jsonoptions']); - $this->setDisableFrontend($config['disablefrontend']); + + $disablefrontend = isset($config['disablefrontend']) ? $config['disablefrontend'] : false; + $this->setDisableFrontend($disablefrontend); } /**