vendor/sensio/framework-extra-bundle/src/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\KernelEvent;
  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(KernelEvent $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.             $subjectRef $configuration->getSubject();
  46.             $subject null;
  47.             if ($subjectRef) {
  48.                 if (\is_array($subjectRef)) {
  49.                     foreach ($subjectRef as $ref) {
  50.                         if (!\array_key_exists($ref$arguments)) {
  51.                             throw $this->createMissingSubjectException($ref);
  52.                         }
  53.                         $subject[$ref] = $arguments[$ref];
  54.                     }
  55.                 } else {
  56.                     if (!\array_key_exists($subjectRef$arguments)) {
  57.                         throw $this->createMissingSubjectException($subjectRef);
  58.                     }
  59.                     $subject $arguments[$subjectRef];
  60.                 }
  61.             }
  62.             if (!$this->authChecker->isGranted($configuration->getAttributes(), $subject)) {
  63.                 $argsString $this->getIsGrantedString($configuration);
  64.                 $message $configuration->getMessage() ?: sprintf('Access Denied by controller annotation @IsGranted(%s)'$argsString);
  65.                 if ($statusCode $configuration->getStatusCode()) {
  66.                     throw new HttpException($statusCode$message);
  67.                 }
  68.                 $accessDeniedException = new AccessDeniedException($message);
  69.                 $accessDeniedException->setAttributes($configuration->getAttributes());
  70.                 $accessDeniedException->setSubject($subject);
  71.                 throw $accessDeniedException;
  72.             }
  73.         }
  74.     }
  75.     private function createMissingSubjectException(string $subject)
  76.     {
  77.         return new \RuntimeException(sprintf('Could not find the subject "%s" for the @IsGranted annotation. Try adding a "$%s" argument to your controller method.'$subject$subject));
  78.     }
  79.     private function getIsGrantedString(IsGranted $isGranted)
  80.     {
  81.         $attributes array_map(function ($attribute) {
  82.             return sprintf('"%s"'$attribute);
  83.         }, (array) $isGranted->getAttributes());
  84.         if (=== \count($attributes)) {
  85.             $argsString reset($attributes);
  86.         } else {
  87.             $argsString sprintf('[%s]'implode(', '$attributes));
  88.         }
  89.         if (null !== $isGranted->getSubject()) {
  90.             $argsString sprintf('%s, %s'$argsString$isGranted->getSubject());
  91.         }
  92.         return $argsString;
  93.     }
  94.     /**
  95.      * @return array
  96.      */
  97.     public static function getSubscribedEvents()
  98.     {
  99.         return [KernelEvents::CONTROLLER_ARGUMENTS => 'onKernelControllerArguments'];
  100.     }
  101. }