vendor/sensio/framework-extra-bundle/EventListener/SecurityListener.php line 48

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony framework.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sensio\Bundle\FrameworkExtraBundle\EventListener;
  11. use Sensio\Bundle\FrameworkExtraBundle\Security\ExpressionLanguage;
  12. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  19. use Symfony\Component\Security\Core\SecurityContextInterface;
  20. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  21. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  22. /**
  23.  * SecurityListener handles security restrictions on controllers.
  24.  *
  25.  * @author Fabien Potencier <fabien@symfony.com>
  26.  */
  27. class SecurityListener implements EventSubscriberInterface
  28. {
  29.     private $tokenStorage;
  30.     private $authChecker;
  31.     private $language;
  32.     private $trustResolver;
  33.     private $roleHierarchy;
  34.     public function __construct(SecurityContextInterface $securityContext nullExpressionLanguage $language nullAuthenticationTrustResolverInterface $trustResolver nullRoleHierarchyInterface $roleHierarchy nullTokenStorageInterface $tokenStorage nullAuthorizationCheckerInterface $authChecker null)
  35.     {
  36.         $this->tokenStorage $tokenStorage ?: $securityContext;
  37.         $this->authChecker $authChecker ?: $securityContext;
  38.         $this->language $language;
  39.         $this->trustResolver $trustResolver;
  40.         $this->roleHierarchy $roleHierarchy;
  41.     }
  42.     public function onKernelController(FilterControllerEvent $event)
  43.     {
  44.         $request $event->getRequest();
  45.         if (!$configuration $request->attributes->get('_security')) {
  46.             return;
  47.         }
  48.         if (null === $this->tokenStorage || null === $this->trustResolver) {
  49.             throw new \LogicException('To use the @Security tag, you need to install the Symfony Security bundle.');
  50.         }
  51.         if (null === $this->tokenStorage->getToken()) {
  52.             throw new \LogicException('To use the @Security tag, your controller needs to be behind a firewall.');
  53.         }
  54.         if (null === $this->language) {
  55.             throw new \LogicException('To use the @Security tag, you need to use the Security component 2.4 or newer and install the ExpressionLanguage component.');
  56.         }
  57.         if (!$this->language->evaluate($configuration->getExpression(), $this->getVariables($request))) {
  58.             throw new AccessDeniedException(sprintf('Expression "%s" denied access.'$configuration->getExpression()));
  59.         }
  60.     }
  61.     // code should be sync with Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter
  62.     private function getVariables(Request $request)
  63.     {
  64.         $token $this->tokenStorage->getToken();
  65.         if (null !== $this->roleHierarchy) {
  66.             $roles $this->roleHierarchy->getReachableRoles($token->getRoles());
  67.         } else {
  68.             $roles $token->getRoles();
  69.         }
  70.         $variables = array(
  71.             'token' => $token,
  72.             'user' => $token->getUser(),
  73.             'object' => $request,
  74.             'subject' => $request,
  75.             'request' => $request,
  76.             'roles' => array_map(function ($role) { return $role->getRole(); }, $roles),
  77.             'trust_resolver' => $this->trustResolver,
  78.             // needed for the is_granted expression function
  79.             'auth_checker' => $this->authChecker,
  80.         );
  81.         // controller variables should also be accessible
  82.         return array_merge($request->attributes->all(), $variables);
  83.     }
  84.     public static function getSubscribedEvents()
  85.     {
  86.         return array(KernelEvents::CONTROLLER => 'onKernelController');
  87.     }
  88. }