|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Http\EventListener; |
| 13 | + |
| 14 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 15 | +use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent; |
| 16 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 17 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 18 | +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
| 19 | +use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
| 20 | +use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 21 | + |
| 22 | +/** |
| 23 | + * Handles the IsGranted attribute on controllers. |
| 24 | + * |
| 25 | + * @author Ryan Weaver <ryan@knpuniversity.com> |
| 26 | + */ |
| 27 | +class IsGrantedAttributeListener implements EventSubscriberInterface |
| 28 | +{ |
| 29 | + public function __construct( |
| 30 | + private AuthorizationCheckerInterface $authChecker, |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + public function onKernelControllerArguments(ControllerArgumentsEvent $event) |
| 35 | + { |
| 36 | + /** @var IsGranted[] $attributes */ |
| 37 | + if (!\is_array($attributes = $event->getAttributes()[IsGranted::class] ?? null)) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + $namedArguments = []; |
| 42 | + $arguments = $event->getArguments(); |
| 43 | + $r = $event->getRequest()->attributes->get('_controller_reflectors')[1] ?? new \ReflectionFunction($event->getController()); |
| 44 | + |
| 45 | + foreach ($r->getParameters() as $i => $param) { |
| 46 | + if ($param->isVariadic()) { |
| 47 | + $namedArguments[$param->name] = \array_slice($arguments, $i); |
| 48 | + break; |
| 49 | + } |
| 50 | + if (\array_key_exists($i, $arguments)) { |
| 51 | + $namedArguments[$param->name] = $arguments[$i]; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + foreach ($attributes as $attribute) { |
| 56 | + $subjectRef = $attribute->subject; |
| 57 | + $subject = null; |
| 58 | + |
| 59 | + if ($subjectRef) { |
| 60 | + if (\is_array($subjectRef)) { |
| 61 | + foreach ($subjectRef as $ref) { |
| 62 | + if (!\array_key_exists($ref, $namedArguments)) { |
| 63 | + throw new \RuntimeException(sprintf('Could not find the subject "%s" for the #[IsGranted] attribute. Try adding a "$%s" argument to your controller method.
E377
', $ref, $ref)); |
| 64 | + } |
| 65 | + $subject[$ref] = $namedArguments[$ref]; |
| 66 | + } |
| 67 | + } elseif (!\array_key_exists($subjectRef, $namedArguments)) { |
| 68 | + throw new \RuntimeException(sprintf('Could not find the subject "%s" for the #[IsGranted] attribute. Try adding a "$%s" argument to your controller method.', $subjectRef, $subjectRef)); |
| 69 | + } else { |
| 70 | + $subject = $namedArguments[$subjectRef]; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (!$this->authChecker->isGranted($attribute->attributes, $subject)) { |
| 75 | + $message = $attribute->message ?: sprintf('Access Denied by #[IsGranted(%s)] on controller', $this->getIsGrantedString($attribute)); |
| 76 | + |
| 77 | + if ($statusCode = $attribute->statusCode) { |
| 78 | + throw new HttpException($statusCode, $message); |
| 79 | + } |
| 80 | + |
| 81 | + $accessDeniedException = new AccessDeniedException($message); |
| 82 | + $accessDeniedException->setAttributes($attribute->attributes); |
| 83 | + $accessDeniedException->setSubject($subject); |
| 84 | + |
| 85 | + throw $accessDeniedException; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public static function getSubscribedEvents(): array |
| 91 | + { |
| 92 | + return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 10]]; |
| 93 | + } |
| 94 | + |
| 95 | + private function getIsGrantedString(IsGranted $isGranted): string |
| 96 | + { |
| 97 | + $attributes = array_map(fn ($attribute) => '"'.$attribute.'"', (array) $isGranted->attributes); |
| 98 | + $argsString = 1 === \count($attributes) ? reset($attributes) : '['.implode(', ', $attributes).']'; |
| 99 | + |
| 100 | + if (null !== $isGranted->subject) { |
| 101 | + $argsString .= ', "'.implode('", "', (array) $isGranted->subject).'"'; |
| 102 | + } |
| 103 | + |
| 104 | + return $argsString; |
| 105 | + } |
| 106 | +} |
0 commit comments