-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security/Core] Add "is_granted()" to security expressions, deprecate "has_role()" #27305
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
Conversation
{ | ||
if ($authChecker instanceof RoleHierarchyInterface) { | ||
@trigger_error(sprintf('Passing a RoleHierarchyInterface to "%s()" is deprecated since Symfony 4.2. Pass an AuthorizationCheckerInterface instead.', __METHOD__), E_USER_DEPRECATED); | ||
$authChecker = $roleHierarchy; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a single argument for either the auth checker or the role hierarchy is broken. We still need to pass the role hierarchy all the time, otherwise we cannot provide the Bc layer for has_role
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong assignation, fixed
8e6cba4
to
1ceffe1
Compare
UPGRADE-4.2.md
Outdated
Security | ||
-------- | ||
|
||
* Using the "has_role()" function in security expressions is deprecated, use the "is_granted()' function instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use backticks instead of quotes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, would be much better.
Or if you really want to use quotes, use the same one on both sides at least.
new ExpressionFunction('is_remember_me', function () { | ||
return '$trust_resolver->isRememberMe($token)'; | ||
}, function (array $variables) { | ||
return $variables['trust_resolver']->isRememberMe($variables['token']); | ||
}), | ||
|
||
new ExpressionFunction('has_role', function ($role) { | ||
@trigger_error('Using the "has_role()" function in security expressions is deprecated since Symfony 4.2, use "is_granted()" instead.', E_USER_DEPRECATED); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this trigger the deprecation too early (i.e. at compile instead of runtime)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, it will trigger it at compile time when compiling an expression using the deprecated function. that's totally fine, as Symfony 5.0 will also fail at compile time.
$roleHierarchy = $authChecker; | ||
$authChecker = null; | ||
} elseif (null === $authChecker) { | ||
@trigger_error(sprintf('"%s()" expects an AuthorizationCheckerInterface as 3rd argument, not passing it is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[...] as the 3rd [...]
} elseif (null === $authChecker) { | ||
@trigger_error(sprintf('"%s()" expects an AuthorizationCheckerInterface as 3rd argument, not passing it is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED); | ||
} elseif (!$authChecker instanceof AuthorizationCheckerInterface) { | ||
throw new \InvalidArgumentException(sprintf('Argument 3 passed to %s() must be instance of %s or null, %s given.', __METHOD__, AuthorizationCheckerInterface::class, is_object($authChecker) ? get_class($authChecker) : gettype($authChecker))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[...] be an instance [...]
@@ -119,6 +119,7 @@ | |||
<service id="security.access.expression_voter" class="Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter"> | |||
<argument type="service" id="security.expression_language" /> | |||
<argument type="service" id="security.authentication.trust_resolver" /> | |||
<argument type="service" id="security.authorization_checker" on-invalid="null" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why making it optional ? If you have a voter, you will also have the auth checker (otherwise your voter is useless anyway as it is used by the auth checker)
1ceffe1
to
2ecd8f1
Compare
looks like some tests still need to be updated |
2ecd8f1
to
9dbf399
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add tests fo the two deprecations triggered from ExpressionVoter
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes #23084 then 👍 😊
Thank you @nicolas-grekas. |
…ions, deprecate "has_role()" (nicolas-grekas) This PR was merged into the 4.2-dev branch. Discussion ---------- [Security/Core] Add "is_granted()" to security expressions, deprecate "has_role()" | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #23084 | License | MIT | Doc PR | - Because `has_role()` doesn't use the auth checker, it is confusing. Let's move `is_granted()` to core (it's provided by SensioFrameworkExtraBundle / ApiPlatform for now. Commits ------- 9dbf399 [Security/Core] Add "is_granted()" to security expressions, deprecate "has_role()"
…tark) This PR was merged into the master branch. Discussion ---------- use is_granted() instead of deprecated has_role() using `has_role()` was deprecated in symfony/symfony#27305 since `4.2` by @nicolas-grekas Commits ------- 5e6031b use is_granted() instead of deprecated has_role()
…guage functions (wouterj) This PR was merged into the 5.1-dev branch. Discussion ---------- [Security] Use new IS_* attributes in the expression language functions | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | n/a #31189 has been merged which introduces some new attributes (`IS_ANONYMOUS` & friends). We can now modify the code behind the `is_*()` expression language functions to use these new attributes. This avoids any possibility of having them out of sync. In case you - just like me - are interested why `isGranted("IS_AUTHENTICATED_FULLY")` wasn't used before: These functions were implemented without `auth_checker` being available. The auth checker variable was introduced in 4.2 by #27305, so now we can use this. Commits ------- 3f0c599 Use new IS_* attributes in the expression language functions
Because
has_role()
doesn't use the auth checker, it is confusing. Let's moveis_granted()
to core (it's provided by SensioFrameworkExtraBundle / ApiPlatform for now.