Skip to content

Commit d99e355

Browse files
authored
Merge pull request #78 from Raistlfiren/feature-disable-frontend
Feature - Disable Frontend...
2 parents 571d9b4 + 02efebf commit d99e355

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

config/config.yml.dist

+3
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@ date-iso-8601: true
5050
# - 0 for no options (recommended for production)
5151
# - 128 for pretty print (recommended for testing/development)
5252
jsonoptions: 0
53+
54+
# Set this to true to disable frontend completely and display an empty page
55+
disablefrontend: false

src/Config/Config.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,17 @@ class Config
6262
*/
6363
private $jsonOptions;
6464

65+
/**
66+
* @var boolean
67+
*/
68+
private $disableFrontend;
69+
6570
public function __construct($config, Application $app)
6671
{
6772
if (isset($config['base'])) {
6873
$this->base = $config['base'];
6974
}
70-
75+
7176
$this->setBasePath($app['paths']['hosturl'] . $this->base);
7277
$this->setContentTypes($config['contenttypes']);
7378
$this->setReplacements($config['replacements']);
@@ -77,6 +82,9 @@ public function __construct($config, Application $app)
7782
$this->setDateIso($config['date-iso-8601']);
7883
$this->setHeaders($config['headers']);
7984
$this->setJsonOptions($config['jsonoptions']);
85+
86+
$disablefrontend = isset($config['disablefrontend']) ? $config['disablefrontend'] : false;
87+
$this->setDisableFrontend($disablefrontend);
8088
}
8189

8290
/**
@@ -322,4 +330,24 @@ public function getSort($contentType)
322330

323331
return '';
324332
}
333+
334+
/**
335+
* @return bool
336+
*/
337+
public function isDisableFrontend()
338+
{
339+
return $this->disableFrontend;
340+
}
341+
342+
/**
343+
* @param bool $disableFrontend
344+
*
345+
* @return Config
346+
*/
347+
public function setDisableFrontend($disableFrontend)
348+
{
349+
$this->disableFrontend = $disableFrontend;
350+
351+
return $this;
352+
}
325353
}

src/JSONAPIExtension.php

+35
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010

1111
namespace Bolt\Extension\Bolt\JsonApi;
1212

13+
use Bolt\Controller\Zone;
1314
use Bolt\Extension\Bolt\JsonApi\Controllers\ContentController;
1415
use Bolt\Extension\Bolt\JsonApi\Exception\ApiException;
16+
use Bolt\Extension\Bolt\JsonApi\Exception\FrontendDisabledException;
1517
use Bolt\Extension\Bolt\JsonApi\Provider\APIProvider;
1618
use Bolt\Extension\Bolt\JsonApi\Response\ApiErrorResponse;
1719
use Bolt\Extension\SimpleExtension;
1820
use Symfony\Component\HttpFoundation\Response;
21+
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
1922
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
23+
use Symfony\Component\HttpKernel\Exception\HttpException;
2024
use Symfony\Component\HttpKernel\KernelEvents;
2125

2226
/**
@@ -63,11 +67,42 @@ public static function getSubscribedEvents()
6367
KernelEvents::EXCEPTION => [
6468
['error', 515],
6569
],
70+
KernelEvents::CONTROLLER => [
71+
['disableFrontend', 10]
72+
]
6673
];
6774

6875
return $parentEvents + $localEvents;
6976
}
7077

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+
71106
/**
72107
* Listener to handle all exceptions thrown of type ApiException. It converts
73108
* the exception into an ApiErrorResponse.

0 commit comments

Comments
 (0)