1111
1212from openedx_authz .api .data import (
1313 ActionData ,
14+ ContentLibraryData ,
15+ CourseOverviewData ,
16+ OrgContentLibraryGlobData ,
17+ OrgCourseOverviewGlobData ,
1418 PermissionData ,
1519 RoleAssignmentData ,
1620 RoleData ,
1721 ScopeData ,
22+ UserAssignments ,
23+ UserAssignmentsFilter ,
1824 UserData ,
1925)
2026from openedx_authz .api .permissions import is_subject_allowed
3339 unassign_role_from_subject_in_scope ,
3440 unassign_subject_from_all_roles ,
3541)
42+ from openedx_authz .api .utils import filter_user_assignments , get_user_assignment_map
43+ from openedx_authz .constants .permissions import COURSES_VIEW_COURSE , VIEW_LIBRARY
3644
3745__all__ = [
3846 "assign_role_to_user_in_scope" ,
3947 "batch_assign_role_to_users_in_scope" ,
4048 "unassign_role_from_user" ,
4149 "batch_unassign_role_from_users" ,
42- "get_all_user_role_assignments" ,
4350 "get_user_role_assignments" ,
4451 "get_user_role_assignments_in_scope" ,
4552 "get_user_role_assignments_for_role_in_scope" ,
4653 "get_user_role_assignments_filtered" ,
54+ "get_all_user_role_assignments" ,
4755 "get_all_user_role_assignments_in_scope" ,
56+ "get_all_user_role_assignments_by_user_filtered" ,
4857 "is_user_allowed" ,
4958 "get_scopes_for_user_and_permission" ,
5059 "get_users_for_role_in_scope" ,
@@ -120,15 +129,6 @@ def batch_unassign_role_from_users(users: list[str], role_external_key: str, sco
120129 )
121130
122131
123- def get_all_user_role_assignments () -> list [RoleAssignmentData ]:
124- """Get all roles for all users across all scopes.
125-
126- Returns:
127- list[RoleAssignmentData]: A list of role assignments and all their metadata assigned to the user.
128- """
129- return get_all_subject_role_assignments ()
130-
131-
132132def get_user_role_assignments (user_external_key : str ) -> list [RoleAssignmentData ]:
133133 """Get all roles for a user across all scopes.
134134
@@ -202,6 +202,15 @@ def get_user_role_assignments_filtered(
202202 )
203203
204204
205+ def get_all_user_role_assignments () -> list [RoleAssignmentData ]:
206+ """Get all roles for all users across all scopes.
207+
208+ Returns:
209+ list[RoleAssignmentData]: A list of role assignments and all their metadata assigned to the user.
210+ """
211+ return get_all_subject_role_assignments ()
212+
213+
205214def get_all_user_role_assignments_in_scope (
206215 scope_external_key : str ,
207216) -> list [RoleAssignmentData ]:
@@ -216,6 +225,71 @@ def get_all_user_role_assignments_in_scope(
216225 return get_all_subject_role_assignments_in_scope (ScopeData (external_key = scope_external_key ))
217226
218227
228+ def _filter_allowed_assignments (
229+ user_external_key : str , assignments : list [RoleAssignmentData ]
230+ ) -> list [RoleAssignmentData ]:
231+ """
232+ Filter the given role assignments to only include those that the user has permission to view.
233+ """
234+ allowed_assignments : list [RoleAssignmentData ] = []
235+ for assignment in assignments :
236+ permission = None
237+
238+ # For CourseOverviewData and ContentLibraryData, check for the view permission
239+ if isinstance (assignment .scope , (CourseOverviewData , OrgCourseOverviewGlobData )):
240+ permission = COURSES_VIEW_COURSE .identifier
241+ elif isinstance (assignment .scope , (ContentLibraryData , OrgContentLibraryGlobData )):
242+ permission = VIEW_LIBRARY .identifier
243+
244+ if permission and is_user_allowed (
245+ user_external_key = user_external_key ,
246+ action_external_key = permission ,
247+ scope_external_key = assignment .scope .external_key ,
248+ ):
249+ allowed_assignments .append (assignment )
250+
251+ return allowed_assignments
252+
253+
254+ def get_all_user_role_assignments_by_user_filtered (
255+ orgs : list [str ] = None ,
256+ scopes : list [str ] = None ,
257+ allowed_for_user_external_key : str = None ,
258+ ) -> list [UserAssignments ]:
259+ """
260+ Get all user role assignments filtered by orgs and/or scopes, and only include
261+ assignments that the specified user has permission to view.
262+
263+ Args:
264+ orgs: Optional list of orgs to filter by (e.g., ['edX', 'MITx']).
265+ scopes: Optional list of scopes to filter by (e.g., ['lib:DemoX:CSPROB']).
266+ allowed_for_user_external_key: The username to check permissions against (e.g., 'john_doe').
267+
268+ Returns:
269+ list[UserAssignments]: A list of users with their role assignments, filtered by orgs/scopes and permissions.
270+ """
271+ user_role_assignments = get_all_user_role_assignments ()
272+ # Filter assignments based on the user's permissions
273+ user_role_assignments = _filter_allowed_assignments (
274+ user_external_key = allowed_for_user_external_key ,
275+ assignments = user_role_assignments ,
276+ )
277+ # Group assignments by user
278+ users_with_assignments = get_user_assignment_map (user_role_assignments )
279+
280+ users_with_assignments = filter_user_assignments (
281+ users_with_assignments = users_with_assignments ,
282+ by = UserAssignmentsFilter .SCOPES ,
283+ values = scopes ,
284+ )
285+ users_with_assignments = filter_user_assignments (
286+ users_with_assignments = users_with_assignments ,
287+ by = UserAssignmentsFilter .ORGS ,
288+ values = orgs ,
289+ )
290+ return users_with_assignments
291+
292+
219293def is_user_allowed (
220294 user_external_key : str ,
221295 action_external_key : str ,
0 commit comments