3 namespace Rapsys\AirBundle\Security
;
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
;
16 class AccessDeniedHandler
implements AccessDeniedHandlerInterface
{
23 //Environment instance
24 protected $environment;
27 protected $translator;
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') {
35 $this->config
= $container->getParameter($alias);
38 $this->translator
= $translator;
41 $this->environment
= $environment;
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']
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']),
65 #$currentLocale = $router->getContext()->getParameters()['_locale'];
66 $currentLocale = $requestStack->getCurrentRequest()->getLocale();
68 //Set translator locale
69 //XXX: allow LocaleSubscriber on the fly locale change for first page
70 $this->translator
->setLocale($currentLocale);
72 //Iterate on locales excluding current one
73 foreach($this->config
['locales'] as $locale) {
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);
83 $path = $router->getContext()->getPathInfo();
85 //Retrieve route matching path
86 $route = $router->match($path);
89 $name = $route['_route'];
92 unset($route['_route']);
95 if ($locale == $currentLocale) {
96 //Set locale locales context
97 $this->context
['canonical'] = $router->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
);
99 //Set locale locales context
100 $this->context
['alternates'][] = [
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)
114 public function handle(Request
$request, AccessDeniedException
$exception) {
116 $section = $this->translator
->trans('Access denied');
119 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
122 //XXX: we assume that it's already translated
123 $message = $exception->getMessage();
127 $this->environment
->render(
128 '@RapsysAir/security/denied.html.twig',
131 'section' => $section,
132 'message' => $message