3 namespace Rapsys\AirBundle\Handler
;
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
;
15 use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait
;
17 use Rapsys\AirBundle\RapsysAirBundle
;
19 class AccessDeniedHandler
implements AccessDeniedHandlerInterface
{
28 //Environment instance
29 protected $environment;
32 protected $translator;
37 public function __construct(ContainerInterface
$container, Environment
$environment, RouterInterface
$router, RequestStack
$stack, TranslatorInterface
$translator) {
39 $this->config
= $container->getParameter(RapsysAirBundle
::getAlias());
42 $this->container
= $container;
45 $this->translator
= $translator;
48 $this->environment
= $environment;
53 'title' => $translator->trans($this->config
['contact']['title']),
54 'mail' => $this->config
['contact']['mail']
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']
64 'description' => null,
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'])
81 'og' => 'http://ogp.me/ns#',
82 'fb' => 'http://ogp.me/ns/fb#'
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']
96 $request = $stack->getCurrentRequest();
99 $locale = $request->getLocale();
102 $this->context
['locale'] = str_replace('_', '-', $locale);
105 $pathInfo = $router->getContext()->getPathInfo();
107 //Iterate on locales excluding current one
108 foreach($this->config
['locales'] as $current) {
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);
117 //Retrieve route matching path
118 $route = $router->match($pathInfo);
121 $name = $route['_route'];
124 unset($route['_route']);
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
);
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)
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)
156 public function handle(Request
$request, AccessDeniedException
$exception) {
158 $this->context
['page']['title'] = $this->translator
->trans('Access denied');
161 //XXX: we assume that it's already translated
162 $this->context
['message'] = $exception->getMessage();
165 if ($this->isGranted('ROLE_ADMIN')) {
166 //Add trace for admin
167 $this->context
['trace'] = $exception->getTraceAsString();
172 $this->environment
->render(
173 '@RapsysAir/security/denied.html.twig',