Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[TwigBridge] Deprecate passing $field argument to is_granted() #59282

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

Closed
wants to merge 6 commits into from

Conversation

smnandre
Copy link
Member

Q A
Branch? 7.3
Bug fix? no
New feature? no
Deprecations? yes
Issues Fix #...
License MIT

This PR deprecates the $field argument of the is_granted Twig function, and removes it from the just-added is_user_granted function.

It seems that passing a third argument "field" is a feature made/kept for the AclBundle.

The Symfony documentation states:

Optionally, an object can be passed to be used by the voter. More information can be found in Security.

But in the linked security page, no mention is made about it.

The last version of the documentation about ACL writes

ACL support was deprecated in Symfony 3.4 and will be removed in 4.0. Install the Symfony ACL bundle if you want to keep using ACL.

This feature is, afaik, not available in the rest of the similar security tools/helpers (IsGranted attribute, is_granted function in expression language, ...)

Finally, passing an value here will throw an error as the FieldVote class cannot be used without installing the AclBundle (something not documented that can lead to very frustating DX).

...

I may not have all context/history here, so please don't bite :)

This PR deprecates the `$field` argument of the `is_granted` Twig function. https://github.com/symfony/symfony/blob/78f4d9a1059725384ce563c4e1696b8e66c6983a/src/Symfony/Bridge/Twig/Extension/SecurityExtension.php#L37

Passing a third argument "field" is a feature only made/kept for the AclBundle. https://github.com/symfony/acl-bundle

The symfony documentation (https://symfony.com/doc/current/reference/twig_reference.html#is-granted) states:
> Optionally, an object can be passed to be used by the voter. More information can be found in Security.

But in the linked security page, no mention is made about it: https://symfony.com/doc/current/security.html#security-template

The last version of the documentation about ACL in Symfony (https://symfony.com/doc/3.x/security/acl_advanced.html)
> ACL support was deprecated in Symfony 3.4 and will be removed in 4.0. Install the Symfony ACL bundle if you want to keep using ACL.

This feature is, **afaik**, not available in the rest of the similar security tools/helpers (IsGranted attribute, is_granted function in expression language, ...)

And passing an argument will throw an error as the FieldVote class cannot be used without installing the AclBundle (something not documented that can lead to very frustating DX).

...

I may not have all context/history here, so please don't bite :)
Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

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

Although it's not deprecated, ACL is indeed mostly a thing of the past so 👍 to simplify the function and get rid of this integration in core.

UPGRADE-7.3 should mention the deprecation too.

src/Symfony/Bridge/Twig/Extension/SecurityExtension.php Outdated Show resolved Hide resolved
smnandre and others added 2 commits December 22, 2024 15:50
Co-authored-by: Robin Chalas <chalasr@users.noreply.github.com>
@smnandre
Copy link
Member Author

UPGRADE-7.3 should mention the deprecation too.

Done!

@fabpot
Copy link
Member

fabpot commented Jan 2, 2025

But AclBundle is still maintained, so it's going to break it, right? Or maybe I'm missing something.

@OskarStark OskarStark changed the title [TwigBridge] Deprecate passing $field argument to is_granted() [TwigBridge] Deprecate passing $field argument to is_granted() Jan 2, 2025
@smnandre
Copy link
Member Author

smnandre commented Jan 3, 2025

But AclBundle is still maintained

I didn’t know that, to be honest. Not having new features doesn’t mean "not maintained", and I’m sorry for disregarding this aspect. I based this PR on what I read in the current Symfony documentation ("support will be removed in 4.0").

Since AclBundle is not compatible with Symfony 8.0 (obviously), I didn’t see a BC issue here. I’ve always considered BC from a "past" perspective (stating this genuinely — still learning here).

However, I don’t understand why we added compatibility in the new is_user_granted() Twig function.


I may have been too "solution-oriented" instead of describing the problem(s) I tried to solve here.

Current problem

The ACL bundle is not mentioned on any of the pages related to the "is granted" toolkit: is_granted() Twig function, #[IsGranted] attribute, isGranted() Security method, etc.

But... https://symfony.com/doc/current/reference/twig_reference.html#is-granted

Capture d’écran 2025-01-03 à 19 32 22

(Twig functions reference also redirects to this page)

The documentation of the is_granted Twig function shown here may lead users to believe they can target a specific field (deducing that the attribute can be checked on the value of the given object's field). This is not the case, and they see an exception message related to a class that isn’t installed.

This may result in users installing a bundle they neither intended to nor needed to install.


If we need to maintain both present and future compatibility, I suggest the following:

0) Fix is_granted_for_user()

Either we decide to remove the third argument, or we should at least make its behavior strictly consistent. Currently, passing a false-ish third argument (e.g., an empty string) is handled differently and won’t trigger any alert if the FieldVote class is missing, whereas the same scenario with is_granted would.

1) Add a custom exception

When a third argument is passed to the method (whatever its value):

  • If AclBundle (and/or (?) Acl package) is installed: Use the argument as it works today.
  • Else throw a clear, explicit message.

Additionally, in Symfony 8.0, we could consider ignoring the argument entirely when the bundle is not enabled.

2) Update the documentation

