基于用户配置文件模型归档角色的自定义用户权限 [英] Custom user permission based on User Profile Model Filed Role

查看:117
本文介绍了基于用户配置文件模型归档角色的自定义用户权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django休息框架创建一个Todo列表应用程序。



在这个应用程序中,只有经理可以在列表中发布任务。
用户个人资料有一个名为角色的字段。
我有一个扩展用户模型的用户配置文件模型。



model.py

 class UserProfile(models.Model):
user = models.OneToOneField(User,unique = False)
department = models.CharField(max_length = 50,choices = DEPARTMENT_CHOICES, ='technology')
role = models.CharField(max_length = 50,choices = ROLE_CHOICES,default ='manager')

用户个人资料有一个角色字段。



我希望唯一的经理可以在我的应用程序中发布任务。



为了达到这个目的,我如何编写自定义用户权限?
仅限于POST请求。所有其他请求都可以被允许。

解决方案

您将创建一个权限,它将子类 rest_framework.permissions.IsAuthenticated 并将您的逻辑添加到 has_permission(self,request,view)查看更多)。



如果你只想要应用于 POST ,只需检查请求的方法并返回 True ,如果它是一种不同的方法。如下所示:

 从rest_framework导入权限

class CustomPermission(permissions.IsAuthenticated):

def has_permission(self,request,view):
如果request.user.user_profile.role =='ADMIN'或request.method!='POST':
return True

返回False

不要忘记在您的视图的 permission_classes



PS:尽管如此,我应该提醒你,只允许管理员POST任务,同时允许每个人都可以使用PUT并删除它们。也许你的意思是想让所有人只能使用 safe_methods (GET,OPTIONS和HEAD)?如果是这种情况,请将$ code> request.method!='POST'与 request.method替换为permissions.SAFE_METHODS 。 / p>

i am creating a Todo list app using Django rest framework.

in this app only manager can post task in the list. User Profile has a field named as a role. I have a User Profile Model with extended User Model.

model.py

class UserProfile(models.Model):
    user = models.OneToOneField(User,unique=False)
    department = models.CharField(max_length=50, choices=DEPARTMENT_CHOICES,default='technology')
    role = models.CharField(max_length=50, choices=ROLE_CHOICES,default='manager')

User Profile have a role field.

I want the only manager can post Task in my app.

How can I write custom user permission in order to achieve this? Restricted to POST request only. All other requests can be permitted.

解决方案

You would create a permission which subclasses rest_framework.permissions.IsAuthenticated and add your logic in has_permission(self, request, view) (see more).

If you only want it to be applied to POST, simply check the request's method and return True if it's a different method. Something like:

from rest_framework import permissions

class CustomPermission(permissions.IsAuthenticated):

    def has_permission(self, request, view):
        if request.user.user_profile.role == 'ADMIN' or request.method != 'POST':
            return True

        return False

Don't forget to include this permission in your view's permission_classes.

PS: I should warn you, though, it's a bit odd that you'd allow only admins to POST tasks while allowing everyone to PUT and DELETE on them. Maybe you mean you want to allow everyone in safe_methods only (GET, OPTIONS and HEAD)? If that's the case, replace request.method != 'POST' with request.method in permissions.SAFE_METHODS.

这篇关于基于用户配置文件模型归档角色的自定义用户权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