Skip to main content

cognee_lib/api/
user.rs

1//! Default user management.
2//!
3//! Mirrors Python's `get_default_user()` / `create_default_user()`.
4
5use cognee_database::DatabaseError;
6use cognee_models::User;
7use uuid::Uuid;
8
9/// Materialise the OSS default user **without** touching the database.
10///
11/// The OSS build does not implement authentication (the `users` table is
12/// touched only by the closed cloud build via its own DB-backed
13/// `get_or_create_default_user`), so this function constructs an in-memory
14/// [`User`] record from the configured `default_user_email`.
15///
16/// The id is `Uuid::new_v5(&NAMESPACE_OID, default_user_email.as_bytes())`
17/// — the same derivation used by the Python reference SDK
18/// (`uuid5(NAMESPACE_OID, email)`). **This MUST NOT change** without a
19/// corresponding update in the Python SDK; cross-SDK parity tests
20/// (`e2e-cross-sdk` and the Neon binding's `sdk_handle.test.ts`) assert
21/// this exact derivation.
22///
23/// Takes `&str` rather than `&Settings` so callers can read `Settings`
24/// under an `RwLock` guard, snapshot the email, **drop the guard**, and
25/// then `.await` — `std::sync::RwLockReadGuard` is `!Send` and would
26/// otherwise poison the surrounding future's `Send` bound.
27///
28/// Kept `async` and fallible so call sites stay uniform with the
29/// closed-build replacement (which performs real DB I/O and can fail).
30pub async fn get_or_create_default_user(default_user_email: &str) -> Result<User, DatabaseError> {
31    let id = Uuid::new_v5(&Uuid::NAMESPACE_OID, default_user_email.as_bytes());
32    Ok(User {
33        id,
34        email: default_user_email.to_string(),
35        is_active: true,
36        is_superuser: true,
37        tenant_id: None,
38        created_at: chrono::Utc::now(),
39        updated_at: None,
40    })
41}