|
11 | 11 |
|
12 | 12 | from openedx_authz.api.data import ( |
13 | 13 | ActionData, |
| 14 | + ContentLibraryData, |
| 15 | + CourseOverviewData, |
| 16 | + OrgContentLibraryGlobData, |
| 17 | + OrgCourseOverviewGlobData, |
14 | 18 | PermissionData, |
15 | 19 | RoleAssignmentData, |
16 | 20 | RoleData, |
17 | 21 | ScopeData, |
| 22 | + UserAssignments, |
| 23 | + UserAssignmentsFilter, |
18 | 24 | UserData, |
19 | 25 | ) |
20 | 26 | from openedx_authz.api.permissions import is_subject_allowed |
|
32 | 38 | unassign_role_from_subject_in_scope, |
33 | 39 | unassign_subject_from_all_roles, |
34 | 40 | ) |
| 41 | +from openedx_authz.api.utils import filter_user_assignments, get_user_assignment_map |
| 42 | +from openedx_authz.constants.permissions import COURSES_MANAGE_COURSE_TEAM, MANAGE_LIBRARY_TEAM |
35 | 43 |
|
36 | 44 | __all__ = [ |
37 | 45 | "assign_role_to_user_in_scope", |
|
43 | 51 | "get_user_role_assignments_for_role_in_scope", |
44 | 52 | "get_user_role_assignments_filtered", |
45 | 53 | "get_all_user_role_assignments_in_scope", |
| 54 | + "get_visible_role_assignments_for_user", |
46 | 55 | "is_user_allowed", |
47 | 56 | "get_scopes_for_user_and_permission", |
48 | 57 | "get_users_for_role_in_scope", |
@@ -205,6 +214,71 @@ def get_all_user_role_assignments_in_scope( |
205 | 214 | return get_all_subject_role_assignments_in_scope(ScopeData(external_key=scope_external_key)) |
206 | 215 |
|
207 | 216 |
|
| 217 | +def _filter_allowed_assignments( |
| 218 | + user_external_key: str, assignments: list[RoleAssignmentData] |
| 219 | +) -> list[RoleAssignmentData]: |
| 220 | + """ |
| 221 | + Filter the given role assignments to only include those that the user has permission to view. |
| 222 | + """ |
| 223 | + allowed_assignments: list[RoleAssignmentData] = [] |
| 224 | + for assignment in assignments: |
| 225 | + permission = None |
| 226 | + |
| 227 | + # For CourseOverviewData and ContentLibraryData, check for the view permission |
| 228 | + if isinstance(assignment.scope, (CourseOverviewData, OrgCourseOverviewGlobData)): |
| 229 | + permission = COURSES_MANAGE_COURSE_TEAM.identifier |
| 230 | + elif isinstance(assignment.scope, (ContentLibraryData, OrgContentLibraryGlobData)): |
| 231 | + permission = MANAGE_LIBRARY_TEAM.identifier |
| 232 | + |
| 233 | + if permission and is_user_allowed( |
| 234 | + user_external_key=user_external_key, |
| 235 | + action_external_key=permission, |
| 236 | + scope_external_key=assignment.scope.external_key, |
| 237 | + ): |
| 238 | + allowed_assignments.append(assignment) |
| 239 | + |
| 240 | + return allowed_assignments |
| 241 | + |
| 242 | + |
| 243 | +def get_visible_role_assignments_for_user( |
| 244 | + orgs: list[str] = None, |
| 245 | + scopes: list[str] = None, |
| 246 | + allowed_for_user_external_key: str = None, |
| 247 | +) -> list[UserAssignments]: |
| 248 | + """ |
| 249 | + Get all user role assignments filtered by orgs and/or scopes, and only include |
| 250 | + assignments that the specified user has permission to view. |
| 251 | +
|
| 252 | + Args: |
| 253 | + orgs: Optional list of orgs to filter by (e.g., ['edX', 'MITx']). |
| 254 | + scopes: Optional list of scopes to filter by (e.g., ['lib:DemoX:CSPROB']). |
| 255 | + allowed_for_user_external_key: The username to check permissions against (e.g., 'john_doe'). |
| 256 | +
|
| 257 | + Returns: |
| 258 | + list[UserAssignments]: A list of users with their role assignments, filtered by orgs/scopes and permissions. |
| 259 | + """ |
| 260 | + user_role_assignments = get_user_role_assignments_filtered() |
| 261 | + # Filter assignments based on the user's permissions |
| 262 | + user_role_assignments = _filter_allowed_assignments( |
| 263 | + user_external_key=allowed_for_user_external_key, |
| 264 | + assignments=user_role_assignments, |
| 265 | + ) |
| 266 | + # Group assignments by user |
| 267 | + users_with_assignments = get_user_assignment_map(user_role_assignments) |
| 268 | + |
| 269 | + users_with_assignments = filter_user_assignments( |
| 270 | + users_with_assignments=users_with_assignments, |
| 271 | + by=UserAssignmentsFilter.SCOPES, |
| 272 | + values=scopes, |
| 273 | + ) |
| 274 | + users_with_assignments = filter_user_assignments( |
| 275 | + users_with_assignments=users_with_assignments, |
| 276 | + by=UserAssignmentsFilter.ORGS, |
| 277 | + values=orgs, |
| 278 | + ) |
| 279 | + return users_with_assignments |
| 280 | + |
| 281 | + |
208 | 282 | def is_user_allowed( |
209 | 283 | user_external_key: str, |
210 | 284 | action_external_key: str, |
|
0 commit comments