Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Merged
merged 1 commit into from
May 21, 2018

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented May 18, 2018

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.

{
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;
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong assignation, fixed

UPGRADE-4.2.md Outdated
Security
--------

* Using the "has_role()" function in security expressions is deprecated, use the "is_granted()' function instead.
Copy link
Member

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?

Copy link
Member

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);
Copy link
Member

@xabbuh xabbuh May 18, 2018

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)?

Copy link
Member

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);
Copy link
Member

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)));
Copy link
Member

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" />
Copy link
Member

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)

@nicolas-grekas
Copy link
Member Author

@xabbuh @stof comments addressed thanks!

@xabbuh
Copy link
Member

xabbuh commented May 18, 2018

looks like some tests still need to be updated

Copy link
Contributor

@ogizanagi ogizanagi left a 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?

Copy link
Contributor

@dmaicher dmaicher left a 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 👍 😊

@fabpot
Copy link
Member

fabpot commented May 21, 2018

Thank you @nicolas-grekas.

@fabpot fabpot merged commit 9dbf399 into symfony:master May 21, 2018
fabpot added a commit that referenced this pull request May 21, 2018
…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()"
@nicolas-grekas nicolas-grekas deleted the is_granted branch May 21, 2018 09:50
OskarStark added a commit to OskarStark/symfony-docs that referenced this pull request Jun 12, 2018
OskarStark added a commit to OskarStark/symfony-docs that referenced this pull request Jun 13, 2018
OskarStark added a commit to OskarStark/symfony-docs that referenced this pull request Jun 18, 2018
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Jun 19, 2018
…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()
Liinkiing pushed a commit to Liinkiing/symfony-docs that referenced this pull request Aug 24, 2018
Liinkiing pushed a commit to Liinkiing/symfony-docs that referenced this pull request Aug 24, 2018
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
fabpot added a commit that referenced this pull request Feb 25, 2020
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants