|
| 1 | +# Generated by Django migration for carrier permission groups |
| 2 | + |
| 3 | +from django.db import migrations |
| 4 | + |
| 5 | + |
| 6 | +def setup_carrier_groups(apps, schema_editor): |
| 7 | + """ |
| 8 | + Create read_carriers and write_carriers permission groups and update |
| 9 | + existing organization user permissions to include read_carriers. |
| 10 | +
|
| 11 | + This migration addresses the permission regression where: |
| 12 | + 1. read_carriers and write_carriers groups were added to ROLES_GROUPS |
| 13 | + 2. But existing user ContextPermissions weren't updated with these groups |
| 14 | + """ |
| 15 | + Group = apps.get_model('user', 'Group') |
| 16 | + Permission = apps.get_model('auth', 'Permission') |
| 17 | + ContentType = apps.get_model('contenttypes', 'ContentType') |
| 18 | + ContextPermission = apps.get_model('iam', 'ContextPermission') |
| 19 | + |
| 20 | + # Create the new permission groups if they don't exist |
| 21 | + read_carriers_group, _ = Group.objects.get_or_create(name='read_carriers') |
| 22 | + write_carriers_group, _ = Group.objects.get_or_create(name='write_carriers') |
| 23 | + |
| 24 | + # Get providers content types for permissions |
| 25 | + try: |
| 26 | + providers_ct = ContentType.objects.filter(app_label='providers') |
| 27 | + |
| 28 | + # Set up read_carriers with view permissions |
| 29 | + view_perms = Permission.objects.filter( |
| 30 | + content_type__in=providers_ct, |
| 31 | + codename__icontains='view' |
| 32 | + ) |
| 33 | + if view_perms.exists(): |
| 34 | + read_carriers_group.permissions.set(view_perms) |
| 35 | + |
| 36 | + # Set up write_carriers with all providers permissions |
| 37 | + all_provider_perms = Permission.objects.filter(content_type__in=providers_ct) |
| 38 | + if all_provider_perms.exists(): |
| 39 | + write_carriers_group.permissions.set(all_provider_perms) |
| 40 | + except Exception: |
| 41 | + # Providers app may not be installed yet |
| 42 | + pass |
| 43 | + |
| 44 | + # Update all existing ContextPermissions that have manage_carriers to also include read_carriers |
| 45 | + manage_carriers_group = Group.objects.filter(name='manage_carriers').first() |
| 46 | + |
| 47 | + if manage_carriers_group: |
| 48 | + # Find all context permissions that have manage_carriers |
| 49 | + context_perms_with_manage = ContextPermission.objects.filter( |
| 50 | + groups=manage_carriers_group |
| 51 | + ) |
| 52 | + |
| 53 | + # Add read_carriers and write_carriers to these context permissions |
| 54 | + for ctx_perm in context_perms_with_manage: |
| 55 | + ctx_perm.groups.add(read_carriers_group) |
| 56 | + ctx_perm.groups.add(write_carriers_group) |
| 57 | + |
| 58 | + # Also update any OrganizationUser context permissions based on their roles |
| 59 | + # This ensures all users who should have read_carriers get it |
| 60 | + try: |
| 61 | + OrganizationUser = apps.get_model('orgs', 'OrganizationUser') |
| 62 | + org_user_ct = ContentType.objects.get_for_model(OrganizationUser) |
| 63 | + |
| 64 | + # Roles that should have read_carriers (all roles) |
| 65 | + org_user_context_perms = ContextPermission.objects.filter( |
| 66 | + content_type=org_user_ct |
| 67 | + ) |
| 68 | + |
| 69 | + for ctx_perm in org_user_context_perms: |
| 70 | + # All organization users should have read_carriers by default |
| 71 | + ctx_perm.groups.add(read_carriers_group) |
| 72 | + except Exception: |
| 73 | + # Orgs app may not be installed |
| 74 | + pass |
| 75 | + |
| 76 | + |
| 77 | +def reverse_migration(apps, schema_editor): |
| 78 | + """Reverse migration - remove the new groups from context permissions.""" |
| 79 | + Group = apps.get_model('user', 'Group') |
| 80 | + ContextPermission = apps.get_model('iam', 'ContextPermission') |
| 81 | + |
| 82 | + read_carriers_group = Group.objects.filter(name='read_carriers').first() |
| 83 | + write_carriers_group = Group.objects.filter(name='write_carriers').first() |
| 84 | + |
| 85 | + if read_carriers_group: |
| 86 | + for ctx_perm in ContextPermission.objects.filter(groups=read_carriers_group): |
| 87 | + ctx_perm.groups.remove(read_carriers_group) |
| 88 | + |
| 89 | + if write_carriers_group: |
| 90 | + for ctx_perm in ContextPermission.objects.filter(groups=write_carriers_group): |
| 91 | + ctx_perm.groups.remove(write_carriers_group) |
| 92 | + |
| 93 | + |
| 94 | +class Migration(migrations.Migration): |
| 95 | + |
| 96 | + dependencies = [ |
| 97 | + ('iam', '0001_initial'), |
| 98 | + ('user', '0004_group'), |
| 99 | + ] |
| 100 | + |
| 101 | + operations = [ |
| 102 | + migrations.RunPython(setup_carrier_groups, reverse_migration), |
| 103 | + ] |
0 commit comments