Milano
Milano

Reputation: 18705

Django-guardian has_perm("codename", obj) is False even if group has the permission

I'm trying to implement django-guardian permissions in my project. In the TestCase below, I assigned view_income permission to staff group. That means, also user_c has the permission.

The problem is that self.user_c.has_perm("view_income", income) returns False.

To be clear, I understand how it works and that I can check first the group permission without object. I'm curious if there is a shortcut that will tell me whether a given user has a permission to a given object no matter if it is defined in a group, if it is an object permission or general permission etc...

I don't want to write multiple different checks every time I want to check permissions.

class IncomePermissionsTestCase(TestCase):
    def setUp(self):
        self.user_a = baker.make(settings.AUTH_USER_MODEL)
        self.user_c = baker.make(settings.AUTH_USER_MODEL)
        staff_group:Group = baker.make('Group', name='staff')
        assign_perm('view_income', staff_group)
        self.user_c.groups.add(staff_group)

    def test_permissions(self):
        income = baker.make("clients.Income", client=self.user_a)
        self.assertTrue(self.user_a.has_perm("view_income", income))
        self.assertTrue(self.user_c.has_perm("view_income", income))

Upvotes: 1

Views: 65

Answers (0)

Related Questions