Skip to content

Commit 4a93340

Browse files
authored
Merge pull request django-guardian#931 from django-guardian/930-v313-introduced-bug-in-get_objects_for_user
global perms with uuid hypens bug is fixed
2 parents 4120d00 + 4a915d6 commit 4a93340

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

guardian/shortcuts.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,11 @@ def filter_perms_queryset_by_objects(perms_queryset, objects):
900900
handle_pk_field = _handle_pk_field(objects)
901901
if handle_pk_field is not None:
902902
objects = objects.values(_pk=Cast(handle_pk_field("pk"), output_field=CharField()))
903+
# Apply the same transformation to the object_pk field for consistent comparison (#930)
904+
perms_queryset = perms_queryset.annotate(
905+
_transformed_object_pk=Cast(handle_pk_field(field), output_field=CharField())
906+
)
907+
field = "_transformed_object_pk"
903908
else:
904909
objects = objects.values("pk")
905910
else:

guardian/testapp/tests/test_shortcuts.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,64 @@ def test_uuid_primary_key_with_group_values(self):
10911091
self.assertTrue(isinstance(objects, QuerySet))
10921092
self.assertEqual(set(objects.values_list("pk", flat=True)), {obj_with_uuid_pk.pk})
10931093

1094+
def test_uuid_primary_key_accept_global_perms_false_bug_fix(self):
1095+
"""
1096+
Test for the UUID bug fix where get_objects_for_user with accept_global_perms=False
1097+
fails to match UUID primary keys due to hyphen inconsistency.
1098+
1099+
This test reproduces the bug described in the issue where object_pk field
1100+
(with hyphens) was compared against transformed PKs (without hyphens).
1101+
"""
1102+
# Create multiple UUID objects to test filtering
1103+
obj1 = UUIDPKModel.objects.create()
1104+
obj2 = UUIDPKModel.objects.create()
1105+
obj3 = UUIDPKModel.objects.create()
1106+
1107+
# Assign permissions to specific objects only
1108+
assign_perm("add_uuidpkmodel", self.user, obj1)
1109+
assign_perm("add_uuidpkmodel", self.user, obj2)
1110+
# obj3 deliberately has no permissions
1111+
1112+
# Create a queryset of all objects to pass as klass parameter
1113+
obj_queryset = UUIDPKModel.objects.all()
1114+
1115+
# This should only return obj1 and obj2, not obj3
1116+
objects = get_objects_for_user(
1117+
klass=obj_queryset,
1118+
user=self.user,
1119+
perms=["add_uuidpkmodel"],
1120+
accept_global_perms=False,
1121+
)
1122+
1123+
# Verify correct objects are returned
1124+
self.assertEqual(len(objects), 2)
1125+
self.assertTrue(isinstance(objects, QuerySet))
1126+
returned_pks = set(objects.values_list("pk", flat=True))
1127+
expected_pks = {obj1.pk, obj2.pk}
1128+
self.assertEqual(returned_pks, expected_pks)
1129+
1130+
# Ensure obj3 is not included
1131+
self.assertNotIn(obj3.pk, returned_pks)
1132+
1133+
# Also test with groups to ensure group permissions work correctly too
1134+
group_obj = UUIDPKModel.objects.create()
1135+
assign_perm("add_uuidpkmodel", self.group, group_obj)
1136+
self.user.groups.add(self.group)
1137+
1138+
objects_with_groups = get_objects_for_user(
1139+
klass=UUIDPKModel.objects.all(),
1140+
user=self.user,
1141+
perms=["add_uuidpkmodel"],
1142+
accept_global_perms=False,
1143+
use_groups=True,
1144+
)
1145+
1146+
# Should now include the group object too
1147+
self.assertEqual(len(objects_with_groups), 3)
1148+
group_returned_pks = set(objects_with_groups.values_list("pk", flat=True))
1149+
expected_group_pks = {obj1.pk, obj2.pk, group_obj.pk}
1150+
self.assertEqual(group_returned_pks, expected_group_pks)
1151+
10941152
def test_exception_different_ctypes(self):
10951153
self.assertRaises(
10961154
MixedContentTypeError, get_objects_for_user, self.user, ["auth.change_permission", "auth.change_group"]

0 commit comments

Comments
 (0)