-
-
Notifications
You must be signed in to change notification settings - Fork 565
feat: support union operator on BasePermission #3315
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
base: main
Are you sure you want to change the base?
Conversation
Thanks for adding the Here's a preview of the changelog: Permissions can now be combined with the import strawberry
from strawberry.permission import PermissionExtension
@strawberry.type
class User:
@strawberry.field(
extensions=[
PermissionExtension(
# require auth AND (node is current user OR current user is staff)
permissions=[IsAuthenticated(), IsExactUser() | IsStaff()]
)
]
)
def ssn(self) -> str:
return "555-55-5555" Here's the tweet text:
|
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 looks like a great usability addition. I will conduct more thorough testing later, but for now, can you please add the following test cases so we don't accidentally break something later:
- two resolvers use the same OR filter -> directive duplication doesn't cause an error
- permutation of entries in the OR filter in two separate resolve (My preference is to use the same directive in this case and not create two separate directives, i.e. always
AorB
instead ofAorB
andBorA
. We can sort this out by ordering the permission names lexicographically using sort)
How should we handle multiple ORs that are chained? Should the or-filter flat map them into a single filter ,or should there be nested OR-filters? In this case, there should also be test cases on schema generation
- More than 2 permissions disjuncted, (
AorBorC
)
@erikwrede all sounds good! i'll have time to come back to this in the next few days
nesting is my instinct, because it seems supporting other operators would be more straightforward, but i'll think about it more and look at DRF +others again |
@replygirl the only problem I see with nesting is the handling of schema directives. Maybe they need to be flatMapped in this case, but this should be easy to handle since only the outermost OR-extension will be respected. I'm open to any solutions, so just go with what works best for the next draft 😊 |
CodSpeed Performance ReportMerging #3315 will not alter performanceComparing Summary
|
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #3315 +/- ##
==========================================
- Coverage 96.61% 94.80% -1.82%
==========================================
Files 483 482 -1
Lines 30161 30187 +26
Branches 3726 3728 +2
==========================================
- Hits 29140 28618 -522
- Misses 832 1364 +532
- Partials 189 205 +16 |
Description
👉 tl;dr: Lets you use
|
between permission instances, e.g.is_cool_color = IsBlue() | IsGreen()
BasePermission.__or__
instance method, which returns anOrPermission
instanceOrPermission
, inheriting fromBasePermission
__init__
takes a tuple ofBasePermission
instances to be evaluatedhas_permission
returnsTrue
as long ashas_permission
returnsTrue
from any of its input permissionsmessage
andon_unauthorized
refer to the last failed permissionTypes of Changes
Issues Fixed or Closed by This PR
Checklist