32

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

user.get_all_permissions()

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

4

7 回答 7

43

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)
于 2014-12-23T16:31:46.630 回答
24

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 id property like this:

permissions[0].id

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

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

Hope it helps!

于 2013-05-15T19:47:36.377 回答
9

If you are using Django 3.0+, user.get_user_permissions() gives the codename of all the permissions.

More information here: https://docs.djangoproject.com/en/3.0/ref/contrib/auth/#django.contrib.auth.models.User.get_user_permissions

于 2020-06-09T20:04:55.810 回答
8

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....

于 2018-03-19T09:15:21.050 回答
4

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

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()


于 2019-05-13T15:12:11.653 回答
1
from django.contrib.auth.models import Permission
permissions = Permission.objects.filter(user=user)

permissions[0].id
于 2019-05-14T11:00:57.437 回答
0

Returns the set of permission strings the user_obj has, including both user permissions and group permissions. Returns an empty set if is_anonymous or is_active is False.

user_permissions_list = request.user.get_all_permissions()

Returns the set of permission strings the user_obj has from the permissions of the groups they belong. Returns an empty set if is_anonymous or is_active is False.

request.user.get_group_permissions()

Returns the set of permission strings the user_obj has from their own user permissions. Returns an empty set if is_anonymous or is_active is False.

request.user.get_user_permissions()

Unfortunately these built in method give me duplicate queries.

Or you can use your own filter from permission tables to get all permissions of current user

user_permissions_list = list(Permission.objects.filter(Q(user=request.user) | Q(group__user=request.user)).values_list('codename', flat=True))

Doc

于 2021-11-13T10:50:02.080 回答