Skip to content

Commit 947d1cc

Browse files
refactor: return typed role assignemnts for easier management
1 parent 6c7d069 commit 947d1cc

3 files changed

Lines changed: 35 additions & 34 deletions

File tree

openedx_authz/api/roles.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"unassign_role_from_subject_in_scope",
3535
"batch_unassign_role_from_subjects_in_scope",
3636
"get_subject_role_assignments_in_scope",
37-
"get_role_assignments_for_role_in_scope",
37+
"get_subjects_role_assignments_for_role_in_scope",
3838
"get_subject_role_assignments",
3939
]
4040

@@ -293,7 +293,7 @@ def get_subject_role_assignments_in_scope(
293293
return role_assignments
294294

295295

296-
def get_role_assignments_for_role_in_scope(
296+
def get_subjects_role_assignments_for_role_in_scope(
297297
role: RoleData, scope: ScopeData
298298
) -> list[RoleAssignmentData]:
299299
"""Get the subjects assigned to a specific role in a specific scope.
@@ -307,7 +307,7 @@ def get_role_assignments_for_role_in_scope(
307307
"""
308308
role_assignments = []
309309
for subject in enforcer.get_users_for_role_in_domain(role.role_id, scope.scope_id):
310-
if subject.startswith("role@"):
310+
if subject.startswith(RoleData.NAMESPACE):
311311
# Skip roles that are also subjects
312312
continue
313313
role_assignments.append(

openedx_authz/api/users.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
(e.g., 'user@john_doe').
1010
"""
1111

12-
from openedx_authz.api.data import RoleData, ScopeData, SubjectData, UserData
12+
from openedx_authz.api.data import RoleAssignmentData, RoleData, ScopeData, SubjectData, UserData
1313
from openedx_authz.api.roles import (
1414
assign_role_to_subject_in_scope,
1515
batch_assign_role_to_subjects_in_scope,
1616
batch_unassign_role_from_subjects_in_scope,
1717
get_subject_role_assignments,
1818
get_subject_role_assignments_in_scope,
19+
get_subjects_role_assignments_for_role_in_scope,
1920
unassign_role_from_subject_in_scope,
2021
)
2122

@@ -29,102 +30,102 @@
2930
]
3031

3132

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:
3334
"""Assign a role to a user in a specific scope.
3435
3536
Args:
3637
user (str): ID of the user (e.g., 'john_doe').
3738
role_name (str): Name of the role to assign.
3839
scope (str): Scope in which to assign the role.
39-
40-
Returns:
41-
bool: True if the assignment was successful, False otherwise.
4240
"""
43-
return assign_role_to_subject_in_scope(
41+
assign_role_to_subject_in_scope(
4442
UserData(username=username),
4543
RoleData(name=role_name),
46-
ScopeData(scope_id=scope_id),
44+
ScopeData(name=scope),
4745
)
4846

4947

5048
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
5250
) -> dict[str, bool]:
5351
"""Assign a role to multiple users in a specific scope.
5452
5553
Args:
5654
users (list of str): List of user IDs (e.g., ['john_doe', 'jane_smith']).
5755
role_name (str): Name of the role to assign.
5856
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).
6257
"""
6358
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)
6661
)
6762

6863

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:
7065
"""Unassign a role from a user in a specific scope.
7166
7267
Args:
7368
user (str): ID of the user (e.g., 'john_doe').
7469
role_name (str): Name of the role to unassign.
7570
scope (str): Scope in which to unassign the role.
76-
77-
Returns:
78-
bool: True if the unassignment was successful, False otherwise.
7971
"""
80-
return unassign_role_from_subject_in_scope(
72+
unassign_role_from_subject_in_scope(
8173
UserData(username=user),
8274
RoleData(name=role_name),
83-
ScopeData(scope_id=scope_id),
75+
ScopeData(name=scope),
8476
)
8577

8678

8779
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
8981
) -> dict[str, bool]:
9082
"""Unassign a role from multiple users in a specific scope.
9183
9284
Args:
9385
users (list of str): List of user IDs (e.g., ['john_doe', 'jane_smith']).
9486
role_name (str): Name of the role to unassign.
9587
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).
9988
"""
10089
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)
10392
)
10493

10594

106-
def get_user_role_assignments(username: str) -> list[dict]:
95+
def get_user_role_assignments(username: str) -> list[RoleAssignmentData]:
10796
"""Get all roles for a user across all scopes.
10897
10998
Args:
11099
user (str): ID of the user (e.g., 'john_doe').
111100
112101
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.
114103
"""
115104
return get_subject_role_assignments(UserData(username=username))
116105

117106

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]:
119108
"""Get the roles assigned to a user in a specific scope.
120109
121110
Args:
122111
user (str): ID of the user (e.g., 'john_doe').
123112
scope (str): Scope in which to retrieve the roles.
124113
125114
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.
127116
"""
128117
return get_subject_role_assignments_in_scope(
129-
UserData(username=username), ScopeData(scope_id=scope_id)
118+
UserData(username=username), ScopeData(name=scope)
130119
)
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))

openedx_authz/tests/api/test_roles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ def test_get_role_assignments_in_scope(self, role_name, scope_name, expected_cou
851851
Expected result:
852852
- The number of role assignments in the given scope is correctly retrieved.
853853
"""
854-
role_assignments = get_role_assignments_for_role_in_scope(
854+
role_assignments = get_subject_role_assignments_for_role_in_scope(
855855
RoleData(name=role_name), ScopeData(name=scope_name)
856856
)
857857

0 commit comments

Comments
 (0)