|
8 | 8 |
|
9 | 9 | from casbin import Enforcer |
10 | 10 |
|
| 11 | +from openedx_authz.api.data import CourseOverviewData |
11 | 12 | from openedx_authz.api.users import ( |
12 | 13 | assign_role_to_user_in_scope, |
13 | 14 | batch_assign_role_to_users_in_scope, |
@@ -193,7 +194,9 @@ def migrate_legacy_course_roles_to_authz(CourseAccessRole, delete_after_migratio |
193 | 194 | param CourseAccessRole: The CourseAccessRole model to use. |
194 | 195 | """ |
195 | 196 |
|
196 | | - legacy_permissions = CourseAccessRole.objects.select_related("user").all() |
| 197 | + legacy_permissions = ( |
| 198 | + CourseAccessRole.objects.filter(course_id__startswith="course-v1:").select_related("user").all() |
| 199 | + ) |
197 | 200 |
|
198 | 201 | # List to keep track of any permissions that could not be migrated |
199 | 202 | permissions_with_errors = [] |
@@ -224,11 +227,20 @@ def migrate_legacy_course_roles_to_authz(CourseAccessRole, delete_after_migratio |
224 | 227 | f"to Role: {role.external_key} in Scope: {permission.course_id}" |
225 | 228 | ) |
226 | 229 |
|
227 | | - assign_role_to_user_in_scope( |
| 230 | + is_user_added = assign_role_to_user_in_scope( |
228 | 231 | user_external_key=permission.user.username, |
229 | 232 | role_external_key=role.external_key, |
230 | 233 | scope_external_key=str(permission.course_id), |
231 | 234 | ) |
| 235 | + |
| 236 | + if not is_user_added: |
| 237 | + logger.error( |
| 238 | + f"Failed to migrate permission for User: {permission.user.username} " |
| 239 | + f"to Role: {role.external_key} in Scope: {permission.course_id}" |
| 240 | + ) |
| 241 | + permissions_with_errors.append(permission) |
| 242 | + continue |
| 243 | + |
232 | 244 | permissions_with_no_errors.append(permission) |
233 | 245 |
|
234 | 246 | if delete_after_migration: |
@@ -264,6 +276,10 @@ def migrate_authz_to_legacy_course_roles(CourseAccessRole, UserSubject, delete_a |
264 | 276 | role_assignments = get_user_role_assignments(user_external_key=user_external_key) |
265 | 277 |
|
266 | 278 | for assignment in role_assignments: |
| 279 | + if not isinstance(assignment.scope, CourseOverviewData): |
| 280 | + logger.error(f"Skipping role assignment for User: {user_external_key} due to missing course scope.") |
| 281 | + continue |
| 282 | + |
267 | 283 | scope = assignment.scope.external_key |
268 | 284 |
|
269 | 285 | course_overview = assignment.scope.get_object() |
|
0 commit comments