]> Raphaƫl G. Git Repositories - airbundle/blobdiff - Security/AccessDeniedHandler.php
Reorder rapsys_user routes
[airbundle] / Security / AccessDeniedHandler.php
index 5494fb0f017d8ae567a3cf5935860a9622be3152..e4ca03614bef2e7989b426d09a4d4b53d614f97a 100644 (file)
@@ -3,10 +3,13 @@
 namespace Rapsys\AirBundle\Security;
 
 use Symfony\Component\HttpFoundation\Request;
 namespace Rapsys\AirBundle\Security;
 
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\RequestStack;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
 use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
 use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
+use Symfony\Component\Routing\RouterInterface;
 use Symfony\Component\Translation\TranslatorInterface;
 use Twig\Environment;
 
 use Symfony\Component\Translation\TranslatorInterface;
 use Twig\Environment;
 
@@ -14,32 +17,101 @@ class AccessDeniedHandler implements AccessDeniedHandlerInterface {
        //Config array
        protected $config;
 
        //Config array
        protected $config;
 
-       //Translator instance
-       protected $translator;
+       //Context array
+       protected $context;
 
        //Environment instance
        protected $environment;
 
 
        //Environment instance
        protected $environment;
 
+       //Translator instance
+       protected $translator;
+
        /**
         * {@inheritdoc}
         */
        /**
         * {@inheritdoc}
         */
-       public function __construct(ContainerInterface $container, TranslatorInterface $translator, Environment $environment) {
+       #public function __construct(ContainerInterface $container, Environment $environment, RouterInterface $router, TranslatorInterface $translator, string $alias = 'rapsys_air') {
+       public function __construct(ContainerInterface $container, Environment $environment, RouterInterface $router, RequestStack $requestStack, TranslatorInterface $translator, string $alias = 'rapsys_air') {
                //Retrieve config
                //Retrieve config
-               $this->config = $container->getParameter($this->getAlias());
+               $this->config = $container->getParameter($alias);
 
                //Set the translator
                $this->translator = $translator;
 
                //Set the environment
                $this->environment = $environment;
 
                //Set the translator
                $this->translator = $translator;
 
                //Set the environment
                $this->environment = $environment;
+
+               //Set the context
+               $this->context = [
+                       'copy' => [
+                               'by' => $translator->trans($this->config['copy']['by']),
+                               'link' => $this->config['copy']['link'],
+                               'long' => $translator->trans($this->config['copy']['long']),
+                               'short' => $translator->trans($this->config['copy']['short']),
+                               'title' => $this->config['copy']['title']
+                       ],
+                       'site' => [
+                               'ico' => $this->config['site']['ico'],
+                               'logo' => $this->config['site']['logo'],
+                               'png' => $this->config['site']['png'],
+                               'svg' => $this->config['site']['svg'],
+                               'title' => $translator->trans($this->config['site']['title']),
+                               'url' => $router->generate($this->config['site']['url']),
+                       ],
+                       'canonical' => null,
+                       'alternates' => []
+               ];
+
+               //Get current locale
+               #$currentLocale = $router->getContext()->getParameters()['_locale'];
+               $currentLocale = $requestStack->getCurrentRequest()->getLocale();
+
+               //Set translator locale
+               //XXX: allow LocaleSubscriber on the fly locale change for first page
+               $this->translator->setLocale($currentLocale);
+
+               //Iterate on locales excluding current one
+               foreach($this->config['locales'] as $locale) {
+                       //Set titles
+                       $titles = [];
+
+                       //Iterate on other locales
+                       foreach(array_diff($this->config['locales'], [$locale]) as $other) {
+                               $titles[$other] = $translator->trans($this->config['languages'][$locale], [], null, $other);
+                       }
+
+                       //Get context path
+                       $path = $router->getContext()->getPathInfo();
+
+                       //Retrieve route matching path
+                       $route = $router->match($path);
+
+                       //Get route name
+                       $name = $route['_route'];
+
+                       //Unset route name
+                       unset($route['_route']);
+
+                       //With current locale
+                       if ($locale == $currentLocale) {
+                               //Set locale locales context
+                               $this->context['canonical'] = $router->generate($name, ['_locale' => $locale]+$route, UrlGeneratorInterface::ABSOLUTE_URL);
+                       } else {
+                               //Set locale locales context
+                               $this->context['alternates'][] = [
+                                       'lang' => $locale,
+                                       'absolute' => $router->generate($name, ['_locale' => $locale]+$route, UrlGeneratorInterface::ABSOLUTE_URL),
+                                       'relative' => $router->generate($name, ['_locale' => $locale]+$route),
+                                       'title' => implode('/', $titles),
+                                       'translated' => $translator->trans($this->config['languages'][$locale], [], null, $locale)
+                               ];
+                       }
+               }
        }
 
        /**
         * {@inheritdoc}
         */
        }
 
        /**
         * {@inheritdoc}
         */
-#use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
-       #public function login(Request $request, AuthenticationUtils $authenticationUtils) {
-       public function handle(Request $request, AccessDeniedException $accessDeniedException) {
+       public function handle(Request $request, AccessDeniedException $exception) {
                //Set section
                $section = $this->translator->trans('Access denied');
 
                //Set section
                $section = $this->translator->trans('Access denied');
 
@@ -47,22 +119,20 @@ class AccessDeniedHandler implements AccessDeniedHandlerInterface {
                $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
 
                //Set message
                $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
 
                //Set message
-               $message = $this->translator->trans($accessDeniedException->getMessage());
+               //XXX: we assume that it's already translated
+               $message = $exception->getMessage();
 
                //Render template
                return new Response(
                        $this->environment->render(
                                '@RapsysAir/security/denied.html.twig',
 
                //Render template
                return new Response(
                        $this->environment->render(
                                '@RapsysAir/security/denied.html.twig',
-                               ['title' => $title, 'section' => $section, 'message' => $message]
+                               [
+                                       'title' => $title,
+                                       'section' => $section,
+                                       'message' => $message
+                               ]+$this->context
                        ),
                        403
                );
        }
                        ),
                        403
                );
        }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function getAlias() {
-               return 'rapsys_air';
-       }
 }
 }