]> Raphaƫl G. Git Repositories - airbundle/blob - Security/AccessDeniedHandler.php
Add new findTranslatedSortedByPeriod
[airbundle] / Security / AccessDeniedHandler.php
1 <?php
2
3 namespace Rapsys\AirBundle\Security;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\Response;
7 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
8 use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\Routing\RouterInterface;
11 use Symfony\Component\Translation\TranslatorInterface;
12 use Twig\Environment;
13
14 class AccessDeniedHandler implements AccessDeniedHandlerInterface {
15 //Config array
16 protected $config;
17
18 //Context array
19 protected $context;
20
21 //Environment instance
22 protected $environment;
23
24 //Translator instance
25 protected $translator;
26
27 /**
28 * {@inheritdoc}
29 */
30 public function __construct(ContainerInterface $container, Environment $environment, RouterInterface $router, TranslatorInterface $translator, string $alias = 'rapsys_air') {
31 //Retrieve config
32 $this->config = $container->getParameter($alias);
33
34 //Set the translator
35 $this->translator = $translator;
36
37 //Set the environment
38 $this->environment = $environment;
39
40 //Set the context
41 $this->context = [
42 'copy_long' => $translator->trans($this->config['copy']['long']),
43 'copy_short' => $translator->trans($this->config['copy']['short']),
44 'site_ico' => $this->config['site']['ico'],
45 'site_logo' => $this->config['site']['logo'],
46 'site_png' => $this->config['site']['png'],
47 'site_svg' => $this->config['site']['svg'],
48 'site_title' => $translator->trans($this->config['site']['title']),
49 'site_url' => $router->generate($this->config['site']['url'])
50 ];
51 }
52
53 /**
54 * {@inheritdoc}
55 */
56 public function handle(Request $request, AccessDeniedException $exception) {
57 //Set section
58 $section = $this->translator->trans('Access denied');
59
60 //Set title
61 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
62
63 //Set message
64 //XXX: we assume that it's already translated
65 $message = $exception->getMessage();
66
67 //Render template
68 return new Response(
69 $this->environment->render(
70 '@RapsysAir/security/denied.html.twig',
71 [
72 'title' => $title,
73 'section' => $section,
74 'message' => $message
75 ]+$this->context
76 ),
77 403
78 );
79 }
80 }