-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Security] Allow using a callable with #[IsGranted]
#59150
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
Merged
nicolas-grekas
merged 1 commit into
symfony:7.3
from
alexandre-daubois:is-granted-callable
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/Symfony/Component/Security/Core/Authorization/Voter/ClosureVoter.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?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\Core\Authorization\Voter; | ||
|
|
||
| use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; | ||
| use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
| use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; | ||
| use Symfony\Component\Security\Http\Attribute\IsGranted; | ||
|
|
||
| /** | ||
| * This voter allows using a closure as the attribute being voted on. | ||
| * | ||
| * The following named arguments are passed to the closure: | ||
| * | ||
| * - `token`: The token being used for voting | ||
| * - `subject`: The subject of the vote | ||
| * - `accessDecisionManager`: The access decision manager | ||
| * - `trustResolver`: The trust resolver | ||
| * | ||
| * @see IsGranted doc for the complete closure signature. | ||
| * | ||
| * @author Alexandre Daubois <alex.daubois@gmail.com> | ||
| */ | ||
| final class ClosureVoter implements CacheableVoterInterface | ||
| { | ||
| public function __construct( | ||
| private AccessDecisionManagerInterface $accessDecisionManager, | ||
| private AuthenticationTrustResolverInterface $trustResolver, | ||
| ) { | ||
| } | ||
|
|
||
| public function supportsAttribute(string $attribute): bool | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| public function supportsType(string $subjectType): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| public function vote(TokenInterface $token, mixed $subject, array $attributes, ?Vote $vote = null): int | ||
| { | ||
| $vote ??= new Vote(); | ||
| $failingClosures = []; | ||
| $result = VoterInterface::ACCESS_ABSTAIN; | ||
| foreach ($attributes as $attribute) { | ||
| if (!$attribute instanceof \Closure) { | ||
| continue; | ||
| } | ||
|
|
||
| $name = (new \ReflectionFunction($attribute))->name; | ||
| $result = VoterInterface::ACCESS_DENIED; | ||
| if ($attribute(token: $token, subject: $subject, accessDecisionManager: $this->accessDecisionManager, trustResolver: $th 8000 is->trustResolver)) { | ||
| $vote->reasons[] = \sprintf('Closure %s returned true.', $name); | ||
|
|
||
| return VoterInterface::ACCESS_GRANTED; | ||
| } | ||
|
|
||
| $failingClosures[] = $name; | ||
| } | ||
|
|
||
| if ($failingClosures) { | ||
| $vote->reasons[] = \sprintf('Closure%s %s returned false.', 1 < \count($failingClosures) ? 's' : '', implode(', ', $failingClosures)); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/Symfony/Component/Security/Core/Tests/Authorization/Voter/ClosureVoterTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| <?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\Core\Tests\Authorization\Voter; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; | ||
| use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
| use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; | ||
| use Symfony\Component\Security\Core\Authorization\Voter\ClosureVoter; | ||
| use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; | ||
| use Symfony\Component\Security\Core\User\UserInterface; | ||
|
|
||
| class ClosureVoterTest extends TestCase | ||
| { | ||
| private ClosureVoter $voter; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->voter = new ClosureVoter( | ||
| $this->createMock(AccessDecisionManagerInterface::class), | ||
| $this->createMock(AuthenticationTrustResolverInterface::class), | ||
| ); | ||
| } | ||
|
|
||
| public function testEmptyAttributeAbstains() | ||
| { | ||
| $this->assertSame(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote( | ||
| $this->createMock(TokenInterface::class), | ||
| null, | ||
| []) | ||
| ); | ||
| } | ||
|
|
||
| public function testClosureReturningFalseDeniesAccess() | ||
| { | ||
| $token = $this->createMock(TokenInterface::class); | ||
| $token->method('getRoleNames')->willReturn([]); | ||
| $token->method('getUser')->willReturn($this->createMock(UserInterface::class)); | ||
|
|
||
| $this->assertSame(VoterInterface::ACCESS_DENIED, $this->voter->vote( | ||
| $token, | ||
| null, | ||
| [fn (...$vars) => false] | ||
| )); | ||
| } | ||
|
|
||
| public function testClosureReturningTrueGrantsAccess() | ||
| { | ||
| $token = $this->createMock(TokenInterface::class); | ||
| $token->method('getRoleNames')->willReturn([]); | ||
| $token->method('getUser')->willReturn($this->createMock(UserInterface::class)); | ||
|
|
||
| $this->assertSame(VoterInterface::ACCESS_GRANTED, $this->voter->vote( | ||
| $token, | ||
| null, | ||
| [fn (...$vars) => true] | ||
| )); | ||
| } | ||
|
|
||
| public function testArgumentsContent() | ||
| { | ||
| $token = $this->createMock(TokenInterface::class); | ||
| $token->method('getRoleNames')->willReturn(['MY_ROLE', 'ANOTHER_ROLE']); | ||
| $token->method('getUser')->willReturn($this->createMock(UserInterface::class)); | ||
|
|
||
| $outerSubject = new \stdClass(); | ||
|
|
||
| $this->voter->vote( | ||
| $token, | ||
| $outerSubject, | ||
| [function (...$vars) use ($outerSubject) { | ||
| $this->assertInstanceOf(TokenInterface::class, $vars['token']); | ||
| $this->assertSame($outerSubject, $vars['subject']); | ||
|
|
||
| $this->assertInstanceOf(AccessDecisionManagerInterface::class, $vars['accessDecisionManager']); | ||
| $this->assertInstanceOf(AuthenticationTrustResolverInterface::class, $vars['trustResolver']); | ||
|
|
||
| return true; | ||
| }] | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.