2022-06-08 17:59:36 +02:00
|
|
|
"""add user role
|
|
|
|
|
|
|
|
Revision ID: b514cca2d47b
|
|
|
|
Revises: 5f462d2d9d25
|
|
|
|
Create Date: 2022-06-08 17:24:51.305129
|
|
|
|
|
|
|
|
"""
|
|
|
|
from alembic import op
|
|
|
|
import sqlalchemy as sa
|
|
|
|
|
2022-06-10 16:43:10 +02:00
|
|
|
from areas.apps.apps_service import AppsService
|
2022-06-08 17:59:36 +02:00
|
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
|
|
revision = 'b514cca2d47b'
|
|
|
|
down_revision = '5f462d2d9d25'
|
|
|
|
branch_labels = None
|
|
|
|
depends_on = None
|
|
|
|
|
|
|
|
|
|
|
|
def upgrade():
|
|
|
|
# ### end Alembic commands ###
|
|
|
|
|
|
|
|
# Insert role "user" as ID 2
|
|
|
|
op.execute("INSERT INTO `role` (id, `name`) VALUES (2, 'user')")
|
2022-06-10 16:43:10 +02:00
|
|
|
# Insert role "no access" as ID 3
|
|
|
|
op.execute("INSERT INTO `role` (id, `name`) VALUES (3, 'no access')")
|
2022-06-08 17:59:36 +02:00
|
|
|
# Set role_id 2 to all current "user" users which by have NULL role ID
|
|
|
|
op.execute("UPDATE app_role SET role_id = 2 WHERE role_id IS NULL")
|
|
|
|
|
2022-06-10 16:43:10 +02:00
|
|
|
# Add 'no access' role for all users that don't have any roles for specific apps
|
|
|
|
app_ids = [app['id'] for app in AppsService.get_apps()]
|
|
|
|
app_roles = AppsService.get_app_roles()
|
|
|
|
user_ids = [app_role['user_id'] for app_role in app_roles]
|
|
|
|
|
|
|
|
for user_id in user_ids:
|
|
|
|
existing_app_ids = [x['app_id'] for x in list(filter(lambda role: role['user_id'] == user_id, app_roles))]
|
|
|
|
missing_app_ids = [x for x in app_ids if x not in existing_app_ids]
|
|
|
|
|
|
|
|
if len(missing_app_ids) > 0:
|
|
|
|
insert_statement = "INSERT INTO app_role (user_id, app_id, role_id) VALUES"
|
|
|
|
for app_id in missing_app_ids:
|
|
|
|
insert_statement += " ('"+ user_id +"'," + str(app_id) +",3),"
|
|
|
|
op.execute(insert_statement[:-1])
|
|
|
|
|
2022-06-08 17:59:36 +02:00
|
|
|
|
|
|
|
def downgrade():
|
2022-06-10 16:43:10 +02:00
|
|
|
# Revert all users role_id to NULL where role is 'user'
|
2022-06-08 17:59:36 +02:00
|
|
|
op.execute("UPDATE app_role SET role_id = NULL WHERE role_id = 2")
|
2022-06-10 16:43:10 +02:00
|
|
|
# Delete role 'user' from roles
|
2022-06-08 17:59:36 +02:00
|
|
|
op.execute("DELETE FROM `role` WHERE id = 2")
|
2022-06-10 16:43:10 +02:00
|
|
|
|
|
|
|
# Delete all user app roles where role is 'no access' with role_id 3
|
|
|
|
op.execute("DELETE FROM app_role WHERE role_id = 3")
|
|
|
|
# Delete role 'no access' from roles
|
|
|
|
op.execute("DELETE FROM `role` WHERE id = 3")
|