Python 如何获取用户权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16573174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:03:00  来源:igfitidea点击:

How to get user permissions?

pythondjangodjango-authentication

提问by Nips

I want to retrieve all permission for user as list of premission id's but:

我想检索用户的所有权限作为权限 ID 列表,但是:

user.get_all_permissions()

give me list of permission names. How to do it?

给我权限名称列表。怎么做?

采纳答案by Paulo Bu

The key is get the permission objects like this:

关键是获取这样的权限对象:

from django.contrib.auth.models import Permission
permissions = Permission.objects.filter(user=user)

and there you can access the idproperty like this:

在那里您可以id像这样访问该属性:

permissions[0].id

If you want the list (id, permission_name)do the following:

如果您想要列表,(id, permission_name)请执行以下操作:

perm_tuple = [(x.id, x.name) for x in Permission.objects.filter(user=user)]

Hope it helps!

希望能帮助到你!

回答by DRC

to get all the permissions of a given user, also the permissions associated with a group this user is part of:

获取给定用户的所有权限,以及与该用户所属的组关联的权限:

from django.contrib.auth.models import Permission

def get_user_permissions(user):
    if user.is_superuser:
        return Permission.objects.all()
    return user.user_permissions.all() | Permission.objects.filter(group__user=user)

回答by Shihabudheen K M

we can get user permission from user objects directly into a list like this

我们可以从用户对象中直接获取用户权限到这样的列表中

perm_list = user_obj.user_permissions.all().values_list('codename', flat=True)

Try this....

尝试这个....

回答by Moritz

This is an routine to query for the Permission objects returned by user.get_all_permissions()in a single query.

这是一个例程,用于查询user.get_all_permissions()单个查询中返回的 Permission 对象。

from functools import reduce
from operator import or_
from django.db.models import Q
from django.contrib.auth.models import Permission

def get_user_permission_objects(user):
    user_permission_strings = user.get_all_permissions()
    if len(user_permission_strings) > 0:
        perm_comps = [perm_string.split('.', 1) for perm_string in user_permission_strings]
        q_query = reduce(
            or_,
            [Q(content_type__app_label=app_label) & Q(codename=codename) for app_label, codename in perm_comps]
        )
        return Permission.objects.filter(q_query)
    else:
        return Permission.objects.none()

Alternatively, querying Permission directly:

或者,直接查询权限:

from django.db.models import Q
from django.contrib.auth.models import Permission

def get_user_permission_objects(user):
    if user.is_superuser:
        return Permission.objects.all()
    else:
        return Permission.objects.filter(Q(user=user) | Q(group__user=user)).distinct()


回答by Amey Dhuri

from django.contrib.auth.models import Permission
permissions = Permission.objects.filter(user=user)

permissions[0].id