2323from openedx_authz .engine .enforcer import enforcer
2424
2525__all__ = [
26+ "get_permissions_for_single_role" ,
2627 "get_permissions_for_roles" ,
2728 "get_all_roles_names" ,
2829 "get_all_roles_in_scope" ,
4748# in this case, ALL the policies, but that might not be the case
4849
4950
51+ def get_permissions_for_single_role (
52+ role : RoleData ,
53+ ) -> list [PermissionData ]:
54+ """Get the permissions (actions) for a single role.
55+
56+ Args:
57+ role: A RoleData object representing the role.
58+
59+ Returns:
60+ list[PermissionData]: A list of PermissionData objects associated with the given role.
61+ """
62+ policies = enforcer .get_implicit_permissions_for_user (role .namespaced_key )
63+ return [get_permission_from_policy (policy ) for policy in policies ]
64+
65+
5066def get_permissions_for_roles (
51- roles : list [RoleData ] | RoleData ,
67+ roles : list [RoleData ],
5268) -> dict [str , dict [str , list [PermissionData | str ]]]:
5369 """Get the permissions (actions) for a list of roles.
5470
@@ -59,22 +75,11 @@ def get_permissions_for_roles(
5975 dict[str, list[PermissionData]]: A dictionary mapping role names to their permissions and scopes.
6076 """
6177 permissions_by_role = {}
62- if not roles :
63- return permissions_by_role
64-
65- if isinstance (roles , RoleData ):
66- roles = [roles ]
6778
6879 for role in roles :
69- policies = enforcer .get_implicit_permissions_for_user (role .namespaced_key )
70-
71- permissions_by_role [role .external_key ] = (
72- { # Index by role external_key for easy lookup
73- "permissions" : [
74- get_permission_from_policy (policy ) for policy in policies
75- ],
76- }
77- )
80+ permissions_by_role [role .external_key ] = {
81+ "permissions" : get_permissions_for_single_role (role )
82+ }
7883
7984 return permissions_by_role
8085
@@ -252,7 +257,7 @@ def get_subject_role_assignments(subject: SubjectData) -> list[RoleAssignmentDat
252257 """Get all the roles for a subject across all scopes.
253258
254259 Args:
255- subject: The ID of the subject namespaced (e.g., 'subject: john_doe').
260+ subject: The ID of the subject namespaced (e.g., 'subject^ john_doe').
256261
257262 Returns:
258263 list[Role]: A list of role names and all their metadata assigned to the subject.
@@ -262,11 +267,7 @@ def get_subject_role_assignments(subject: SubjectData) -> list[RoleAssignmentDat
262267 GroupingPolicyIndex .SUBJECT .value , subject .namespaced_key
263268 ):
264269 role = RoleData (namespaced_key = policy [GroupingPolicyIndex .ROLE .value ])
265- role .permissions = get_permissions_for_roles (role )[
266- role .external_key
267- ][ # Index by role external_key for readability
268- "permissions"
269- ]
270+ role .permissions = get_permissions_for_single_role (role )
270271
271272 role_assignments .append (
272273 RoleAssignmentData (
@@ -284,7 +285,7 @@ def get_subject_role_assignments_in_scope(
284285 """Get the roles for a subject in a specific scope.
285286
286287 Args:
287- subject: The ID of the subject namespaced (e.g., 'subject: john_doe').
288+ subject: The ID of the subject namespaced (e.g., 'subject^ john_doe').
288289 scope: The scope to filter roles (e.g., 'library:123').
289290
290291 Returns:
@@ -301,9 +302,7 @@ def get_subject_role_assignments_in_scope(
301302 subject = subject ,
302303 role = RoleData (
303304 namespaced_key = namespaced_key ,
304- permissions = get_permissions_for_roles (role )[role .external_key ][
305- "permissions"
306- ],
305+ permissions = get_permissions_for_single_role (role ),
307306 ),
308307 scope = scope ,
309308 )
@@ -332,14 +331,10 @@ def get_subjects_role_assignments_for_role_in_scope(
332331 continue
333332 role_assignments .append (
334333 RoleAssignmentData (
335- subject = SubjectData (
336- namespaced_key = subject
337- ),
334+ subject = SubjectData (namespaced_key = subject ),
338335 role = RoleData (
339336 external_key = role .external_key ,
340- permissions = get_permissions_for_roles (role )[role .external_key ][
341- "permissions"
342- ],
337+ permissions = get_permissions_for_single_role (role ),
343338 ),
344339 scope = scope ,
345340 )
@@ -364,9 +359,7 @@ def get_all_subject_role_assignments_in_scope(
364359 for policy in roles_in_scope :
365360 subject = SubjectData (namespaced_key = policy [GroupingPolicyIndex .SUBJECT .value ])
366361 role = RoleData (namespaced_key = policy [GroupingPolicyIndex .ROLE .value ])
367- role .permissions = get_permissions_for_roles (role )[role .external_key ][
368- "permissions"
369- ] # Index by role external_key for easy lookup
362+ role .permissions = get_permissions_for_single_role (role )
370363
371364 role_assignments .append (
372365 RoleAssignmentData (
0 commit comments