-
Notifications
You must be signed in to change notification settings - Fork 111
[Symfony 7.3] Replace AuthorizationChecker with AccessDecisionManager… #913
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
TomasVotruba
merged 1 commit into
rectorphp:main
from
meddrh:symfony73/authorization-checker-to-access-decision-manager
Jan 20, 2026
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
28 changes: 28 additions & 0 deletions
28
...sionManagerInVoterRector/AuthorizationCheckerToAccessDecisionManagerInVoterRectorTest.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,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\AuthorizationCheckerToAccessDecisionManagerInVoterRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class AuthorizationCheckerToAccessDecisionManagerInVoterRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
...onCheckerToAccessDecisionManagerInVoterRector/Fixture/authorization_checker_voter.php.inc
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,49 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\AuthorizationCheckerToAccessDecisionManagerInVoterRector\Fixture; | ||
|
|
||
| use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
| use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
| use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
|
|
||
| final class AuthorizationCheckerVoter extends Voter | ||
| { | ||
| public function __construct( | ||
| private AuthorizationCheckerInterface $authorizationChecker | ||
| ) {} | ||
|
|
||
| protected function supports(string $attribute, mixed $subject): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool | ||
| { | ||
| return $this->authorizationChecker->isGranted('ROLE_ADMIN'); | ||
| } | ||
| } | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\AuthorizationCheckerToAccessDecisionManagerInVoterRector\Fixture; | ||
|
|
||
| use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
| use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
| use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
|
|
||
| final class AuthorizationCheckerVoter extends Voter | ||
| { | ||
| public function __construct( | ||
| private \Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface $accessDecisionManager | ||
| ) {} | ||
|
|
||
| protected function supports(string $attribute, mixed $subject): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool | ||
| { | ||
| return $this->accessDecisionManager->decide($token, ['ROLE_ADMIN']); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
...lass_/AuthorizationCheckerToAccessDecisionManagerInVoterRector/config/configured_rule.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,11 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\Symfony\Symfony73\Rector\Class_\AuthorizationCheckerToAccessDecisionManagerInVoterRector; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([ | ||
| AuthorizationCheckerToAccessDecisionManagerInVoterRector::class, | ||
| ]); |
217 changes: 217 additions & 0 deletions
217
rules/Symfony73/Rector/Class_/AuthorizationCheckerToAccessDecisionManagerInVoterRector.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,217 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Symfony\Symfony73\Rector\Class_; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\ArrayItem; | ||
| use PhpParser\Node\Expr\Array_; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Expr\PropertyFetch; | ||
| use PhpParser\Node\Expr\Variable; | ||
| use PhpParser\Node\Identifier; | ||
| use PhpParser\Node\Name\FullyQualified; | ||
| use PhpParser\Node\Stmt\Class_; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| use PhpParser\Node\Stmt\Function_; | ||
| use PhpParser\NodeVisitor; | ||
| use PHPStan\Type\ObjectType; | ||
| use Rector\Rector\AbstractRector; | ||
| use Rector\Symfony\Enum\SymfonyClass; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\Symfony\Tests\Symfony73\Rector\Class_\AuthorizationCheckerToAccessDecisionManagerInVoterRector\AuthorizationCheckerToAccessDecisionManagerInVoterRectorTest | ||
| */ | ||
| final class AuthorizationCheckerToAccessDecisionManagerInVoterRector extends AbstractRector | ||
| { | ||
| private const string AUTHORIZATION_CHECKER_PROPERTY = 'authorizationChecker'; | ||
| private const string ACCESS_DECISION_MANAGER_PROPERTY = 'accessDecisionManager'; | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Replaces AuthorizationCheckerInterface with AccessDecisionManagerInterface inside Symfony Voters', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
| use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
| use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
|
|
||
| final class AuthorizationCheckerVoter extends Voter | ||
| { | ||
| public function __construct( | ||
| private AuthorizationCheckerInterface $authorizationChecker | ||
| ) {} | ||
|
|
||
| protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool | ||
| { | ||
| return $this->authorizationChecker->isGranted('ROLE_ADMIN'); | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; | ||
| use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
| use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
|
|
||
| final class AuthorizationCheckerVoter extends Voter | ||
| { | ||
| public function __construct( | ||
| private AccessDecisionManagerInterface $accessDecisionManager | ||
| ) {} | ||
|
|
||
| protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool | ||
| { | ||
| return $this->accessDecisionManager->decide($token, ['ROLE_ADMIN']); | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| ), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [Class_::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param Class_ $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| if ($node->extends === null || ! $this->isName($node->extends, SymfonyClass::VOTER_CLASS)) { | ||
| return null; | ||
| } | ||
|
|
||
| $hasChanged = false; | ||
| $renamedProperties = []; | ||
|
|
||
| $authorizationCheckerType = new ObjectType( | ||
| SymfonyClass::AUTHORIZATION_CHECKER | ||
| ); | ||
|
|
||
| // 1) Regular properties | ||
| foreach ($node->getProperties() as $property) { | ||
| if (! $this->isObjectType($property, $authorizationCheckerType)) { | ||
| continue; | ||
| } | ||
|
|
||
| $property->type = new FullyQualified( | ||
| SymfonyClass::ACCESS_DECISION_MANAGER_INTERFACE | ||
| ); | ||
|
|
||
| foreach ($property->props as $prop) { | ||
| if ($this->getName($prop) === self::AUTHORIZATION_CHECKER_PROPERTY) { | ||
| $prop->name = new Identifier(self::ACCESS_DECISION_MANAGER_PROPERTY); | ||
| $renamedProperties[self::AUTHORIZATION_CHECKER_PROPERTY] | ||
| = self::ACCESS_DECISION_MANAGER_PROPERTY; | ||
| } | ||
| } | ||
|
|
||
| $hasChanged = true; | ||
| } | ||
|
|
||
| // 2) Promoted properties (constructor) | ||
| $constructor = $node->getMethod('__construct'); | ||
| if ($constructor instanceof ClassMethod) { | ||
| foreach ($constructor->params as $param) { | ||
| if ( | ||
| $param->type === null | ||
| || ! $this->isName($param->type, SymfonyClass::AUTHORIZATION_CHECKER) | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| $param->type = new FullyQualified( | ||
| SymfonyClass::ACCESS_DECISION_MANAGER_INTERFACE | ||
| ); | ||
|
|
||
| if ( | ||
| $param->var instanceof Variable | ||
| && $this->getName($param->var) === self::AUTHORIZATION_CHECKER_PROPERTY | ||
| ) { | ||
| $param->var->name = self::ACCESS_DECISION_MANAGER_PROPERTY; | ||
| $renamedProperties[self::AUTHORIZATION_CHECKER_PROPERTY] | ||
| = self::ACCESS_DECISION_MANAGER_PROPERTY; | ||
| } | ||
|
|
||
| $hasChanged = true; | ||
| } | ||
| } | ||
|
|
||
| // 3) Replace isGranted() with decide() | ||
| $voteMethod = $node->getMethod('voteOnAttribute'); | ||
| if ($voteMethod instanceof ClassMethod) { | ||
| $this->traverseNodesWithCallable( | ||
| $voteMethod, | ||
| function (Node $node) use (&$hasChanged, $voteMethod, $renamedProperties) { | ||
| if ($node instanceof Class_ || $node instanceof Function_) { | ||
| return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; | ||
| } | ||
|
|
||
| if (! $node instanceof MethodCall) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $this->isObjectType( | ||
| $node->var, | ||
| new ObjectType(SymfonyClass::AUTHORIZATION_CHECKER) | ||
| )) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $node->var instanceof PropertyFetch) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $this->isName($node->name, 'isGranted')) { | ||
| return null; | ||
| } | ||
|
|
||
| $propertyName = $this->getName($node->var->name); | ||
| if ($propertyName === null || ! isset($renamedProperties[$propertyName])) { | ||
| return null; | ||
| } | ||
|
|
||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $node->var->name = new Identifier($renamedProperties[$propertyName]); | ||
| $node->name = new Identifier('decide'); | ||
|
|
||
| $tokenVariable = $voteMethod->params[2]->var ?? null; | ||
| if (! $tokenVariable instanceof Variable) { | ||
| return null; | ||
| } | ||
|
|
||
| $attributeArg = $node->args[0] ?? null; | ||
| if (! $attributeArg instanceof Arg) { | ||
| return null; | ||
| } | ||
|
|
||
| $attributeExpr = $attributeArg->value; | ||
|
|
||
| $node->args = [ | ||
| new Arg($tokenVariable), | ||
| new Arg(new Array_([ | ||
| new ArrayItem($attributeExpr), | ||
| ])), | ||
| ]; | ||
|
|
||
| $hasChanged = true; | ||
| return $node; | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| return $hasChanged ? $node : null; | ||
| } | ||
| } | ||
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
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.