-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Allow using a callable with #[IsGranted]
#59150
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 a 8000 gree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Security] Allow using a callable with #[IsGranted]
#59150
Conversation
#[IsGranted]
#[IsGranted]
b493c5d
to
d1cd7c3
Compare
src/Symfony/Component/Security/Core/Authorization/IsGrantedPayload.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
d1cd7c3
to
6eedfaa
Compare
The WIP fix on |
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/IsGrantedPayload.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/IsGrantedPayload.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
6eedfaa
to
a28dcfd
Compare
Thank you for your feedbacks. I updated the PR. Also, I'm now using |
a28dcfd
to
da421f7
Compare
src/Symfony/Component/Security/Http/EventListener/IsGrantedAttributeListener.php
Outdated
Show resolved
Hide resolved
Status: Needs Review |
I think the Attribute phpdoc and future docs should mention that only static closures are supported (for now). |
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/Voter/CallableVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Http/EventListener/IsGrantedAttributeListener.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Http/EventListener/IsGrantedAttributeListener.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Http/EventListener/IsGrantedAttributeListener.php
Outdated
Show resolved
Hide resolved
@chalasr supporting static closures only is not a limitation of our code. It is a limitation of PHP attributes. If PHP supports more closures (quite unlikely though, as I don't see how they would work), the code in Symfony would not require any change to support them. |
src/Symfony/Bundle/SecurityBundle/Resources/config/security.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Http/EventListener/IsGrantedAttributeListener.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Http/EventListener/IsGrantedAttributeListener.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Http/EventListener/IsGrantedAttributeListener.php
Outdated
Show resolved
Hide resolved
3c1d6cd
to
2dafa16
Compare
2dafa16
to
6ce5080
Compare
6ce5080
to
897ac91
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.
Some comments to improve the DX hopefully.
src/Symfony/Component/Security/Core/Authorization/Voter/ClosureVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/Voter/ClosureVoter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Http/EventListener/IsGrantedAttributeListener.php
Outdated
Show resolved
Hide resolved
816c445
to
f3cfa81
Compare
src/Symfony/Component/Security/Core/Authorization/Voter/ClosureVoter.php
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Authorization/AuthorizationCheckerInterface.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Tests/Authorization/Voter/ClosureVoterTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Tests/Authorization/Voter/ClosureVoterTest.php
Outdated
Show resolved
Hide resolved
f3cfa81
to
30f3f4e
Compare
...y/Component/Security/Http/Tests/Fixtures/IsGrantedAttributeMethodsWithCallableController.php
Outdated
Show resolved
Hide resolved
54712d5
to
d4bad72
Compare
There's one failure to check (at least on the windows build) |
d4bad72
to
a53efbf
Compare
a53efbf
to
9c31038
Compare
Thank you @alexandre-daubois. |
…kas) This PR was merged into the 7.3 branch. Discussion ---------- [Security] Improve DX of recent additions | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | not really | Deprecations? | no | Issues | - | License | MIT This is a follow up of #48142 and #59150 which were merged recently into 7.3. Summary of the changes: - `UserAuthorizationChecker`, the implementation of the corresponding interface is merged into the existing `AuthorizationChecker`. - `AuthorizationChecker::isGranted()` is made aware of token changed by its new `isGrantedForUser()` method, so that calls to `is_granted()` nested into `is_granted_for_user()` calls will check the provided user, not the logged in one. - Replace the many arguments passed to `IsGranted`'s closures by 1. a new `IsGrantedContext` and 2. the `$subject`. This makes everything simpler, easier to discover, and more extensible. Thanks to the previous item, IsGrantedContext only needs the auth-checker, not the access-decision-manager anymore. Simpler again. - Fix to the tests, those were broken in both PRs. Commits ------- aa38eb8 [Security] Improve DX of recent additions
Thanks to the latest RFC that successfully passed for the next version of PHP, closures are now allowed in attributes. Symfony could leverage this at multiple places, especially where the ExpressionLanguage is currently used. What's nice is that, compared to expressions, closures can benefit from typing, autocomplete, etc. Way better for the DX.
This PR propose to leverage this new feature in
#[IsGranted]
, which would allow to write such code: