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