Skip to content

Commit 217f469

Browse files
committed
feature #33584 [Security] Deprecate isGranted()/decide() on more than one attribute (wouterj)
This PR was squashed before being merged into the 4.4 branch (closes #33584). Discussion ---------- [Security] Deprecate isGranted()/decide() on more than one attribute | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | - | License | MIT | Doc PR | tbd While I expect it not be used much, it is currently possible to call `isGranted()` on more than one attribute: ```php if ($this->authorizationChecker->isGranted(['ROLE_USER', 'ROLE_ADMIN'])) { // ... } ``` Supporting this includes a couple of problems/questions: - It is not clear whether this is `OR` or `AND`; - In fact, this is left over to the voter to decide upon. So it can vary for each voter and writers of new voters need to consider this (otherwise, you get issues like leaseweb/LswSecureControllerBundle#4 ); - It promotes to vote over roles instead of actions. I think we can do better. In the past, we've created all tooling for this to be self-explaining and easier: ```php // ExpressionLanguage component (also includes other functions, like `is_granted('EDIT')`) if ($this->authorizationChecker->isGranted("has_role('ROLE_USER') or has_role('ROLE_ADMIN')")) { // ... } // calling it multiple times in PHP (may reduce performance) if ($this->authorizationChecker->isGranted('ROLE_USER') || $this->authorizationChecker->isGranted('ROLE_ADMIN') ) { // ... } // or by using Role Hierarchy, if a user really wants to vote on roles ``` This PR deprecates passing more than one attribute to `isGranted()` and `decide()` to remove this confusing bit in Security usage. Backwards compatiblity help --- I need some help in how to approach changing the `VoterInterface::vote(TokenInterface $token, $subject, array $attributes)` method in a backwards compatible way. Removing `array` breaks all Voters, so does changing it to `string` and removed the parameter all together. Commits ------- c64b0beffb [Security] Deprecate isGranted()/decide() on more than one attribute
2 parents 5a07c29 + a718b28 commit 217f469

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

Authorization/AccessDecisionManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public function __construct(iterable $voters = [], string $strategy = self::STRA
5757
*/
5858
public function decide(TokenInterface $token, array $attributes, $object = null)
5959
{
60+
if (\count($attributes) > 1) {
61+
@trigger_error('Passing more than one Security attribute to '.__METHOD__.' is deprecated since Symfony 4.4. Use multiple decide() calls or the expression language (e.g. "has_role(...) or has_role(...)") instead.', \E_USER_DEPRECATED);
62+
}
63+
6064
return $this->{$this->strategy}($token, $attributes, $object);
6165
}
6266

Authorization/AuthorizationChecker.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ final public function isGranted($attributes, $subject = null): bool
5555

5656
if (!\is_array($attributes)) {
5757
$attributes = [$attributes];
58+
} else {
59+
@trigger_error('Passing an array of Security attributes to '.__METHOD__.' is deprecated since Symfony 4.4. Use multiple isGranted() calls or the expression language (e.g. "has_role(...) or has_role(...)") instead.', \E_USER_DEPRECATED);
5860
}
5961

6062
return $this->accessDecisionManager->decide($token, $attributes, $subject);

Tests/Authorization/AccessDecisionManagerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testStrategies($strategy, $voters, $allowIfAllAbstainDecisions,
3737
/**
3838
* @dataProvider getStrategiesWith2RolesTests
3939
*/
40-
public function testStrategiesWith2Roles($token, $strategy, $voter, $expected)
40+
public function testLegacyStrategiesWith2Roles($token, $strategy, $voter, $expected)
4141
{
4242
$manager = new AccessDecisionManager([$voter], $strategy);
4343

0 commit comments

Comments
 (0)