【问题标题】:DRF permission not taking effectDRF 权限未生效
【发布时间】:2014-08-04 16:47:28
【问题描述】:

以下权限不生效IsOwnerOrReadOnly我不明白为什么:

class PermissionMixin(object):
    """
    API Permission Mixin.
    Permission checks authentication information in the request.user and request.auth
    properties to determine if the incoming request should be permitted.
    """

    permission_classes = [Or(permissions.IsAdminUser, TokenHasReadWriteScope), And (IsOwnerOrReadOnly)]

我希望允许 IsAdminUser 或 TokenHasReadWriteScope 用户,但始终检查他们是所有者 IsOwnerOrReadOnly

class IsOwnerOrReadOnly(permissions.BasePermission):
    """
    Custom permission to only allow owners of an object to edit it.
    """

    def has_object_permission(self, request, view, obj):
        # Read permissions are allowed to any request,
        # so we'll always allow GET, HEAD or OPTIONS requests.
        if request.method in permissions.SAFE_METHODS:
            return True

        # Write permissions are only allowed to the owner of object.
        return obj.user == request.user

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    我认为设置权限的正确方法是:

    permission_classes = [And(Or(permissions.IsAdminUser, TokenHasReadWriteScope), IsOwnerOrReadOnly)]
    

    告诉我这是否有效。

    【讨论】:

    • 您好,感谢您的回复。我测试了你的代码,它仍然没有运行 IsOwnerOrReadOnly 函数。我在类中添加了一个打印语句,它只是没有运行。但是,admin 和 token 可以。还有什么想法吗? :)
    • 看来您的回答确实有效!!!!问题在于我将 mixin 添加到视图中的顺序。谢谢你的回答。
    猜你喜欢
    • 2021-04-30
    • 2023-03-08
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 2021-03-30
    • 1970-01-01
    • 2021-08-03
    相关资源
    最近更新 更多