Skip to content

Commit d60bc0d

Browse files
Fix null created by (#344)
* perf: add deleted user db entry * perf: use submodules DELETED_USER_ID * chore: update submodules * perf: deleted user ids/email support * chore: update submodules * chore: update submodules
1 parent ee57aba commit d60bc0d

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""adds deleted user
2+
3+
Revision ID: 58c0e071db99
4+
Revises: de396670d10f
5+
Create Date: 2025-09-15 14:08:25.901703
6+
7+
"""
8+
9+
from alembic import op
10+
from submodules.model import DELETED_USER_ID, DELETED_USER_EMAIL
11+
12+
# revision identifiers, used by Alembic.
13+
revision = "58c0e071db99"
14+
down_revision = "de396670d10f"
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
connection = op.get_bind()
21+
insert_deleted_user_sql = f"""
22+
insert into public."user" (
23+
id,
24+
organization_id,
25+
"role",
26+
last_interaction,
27+
language_display,
28+
email,
29+
verified,
30+
created_at,
31+
metadata_public,
32+
sso_provider,
33+
use_new_cognition_ui,
34+
oidc_identifier
35+
) values (
36+
'{DELETED_USER_ID}',
37+
NULL,
38+
NULL,
39+
NULL,
40+
NULL,
41+
'{DELETED_USER_EMAIL}',
42+
false,
43+
NOW(),
44+
NULL,
45+
NULL,
46+
false,
47+
null
48+
);
49+
"""
50+
connection.execute(insert_deleted_user_sql)
51+
52+
53+
def downgrade():
54+
connection = op.get_bind()
55+
delete_deleted_user_sql = f"""
56+
delete from public."user" where id = '{DELETED_USER_ID}';
57+
"""
58+
connection.execute(delete_deleted_user_sql)

controller/auth/kratos.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from urllib.parse import quote
99

1010
from controller.user import manager
11+
from submodules.model import DELETED_USER_ID, DELETED_USER_EMAIL
1112

1213

1314
logging.basicConfig(level=logging.INFO)
@@ -75,6 +76,24 @@ def __refresh_identity_cache(update_db_users: bool = True) -> None:
7576
else:
7677
KRATOS_IDENTITY_CACHE = {}
7778

79+
# dummy identity for deleted users
80+
# this identity should not be in kratos but in db only
81+
# note that deleted users usually SET_NULL on foreign keys so the id is not in use anymore
82+
KRATOS_IDENTITY_CACHE[DELETED_USER_ID] = {
83+
"identity": {
84+
"id": DELETED_USER_ID,
85+
"traits": {
86+
"email": DELETED_USER_EMAIL,
87+
"name": {"first": "Deleted", "last": "User"},
88+
},
89+
},
90+
"simple": {
91+
"id": DELETED_USER_ID,
92+
"mail": DELETED_USER_EMAIL,
93+
"firstName": "Deleted",
94+
"lastName": "User",
95+
},
96+
}
7897
if update_db_users:
7998
manager.migrate_kratos_users()
8099

controller/monitor/manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
from submodules.model.business_objects import monitor as task_monitor
33
from controller.auth import kratos
44
from submodules.model.util import sql_alchemy_to_dict
5+
from submodules.model import DELETED_USER_ID
56

67

78
def monitor_all_tasks(page: int, limit: int) -> List[Any]:
89
tasks = task_monitor.get_all_tasks(page, limit)
910
tasks_dict = sql_alchemy_to_dict(tasks)
1011
user_ids = {str(t["created_by"]) for t in tasks} # set comprehension
1112
name_lookup = {u_id: kratos.resolve_user_name_by_id(u_id) for u_id in user_ids}
13+
name_lookup[DELETED_USER_ID] = {"first": "Deleted", "last": "User"}
1214

1315
for t in tasks_dict:
1416
created_by_first_last = name_lookup[str(t["created_by"])]

controller/user/manager.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Dict, Optional, Any
2-
from submodules.model import User, daemon, enums
2+
from submodules.model import User, daemon, enums, DELETED_USER_ID
33
from submodules.model.business_objects import user, general
44
from controller.auth import kratos
55
from submodules.model.exceptions import EntityNotFoundException
@@ -138,7 +138,11 @@ def __migrate_kratos_users():
138138

139139
for user_database in users_database:
140140
user_id = str(user_database.id)
141-
if user_id not in users_kratos or users_kratos[user_id] is None:
141+
if (
142+
user_id not in users_kratos
143+
or users_kratos[user_id] is None
144+
or user_id == DELETED_USER_ID
145+
):
142146
continue
143147
user_identity = users_kratos[user_id]["identity"]
144148
if user_database.email != user_identity["traits"]["email"]:

submodules/model

Submodule model updated 1 file

0 commit comments

Comments
 (0)