]> Raphaƫl G. Git Repositories - airbundle/blob - Security/AccessDeniedHandler.php
Add access denied security handler
[airbundle] / Security / AccessDeniedHandler.php
1 <?php
2
3 namespace Rapsys\AirBundle\Security;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\Response;
7 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
8 use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\Translation\TranslatorInterface;
11 use Twig\Environment;
12
13 class AccessDeniedHandler implements AccessDeniedHandlerInterface {
14 //Config array
15 protected $config;
16
17 //Translator instance
18 protected $translator;
19
20 //Environment instance
21 protected $environment;
22
23 /**
24 * {@inheritdoc}
25 */
26 public function __construct(ContainerInterface $container, TranslatorInterface $translator, Environment $environment) {
27 //Retrieve config
28 $this->config = $container->getParameter($this->getAlias());
29
30 //Set the translator
31 $this->translator = $translator;
32
33 //Set the environment
34 $this->environment = $environment;
35 }
36
37 /**
38 * {@inheritdoc}
39 */
40 #use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
41 #public function login(Request $request, AuthenticationUtils $authenticationUtils) {
42 public function handle(Request $request, AccessDeniedException $accessDeniedException) {
43 //Set section
44 $section = $this->translator->trans('Access denied');
45
46 //Set title
47 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
48
49 //Set message
50 $message = $this->translator->trans($accessDeniedException->getMessage());
51
52 //Render template
53 return new Response(
54 $this->environment->render(
55 '@RapsysAir/security/denied.html.twig',
56 ['title' => $title, 'section' => $section, 'message' => $message]
57 ),
58 403
59 );
60 }
61
62 /**
63 * {@inheritdoc}
64 */
65 public function getAlias() {
66 return 'rapsys_air';
67 }
68 }