-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Closed
Description
I am receiving unintended results when implementing a composed permission set. I am using several simple custom permissions which I have included below for reference.
The following composition works as expected:
permission_classes = [IsAuthenticated & (IsPublished | IsOwner)]
However, when I add a third OR condition the result is not as expected:
permission_classes = [IsAuthenticated & (IsPublished | IsOwner | IsAdmin)]
In a case where a user is authenticated, not the owner, not an admin, and the object is not published the composition returns true. I would expect True & (False | False | False) == False. My apologies if I am not utilizing the compose operations correctly.
Any insight is appreciated.
Django v2.1.5
DRF: v3.9.2
#permissions.py
from rest_framework import permissions
class IsAuthenticated(permissions.BasePermission):
def has_permission(self, request, view):
return bool(request.user and request.user.is_authenticated)
class IsAdmin(permissions.BasePermission):
def has_permission(self, request, view):
return bool(request.user and request.user.is_superuser)
class IsOwner(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
owner = getattr(obj, 'owner', None)
return bool(request.user and request.user == owner)
class IsPublished(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
status = getattr(obj, 'status', None)
return bool(status == 'published')
Metadata
Metadata
Assignees
Labels
No labels