What about removing any mention of the third argument from the Symfony documentation?

Conversely, I suggest adding comments in the code and perhaps one or two tests to ensure non-regression.

Next?

I think such checks should not be performed at runtime but rather during compilation/configuration. If, for any reason, the ACLBundle wasn’t installed correctly, a simple class_alias on Symfony\Component\Security\Acl\Voter\FieldVote could provide a solution.


I’m, of course, ready to implement any of these changes… or none at all — feel free to drop this PR if you think it’s not worth it!

@chalasr
Copy link
Member

chalasr commented Jan 3, 2025

Could ACLBundle override these functions? I think it'd be worth it for the sake of not polluting the core API with something we took care of extracting from core.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

Could ACLBundle override these functions?

is_field_granted + is_field_granted_for_user would be nice replacements.
PR needed on AclBundle before we can consider this one IMHO.

@smnandre
Copy link
Member Author

smnandre commented Jan 7, 2025

PR needed on AclBundle before we can consider this one IMHO.

There’s no mention of Twig in general—or is_granted in particular—in the entire ACLBundle repository or its documentation. The same goes for the security-acl component, with the last references to Twig being in 2015 changelogs.

So, I’ll politely decline the invitation… that wasn’t even one, haha 😘

As I said, if you think it's good as it is, let's close here :)

@nicolas-grekas
Copy link
Member

If we need to maintain both present and future compatibility, I suggest the following: [...]

I agree with all items then. Can you please update the PR accordingly?

@nicolas-grekas
Copy link
Member

Actually, let's do the follow up in another PR to keep the discussion relevant.

smnandre added a commit to smnandre/symfony-docs that referenced this pull request Jan 8, 2025
The third argument of is_granted / is_granted_for_user is undocumented (except on this page) (neither on Symfony docs, code, nor on... Symfony ACL Bundle docs).

It must be kept and maintained for BC reasons. But it can be missleading/frustrating to show this argument to users, when they cannot use it with a standard/recommended Symfony installation.

And if they just test it, the error could lead them to believe the AclBundle needs to be installed to use the `is_granted` function(s).

As suggested in symfony/symfony#59282 (comment) I believe "not showing it" is the "best" solution here.
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Jan 8, 2025
…nt (smnandre)

This PR was merged into the 7.3 branch.

Discussion
----------

[Security] Remove mention of is_granted_ `$field` argument

The third argument of is_granted / is_granted_for_user is undocumented (except on this page) (neither on Symfony docs, code, nor on... Symfony ACL Bundle docs).

It must be kept and maintained for BC reasons. But it can be missleading/frustrating to show this argument to users, when they cannot use it with a standard/recommended Symfony installation.

And if they just test it, the error could lead them to believe the AclBundle needs to be installed to use the `is_granted` function(s).

As suggested in symfony/symfony#59282 (comment) I believe "not showing it" is the "best" solution here.

Commits
-------

693b758 [Security] Remove mention of is_granted_ `$field` argument
nicolas-grekas added a commit that referenced this pull request Jan 10, 2025
…lsy $field (smnandre)

This PR was squashed before being merged into the 7.3 branch.

Discussion
----------

[TwigBridge] Align isGrantedForUser on isGranted with falsy $field

| Q             | A
| ------------- | ---
| Branch?       | 7.3
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues        | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->
| License       | MIT

Align `is_granted_for_user` on `is_granted` behaviour when passing a falsy $field argument.

(Follows #59282 (comment))

Commits
-------

cc92b65 [TwigBridge] Align isGrantedForUser on isGranted with falsy $field
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.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.