Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion assets/scss/modules/base/_status.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

.badge-homepage,
.badge-404,
.badge-403,
.badge-maintenance {
font-weight: normal;
vertical-align: middle;
Expand All @@ -57,7 +58,8 @@
background: $secondary;
}

.badge-404 {
.badge-404,
.badge-403 {
background: $warning;
}

Expand Down
4 changes: 4 additions & 0 deletions config/bolt/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ homepage_template: index.twig
# identifiers for records, which will be tried until a match is found.
notfound: [ blocks/404-not-found, 'helpers/page_404.html.twig' ]

# The default content for the 403 page. Can be an (array of) template names or
# identifiers for records, which will be tried until a match is found.
forbidden: [ blocks/403-forbidden, 'helpers/page_403.html.twig' ]

# The default template and amount of records to use for listing-pages on the
# site.
#
Expand Down
39 changes: 28 additions & 11 deletions src/Controller/ErrorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,20 @@ public function showAction(Environment $twig, \Throwable $exception): Response
$code = Response::HTTP_INTERNAL_SERVER_ERROR;
}

if ($code === Response::HTTP_NOT_FOUND) {
$twig->addGlobal('exception', $exception);

if ($code === Response::HTTP_SERVICE_UNAVAILABLE || $this->isMaintenanceEnabled($code)) {
$twig->addGlobal('exception', $exception);

// If Maintenance is on, show that, instead of the 404.
if ($this->isMaintenanceEnabled()) {
return $this->showMaintenance();
}
return $this->showMaintenance();
}

if ($code === Response::HTTP_NOT_FOUND) {
return $this->showNotFound();
}

if ($code === Response::HTTP_SERVICE_UNAVAILABLE) {
$twig->addGlobal('exception', $exception);

return $this->showMaintenance();
if ($code === Response::HTTP_FORBIDDEN) {
return $this->showForbidden();
}

// If not a 404, we'll let Symfony handle it as usual.
Expand All @@ -82,9 +81,17 @@ private function showNotFound(): Response
return new Response('404: Not found (and there was no proper page configured to display)');
}

private function isMaintenanceEnabled()
private function showForbidden(): Response
{
return $this->config->get('general/maintenance_mode', false);
foreach ($this->config->get('general/forbidden') as $item) {
$output = $this->attemptToRender($item);

if ($output instanceof Response) {
return $output;
}
}

return new Response('403: Forbidden (and there was no proper page configured to display)');
}

private function showMaintenance(): Response
Expand All @@ -100,6 +107,16 @@ private function showMaintenance(): Response
return new Response('503: Maintenance mode (and there was no proper page configured to display)');
}

private function isMaintenanceEnabled(int $code): bool
{
// Only applies to NOT_FOUND and FORBIDDEN in frontend
if (! in_array($code, [Response::HTTP_NOT_FOUND, Response::HTTP_FORBIDDEN], true)) {
return false;
}

return $this->config->get('general/maintenance_mode', false);
}

private function attemptToRender(string $item): ?Response
{
// First, see if it's a contenttype/slug pair:
Expand Down
5 changes: 5 additions & 0 deletions src/DataFixtures/ContentFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ private function getPresetRecords(): array
'slug' => '404-not-found',
'status' => Statuses::HELD,
];
$records['blocks'][] = [
'title' => '403 Forbidden',
'slug' => '403-forbidden',
'status' => Statuses::HELD,
];
$records['blocks'][] = [
'title' => '503 Service Unavailable (Maintenance Mode)',
'slug' => '503-maintenance mode',
Expand Down
9 changes: 9 additions & 0 deletions src/Twig/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,10 @@ public function getSpecialFeature(Content $record): string
return '404';
}

if ($this->is403($record)) {
return '403';
}

if ($this->isMaintenance($record)) {
return 'maintenance';
}
Expand All @@ -698,6 +702,11 @@ public function is404(Content $content): bool
return $this->contentHelper->is404($content);
}

public function is403(Content $content): bool
{
return $this->contentHelper->is403($content);
}

public function isMaintenance(Content $content): bool
{
return $this->contentHelper->isMaintenance($content);
Expand Down
5 changes: 5 additions & 0 deletions src/Utils/ContentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ public function is404(Content $content): bool
return $this->isSpecialpage($content, 'notfound');
}

public function is403(Content $content): bool
{
return $this->isSpecialpage($content, 'forbidden');
}

public function isMaintenance(Content $content): bool
{
return $this->isSpecialpage($content, 'maintenance');
Expand Down