vendor/sensio/framework-extra-bundle/EventListener/IsGrantedListener.php line 39

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Sensio\Bundle\FrameworkExtraBundle\EventListener;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  12. use Sensio\Bundle\FrameworkExtraBundle\Request\ArgumentNameConverter;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
  15. use Symfony\Component\HttpKernel\Exception\HttpException;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  18. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  19. /**
  20.  * Handles the IsGranted annotation on controllers.
  21.  *
  22.  * @author Ryan Weaver <ryan@knpuniversity.com>
  23.  */
  24. class IsGrantedListener implements EventSubscriberInterface
  25. {
  26.     private $argumentNameConverter;
  27.     private $authChecker;
  28.     public function __construct(ArgumentNameConverter $argumentNameConverterAuthorizationCheckerInterface $authChecker null)
  29.     {
  30.         $this->argumentNameConverter $argumentNameConverter;
  31.         $this->authChecker $authChecker;
  32.     }
  33.     public function onKernelControllerArguments(FilterControllerArgumentsEvent $event)
  34.     {
  35.         $request $event->getRequest();
  36.         /** @var $configurations IsGranted[] */
  37.         if (!$configurations $request->attributes->get('_is_granted')) {
  38.             return;
  39.         }
  40.         if (null === $this->authChecker) {
  41.             throw new \LogicException('To use the @IsGranted tag, you need to install symfony/security-bundle and configure your security system.');
  42.         }
  43.         $arguments $this->argumentNameConverter->getControllerArguments($event);
  44.         foreach ($configurations as $configuration) {
  45.             $subject null;
  46.             if ($configuration->getSubject()) {
  47.                 if (!isset($arguments[$configuration->getSubject()])) {
  48.                     throw new \RuntimeException(sprintf('Could not find the subject "%s" for the @IsGranted annotation. Try adding a "$%s" argument to your controller method.'$configuration->getSubject(), $configuration->getSubject()));
  49.                 }
  50.                 $subject $arguments[$configuration->getSubject()];
  51.             }
  52.             if (!$this->authChecker->isGranted($configuration->getAttributes(), $subject)) {
  53.                 $argsString $this->getIsGrantedString($configuration);
  54.                 $message $configuration->getMessage() ?: sprintf('Access Denied by controller annotation @IsGranted(%s)'$argsString);
  55.                 if ($statusCode $configuration->getStatusCode()) {
  56.                     throw new HttpException($statusCode$message);
  57.                 }
  58.                 throw new AccessDeniedException($message);
  59.             }
  60.         }
  61.     }
  62.     private function getIsGrantedString(IsGranted $isGranted)
  63.     {
  64.         $attributes array_map(function ($attribute) {
  65.             return sprintf('"%s"'$attribute);
  66.         }, (array) $isGranted->getAttributes());
  67.         if (=== \count($attributes)) {
  68.             $argsString reset($attributes);
  69.         } else {
  70.             $argsString sprintf('[%s]'implode(', '$attributes));
  71.         }
  72.         if (null !== $isGranted->getSubject()) {
  73.             $argsString sprintf('%s, %s'$argsString$isGranted->getSubject());
  74.         }
  75.         return $argsString;
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public static function getSubscribedEvents()
  81.     {
  82.         return [KernelEvents::CONTROLLER_ARGUMENTS => 'onKernelControllerArguments'];
  83.     }
  84. }