繁体   English   中英

Django REST Framework - 每个方法的单独权限

[英]Django REST Framework - Separate permissions per methods

我正在使用 Django REST Framework 编写一个 API,我想知道在使用基于类的视图时是否可以为每个方法指定权限。

如果您正在编写基于函数的视图, 阅读文档我认为这很容易做到,只需在要使用权限保护的视图的函数上使用@permission_classes装饰器。 但是,在将 CBV 与APIView类一起使用时,我看不到这样做的方法,因为我随后使用permission_classes属性指定了完整类的权限,但这将应用于所有类方法( getpostput ...)。

那么,是否可以使用 CBV 编写 API 视图,并为视图类的每个方法指定不同的权限?

权限应用于整个 View 类,但您可以在授权决策中考虑请求的各个方面(例如 GET 或 POST 等方法)。

以内置的IsAuthenticatedOrReadOnly为例:

SAFE_METHODS = ['GET', 'HEAD', 'OPTIONS']

class IsAuthenticatedOrReadOnly(BasePermission):
    """
    The request is authenticated as a user, or is a read-only request.
    """

    def has_permission(self, request, view):
        if (request.method in SAFE_METHODS or
            request.user and
            request.user.is_authenticated()):
            return True
        return False

我在使用 CBV 时遇到了同样的问题,因为我有相当复杂的权限逻辑,具体取决于请求方法。

我想出的解决方案是使用本页底部列出的第三方“rest_condition”应用程序

http://www.django-rest-framework.org/api-guide/permissions

https://github.com/caxap/rest_condition

我只是拆分权限流逻辑,以便每个分支都会根据请求方法运行。

from rest_condition import And, Or, Not

class MyClassBasedView(APIView):

    permission_classes = [Or(And(IsReadOnlyRequest, IsAllowedRetrieveThis, IsAllowedRetrieveThat),
                             And(IsPostRequest, IsAllowedToCreateThis, ...),
                             And(IsPutPatchRequest, ...),
                             And(IsDeleteRequest, ...)]

因此,“或”根据请求方法确定应该运行权限的哪个分支,而“与”包装了与接受的请求方法相关的权限,因此所有权限都必须通过才能被授予。 您还可以在每个流程中混合使用“或”、“与”和“非”来创建更复杂的权限。

运行每个分支的权限类看起来像这样,

class IsReadyOnlyRequest(permissions.BasePermission):

    def has_permission(self, request, view):
        return request.method in permissions.SAFE_METHODS


class IsPostRequest(permissions.BasePermission):

    def has_permission(self, request, view):
        return request.method == "POST"


... #You get the idea

2020 年 3 月 30 日更新:我原来的解决方案只修补了对象权限,而不是请求权限。 我在下面包含了一个更新,以使这项工作也与请求权限一起工作。

我知道这是一个老问题,但我最近遇到了同样的问题并想分享我的解决方案(因为接受的答案并不是我所需要的)。 @GDorn 的回答让我走上了正确的道路,但它只适用于ViewSet因为self.action

我已经解决了它创建自己的装饰器:

def method_permission_classes(classes):
    def decorator(func):
        def decorated_func(self, *args, **kwargs):
            self.permission_classes = classes
            # this call is needed for request permissions
            self.check_permissions(self.request)
            return func(self, *args, **kwargs)
        return decorated_func
    return decorator

我的装饰器没有像内置装饰器那样在函数上设置permission_classes属性,而是包装调用并在被调用的视图实例上设置权限类。 这样,普通的get_permissions()不需要任何更改,因为它只依赖于self.permission_classes

要使用请求权限,我们确实需要从装饰器中调用check_permission() ,因为它最初是在initial()中调用的,所以在修补permission_classes属性之前。

注意通过装饰器设置的权限是唯一调用对象权限的权限,但对于请求权限,它们是类范围权限的补充,因为在调用请求方法之前总是检查这些权限。 如果您只想为每个方法指定所有权限,请在类上设置permission_classes = []

示例用例:

from rest_framework import views, permissions

class MyView(views.APIView):
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)  # used for default APIView endpoints
    queryset = MyModel.objects.all()
    serializer_class = MySerializer


    @method_permission_classes((permissions.IsOwnerOfObject,))  # in addition to IsAuthenticatedOrReadOnly
    def delete(self, request, id):
        instance = self.get_object()  # ...

希望这可以帮助遇到同样问题的人!

我遇到了这个问题,真的很想使用@permission_classes装饰器来标记一些具有特定权限的自定义视图方法。 我最终想出了一个mixin:

class PermissionsPerMethodMixin(object):
    def get_permissions(self):
        """
        Allows overriding default permissions with @permission_classes
        """
        view = getattr(self, self.action)
        if hasattr(view, 'permission_classes'):
            return [permission_class() for permission_class in view.permission_classes]
        return super().get_permissions()

一个示例用例:

from rest_framework.decorators import action, permission_classes  # other imports elided

