How to get OR permissions instead of AND in REST framework

前端 未结 6 1419
遇见更好的自我
遇见更好的自我 2021-02-05 06:45

It seems that permission classes are ANDed when REST framework checks permissions. That is every permission class needs to return True for permission to be granted. This makes t

6条回答
  •  滥情空心
    2021-02-05 07:11

    One way would be to add another permission class which combines existing classes the way you want it, e.g.:

    class IsAdmin(BasePermission):
        """Allow access to admins"""
        def has_object_permission(self, request, view, obj):
            return request.user.is_admin()
    
    
    class IsOwner(BasePermission):
        """Allow access to owners"""
        def has_object_permission(self, request, view, obj):
            request.user.is_owner(obj)
    
    
    class IsAdminOrOwner(BasePermission):
        """Allow access to admins and owners"""  
        def has_object_permission(*args):
            return (IsAdmin.has_object_permission(*args) or
                    IsOwner.has_object_permission(*args))
    

提交回复
热议问题