8000 [Security] Deprecate isGranted()/decide() on more than one attribute · symfony/symfony@c64b0be · GitHub
[go: up one dir, main page]

Skip to content

Commit c64b0be

Browse files
wouterjfabpot
authored andcommitted
[Security] Deprecate isGranted()/decide() on more than one attribute
1 parent d4e6a37 commit c64b0be

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

UPGRADE-4.4.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,24 @@ Security
197197
* The `LdapUserProvider` class has been deprecated, use `Symfony\Component\Ldap\Security\LdapUserProvider` instead.
198198
* Implementations of `PasswordEncoderInterface` and `UserPasswordEncoderInterface` should add a new `needsRehash()` method
199199
* Deprecated returning a non-boolean value when implementing `Guard\AuthenticatorInterface::checkCredentials()`. Please explicitly return `false` to indicate invalid credentials.
200+
* Deprecated passing more than one attribute to `AccessDecisionManager::decide()` and `AuthorizationChecker::isGranted()` (and indirectly the `is_granted()` Twig and ExpressionLanguage function)
201+
202+
**Before**
203+
```php
204+
if ($this->authorizationChecker->isGranted(['ROLE_USER', 'ROLE_ADMIN'])) {
205+
// ...
206+
}
207+
```
208+
209+
**After**
210+
```php
211+
if ($this->authorizationChecker->isGranted(new Expression("has_role('ROLE_USER') or has_role('ROLE_ADMIN')"))) {}
212+
213+
// or:
214+
if ($this->authorizationChecker->isGranted('ROLE_USER')
215+
|| $this->authorizationChecker->isGranted('ROLE_ADMIN')
216+
) {}
217+
```
200218

201219
Stopwatch
202220
---------

src/Symfony/Component/Security/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
for "guard" authenticators that deal with user passwords
1313
* Marked all dispatched event classes as `@final`
1414
* Deprecated returning a non-boolean value when implementing `Guard\AuthenticatorInterface::checkCredentials()`.
15+
* Deprecated passing more than one attribute to `AccessDecisionManager::decide()` and `AuthorizationChecker::isGranted()`
1516

1617
4.3.0
1718
-----

src/Symfony/Component/Security/Core/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

src/Symfony/Component/Security/Core/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);

src/Symfony/Component/Security/Core/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)
0