How can I get all users an object has specific permissions to in django guardian?
Asked Answered
C

4

7

There is

from guardian.shortcuts import get_objects_for_user

But what about

from guardian.shortcuts import get_users_for_object

Thanks.

Charmain answered 28/2, 2013 at 14:35 Comment(0)
G
2

This is my workaround. This is Model method. You can encapsulate it in mixin.

def get_users_with_perm(self, permission):
    '''
    Returns list of users(worn:not QuerySet) with specific permission for this object
    :param permission: permission string
    '''

    anyperm = get_users_with_perms(self, attach_perms=True)
    result = []
    for user, perms in anyperm.iteritems():
        if permission in perms: result.append(user)
    return result
Galileo answered 23/5, 2014 at 10:44 Comment(0)
R
9

Actually there is a shortcut for that

http://django-guardian.readthedocs.io/en/stable/api/guardian.shortcuts.html#get-users-with-perms

>>> from django.contrib.flatpages.models import FlatPage
>>> from django.contrib.auth.models import User
>>> from guardian.shortcuts import assign_perm, get_users_with_perms
>>>
>>> page = FlatPage.objects.create(title='Some page', path='/some/page/')
>>> joe = User.objects.create_user('joe', '[email protected]', 'joesecret')
>>> assign_perm('change_flatpage', joe, page)
>>>
>>> get_users_with_perms(page)
[<User: joe>]
>>>
>>> get_users_with_perms(page, attach_perms=True)
{<User: joe>: [u'change_flatpage']}
Rigel answered 20/6, 2017 at 22:2 Comment(0)
G
2

This is my workaround. This is Model method. You can encapsulate it in mixin.

def get_users_with_perm(self, permission):
    '''
    Returns list of users(worn:not QuerySet) with specific permission for this object
    :param permission: permission string
    '''

    anyperm = get_users_with_perms(self, attach_perms=True)
    result = []
    for user, perms in anyperm.iteritems():
        if permission in perms: result.append(user)
    return result
Galileo answered 23/5, 2014 at 10:44 Comment(0)
C
1

Something like that looks good: https://github.com/lukaszb/django-guardian/blob/master/guardian/shortcuts.py#L214

    >>> from django.contrib.flatpages.models import FlatPage
    >>> from django.contrib.auth.models import User
    >>> from guardian.shortcuts import assign_perm, get_users_with_perms
    >>>
    >>> page = FlatPage.objects.create(title='Some page', path='/some/page/')
    >>> joe = User.objects.create_user('joe', '[email protected]', 'joesecret')
    >>> assign_perm('change_flatpage', joe, page)
    >>>
    >>> get_users_with_perms(page)
    [<User: joe>]
    >>>
    >>> get_users_with_perms(page, attach_perms=True)
    {<User: joe>: [u'change_flatpage']}
Charmain answered 28/2, 2013 at 14:41 Comment(0)
L
1

I solved this issue using the built in get_users_with_perms() shortcut and then using python dictionary comprehension to return a list of users objects that have a specific permission on the given object.

from django.contrib.flatpages.models import FlatPage
from django.contrib.auth.models import User
from guardian.shortcuts import assign_perm, get_users_with_perms

Create a page object and give User 'Joe' change_flatpage permissions

page = FlatPage.objects.create(title='Some page', path='/some/page/')
joe = User.objects.create_user('joe', '[email protected]', 'joesecret')
assign_perm('change_flatpage', joe, page)

Use Guardian to find users who have any permission to the specific page object

perms = get_users_with_perms(page, attach_perms=True)

Use python dictionary comprehension to return a new list of users who contain specific permission

perm_name = "change_flatpage"
change_flatpage_user_list = [ k for k, v in perms.items() if perm_name in v ]
Livre answered 28/2, 2019 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.