]> Raphaƫl G. Git Repositories - airbundle/blob - Handler/AccessDeniedHandler.php
Display all user for admin and only organizers for others
[airbundle] / Handler / AccessDeniedHandler.php
1 <?php
2
3 namespace Rapsys\AirBundle\Handler;
4
5 use Symfony\Component\DependencyInjection\ContainerInterface;
6 use Symfony\Component\HttpFoundation\Request;
7 use Symfony\Component\HttpFoundation\RequestStack;
8 use Symfony\Component\HttpFoundation\Response;
9 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
10 use Symfony\Component\Routing\RouterInterface;
11 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
12 use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
13 use Symfony\Component\Translation\TranslatorInterface;
14 use Twig\Environment;
15 use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
16
17 use Rapsys\AirBundle\RapsysAirBundle;
18
19 class AccessDeniedHandler implements AccessDeniedHandlerInterface {
20 use ControllerTrait;
21
22 //Config array
23 protected $config;
24
25 //Context array
26 protected $context;
27
28 //Environment instance
29 protected $environment;
30
31 //Translator instance
32 protected $translator;
33
34 /**
35 * {@inheritdoc}
36 */
37 public function __construct(ContainerInterface $container, Environment $environment, RouterInterface $router, RequestStack $stack, TranslatorInterface $translator) {
38 //Retrieve config
39 $this->config = $container->getParameter(RapsysAirBundle::getAlias());
40
41 //Set the container
42 $this->container = $container;
43
44 //Set the translator
45 $this->translator = $translator;
46
47 //Set the environment
48 $this->environment = $environment;
49
50 //Set the context
51 $this->context = [
52 'contact' => [
53 'title' => $translator->trans($this->config['contact']['title']),
54 'mail' => $this->config['contact']['mail']
55 ],
56 'copy' => [
57 'by' => $translator->trans($this->config['copy']['by']),
58 'link' => $this->config['copy']['link'],
59 'long' => $translator->trans($this->config['copy']['long']),
60 'short' => $translator->trans($this->config['copy']['short']),
61 'title' => $this->config['copy']['title']
62 ],
63 'page' => [
64 'description' => null,
65 'section' => null,
66 'title' => null
67 ],
68 'site' => [
69 'donate' => $this->config['site']['donate'],
70 'ico' => $this->config['site']['ico'],
71 'logo' => $this->config['site']['logo'],
72 'png' => $this->config['site']['png'],
73 'svg' => $this->config['site']['svg'],
74 'title' => $translator->trans($this->config['site']['title']),
75 'url' => $router->generate($this->config['site']['url'])
76 ],
77 'canonical' => null,
78 'alternates' => [],
79 'facebook' => [
80 'heads' => [
81 'og' => 'http://ogp.me/ns#',
82 'fb' => 'http://ogp.me/ns/fb#'
83 ],
84 'metas' => [
85 'og:type' => 'article',
86 'og:site_name' => $this->translator->trans($this->config['site']['title']),
87 #'fb:admins' => $this->config['facebook']['admins'],
88 'fb:app_id' => $this->config['facebook']['apps']
89 ],
90 'texts' => []
91 ],
92 'forms' => []
93 ];
94
95 //Get current request
96 $request = $stack->getCurrentRequest();
97
98 //Get current locale
99 $locale = $request->getLocale();
100
101 //Set locale
102 $this->context['locale'] = str_replace('_', '-', $locale);
103
104 //Get context path
105 $pathInfo = $router->getContext()->getPathInfo();
106
107 //Iterate on locales excluding current one
108 foreach($this->config['locales'] as $current) {
109 //Set titles
110 $titles = [];
111
112 //Iterate on other locales
113 foreach(array_diff($this->config['locales'], [$current]) as $other) {
114 $titles[$other] = $this->translator->trans($this->config['languages'][$current], [], null, $other);
115 }
116
117 //Retrieve route matching path
118 $route = $router->match($pathInfo);
119
120 //Get route name
121 $name = $route['_route'];
122
123 //Unset route name
124 unset($route['_route']);
125
126 //With current locale
127 if ($current == $locale) {
128 //Set locale locales context
129 $this->context['canonical'] = $router->generate($name, ['_locale' => $current]+$route, UrlGeneratorInterface::ABSOLUTE_URL);
130 } else {
131 //Set locale locales context
132 $this->context['alternates'][str_replace('_', '-', $current)] = [
133 'absolute' => $router->generate($name, ['_locale' => $current]+$route, UrlGeneratorInterface::ABSOLUTE_URL),
134 'relative' => $router->generate($name, ['_locale' => $current]+$route),
135 'title' => implode('/', $titles),
136 'translated' => $this->translator->trans($this->config['languages'][$current], [], null, $current)
137 ];
138 }
139
140 //Add shorter locale
141 if (empty($this->context['alternates'][$shortCurrent = substr($current, 0, 2)])) {
142 //Set locale locales context
143 $this->context['alternates'][$shortCurrent] = [
144 'absolute' => $router->generate($name, ['_locale' => $current]+$route, UrlGeneratorInterface::ABSOLUTE_URL),
145 'relative' => $router->generate($name, ['_locale' => $current]+$route),
146 'title' => implode('/', $titles),
147 'translated' => $this->translator->trans($this->config['languages'][$current], [], null, $current)
148 ];
149 }
150 }
151 }
152
153 /**
154 * {@inheritdoc}
155 */
156 public function handle(Request $request, AccessDeniedException $exception) {
157 //Set title
158 $this->context['page']['title'] = $this->translator->trans('Access denied');
159
160 //Set message
161 //XXX: we assume that it's already translated
162 $this->context['message'] = $exception->getMessage();
163
164 //With admin
165 if ($this->isGranted('ROLE_ADMIN')) {
166 //Add trace for admin
167 $this->context['trace'] = $exception->getTraceAsString();
168 }
169
170 //Render template
171 return new Response(
172 $this->environment->render(
173 '@RapsysAir/security/denied.html.twig',
174 $this->context
175 ),
176 403
177 );
178 }
179 }