]> Raphaël G. Git Repositories - airbundle/blob - Handler/AccessDeniedHandler.php
Update index name
[airbundle] / Handler / AccessDeniedHandler.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys AirBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle\Handler;
13
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17 use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
18
19 use Rapsys\AirBundle\RapsysAirBundle;
20 use Rapsys\AirBundle\Controller\AbstractController;
21
22 /**
23 * {@inheritdoc}
24 */
25 class AccessDeniedHandler extends AbstractController implements AccessDeniedHandlerInterface {
26 /**
27 * {@inheritdoc}
28 */
29 public function handle(Request $request, AccessDeniedException $exception): Response {
30 //Set title
31 $this->context['title'] = $this->translator->trans('Access denied');
32
33 //Set message
34 //XXX: we assume that it's already translated
35 $this->context['message'] = $exception->getMessage();
36
37 //With admin
38 if ($this->checker->isGranted('ROLE_ADMIN')) {
39 //Add trace for admin
40 $this->context['trace'] = $exception->getTraceAsString();
41 }
42
43 //Render template
44 $response = $this->render('@RapsysAir/security/denied.html.twig', $this->context);
45 $response->setStatusCode(403);
46 $response->setEtag(md5($response->getContent()));
47 $response->setPublic();
48 $response->isNotModified($request);
49
50 //Return response
51 return $response;
52 }
53 }