|
9 | 9 | (e.g., 'user@john_doe'). |
10 | 10 | """ |
11 | 11 |
|
12 | | -from openedx_authz.api.data import RoleData, ScopeData, SubjectData, UserData |
| 12 | +from openedx_authz.api.data import RoleAssignmentData, RoleData, ScopeData, SubjectData, UserData |
13 | 13 | from openedx_authz.api.roles import ( |
14 | 14 | assign_role_to_subject_in_scope, |
15 | 15 | batch_assign_role_to_subjects_in_scope, |
16 | 16 | batch_unassign_role_from_subjects_in_scope, |
17 | 17 | get_subject_role_assignments, |
18 | 18 | get_subject_role_assignments_in_scope, |
| 19 | + get_subjects_role_assignments_for_role_in_scope, |
19 | 20 | unassign_role_from_subject_in_scope, |
20 | 21 | ) |
21 | 22 |
|
|
29 | 30 | ] |
30 | 31 |
|
31 | 32 |
|
32 | | -def assign_role_to_user_in_scope(username: str, role_name: str, scope_id: str) -> bool: |
| 33 | +def assign_role_to_user_in_scope(username: str, role_name: str, scope: str) -> bool: |
33 | 34 | """Assign a role to a user in a specific scope. |
34 | 35 |
|
35 | 36 | Args: |
36 | 37 | user (str): ID of the user (e.g., 'john_doe'). |
37 | 38 | role_name (str): Name of the role to assign. |
38 | 39 | scope (str): Scope in which to assign the role. |
39 | | -
|
40 | | - Returns: |
41 | | - bool: True if the assignment was successful, False otherwise. |
42 | 40 | """ |
43 | | - return assign_role_to_subject_in_scope( |
| 41 | + assign_role_to_subject_in_scope( |
44 | 42 | UserData(username=username), |
45 | 43 | RoleData(name=role_name), |
46 | | - ScopeData(scope_id=scope_id), |
| 44 | + ScopeData(name=scope), |
47 | 45 | ) |
48 | 46 |
|
49 | 47 |
|
50 | 48 | def batch_assign_role_to_users( |
51 | | - users: list[str], role_name: str, scope_id: str |
| 49 | + users: list[str], role_name: str, scope: str |
52 | 50 | ) -> dict[str, bool]: |
53 | 51 | """Assign a role to multiple users in a specific scope. |
54 | 52 |
|
55 | 53 | Args: |
56 | 54 | users (list of str): List of user IDs (e.g., ['john_doe', 'jane_smith']). |
57 | 55 | role_name (str): Name of the role to assign. |
58 | 56 | scope (str): Scope in which to assign the role. |
59 | | -
|
60 | | - Returns: |
61 | | - dict: A dictionary mapping user IDs to assignment success status (True/False). |
62 | 57 | """ |
63 | 58 | namespaced_users = [UserData(username=username) for username in users] |
64 | | - return batch_assign_role_to_subjects_in_scope( |
65 | | - namespaced_users, RoleData(name=role_name), ScopeData(scope_id=scope_id) |
| 59 | + batch_assign_role_to_subjects_in_scope( |
| 60 | + namespaced_users, RoleData(name=role_name), ScopeData(name=scope) |
66 | 61 | ) |
67 | 62 |
|
68 | 63 |
|
69 | | -def unassign_role_from_user(user: str, role_name: str, scope_id: str) -> bool: |
| 64 | +def unassign_role_from_user(user: str, role_name: str, scope: str) -> bool: |
70 | 65 | """Unassign a role from a user in a specific scope. |
71 | 66 |
|
72 | 67 | Args: |
73 | 68 | user (str): ID of the user (e.g., 'john_doe'). |
74 | 69 | role_name (str): Name of the role to unassign. |
75 | 70 | scope (str): Scope in which to unassign the role. |
76 | | -
|
77 | | - Returns: |
78 | | - bool: True if the unassignment was successful, False otherwise. |
79 | 71 | """ |
80 | | - return unassign_role_from_subject_in_scope( |
| 72 | + unassign_role_from_subject_in_scope( |
81 | 73 | UserData(username=user), |
82 | 74 | RoleData(name=role_name), |
83 | | - ScopeData(scope_id=scope_id), |
| 75 | + ScopeData(name=scope), |
84 | 76 | ) |
85 | 77 |
|
86 | 78 |
|
87 | 79 | def batch_unassign_role_from_users( |
88 | | - users: list[str], role_name: str, scope_id: str |
| 80 | + users: list[str], role_name: str, scope: str |
89 | 81 | ) -> dict[str, bool]: |
90 | 82 | """Unassign a role from multiple users in a specific scope. |
91 | 83 |
|
92 | 84 | Args: |
93 | 85 | users (list of str): List of user IDs (e.g., ['john_doe', 'jane_smith']). |
94 | 86 | role_name (str): Name of the role to unassign. |
95 | 87 | scope (str): Scope in which to unassign the role. |
96 | | -
|
97 | | - Returns: |
98 | | - dict: A dictionary mapping user IDs to unassignment success status (True/False). |
99 | 88 | """ |
100 | 89 | namespaced_users = [UserData(username=user) for user in users] |
101 | | - return batch_unassign_role_from_subjects_in_scope( |
102 | | - namespaced_users, RoleData(name=role_name), ScopeData(scope_id=scope_id) |
| 90 | + batch_unassign_role_from_subjects_in_scope( |
| 91 | + namespaced_users, RoleData(name=role_name), ScopeData(name=scope) |
103 | 92 | ) |
104 | 93 |
|
105 | 94 |
|
106 | | -def get_user_role_assignments(username: str) -> list[dict]: |
| 95 | +def get_user_role_assignments(username: str) -> list[RoleAssignmentData]: |
107 | 96 | """Get all roles for a user across all scopes. |
108 | 97 |
|
109 | 98 | Args: |
110 | 99 | user (str): ID of the user (e.g., 'john_doe'). |
111 | 100 |
|
112 | 101 | Returns: |
113 | | - list[dict]: A list of role names and all their metadata assigned to the user. |
| 102 | + list[dict]: A list of role assignments and all their metadata assigned to the user. |
114 | 103 | """ |
115 | 104 | return get_subject_role_assignments(UserData(username=username)) |
116 | 105 |
|
117 | 106 |
|
118 | | -def get_user_role_assignments_in_scope(username: str, scope_id: str) -> list[str]: |
| 107 | +def get_user_role_assignments_in_scope(username: str, scope: str) -> list[RoleAssignmentData]: |
119 | 108 | """Get the roles assigned to a user in a specific scope. |
120 | 109 |
|
121 | 110 | Args: |
122 | 111 | user (str): ID of the user (e.g., 'john_doe'). |
123 | 112 | scope (str): Scope in which to retrieve the roles. |
124 | 113 |
|
125 | 114 | Returns: |
126 | | - list: A list of role names assigned to the user in the specified scope. |
| 115 | + list: A list of role assignments assigned to the user in the specified scope. |
127 | 116 | """ |
128 | 117 | return get_subject_role_assignments_in_scope( |
129 | | - UserData(username=username), ScopeData(scope_id=scope_id) |
| 118 | + UserData(username=username), ScopeData(name=scope) |
130 | 119 | ) |
| 120 | + |
| 121 | +def get_user_role_assignments_for_role_in_scope(role_name:str, scope:str) -> list[RoleAssignmentData]: |
| 122 | + """Get all users assigned to a specific role across all scopes. |
| 123 | +
|
| 124 | + Args: |
| 125 | + role_name (str): Name of the role (e.g., 'instructor'). |
| 126 | + scope (str): Scope in which to retrieve the role assignments. |
| 127 | +
|
| 128 | + Returns: |
| 129 | + list[dict]: A list of user names and all their metadata assigned to the role. |
| 130 | + """ |
| 131 | + return get_subjects_role_assignments_for_role_in_scope(RoleData(name=role_name), ScopeData(name=scope)) |
0 commit comments