]> Raphaƫl G. Git Repositories - airbundle/blobdiff - Security/AccessDeniedHandler.php
Add logout success handler that redirect on referer when possible
[airbundle] / Security / AccessDeniedHandler.php
index 5494fb0f017d8ae567a3cf5935860a9622be3152..d637ee5f63907f84ccaa20b5924ecf6b7e08fb36 100644 (file)
@@ -7,6 +7,7 @@ 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\RouterInterface;
 use Symfony\Component\Translation\TranslatorInterface;
 use Twig\Environment;
 
@@ -14,32 +15,45 @@ class AccessDeniedHandler implements AccessDeniedHandlerInterface {
        //Config array
        protected $config;
 
-       //Translator instance
-       protected $translator;
+       //Context array
+       protected $context;
 
        //Environment instance
        protected $environment;
 
+       //Translator instance
+       protected $translator;
+
        /**
         * {@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') {
                //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 context
+               $this->context = [
+                       'copy_long' => $translator->trans($this->config['copy']['long']),
+                       'copy_short' => $translator->trans($this->config['copy']['short']),
+                       'site_ico' => $this->config['site']['ico'],
+                       'site_logo' => $this->config['site']['logo'],
+                       'site_png' => $this->config['site']['png'],
+                       'site_svg' => $this->config['site']['svg'],
+                       'site_title' => $translator->trans($this->config['site']['title']),
+                       'site_url' => $router->generate($this->config['site']['url'])
+               ];
        }
 
        /**
         * {@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');
 
@@ -47,22 +61,20 @@ class AccessDeniedHandler implements AccessDeniedHandlerInterface {
                $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',
-                               ['title' => $title, 'section' => $section, 'message' => $message]
+                               [
+                                       'title' => $title,
+                                       'section' => $section,
+                                       'message' => $message
+                               ]+$this->context
                        ),
                        403
                );
        }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function getAlias() {
-               return 'rapsys_air';
-       }
 }