Skip to content

Commit 70f86d3

Browse files
authored
Merge pull request #6367 from christianbeeznest/ofaj-22701
Internal: Improve custom error handling for NotAllowedException with styled feedback - refs BT#22701
2 parents f90fc8b + 5acaeec commit 70f86d3

File tree

5 files changed

+79
-16
lines changed

5 files changed

+79
-16
lines changed

public/main/inc/global.inc.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Chamilo\CoreBundle\Controller\ExceptionController;
66
use Chamilo\CoreBundle\EventListener\ExceptionListener;
7+
use Chamilo\CoreBundle\Exception\NotAllowedException;
78
use Chamilo\CoreBundle\Framework\Container;
89
use Symfony\Component\Dotenv\Dotenv;
910
use Symfony\Component\ErrorHandler\Debug;
@@ -92,18 +93,35 @@
9293
$router->setContext($context);
9394

9495
set_exception_handler(function ($exception) use ($kernel, $container, $request) {
95-
if (\in_array($kernel->getEnvironment(), ['dev', 'test'], true)) {
96+
if (
97+
in_array($kernel->getEnvironment(), ['dev', 'test'], true) &&
98+
!($exception instanceof NotAllowedException)
99+
) {
96100
throw $exception;
97101
}
98102

99-
$event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception);
103+
$event = new ExceptionEvent(
104+
$kernel,
105+
$request,
106+
HttpKernelInterface::MAIN_REQUEST,
107+
$exception
108+
);
109+
110+
/** @var callable $listener */
100111
$listener = $container->get(ExceptionListener::class);
101112
if (is_callable($listener)) {
102113
$listener($event);
103114
}
104115
$response = $event->getResponse();
105116
if (!$response) {
106-
$response = new Response('An error occurred', Response::HTTP_INTERNAL_SERVER_ERROR);
117+
$statusCode = $exception instanceof NotAllowedException
118+
? Response::HTTP_OK
119+
: Response::HTTP_INTERNAL_SERVER_ERROR;
120+
121+
$response = new Response(
122+
$exception->getMessage(),
123+
$statusCode
124+
);
107125
}
108126
$response->send();
109127
});

public/main/inc/lib/api.lib.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,19 +3553,21 @@ function api_is_anonymous()
35533553

35543554
/**
35553555
* Displays message "You are not allowed here..." and exits the entire script.
3556-
*
3557-
* @param bool $print_headers Whether to print headers (default = false -> does not print them)
3558-
* @param string $message
3559-
* @param int $responseCode
3560-
*
3561-
* @throws Exception
35623556
*/
35633557
function api_not_allowed(
3564-
$print_headers = false,
3565-
$message = null,
3566-
$responseCode = 0
3558+
bool $printHeaders = false,
3559+
string $message = null,
3560+
int $responseCode = 0,
3561+
string $severity = 'warning'
35673562
): never {
3568-
throw new NotAllowedException($message ?: 'You are not allowed', null, $responseCode);
3563+
throw new NotAllowedException(
3564+
$message ?: get_lang('You are not allowed'),
3565+
$severity,
3566+
403,
3567+
[],
3568+
$responseCode,
3569+
null
3570+
);
35693571
}
35703572

35713573
/**

src/CoreBundle/EventListener/ExceptionListener.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ public function __invoke(ExceptionEvent $event): void
4545

4646
return;
4747
}
48+
49+
$severity = $exception->getSeverity();
50+
$message = $exception->getMessage();
51+
if (in_array($severity, ['info', 'warning', 'success'], true)) {
52+
$html = $this->twig->render('@ChamiloCore/Exception/not_allowed_message.html.twig', [
53+
'message' => $message,
54+
'severity' => $severity,
55+
]);
56+
$event->setResponse(new Response($html, 200));
57+
return;
58+
}
4859
}
4960

5061
if (isset($_SERVER['APP_ENV']) && \in_array($_SERVER['APP_ENV'], ['dev', 'test'], true)) {

src/CoreBundle/Exception/NotAllowedException.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,27 @@
66

77
namespace Chamilo\CoreBundle\Exception;
88

9-
use Symfony\Component\HttpFoundation\Response;
109
use Symfony\Component\HttpKernel\Exception\HttpException;
1110
use Throwable;
1211

1312
class NotAllowedException extends HttpException
1413
{
15-
public function __construct(?string $message = 'Not allowed', ?Throwable $previous = null, int $code = 0, array $headers = [])
14+
private string $severity;
15+
16+
public function __construct(
17+
string $message = 'Not allowed',
18+
string $severity = 'warning',
19+
int $statusCode = 403,
20+
array $headers = [],
21+
int $code = 0,
22+
Throwable $previous = null
23+
) {
24+
$this->severity = $severity;
25+
parent::__construct($statusCode, $message, $previous, $headers, $code);
26+
}
27+
28+
public function getSeverity(): string
1629
{
17-
parent::__construct(Response::HTTP_FORBIDDEN, $message, $previous, $headers, $code);
30+
return $this->severity;
1831
}
1932
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{% extends '@ChamiloCore/Layout/layout_one_col.html.twig' %}
2+
3+
{% block content %}
4+
{% set style = {
5+
'info': { 'bg': 'bg-info', 'txt': 'text-white', 'icon': 'mdi mdi-information-outline' },
6+
'success': { 'bg': 'bg-success', 'txt': 'text-white', 'icon': 'mdi mdi-check-circle-outline' },
7+
'warning': { 'bg': 'bg-warning', 'txt': 'text-white', 'icon': 'mdi mdi-alert-outline' },
8+
'danger': { 'bg': 'bg-danger', 'txt': 'text-white', 'icon': 'mdi mdi-alert-octagon-outline' }
9+
}[severity|default('info')] %}
10+
11+
<div class="container max-w-2xl mx-auto mt-10">
12+
<div class="flex items-center gap-4 rounded-2xl p-6 {{ style.bg }} {{ style.txt }}/80 shadow text-bold">
13+
<i class="{{ style.icon }} text-4xl {{ style.txt }}"></i>
14+
<p class="font-extrabold text-xl {{ style.txt }}">
15+
{{ message|trans|striptags|raw }}
16+
</p>
17+
</div>
18+
</div>
19+
{% endblock %}

0 commit comments

Comments
 (0)