Skip to content

Commit 666629f

Browse files
refactor: use is instance instead of matching per namespace
1 parent 28791a6 commit 666629f

2 files changed

Lines changed: 9 additions & 10 deletions

File tree

openedx_authz/api/roles.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,20 +556,21 @@ def unassign_subject_from_all_roles(subject: SubjectData) -> bool:
556556
return enforcer.remove_filtered_grouping_policy(GroupingPolicyIndex.SUBJECT.value, subject.namespaced_key)
557557

558558

559-
def get_all_role_assignments_per_scope_type(scope_type: type[ScopeData]) -> list[RoleAssignmentData]:
560-
"""Get all role assignments for a specific scope type.
559+
def get_all_role_assignments_per_scope_type(scope_types: list[type[ScopeData]]) -> list[RoleAssignmentData]:
560+
"""Get all role assignments matching any of the given scope types.
561561
562562
Loads all grouping policies from the enforcer and filters in Python. Casbin policies
563563
store full scope keys (e.g., 'course-v1^course-v1:Org+Course+Run'), so there is no
564564
way to query by scope type directly so the filtering must happen here.
565565
566566
Args:
567-
scope_type: A ScopeData subclass (not an instance) used to match by NAMESPACE.
567+
scope_types: A list of ScopeData subclasses (not instances). Assignments matching
568+
any of the given types are returned.
568569
569570
Returns:
570-
list[RoleAssignmentData]: All assignments whose scope matches the given scope type.
571+
list[RoleAssignmentData]: All assignments whose scope is an instance of any of the given scope types.
571572
"""
572573
return [
573574
role_assignment for role_assignment in get_role_assignments()
574-
if role_assignment.scope.NAMESPACE == scope_type.NAMESPACE
575+
if isinstance(role_assignment.scope, tuple(scope_types))
575576
]

openedx_authz/engine/utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,9 @@ def migrate_authz_to_legacy_course_roles(
317317
"""
318318
_validate_migration_input(course_id_list, org_id)
319319

320-
# CourseOverviewData and OrgCourseOverviewGlobData share the same NAMESPACE ("course-v1"),
321-
# and get_all_role_assignments_per_scope_type matches by NAMESPACE. Passing CourseOverviewData
322-
# therefore captures both course-level and org-level glob assignments. The exact scope type
323-
# is narrowed per-assignment via isinstance checks in the loop below.
324-
role_assignments = get_all_role_assignments_per_scope_type(scope_type=CourseOverviewData)
320+
role_assignments = get_all_role_assignments_per_scope_type(
321+
scope_types=[CourseOverviewData, OrgCourseOverviewGlobData]
322+
)
325323

326324
# Two cases here:
327325
# 1. org_id provided: filter by org — includes org-level glob and course-level scopes for that org.

0 commit comments

Comments
 (0)