Skip to content

Permissions: Add ~ operator to permissions composition #6361

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
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api-guide/permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Provided they inherit from `rest_framework.permissions.BasePermission`, permissi
}
return Response(content)

__Note:__ it only supports & -and- and | -or-.
__Note:__ it supports & (and), | (or) and ~ (not).

---

Expand Down
24 changes: 24 additions & 0 deletions rest_framework/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ def __rand__(self, other):
def __ror__(self, other):
return OperandHolder(OR, other, self)

def __invert__(self):
return SingleOperandHolder(NOT, self)


class SingleOperandHolder(OperationHolderMixin):
def __init__(self, operator_class, op1_class):
self.operator_class = operator_class
self.op1_class = op1_class

def __call__(self, *args, **kwargs):
op1 = self.op1_class(*args, **kwargs)
return self.operator_class(op1)


class OperandHolder(OperationHolderMixin):
def __init__(self, operator_class, op1_class, op2_class):
Expand Down Expand Up @@ -73,6 +86,17 @@ def has_object_permission(self, request, view, obj):
)


class NOT:
def __init__(self, op1):
self.op1 = op1

def has_permission(self, request, view):
return not self.op1.has_permission(request, view)

def has_object_permission(self, request, view, obj):
return not self.op1.has_object_permission(request, view, obj)


class BasePermissionMetaclass(OperationHolderMixin, type):
pass

Expand Down
25 changes: 24 additions & 1 deletion tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,19 @@ def test_or_true(self):
composed_perm = permissions.IsAuthenticated | permissions.AllowAny
assert composed_perm().has_permission(request, None) is True

def test_several_levels(self):
def test_not_false(self):
request = factory.get('/1', format='json')
request.user = AnonymousUser()
composed_perm = ~permissions.IsAuthenticated
assert composed_perm().has_permission(request, None) is True

def test_not_true(self):
request = factory.get('/1', format='json')
request.user = self.user
composed_perm = ~permissions.AllowAny
assert composed_perm().has_permission(request, None) is False

def test_several_levels_without_negation(self):
request = factory.get('/1', format='json')
request.user = self.user
composed_perm = (
Expand All @@ -590,6 +602,17 @@ def test_several_levels(self):
)
assert composed_perm().has_permission(request, None) is True

def test_several_levels_and_precedence_with_negation(self):
request = factory.get('/1', format='json')
request.user = self.user
composed_perm = (
permissions.IsAuthenticated &
~ permissions.IsAdminUser &
permissions.IsAuthenticated &
~(permissions.IsAdminUser & permissions.IsAdminUser)
)
assert composed_perm().has_permission(request, None) is True

def test_several_levels_and_precedence(self):
request = factory.get('/1', format='json')
request.user = self.user
Expand Down