【问题标题】:Can i print the instance of has_object_permission?我可以打印 has_object_permission 的实例吗?
【发布时间】:2021-12-18 04:28:07
【问题描述】:

我正在尝试使用 django-rest-framework 创建 REST API。我的问题是我可以打印has_object_permission 方法的实例,这样我就可以看到那部分发生了什么。我正在尝试只有对象的所有者才能更新和删除该对象,但现在任何人都可以删除或更新任何对象。请告知除了权限之外是否还有其他方法。我们可以通过序列化程序中的检查来完成所有这些工作吗?如果是,那么请也以示例指导我。我将非常感谢。

class ObjectOwnerPermission(BasePermission):

    message = "This object is expired." # custom error message

    def has_object_permission(self, request, view, obj):
        
        if request.user.is_authenticated:
            return True
        return False

        if obj.author == request.user:
            return True
        return False


class RetrieveUpdateProjectAPIView(generics.RetrieveUpdateAPIView,ObjectOwnerPermission):
    """This endpoint allows for updating a specific Project by passing in the id of the 
Project to update/Retrieve"""
    permissions_classes = [ObjectOwnerPermission]
    queryset = Project.objects.all()
    serializer_class = serializers.ProjectSerializer

class DeleteProjectAPIView(generics.DestroyAPIView,ObjectOwnerPermission):
    """This endpoint allows for deletion of a specific Project from the database"""
    permissions_classes = [ObjectOwnerPermission]
    queryset = Project.objects.all()
    serializer_class = serializers.ProjectSerializer

【问题讨论】:

    标签: django serialization django-rest-framework permissions


    【解决方案1】:

    您的权限不起作用,因为当用户通过身份验证时,您在ObjectOwnerPermission 中返回True,这意味着任何经过身份验证的人都可以通过此权限。

    编辑: 在原来的问题permissionS_classes 中使用了什么而不是permission_classes

    这是我的固定版本:

    class ObjectOwnerPermission(BasePermission):
    
        message = "This object is expired." # custom error message
    
        def has_object_permission(self, request, view, obj):    
            return obj.author == request.user
    
    
    class RetrieveUpdateProjectAPIView(generics.RetrieveUpdateAPIView):
        """This endpoint allows for updating a specific Project by passing in the id of the 
    Project to update/Retrieve"""
        permission_classes = [IsAuthenticated, ObjectOwnerPermission]
        queryset = Project.objects.all()
        serializer_class = serializers.ProjectSerializer
    
    class DeleteProjectAPIView(generics.DestroyAPIView):
        """This endpoint allows for deletion of a specific Project from the database"""
        permission_classes = [IsAuthenticated, ObjectOwnerPermission]
        queryset = Project.objects.all()
        serializer_class = serializers.ProjectSerializer
    
    • 不要从您的视图中的权限类继承 - 它应该只用于permission_classes
    • 如果你想链接你的权限,应该在permission_classes列表中实现
    • 权限类是从左到右读取的,这意味着在你的类之前首先检查IsAuthenticated(在你的类中你确定用户已登录)

    【讨论】:

    • 首先,非常感谢您的时间和回复,我更新了项目中的代码,但任何用户都可以更新或删除任何对象。我猜权限检查不起作用
    • 哦,用了坏词..应该是permission_classes 不是permissionS_classes。我更新了我的答案。
    • 完美兄弟!它工作顺利。我真的很感激。
    • 兄弟,如果任何人都可以检索对象但只有所有者可以更新对象,该怎么办。目前只有所有者才能看到他的项目并进行更新。
    • 您可以在has_object_permission 中检查action.name 并使用destroy updatepartial_update 的当前语句,否则返回True,例如。 if view.action in ('destroy', 'update', 'partial_update'): return obj.author == request.user
    猜你喜欢
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 2011-01-12
    相关资源
    最近更新 更多