-
-
Notifications
You must be signed in to change notification settings - Fork 978
feat(symfony): expose voter reasons #8448
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Member
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. I think this whole class can go away. We already own the // evaluator
static fn (array $variables, $attributes, $object = null) => $variables['auth_checker']->isGranted($attributes, $object, $variables['access_decision'] ?? null)
// compiler
static fn ($attributes, $object = 'null'): string => sprintf('$auth_checker->isGranted(%s, %s, $access_decision ?? null)', $attributes, $object)with That removes this class and its 242-line test. One behaviour delta worth calling out: with a single decision shared per expression, |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Symfony\Security; | ||
|
|
||
| use Symfony\Component\Security\Core\Authorization\AccessDecision; | ||
| use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| final class AccessDecisionCapturingAuthorizationChecker implements AuthorizationCheckerInterface | ||
| { | ||
| private ?AccessDecision $accessDecision = null; | ||
|
|
||
| public function __construct(private readonly AuthorizationCheckerInterface $decorated) | ||
| { | ||
| } | ||
|
|
||
| public function isGranted(mixed $attribute, mixed $subject = null, ?AccessDecision $accessDecision = null): bool | ||
| { | ||
| $accessDecision ??= new AccessDecision(); | ||
| $accessDecision->isGranted = $this->decorated->isGranted($attribute, $subject, $accessDecision); | ||
| $this->accessDecision = $accessDecision; | ||
|
|
||
| return $accessDecision->isGranted; | ||
| } | ||
|
|
||
| public function getAccessDeniedMessage(): ?string | ||
| { | ||
| if (null === $this->accessDecision || $this->accessDecision->isGranted) { | ||
| return null; | ||
| } | ||
|
|
||
| return $this->accessDecision->getMessage(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Symfony\Security; | ||
|
|
||
| /** | ||
| * Exposes the applicable denial message from the latest completed access check. | ||
| */ | ||
| interface AccessDeniedMessageProviderInterface | ||
| { | ||
| public function getAccessDeniedMessage(): ?string; | ||
| } |
|
Member
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. This class is deprecated since 4.4, so I'd rather not grow it with |
|
Member
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. Thanks for this — exposing voter reasons is definitely something we want, and the contract you describe (explicit The PR does two separable things: (1) capture the On the transport: I'd prefer the caller to own the decision, the way Symfony itself did it. Keep interface AccessDecisionAwareResourceAccessCheckerInterface
{
public function decide(string $resourceClass, string $expression, array $extraVariables = []): AccessDecision;
}
One trap to guard: |
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.
Wiring
%kernel.debug%into four provider definitions duplicates a decision we already make in exactly one place:ErrorProvider(src/State/ErrorProvider.php) already receives$debugand already scrubs the detail so we don't leak internals in prod.If a
nulldetail on the exception means "no developer-configured message, safe to scrub", the whole debug gate fits as one extra rule there and none of these providers needs to know aboutkernel.debug. The two strings on the exception are unavoidable either way, since a configuredsecurityMessagehas to stay visible in production — but the flag doesn't have to travel through the security providers.