@@ -307,8 +307,10 @@ def migrate_authz_to_legacy_course_roles(
307307 "At least one of course_id_list or org_id must be provided to limit the scope of the rollback migration."
308308 )
309309
310- # CourseOverviewData and OrgCourseOverviewGlobData share the same namespace,
311- # so filtering by CourseOverviewData captures both course-level and org-level glob assignments.
310+ # CourseOverviewData and OrgCourseOverviewGlobData share the same NAMESPACE ("course-v1"),
311+ # and get_all_role_assignments_per_scope_type matches by NAMESPACE. Passing CourseOverviewData
312+ # therefore captures both course-level and org-level glob assignments. The exact scope type
313+ # is narrowed per-assignment via isinstance checks in the loop below.
312314 role_assignments = get_all_role_assignments_per_scope_type (scope_type = CourseOverviewData )
313315
314316 # Two cases here:
@@ -333,6 +335,14 @@ def migrate_authz_to_legacy_course_roles(
333335 roles_with_no_errors = []
334336 unassignments = defaultdict (list )
335337
338+ user_external_keys = {assignment .subject .external_key for assignment in role_assignments }
339+ users_by_username = {
340+ subject .user .username : subject .user
341+ for subject in user_subject_model .objects .filter (
342+ user__username__in = user_external_keys
343+ ).select_related ("user" )
344+ }
345+
336346 for role_assignment in role_assignments :
337347
338348 # Per valid role assignment, create corresponding CourseAccessRole entry
@@ -343,7 +353,7 @@ def migrate_authz_to_legacy_course_roles(
343353 scope_external_key = role_assignment .scope .external_key
344354
345355 course_access_role_kwargs = {
346- "user" : user_subject_model . objects . get ( user__username = user_external_key ). user ,
356+ "user" : users_by_username [ user_external_key ] ,
347357 "role" : COURSE_ROLE_EQUIVALENCES [role_external_key ],
348358 }
349359
@@ -353,6 +363,8 @@ def migrate_authz_to_legacy_course_roles(
353363 elif isinstance (role_assignment .scope , OrgCourseOverviewGlobData ):
354364 course_access_role_kwargs ["org" ] = role_assignment .scope .org
355365 else :
366+ # This would only happen for course roles assigned instance-wide
367+ # which is not yet supported
356368 logger .error (
357369 f"Unexpected scope type: { type (role_assignment .scope )} for RoleAssignment with "
358370 f"scope: { scope_external_key } , user: { user_external_key } and role: { role_external_key } , skipping."
0 commit comments