|
9 | 9 | (e.g., 'user^john_doe'). |
10 | 10 | """ |
11 | 11 |
|
| 12 | +from django.contrib.auth import get_user_model |
| 13 | + |
12 | 14 | from openedx_authz.api.data import ( |
13 | 15 | ActionData, |
14 | 16 | PermissionData, |
15 | 17 | RoleAssignmentData, |
16 | 18 | RoleData, |
17 | 19 | ScopeData, |
| 20 | + SuperAdminAssignmentData, |
18 | 21 | UserAssignments, |
19 | 22 | UserAssignmentsFilter, |
20 | 23 | UserData, |
|
36 | 39 | ) |
37 | 40 | from openedx_authz.api.utils import filter_user_assignments, get_user_assignment_map |
38 | 41 |
|
| 42 | +User = get_user_model() |
| 43 | + |
| 44 | + |
39 | 45 | __all__ = [ |
40 | 46 | "assign_role_to_user_in_scope", |
41 | 47 | "batch_assign_role_to_users_in_scope", |
|
44 | 50 | "get_user_role_assignments", |
45 | 51 | "get_user_role_assignments_in_scope", |
46 | 52 | "get_user_role_assignments_for_role_in_scope", |
| 53 | + "get_user_role_assignments_for_user_filtered", |
47 | 54 | "get_user_role_assignments_filtered", |
48 | 55 | "get_all_user_role_assignments_in_scope", |
49 | 56 | "get_visible_role_assignments_for_user", |
50 | 57 | "is_user_allowed", |
51 | 58 | "get_scopes_for_user_and_permission", |
52 | 59 | "get_users_for_role_in_scope", |
53 | 60 | "unassign_all_roles_from_user", |
| 61 | + "get_superadmins", |
54 | 62 | ] |
55 | 63 |
|
56 | 64 |
|
@@ -168,6 +176,42 @@ def get_user_role_assignments_for_role_in_scope( |
168 | 176 | ) |
169 | 177 |
|
170 | 178 |
|
| 179 | +def get_user_role_assignments_for_user_filtered( |
| 180 | + user_external_key: str, |
| 181 | + orgs: list[str] = None, |
| 182 | + roles: list[str] = None, |
| 183 | + allowed_for_user_external_key: str = None, |
| 184 | +) -> list[RoleAssignmentData]: |
| 185 | + """ |
| 186 | + Get role assignments for a specific user, filtered by orgs and/or roles, |
| 187 | + and only include assignments that the specified user has permission to view. |
| 188 | +
|
| 189 | + Args: |
| 190 | + user_external_key: The user to get assignments for (e.g., 'john_doe'). |
| 191 | + orgs: Optional list of orgs to filter by (e.g., ['edX', 'MITx']). |
| 192 | + roles: Optional list of roles to filter by (e.g., ['library_admin']). |
| 193 | + allowed_for_user_external_key: The username to check permissions against (e.g., 'john_doe'). |
| 194 | +
|
| 195 | + Returns: |
| 196 | + list[RoleAssignmentData]: A list of role assignments for the user, filtered by orgs/roles and permissions. |
| 197 | + """ |
| 198 | + user_role_assignments = get_user_role_assignments(user_external_key=user_external_key) |
| 199 | + # Filter assignments based on the user's permissions |
| 200 | + user_role_assignments = _filter_allowed_assignments( |
| 201 | + user_external_key=allowed_for_user_external_key, |
| 202 | + assignments=user_role_assignments, |
| 203 | + ) |
| 204 | + if orgs: |
| 205 | + # Filter by orgs |
| 206 | + user_role_assignments = [a for a in user_role_assignments if a.scope.org in orgs] |
| 207 | + if roles: |
| 208 | + # Filter by roles |
| 209 | + user_role_assignments = [ |
| 210 | + a for a in user_role_assignments if any(role.external_key in roles for role in a.roles) |
| 211 | + ] |
| 212 | + return user_role_assignments |
| 213 | + |
| 214 | + |
171 | 215 | def get_user_role_assignments_filtered( |
172 | 216 | *, |
173 | 217 | user_external_key: str | None = None, |
@@ -339,3 +383,30 @@ def unassign_all_roles_from_user(user_external_key: str) -> bool: |
339 | 383 | bool: True if any roles were removed, False otherwise. |
340 | 384 | """ |
341 | 385 | return unassign_subject_from_all_roles(UserData(external_key=user_external_key)) |
| 386 | + |
| 387 | + |
| 388 | +def get_superadmins(user_external_keys: list[str] | None = None) -> list[SuperAdminAssignmentData]: |
| 389 | + """Returns all superadmins as SuperAdminAssignmentData. |
| 390 | +
|
| 391 | + A superadmin is a User with a Django staff or superuser role. |
| 392 | + Superadmins automatically are allowed to do any action. |
| 393 | +
|
| 394 | + Args: |
| 395 | + user_external_keys (list[str] or None): To filter by usernames |
| 396 | +
|
| 397 | + Returns: |
| 398 | + list[SuperAdminAssignmentData]: The superadmin data |
| 399 | + """ |
| 400 | + # Retrieve user data to check if they are a superusers |
| 401 | + requested_users = User.objects.filter(username__in=user_external_keys, is_active=True) |
| 402 | + superadmin_assignments: list[SuperAdminAssignmentData] = [] |
| 403 | + for requested_user in requested_users: |
| 404 | + if requested_user.is_staff or requested_user.is_superuser: |
| 405 | + superadmin_assignments.append( |
| 406 | + SuperAdminAssignmentData( |
| 407 | + subject=UserData(external_key=requested_user.username), |
| 408 | + is_staff=requested_user.is_staff, |
| 409 | + is_superuser=requested_user.is_superuser, |
| 410 | + ) |
| 411 | + ) |
| 412 | + return superadmin_assignments |
0 commit comments