class MyViewset(PermissionsPerMethodMixin, viewsets.ModelViewSet):
    permission_classes = (IsAuthenticatedOrReadOnly,)  # used for default ViewSet endpoints
    queryset = MyModel.objects.all()
    serializer_class = MySerializer

    @action(detail=False, methods=['get'])
    @permission_classes((IsAuthenticated,))  # overrides IsAuthenticatedOrReadOnly
    def search(self, request):
        return do_search(request)  # ...

如果您使用ViewSetModelViewSet ,我认为覆盖get_permissions就可以了。
看看djoser如何处理这个问题。

例子:

class UserViewSet(viewsets.ModelViewSet):
    permission_classes = settings.PERMISSIONS.user  # default

    def get_permissions(self):
        if self.action == "activation":  # per action
            self.permission_classes = settings.PERMISSIONS.activation
        return super().get_permissions()

    @action(["post"], detail=False)  # action
    def activation(self, request, *args, **kwargs):
        pass

    

这个问题是关于APIView实例的,但是对于任何登陆这里的人来说,使用@action中的ViewSets装饰器寻找每个方法的权限覆盖:


class SandwichViewSet(ModelViewSet):
  permission_classes = [IsAuthenticated]

  @action(..., permission_classes=[CanSeeIngredients])
  def retrieve__ingredients(self, request):
    ...

我遇到了类似的问题。

我想允许未经身份验证的 POST,但不允许未经身份验证的 GET。

未经身份验证的公众成员可以提交项目,但只有经过身份验证的管理员用户才能检索提交的项目列表。

所以我为 POST 构建了一个自定义权限类 - UnauthenticatedPost - 然后将权限类列表设置为IsAuthentictaedUnauthenticatedPost

注意我只允许通过设置允许的方法来获取和发布http_method_names = ['get', 'post']

from django.http import HttpResponse
from rest_framework import viewsets
from rest_framework.permissions import BasePermission, IsAuthenticated
from MyAPI.serializers import MyAPISerializer
from MyAPI.models import MyAPI


class UnauthenticatedPost(BasePermission):
    def has_permission(self, request, view):
        return request.method in ['POST']


class MyAPIViewSet(viewsets.ModelViewSet):
    permission_classes = [IsAuthenticated|UnauthenticatedPost]
    queryset = MyAPI.objects.all().order_by('-TimeSubmitted')
    serializer_class = MyAPISerializer
    http_method_names = ['get', 'post']

在 GET、PUT 和 POST 的不同权限方面,我们遇到了同样的挑战,并且使用定制的权限类解决了这个问题:

from rest_framework import permissions

class HasRequiredPermissionForMethod(permissions.BasePermission):
    get_permission_required = None
    put_permission_required = None
    post_permission_required = None

    def has_permission(self, request, view):
        permission_required_name = f'{request.method.lower()}_permission_required'
        if not request.user.is_authenticated:
            return False
        if not hasattr(view, permission_required_name):
            view_name = view.__class__.__name__
            self.message = f'IMPLEMENTATION ERROR: Please add the {permission_required_name} variable in the API view class: {view_name}.'
            return False

        permission_required = getattr(view, permission_required_name)
        if not request.user.has_perm(permission_required):
            self.message = f'Access denied. You need the {permission_required} permission to access this service with {request.method}.'
            return False

        return True

我们在我们的 API 中使用它,如下所示:

class MyAPIView(APIView):
    permission_classes = [HasRequiredPermissionForMethod]
    get_permission_required = 'permission_to_read_this'
    put_permission_required = 'permission_to_update_this'
    post_permission_required = 'permission_to_create_this'

    def get(self, request):
        # impl get

    def put(self, request):
        # impl put

    def post(self, request):
        # impl post

我写下我的解决方案,希望它对某人有所帮助。
据我了解,APIView 类有两种处理权限的方法

  1. APIView.permission_classes静态分配适当的Permission类(如扩展BasePermission
  2. 动态决定APIView中的Permission实例(覆盖APIView.get_permission()

APIView检查从.get_permission()返回的权限。
并且.get_permission().permission_classes实例化Permission

在我的情况下,只有我需要一个预定义的Permission ,但取决于方法。 所以我选择了后一种方法。

class TokenView(APIView):
    authentication_classes = [TokenAuthentication]
    
    // return instances of Permission classes
    def get_permissions(self, *args, **kwargs):
        if self.request.method in ['DELETE']:
            return [IsAuthenticated()]
        else:
            return []

    def post(self, request, *args, **kwargs):
        username = request.data["username"]
        password = request.data["password"]
        user = authenticate(username=username, password=password)
        token, created = Token.objects.get_or_create(user=user)
        return Response({"token": token.key}, status=status.HTTP_200_OK)

    def delete(self, request, *args, **kwargs):
        user = request.user
        user.auth_token.delete()
        return Response({"success", status.HTTP_200_OK})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM