|
10 | 10 | logger = logging.getLogger(__name__) |
11 | 11 |
|
12 | 12 |
|
| 13 | +def _log_migration_errors(permissions_with_errors: list) -> None: |
| 14 | + """ |
| 15 | + Log the permissions that could not be migrated during the migration process. |
| 16 | + Args: |
| 17 | + permissions_with_errors (list): List of ContentLibraryPermission instances that failed to migrate. |
| 18 | + """ |
| 19 | + logger.error( |
| 20 | + f"Migration completed with errors for {len(permissions_with_errors)} permissions.\n" |
| 21 | + "The following permissions could not be migrated:" |
| 22 | + ) |
| 23 | + for permission in permissions_with_errors: |
| 24 | + logger.error( |
| 25 | + "Access level: %s, %sLibrary: %s", |
| 26 | + permission.access_level, |
| 27 | + f"User: {permission.user.username}, " if permission.user else f"Group: {permission.group.name}, ", |
| 28 | + permission.library.slug |
| 29 | + ) |
| 30 | + |
| 31 | + |
13 | 32 | def migrate_legacy_permissions(apps, schema_editor): |
14 | 33 | """ |
15 | 34 | Migrate legacy permission data to the new Casbin-based authorization model. |
@@ -46,22 +65,27 @@ def migrate_legacy_permissions(apps, schema_editor): |
46 | 65 | 'library', 'library__org', 'user', 'group' |
47 | 66 | ).all() |
48 | 67 |
|
| 68 | + # List to keep track of any permissions that could not be migrated |
| 69 | + permissions_with_errors = [] |
| 70 | + |
49 | 71 | for permission in legacy_permissions: |
50 | 72 | # Migrate the permission to the new model |
51 | 73 |
|
52 | 74 | # Derive equivalent role based on access level |
53 | | - role = LIBRARY_USER |
54 | | - if permission.access_level == 'admin': |
55 | | - role = LIBRARY_ADMIN |
56 | | - elif permission.access_level == 'author': |
57 | | - role = LIBRARY_AUTHOR |
58 | | - elif permission.access_level == 'read': |
59 | | - role = LIBRARY_USER |
60 | | - else: |
| 75 | + access_level_to_role = { |
| 76 | + 'admin': LIBRARY_ADMIN, |
| 77 | + 'author': LIBRARY_AUTHOR, |
| 78 | + 'read': LIBRARY_USER, |
| 79 | + } |
| 80 | + |
| 81 | + role = access_level_to_role.get(permission.access_level) |
| 82 | + if role is None: |
61 | 83 | # This should not happen as there are no more access_levels defined |
62 | 84 | # in ContentLibraryPermission, log and skip |
63 | | - logger.warning( |
64 | | - f"Unknown access level: {permission.access_level} for User: {permission.user}") |
| 85 | + logger.error( |
| 86 | + f"Unknown access level: {permission.access_level} for User: {permission.user}" |
| 87 | + ) |
| 88 | + permissions_with_errors.append(permission) |
65 | 89 | continue |
66 | 90 |
|
67 | 91 | # Generating scope based on library identifier |
@@ -92,6 +116,9 @@ def migrate_legacy_permissions(apps, schema_editor): |
92 | 116 | scope_external_key=scope |
93 | 117 | ) |
94 | 118 |
|
| 119 | + if permissions_with_errors: |
| 120 | + _log_migration_errors(permissions_with_errors) |
| 121 | + |
95 | 122 |
|
96 | 123 | class Migration(migrations.Migration): |
97 | 124 | """ |
|
0 commit comments