-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Add #[IsGranted()]
#46907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Attribute; | ||
|
||
/** | ||
* @author Ryan Weaver <ryan@knpuniversity.com> | ||
*/ | ||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)] | ||
class IsGranted | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should probably be final, because the usage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #46915 |
||
{ | ||
public function __construct( | ||
/** | ||
* Sets the first argument that will be passed to isGranted(). | ||
*/ | ||
public array|string|null $attributes = null, | ||
|
||
/** | ||
* Sets the second argument passed to isGranted(). | ||
*/ | ||
public array|string|null $subject = null, | ||
|
||
/** | ||
* The message of the exception - has a nice default if not set. | ||
*/ | ||
public ?string $message = null, | ||
|
||
/** | ||
* If set, will throw HttpKernel's HttpException with the given $statusCode. | ||
* If null, Security\Core's AccessDeniedException will be used. | ||
*/ | ||
public ?int $statusCode = null, | ||
) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\EventListener; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
use Symfony\Component\Security\Http\Attribute\IsGranted; | ||
|
||
/** | ||
* Handles the IsGranted attribute on controllers. | ||
* | ||
* @author Ryan Weaver <ryan@knpuniversity.com> | ||
*/ | ||
class IsGrantedAttributeListener implements EventSubscriberInterface | ||
{ | ||
public function __construct( | ||
private AuthorizationCheckerInterface $authChecker, | ||
) { | ||
} | ||
|
||
public function onKernelControllerArguments(ControllerArgumentsEvent $event) | ||
{ | ||
/** @var IsGranted[] $attributes */ | ||
if (!\is_array($attributes = $event->getAttributes()[IsGranted::class] ?? null)) { | ||
return; | ||
} | ||
|
||
$namedArguments = []; | ||
$arguments = $event->getArguments(); | ||
$r = $event->getRequest()->attributes->get('_controller_reflectors')[1] ?? new \ReflectionFunction($event->getController()); | ||
|
||
foreach ($r->getParameters() as $i => $param) { | ||
if ($param->isVariadic()) { | ||
$namedArguments[$param->name] = \array_slice($arguments, $i); | ||
break; | ||
} | ||
if (\array_key_exists($i, $arguments)) { | ||
$namedArguments[$param->name] = $arguments[$i]; | ||
} | ||
} | ||
|
||
foreach ($attributes as $attribute) { | ||
$subjectRef = $attribute->subject; | ||
$subject = null; | ||
|
||
if ($subjectRef) { | ||
if (\is_array($subjectRef)) { | ||
foreach ($subjectRef as $ref) { | ||
if (!\array_key_exists($ref, $namedArguments)) { | ||
throw new \RuntimeException(sprintf('Could not find the subject "%s" for the #[IsGranted] attribute. Try adding a "$%s" argument to your controller method.', $ref, $ref)); | ||
} | ||
$subject[$ref] = $namedArguments[$ref]; | ||
} | ||
} elseif (!\array_key_exists($subjectRef, $namedArguments)) { | ||
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)); | ||
} else { | ||
$subject = $namedArguments[$subjectRef]; | ||
} | ||
} | ||
|
||
if (!$this->authChecker->isGranted($attribute->attributes, $subject)) { | ||
$message = $attribute->message ?: sprintf('Access Denied by #[IsGranted(%s)] on controller', $this->getIsGrantedString($attribute)); | ||
|
||
if ($statusCode = $attribute->statusCode) { | ||
throw new HttpException($statusCode, $message); | ||
} | ||
|
||
$accessDeniedException = new AccessDeniedException($message); | ||
$accessDeniedException->setAttributes($attribute->attributes); | ||
$accessDeniedException->setSubject($subject); | ||
|
||
throw $accessDeniedException; | ||
} | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 10]]; | ||
} | ||
|
||
private function getIsGrantedString(IsGranted $isGranted): string | ||
{ | ||
$attributes = array_map(fn ($attribute) => '"'.$attribute.'"', (array) $isGranted->attributes); | ||
$argsString = 1 === \count($attributes) ? reset($attributes) : '['.implode(', ', $attributes).']'; | ||
|
||
if (null !== $isGranted->subject) { | ||
$argsString .= ', "'.implode('", "', (array) $isGranted->subject).'"'; | ||
} | ||
|
||
return $argsString; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm getting credit without even opening a PR 馃槑