diff --git a/alembic/versions/58c0e071db99_adds_deleted_user.py b/alembic/versions/58c0e071db99_adds_deleted_user.py new file mode 100644 index 00000000..1f35cca9 --- /dev/null +++ b/alembic/versions/58c0e071db99_adds_deleted_user.py @@ -0,0 +1,58 @@ +"""adds deleted user + +Revision ID: 58c0e071db99 +Revises: de396670d10f +Create Date: 2025-09-15 14:08:25.901703 + +""" + +from alembic import op +from submodules.model import DELETED_USER_ID, DELETED_USER_EMAIL + +# revision identifiers, used by Alembic. +revision = "58c0e071db99" +down_revision = "de396670d10f" +branch_labels = None +depends_on = None + + +def upgrade(): + connection = op.get_bind() + insert_deleted_user_sql = f""" +insert into public."user" ( + id, + organization_id, + "role", + last_interaction, + language_display, + email, + verified, + created_at, + metadata_public, + sso_provider, + use_new_cognition_ui, + oidc_identifier +) values ( + '{DELETED_USER_ID}', + NULL, + NULL, + NULL, + NULL, + '{DELETED_USER_EMAIL}', + false, + NOW(), + NULL, + NULL, + false, + null +); +""" + connection.execute(insert_deleted_user_sql) + + +def downgrade(): + connection = op.get_bind() + delete_deleted_user_sql = f""" +delete from public."user" where id = '{DELETED_USER_ID}'; +""" + connection.execute(delete_deleted_user_sql) diff --git a/controller/auth/kratos.py b/controller/auth/kratos.py index a93c09ef..fc295bfd 100644 --- a/controller/auth/kratos.py +++ b/controller/auth/kratos.py @@ -8,6 +8,7 @@ from urllib.parse import quote from controller.user import manager +from submodules.model import DELETED_USER_ID, DELETED_USER_EMAIL logging.basicConfig(level=logging.INFO) @@ -75,6 +76,24 @@ def __refresh_identity_cache(update_db_users: bool = True) -> None: else: KRATOS_IDENTITY_CACHE = {} + # dummy identity for deleted users + # this identity should not be in kratos but in db only + # note that deleted users usually SET_NULL on foreign keys so the id is not in use anymore + KRATOS_IDENTITY_CACHE[DELETED_USER_ID] = { + "identity": { + "id": DELETED_USER_ID, + "traits": { + "email": DELETED_USER_EMAIL, + "name": {"first": "Deleted", "last": "User"}, + }, + }, + "simple": { + "id": DELETED_USER_ID, + "mail": DELETED_USER_EMAIL, + "firstName": "Deleted", + "lastName": "User", + }, + } if update_db_users: manager.migrate_kratos_users() diff --git a/controller/monitor/manager.py b/controller/monitor/manager.py index f4bdd43b..8d7a117d 100644 --- a/controller/monitor/manager.py +++ b/controller/monitor/manager.py @@ -2,6 +2,7 @@ from submodules.model.business_objects import monitor as task_monitor from controller.auth import kratos from submodules.model.util import sql_alchemy_to_dict +from submodules.model import DELETED_USER_ID def monitor_all_tasks(page: int, limit: int) -> List[Any]: @@ -9,6 +10,7 @@ def monitor_all_tasks(page: int, limit: int) -> List[Any]: tasks_dict = sql_alchemy_to_dict(tasks) user_ids = {str(t["created_by"]) for t in tasks} # set comprehension name_lookup = {u_id: kratos.resolve_user_name_by_id(u_id) for u_id in user_ids} + name_lookup[DELETED_USER_ID] = {"first": "Deleted", "last": "User"} for t in tasks_dict: created_by_first_last = name_lookup[str(t["created_by"])] diff --git a/controller/user/manager.py b/controller/user/manager.py index dfdc4363..1c1e9e39 100644 --- a/controller/user/manager.py +++ b/controller/user/manager.py @@ -1,5 +1,5 @@ from typing import Dict, Optional, Any -from submodules.model import User, daemon, enums +from submodules.model import User, daemon, enums, DELETED_USER_ID from submodules.model.business_objects import user, general from controller.auth import kratos from submodules.model.exceptions import EntityNotFoundException @@ -138,7 +138,11 @@ def __migrate_kratos_users(): for user_database in users_database: user_id = str(user_database.id) - if user_id not in users_kratos or users_kratos[user_id] is None: + if ( + user_id not in users_kratos + or users_kratos[user_id] is None + or user_id == DELETED_USER_ID + ): continue user_identity = users_kratos[user_id]["identity"] if user_database.email != user_identity["traits"]["email"]: diff --git a/submodules/model b/submodules/model index 04dcb6cf..13f55cbc 160000 --- a/submodules/model +++ b/submodules/model @@ -1 +1 @@ -Subproject commit 04dcb6cf25cd374b9662f32f95880e165c62b6af +Subproject commit 13f55cbcc547209a4511f8bc077d8f19ca920914