-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
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,78 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
*/ | ||
final class ClosureVoter implements CacheableVoterInterface | ||
{ | ||
public function __construct( | ||
private AccessDecisionManagerInterface $accessDecisionManager, | ||
private AuthenticationTrustResolverInterface $trustResolver, | ||
) { | ||
} | ||
|
||
public function supportsAttribute(string $attribute): bool | ||
Check failure on line 41 in src/Symfony/Component/Security/Core/Authorization/Voter/ClosureVoter.php
|
||
{ | ||
return false; | ||
} | ||
|
||
public function supportsType(string $subjectType): bool | ||
Check failure on line 46 in src/Symfony/Component/Security/Core/Authorization/Voter/ClosureVoter.php
|
||
{ | ||
return true; | ||
} | ||
|
||
public function vote(TokenInterface $token, mixed $subject, array $attributes, ?Vote $vote = null): int | ||
Check failure on line 51 in src/Symfony/Component/Security/Core/Authorization/Voter/ClosureVoter.php
|
||
{ | ||
$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: $this->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; | ||
} | ||
} |
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 <[email protected]> | ||
* | ||
* 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; | ||
}] | ||
); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.