]> Raphaƫl G. Git Repositories - airbundle/blob - Security/AccessDeniedHandler.php
Add missing response use
[airbundle] / Security / AccessDeniedHandler.php
1 <?php
2
3 namespace Rapsys\AirBundle\Security;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\RequestStack;
7 use Symfony\Component\HttpFoundation\Response;
8 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
9 use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12 use Symfony\Component\Routing\RouterInterface;
13 use Symfony\Component\Translation\TranslatorInterface;
14 use Twig\Environment;
15
16 class AccessDeniedHandler implements AccessDeniedHandlerInterface {
17 //Config array
18 protected $config;
19
20 //Context array
21 protected $context;
22
23 //Environment instance
24 protected $environment;
25
26 //Translator instance
27 protected $translator;
28
29 /**
30 * {@inheritdoc}
31 */
32 #public function __construct(ContainerInterface $container, Environment $environment, RouterInterface $router, TranslatorInterface $translator, string $alias = 'rapsys_air') {
33 public function __construct(ContainerInterface $container, Environment $environment, RouterInterface $router, RequestStack $requestStack, TranslatorInterface $translator, string $alias = 'rapsys_air') {
34 //Retrieve config
35 $this->config = $container->getParameter($alias);
36
37 //Set the translator
38 $this->translator = $translator;
39
40 //Set the environment
41 $this->environment = $environment;
42
43 //Set the context
44 $this->context = [
45 'copy' => [
46 'by' => $translator->trans($this->config['copy']['by']),
47 'link' => $this->config['copy']['link'],
48 'long' => $translator->trans($this->config['copy']['long']),
49 'short' => $translator->trans($this->config['copy']['short']),
50 'title' => $this->config['copy']['title']
51 ],
52 'site' => [
53 'ico' => $this->config['site']['ico'],
54 'logo' => $this->config['site']['logo'],
55 'png' => $this->config['site']['png'],
56 'svg' => $this->config['site']['svg'],
57 'title' => $translator->trans($this->config['site']['title']),
58 'url' => $router->generate($this->config['site']['url']),
59 ],
60 'canonical' => null,
61 'alternates' => []
62 ];
63
64 //Get current locale
65 #$currentLocale = $router->getContext()->getParameters()['_locale'];
66 $currentLocale = $requestStack->getCurrentRequest()->getLocale();
67
68 //Set translator locale
69 //XXX: allow LocaleSubscriber on the fly locale change for first page
70 $this->translator->setLocale($currentLocale);
71
72 //Iterate on locales excluding current one
73 foreach($this->config['locales'] as $locale) {
74 //Set titles
75 $titles = [];
76
77 //Iterate on other locales
78 foreach(array_diff($this->config['locales'], [$locale]) as $other) {
79 $titles[$other] = $translator->trans($this->config['languages'][$locale], [], null, $other);
80 }
81
82 //Get context path
83 $path = $router->getContext()->getPathInfo();
84
85 //Retrieve route matching path
86 $route = $router->match($path);
87
88 //Get route name
89 $name = $route['_route'];
90
91 //Unset route name
92 unset($route['_route']);
93
94 //With current locale
95 if ($locale == $currentLocale) {
96 //Set locale locales context
97 $this->context['canonical'] = $router->generate($name, ['_locale' => $locale]+$route, UrlGeneratorInterface::ABSOLUTE_URL);
98 } else {
99 //Set locale locales context
100 $this->context['alternates'][] = [
101 'lang' => $locale,
102 'absolute' => $router->generate($name, ['_locale' => $locale]+$route, UrlGeneratorInterface::ABSOLUTE_URL),
103 'relative' => $router->generate($name, ['_locale' => $locale]+$route),
104 'title' => implode('/', $titles),
105 'translated' => $translator->trans($this->config['languages'][$locale], [], null, $locale)
106 ];
107 }
108 }
109 }
110
111 /**
112 * {@inheritdoc}
113 */
114 public function handle(Request $request, AccessDeniedException $exception) {
115 //Set section
116 $section = $this->translator->trans('Access denied');
117
118 //Set title
119 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
120
121 //Set message
122 //XXX: we assume that it's already translated
123 $message = $exception->getMessage();
124
125 //Render template
126 return new Response(
127 $this->environment->render(
128 '@RapsysAir/security/denied.html.twig',
129 [
130 'title' => $title,
131 'section' => $section,
132 'message' => $message
133 ]+$this->context
134 ),
135 403
136 );
137 }
138 }