Skip to content

Commit

Permalink
Update the permissions migration to look at role names
Browse files Browse the repository at this point in the history
  • Loading branch information
matti-lamppu committed Sep 10, 2024
1 parent 7fb0f00 commit e78e2a2
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions permissions/migrations/0029_migrate_permissions_to_new_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,21 @@ def migrate_permissions_to_new_models(apps, schema_editor):
GeneralRole = apps.get_model("permissions", "GeneralRole")
NewGeneralRole = apps.get_model("permissions", "NewGeneralRole")

new_general_roles = [
NewGeneralRole(
role=_determine_role(permissions=sorted(perm.permission.lower() for perm in role.role.permissions.all())),
user=role.user,
assigner=role.assigner,
created=role.created,
modified=role.modified,
general_roles = GeneralRole.objects.select_related("user", "role").prefetch_related("role__permissions").all()

new_general_roles = []
for role in general_roles:
permissions = sorted(perm.permission.lower() for perm in role.role.permissions.all())
new_general_roles.append(
NewGeneralRole(
role=_determine_role(permissions=permissions, role_name=role.role.code),
user=role.user,
assigner=role.assigner,
created=role.created,
modified=role.modified,
)
)
for role in GeneralRole.objects.select_related("user", "role").prefetch_related("role__permissions").all()
]

NewGeneralRole.objects.bulk_create(new_general_roles)

# Unit permissions
Expand All @@ -68,8 +73,9 @@ def migrate_permissions_to_new_models(apps, schema_editor):
new_unit_roles_units = []
new_unit_roles_unit_groups = []
for role in unit_roles:
permissions = sorted(perm.permission.lower() for perm in role.role.permissions.all())
new_role = NewUnitRole(
role=_determine_role(permissions=sorted(perm.permission.lower() for perm in role.role.permissions.all())),
role=_determine_role(permissions=permissions, role_name=role.role.code),
user=role.user,
assigner=role.assigner,
created=role.created,
Expand All @@ -88,14 +94,16 @@ def migrate_permissions_to_new_models(apps, schema_editor):
NewUnitRoleUnitGroup.objects.bulk_create(new_unit_roles_unit_groups)


def _determine_role(permissions: list[str]) -> str:
if permissions == RESERVER_PERMISSIONS:
def _determine_role(permissions: list[str], role_name: str) -> str:
# Role names can technically be anything, but these names are used in production,
# which is the most critical part for the migration.
if role_name == "Varaaja" or permissions == RESERVER_PERMISSIONS:
return UserRoleChoices.RESERVER.value
if permissions == VIEWER_PERMISSIONS:
if role_name in ["Katselija", "viewer"] or permissions == VIEWER_PERMISSIONS:
return UserRoleChoices.VIEWER.value
if permissions == NOTIFICATION_HANDLER_PERMISSIONS:
if role_name == "communications_ALL" or permissions == NOTIFICATION_HANDLER_PERMISSIONS:
return UserRoleChoices.NOTIFICATION_MANAGER.value
if permissions == HANDLER_PERMISSIONS:
if role_name in ["Käsittelijä", "handler"] or permissions == HANDLER_PERMISSIONS:
return UserRoleChoices.HANDLER.value
return UserRoleChoices.ADMIN.value

Expand Down

0 comments on commit e78e2a2

Please sign in to comment.