Skip to content

Commit 2c6bff7

Browse files
committed
refactor: remove unused role assignment functions and update API views for consistency
1 parent 9b924f8 commit 2c6bff7

4 files changed

Lines changed: 8 additions & 65 deletions

File tree

openedx_authz/api/roles.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -382,39 +382,6 @@ def get_all_subject_role_assignments_in_scope(scope: ScopeData) -> list[RoleAssi
382382
return list(role_assignments_per_subject.values())
383383

384384

385-
def get_all_subject_role_assignments_in_scope_v2(
386-
scope: ScopeData,
387-
) -> list[SubjectRoleAssignmentData]:
388-
"""Get all the subjects assigned to any role in a specific scope.
389-
390-
Args:
391-
scope: The scope to filter subjects (e.g., 'library:123' or '*' for global).
392-
393-
Returns:
394-
list[SubjectRoleAssignmentData]: A list of subjects assigned to roles in the specified scope.
395-
"""
396-
roles_in_scope = get_all_roles_in_scope(scope)
397-
398-
subject_roles_map = {}
399-
for subject, role, _ in roles_in_scope:
400-
if subject not in subject_roles_map:
401-
subject_roles_map[subject] = []
402-
subject_roles_map[subject].append(role)
403-
404-
role_assignments = []
405-
for subject_key, role_keys in subject_roles_map.items():
406-
subject = SubjectData(namespaced_key=subject_key)
407-
roles = [RoleData(namespaced_key=role_key) for role_key in role_keys]
408-
role_assignments.append(
409-
SubjectRoleAssignmentData(
410-
subject=subject,
411-
roles=roles,
412-
)
413-
)
414-
415-
return role_assignments
416-
417-
418385
def get_subjects_for_role(role: RoleData) -> list[SubjectData]:
419386
"""Get all the subjects assigned to a specific role.
420387

openedx_authz/api/users.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
batch_assign_role_to_subjects_in_scope,
2525
batch_unassign_role_from_subjects_in_scope,
2626
get_all_subject_role_assignments_in_scope,
27-
get_all_subject_role_assignments_in_scope_v2,
2827
get_subject_role_assignments,
2928
get_subject_role_assignments_for_role_in_scope,
3029
get_subject_role_assignments_in_scope,
@@ -42,7 +41,6 @@
4241
"get_user_role_assignments_for_role_in_scope",
4342
"get_all_user_role_assignments_in_scope",
4443
"user_has_permission",
45-
"get_all_user_role_assignments_in_scope_v2",
4644
"get_users_for_role",
4745
]
4846

@@ -175,29 +173,6 @@ def get_all_user_role_assignments_in_scope(
175173
)
176174

177175

178-
def get_all_user_role_assignments_in_scope_v2(scope_external_key: str) -> list[SubjectRoleAssignmentData]:
179-
"""Get all user role assignments in a specific scope.
180-
181-
Args:
182-
scope (str): Scope in which to retrieve the user role assignments.
183-
184-
Returns:
185-
list[SubjectRoleAssignmentData]: A list of user role assignments and all their metadata in the specified scope.
186-
"""
187-
user_role_assignments = []
188-
role_assignments = get_all_subject_role_assignments_in_scope_v2(ContentLibraryData(external_key=scope_external_key))
189-
190-
for role_assignment in role_assignments:
191-
user_role_assignments.append(
192-
SubjectRoleAssignmentData(
193-
subject=UserData(namespaced_key=role_assignment.subject.namespaced_key),
194-
roles=role_assignment.roles,
195-
)
196-
)
197-
198-
return user_role_assignments
199-
200-
201176
def user_has_permission(
202177
user_external_key: str,
203178
action_external_key: str,

openedx_authz/rest_api/v1/serializers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from rest_framework import serializers
44

5+
from openedx_authz.api.data import RoleAssignmentData
56
from openedx_authz.rest_api.enums import SortField, SortOrder
67
from openedx_authz.rest_api.utils import get_user_by_username_or_email
78
from openedx_authz.rest_api.v1.fields import CommaSeparatedListField, LowercaseCharField
@@ -88,21 +89,21 @@ def __init__(self, *args, **kwargs):
8889
super().__init__(*args, **kwargs)
8990
self._user_cache = {}
9091

91-
def _get_user(self, obj):
92+
def _get_user(self, obj: RoleAssignmentData):
9293
"""Cache user lookups to avoid duplicate DB queries."""
9394
username = obj.subject.username
9495
if username not in self._user_cache:
9596
self._user_cache[username] = get_user_by_username_or_email(username)
9697
return self._user_cache[username]
9798

98-
def get_username(self, obj):
99+
def get_username(self, obj: RoleAssignmentData):
99100
return obj.subject.username
100101

101-
def get_full_name(self, obj):
102+
def get_full_name(self, obj: RoleAssignmentData):
102103
return self._get_user(obj).profile.name
103104

104-
def get_email(self, obj):
105+
def get_email(self, obj: RoleAssignmentData):
105106
return self._get_user(obj).email
106107

107-
def get_roles(self, obj):
108+
def get_roles(self, obj: RoleAssignmentData):
108109
return [role.external_key for role in obj.roles]

openedx_authz/rest_api/v1/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def get(self, request: HttpRequest) -> Response:
107107
"""Retrieve all users with role assignments within a specific scope."""
108108
serializer = ListUsersInRoleWithScopeSerializer(data=request.query_params)
109109
serializer.is_valid(raise_exception=True)
110-
user_role_assignments = api.get_all_user_role_assignments_in_scope_v2(serializer.validated_data["scope"])
110+
user_role_assignments = api.get_all_user_role_assignments_in_scope(serializer.validated_data["scope"])
111111

112112
paginator = self.pagination_class()
113113
paginated_assignments = paginator.paginate_queryset(user_role_assignments, request)
@@ -218,7 +218,7 @@ def get(self, request: HttpRequest) -> Response:
218218
serializer = ListRolesWithScopeSerializer(data=request.query_params)
219219
serializer.is_valid(raise_exception=True)
220220

221-
scope = api.ContentLibraryData(namespaced_key=serializer.validated_data["scope"])
221+
scope = api.ScopeData(namespaced_key=serializer.validated_data["scope"])
222222
roles = api.get_role_definitions_in_scope(scope)
223223

224224
response_data = []

0 commit comments

Comments
 (0)