Skip to content

Commit

Permalink
Modify assign_perm shortcut allowing lists
Browse files Browse the repository at this point in the history
Modify the 'assign_perm' shortcut function to allow lists of objects to
be passed in
  • Loading branch information
DavisRayM authored and ad-m committed Apr 14, 2020
1 parent b338092 commit 8e8dab2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ Authors ordered by first contribution
- Jeff Hackshaw <intrepidevio@gmail.com>
- Chase Bennett <ch@se.gd>
- Jonny Arnold <jonny.arnold89@gmail.com>
- Davis Raymond Muro <davisraymondmuro@gmail.com>
12 changes: 7 additions & 5 deletions guardian/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def assign_perm(perm, user_or_group, obj=None):
``guardian.exceptions.NotUserNorGroup`` exception
:param obj: persisted Django's ``Model`` instance or QuerySet of Django
``Model`` instances or ``None`` if assigning global permission.
Default is ``None``.
``Model`` instances or list of Django ``Model`` instances or ``None``
if assigning global permission. Default is ``None``.
We can assign permission for ``Model`` instance for specific user:
Expand Down Expand Up @@ -101,14 +101,16 @@ def assign_perm(perm, user_or_group, obj=None):
if '.' in perm:
app_label, perm = perm.split(".", 1)

if isinstance(obj, QuerySet):
if isinstance(obj, (QuerySet, list)):
if isinstance(user_or_group, (QuerySet, list)):
raise MultipleIdentityAndObjectError("Only bulk operations on either users/groups OR objects supported")
if user:
model = get_user_obj_perms_model(obj.model)
model = get_user_obj_perms_model(
obj[0] if isinstance(obj, list) else obj.model)
return model.objects.bulk_assign_perm(perm, user, obj)
if group:
model = get_group_obj_perms_model(obj.model)
model = get_group_obj_perms_model(
obj[0] if isinstance(obj, list) else obj.model)
return model.objects.bulk_assign_perm(perm, group, obj)

if isinstance(user_or_group, (QuerySet, list)):
Expand Down
1 change: 1 addition & 0 deletions guardian/testapp/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def setUp(self):
model='bar', app_label='fake-for-guardian-tests')
self.ctype_qset = ContentType.objects.filter(model='bar',
app_label='fake-for-guardian-tests')
self.ctype_list = [self.ctype_qset.first()]
self.anonymous_user = User.objects.get(
username=guardian_settings.ANONYMOUS_USER_NAME)

Expand Down
28 changes: 28 additions & 0 deletions guardian/testapp/tests/test_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,34 @@ def test_deprecation_warning(self):
self.assertEqual(len(warns), 1)
self.assertTrue(isinstance(warns[0].message, DeprecationWarning))

def test_user_assign_perm_list(self):
"""
Test that one is able to assign permissions for a list of objects
to a user
"""
assign_perm("add_contenttype", self.user, self.ctype_list)
assign_perm("change_contenttype", self.group, self.ctype_list)
assign_perm(self.get_permission("delete_contenttype"), self.user, self.ctype_list)
for obj in self.ctype_list:
self.assertTrue(self.user.has_perm("add_contenttype", obj))
self.assertTrue(self.user.has_perm("change_contenttype", obj))
self.assertTrue(self.user.has_perm("delete_contenttype", obj))

def test_group_assign_perm_list(self):
"""
Test that one is able to assign permissions for a list of
objects to a group
"""
assign_perm("add_contenttype", self.group, self.ctype_list)
assign_perm("change_contenttype", self.group, self.ctype_list)
assign_perm(self.get_permission("delete_contenttype"), self.group, self.ctype_list)

check = ObjectPermissionChecker(self.group)
for obj in self.ctype_list:
self.assertTrue(check.has_perm("add_contenttype", obj))
self.assertTrue(check.has_perm("change_contenttype", obj))
self.assertTrue(check.has_perm("delete_contenttype", obj))


class MultipleIdentitiesOperationsTest(ObjectPermissionTestCase):
"""
Expand Down

0 comments on commit 8e8dab2

Please sign in to comment.