【问题标题】:Django Rest Framework custom permissions per view每个视图的 Django Rest Framework 自定义权限
【发布时间】:2014-07-06 04:19:28
【问题描述】:

我想在Django Rest Framework中创建权限,基于视图+方法+用户权限。

有没有办法实现这一点,而无需手动编写每个权限,并检查用户所在组的权限?

另外,我面临的另一个问题是权限对象与某个模型相关联。由于我有影响不同模型的视图,或者我想授予 PUT 方法的不同权限,具体取决于我访问的视图(因为它影响不同的字段),我希望我的权限绑定到某个视图,而不是某个型号。

有谁知道如何做到这一点?

我正在寻找以下解决方案:

  1. 使用以下参数创建一个 Permissions 对象:View_affectedlist_of_allowed_methods(GET,POST,etc.)

  2. 创建一个关联该权限的组对象

  3. 将用户添加到组

  4. 让我的默认权限类处理所有事情。

从我现在的情况来看,给我带来问题的步骤是步骤 1。因为我看不到将权限与视图绑定的方法,并且因为权限要求模型,而我不想要模型。

【问题讨论】:

    标签: permissions django-rest-framework


    【解决方案1】:

    我接受了这个想法并让它像这样工作:

    class genericPermissionCheck(permissions.BasePermission):
        
        def __init__(self, action, entity):
            self.action = action
            self.entity = entity
        
        def has_permission(self, request, view):
            print self.action
            print self.entity
            if request.user and request.user.role.access_rights.filter(action=self.action,entity=self.entity):
                print 'permission granted'            
                return True
            else:
                return False
    

    我在视图集类中的类别操作中部分使用了装饰器,如下所示:

        @list_route(methods=['get'],permission_classes=[partial(genericPermissionCheck,'Fetch','Categories')])
        def Categories(self, request):
    

    "access_rights" 映射到具有一对动作和对象的对象数组,例如“编辑”和“博客”

    【讨论】:

      【解决方案2】:

      嗯,第一步可以通过 DRF 轻松完成。见http://www.django-rest-framework.org/api-guide/permissions#custom-permissions

      你必须这样做:

      from functools import partial
      
      from rest_framework import permissions
      
      class MyPermission(permissions.BasePermission):
      
          def __init__(self, allowed_methods):
              super().__init__()
              self.allowed_methods = allowed_methods
      
          def has_permission(self, request, view):
              return request.method in self.allowed_methods
      
      
      class ExampleView(APIView):
          permission_classes = (partial(MyPermission, ['GET', 'HEAD']),)
      

      【讨论】:

        【解决方案3】:

        可以通过这种方式创建自定义权限,更多信息在官方文档中(https://www.django-rest-framework.org/api-guide/permissions/):

        from rest_framework.permissions import BasePermission
        
        
        # Custom permission for users with "is_active" = True.
        class IsActive(BasePermission):
            """
            Allows access only to "is_active" users.
            """
            def has_permission(self, request, view):
                return request.user and request.user.is_active
        
        # Usage
        from rest_framework.views import APIView
        from rest_framework.response import Response
        
        from .permissions import IsActive   # Path to our custom permission
        
        class ExampleView(APIView):
            permission_classes = (IsActive,)
        
            def get(self, request, format=None):
                content = {
                    'status': 'request was permitted'
                }
                return Response(content)
        

        【讨论】:

          猜你喜欢
          • 2013-10-19
          • 2020-09-16
          • 1970-01-01
          • 1970-01-01
          • 2015-08-18
          • 2020-12-18
          • 2014-01-22
          • 2021-06-18
          • 2020-02-02
          相关资源
          最近更新 更多