Django - 如何在 DRF 中向某些用户授予自定义权限?

贝曼

我想授予一些用户检索访问权限,一些用户更新访问权限,并且不授予未经身份验证的用户对我的 DRF API 的检索/更新访问权限。
在我的扩展用户模型中,我有两个字段定义是否应允许用户检索或更新 API。我应该如何在我的 DRF 自定义权限类中编写逻辑来检查这两个字段并根据 True 或 False 授予检索或更新权限?我应该为此使用 ViewSet 还是将单独的 ListAPIView、RetrieveAPIView 和 UpdateAPIView 类与 Mixins 一起使用?做这个的最好方式是什么?

模型.py

class UserProfile(models.Model):  
    user = models.OneToOneField(User)
    allowRetrieveAPI = models.BooleanField(default=False,)
    allowUpdateAPI = models.BooleanField(default=False,)

class Track(models.Model):    
    user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL, verbose_name="Submitted by", default=1)
    artist = models.CharField(max_length=100,)
    title = models.CharField(max_length=100,)

视图.py

class CheckAPIPermissions(permissions.BasePermission):
    # allow retrieve if userprofile.allowReadAPI is True
    # allow update if user userprofile.allowUpdateAPI is True  

    def has_permission(self, request, view):
         # return something
    def check_object_permission(self, user, obj):
         # return something      
    def has_object_permission(self, request, view, obj):
         # return something

class TrackViewSet(viewsets.ModelViewSet):
    queryset = Track.objects.all()
    serializer_class = TrackSerializer
    permission_classes = (CheckAPIPermissions,)
扎伊法齐尔
class CheckAPIPermissions(permissions.BasePermission): 
    # allow retrieve if userprofile.allowReadAPI is True 
    # allow update if user userprofile.allowUpdateAPI is True 

    def has_permission(self, request, view): 
        if request.user.is_superuser:
            return True
        elif request.user and request.user.is_authenticated():
            if (request.user.userprofile.allowRetrieveAPI or request.user.userprofile.allowUpdateAPI) and view.action == 'retrieve':
                return True
            elif request.user.userprofile.allowUpdateAPI and view.action == 'update':
                return True
        return False

    def check_object_permission(self, user, obj): 
        return (user and user.is_authenticated() and (user.is_staff or obj == user)) 


    def has_object_permission(self, request, view, obj): 
        if request.user.is_superuser:
            return True
        elif request.user and request.user.is_authenticated():
            if (request.user.userprofile.allowRetrieveAPI or request.user.userprofile.allowUpdateAPI) and view.action == 'retrieve':
                return request.user == obj
            elif request.user.userprofile.allowUpdateAPI and view.action == 'update':
                return request.user == obj
        return False

我还没有测试它,只是在很短的时间内写的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Django - 如何在 DRF 中创建自定义权限以限制用户进行未经授权的 API 调用?

如何在 django 中自定义权限(不在 DRF 中)?

如何在Django中处理PUT请求?(不在drf中)

如何在Django Rest Framework(DRF)中覆盖Response类?

如何在 django DRF 中处理时区而不重复太多?

Django 2.x drf-yasg如何在自定义方法中创建API(如在swagger中)

如何在Django Rest Framework(DRF)中为ModelSerializers编写自定义字段验证,类似于Django中的表单验证?

Django DRF创建用户

如何在DRF中序列化自定义用户模型

DRF Django中的DateField

如何在DRF JWT视图上设置自定义权限类?

如何在generics.ListAPIView中编写功能,可以在Django DRF中的APIView中编写

如何在django drf中检查请求正文中的int值

如何在DRF(Django Rest Framework)generic.ListView中验证url变量?

应该如何在Django中实现基于JWT的身份验证(drf和simplejwt)?

Django DRF:自定义权限:tokenauthentication:为什么要使用 isAuthenticated 获取权限

Django DRF角色级别权限

如何检查请求的用户在 Django Rest Framework 中是否具有特定的自定义权限?

如何在 django 中创建一个可以创建另一个用户但不能授予权限(仅授予预定义组权限)的用户?

DRF自定义权限

如何在Django DRF的GenericAPIView中正确使用queryset?

DRF Django:如何在保存前修改值

如何在DRF中使用django-filter

如何在DRF文档中描述参数

如何在 DRF 中创建发布请求

DRF - django_filters - 使用自定义方法

如何在Django中为群组添加自定义权限?

Django DRF序列化程序自定义关系字段如何构建to_internal_value的返回值?

如何在Django Admin中向更改模型表单添加自定义操作?