First Push
This commit is contained in:
@@ -0,0 +1,774 @@
|
||||
"""SQLite database models, initialization, and user-scoped helpers.
|
||||
|
||||
Multi-user design:
|
||||
- The `users` table stores every account. Root (ID 0) is the administrator and
|
||||
is created automatically on first boot (fresh DB) or when migrating an
|
||||
existing single-user database (all pre-existing data becomes Root's data).
|
||||
- Every per-user table carries a `user_id INTEGER NOT NULL DEFAULT 0` column,
|
||||
so legacy single-user rows are automatically attributed to Root on migration
|
||||
and any NEW column added in the future gets a DEFAULT (the migration fills
|
||||
missing columns with their SCHEMA defaults).
|
||||
- `auth_config` is keyed by `user_id` (legacy `id=1` row maps to user 0).
|
||||
"""
|
||||
import sqlite3
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
SCHEMA = """
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
remark TEXT NOT NULL DEFAULT '',
|
||||
role TEXT NOT NULL DEFAULT 'user' CHECK(role IN ('admin','user')),
|
||||
is_temp INTEGER NOT NULL DEFAULT 0,
|
||||
temp_totp_secret TEXT DEFAULT '',
|
||||
status TEXT NOT NULL DEFAULT 'active' CHECK(status IN ('active','pending')),
|
||||
session_version INTEGER NOT NULL DEFAULT 0,
|
||||
theme TEXT NOT NULL DEFAULT 'dark' CHECK(theme IN ('dark','light','auto')),
|
||||
created_at TEXT DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS auth_config (
|
||||
user_id INTEGER PRIMARY KEY DEFAULT 0,
|
||||
passkey_credential_id TEXT DEFAULT '',
|
||||
passkey_public_key TEXT DEFAULT '',
|
||||
totp_secret TEXT DEFAULT '',
|
||||
totp_enabled INTEGER DEFAULT 0,
|
||||
recovery_codes TEXT DEFAULT '',
|
||||
temp_password TEXT DEFAULT '',
|
||||
must_configure_mfa INTEGER DEFAULT 1
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS calendar_groups (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
name TEXT NOT NULL,
|
||||
color TEXT DEFAULT '#4A90D9',
|
||||
enabled INTEGER DEFAULT 1,
|
||||
created_at TEXT DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS calendar_sources (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
group_id INTEGER NOT NULL,
|
||||
type TEXT NOT NULL CHECK(type IN ('upload', 'subscription', 'manual')),
|
||||
name TEXT NOT NULL,
|
||||
url TEXT,
|
||||
ics_data TEXT,
|
||||
last_sync TEXT,
|
||||
FOREIGN KEY (group_id) REFERENCES calendar_groups(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS calendar_events (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
source_id INTEGER NOT NULL,
|
||||
uid TEXT,
|
||||
summary TEXT,
|
||||
description TEXT,
|
||||
location TEXT,
|
||||
dtstart TEXT,
|
||||
dtend TEXT,
|
||||
is_all_day INTEGER DEFAULT 0,
|
||||
FOREIGN KEY (source_id) REFERENCES calendar_sources(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS reminders (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
calendar_group_id INTEGER NOT NULL,
|
||||
minutes_before INTEGER DEFAULT 15,
|
||||
enabled INTEGER DEFAULT 1,
|
||||
FOREIGN KEY (calendar_group_id) REFERENCES calendar_groups(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS weather_config (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
api_key TEXT DEFAULT '',
|
||||
city TEXT DEFAULT 'Beijing',
|
||||
lat REAL,
|
||||
lon REAL,
|
||||
units TEXT DEFAULT 'metric',
|
||||
update_interval INTEGER DEFAULT 1800,
|
||||
jwt_private_key TEXT DEFAULT '',
|
||||
jwt_public_key TEXT DEFAULT '',
|
||||
credential_id TEXT DEFAULT '',
|
||||
project_id TEXT DEFAULT '',
|
||||
qweather_host TEXT DEFAULT ''
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS weather_cache (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
data TEXT,
|
||||
updated_at TEXT DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS weather_hourly_cache (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
data TEXT,
|
||||
fetched_at TEXT DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS weather_minutely_cache (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
data TEXT,
|
||||
fetched_at TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS weather_warning_cache (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
data TEXT,
|
||||
fetched_at TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS clock_config (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
city TEXT DEFAULT 'Beijing',
|
||||
timezone TEXT DEFAULT 'Asia/Shanghai',
|
||||
is_primary INTEGER DEFAULT 1
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS alarms (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
label TEXT DEFAULT '',
|
||||
time TEXT NOT NULL,
|
||||
days TEXT DEFAULT '1,2,3,4,5,6,7',
|
||||
enabled INTEGER DEFAULT 1,
|
||||
sound TEXT DEFAULT 'default',
|
||||
once INTEGER DEFAULT 0
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS countdowns (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
label TEXT DEFAULT '',
|
||||
duration_seconds INTEGER DEFAULT 300,
|
||||
end_time TEXT DEFAULT '',
|
||||
enabled INTEGER DEFAULT 1,
|
||||
sound TEXT DEFAULT 'default'
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS display_config (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
mode TEXT NOT NULL DEFAULT 'default' CHECK(mode IN ('default', 'custom')),
|
||||
custom_html TEXT DEFAULT '',
|
||||
custom_html_1 TEXT DEFAULT '',
|
||||
custom_html_2 TEXT DEFAULT '',
|
||||
custom_html_3 TEXT DEFAULT ''
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pomodoro_state (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL DEFAULT 0,
|
||||
running INTEGER DEFAULT 0,
|
||||
phase TEXT DEFAULT 'work' CHECK(phase IN ('work', 'break')),
|
||||
work_minutes INTEGER DEFAULT 25,
|
||||
break_minutes INTEGER DEFAULT 5,
|
||||
long_break_minutes INTEGER DEFAULT 15,
|
||||
cycles_before_long_break INTEGER DEFAULT 4,
|
||||
end_time TEXT,
|
||||
remaining_seconds INTEGER DEFAULT 1500,
|
||||
cycles INTEGER DEFAULT 0,
|
||||
enabled INTEGER DEFAULT 0,
|
||||
display_controls_enabled INTEGER DEFAULT 1,
|
||||
cycles_date TEXT DEFAULT '',
|
||||
work_label TEXT DEFAULT '工作中',
|
||||
break_label TEXT DEFAULT '休息中'
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_pomodoro_state_user ON pomodoro_state(user_id);
|
||||
"""
|
||||
|
||||
|
||||
# Tables whose rows are completely per-user (used for cascade delete).
|
||||
USER_DATA_TABLES = [
|
||||
"auth_config", "calendar_groups", "calendar_sources", "calendar_events",
|
||||
"reminders", "weather_config", "weather_cache", "weather_hourly_cache",
|
||||
"weather_minutely_cache", "weather_warning_cache", "clock_config",
|
||||
"alarms", "countdowns", "display_config", "pomodoro_state",
|
||||
]
|
||||
|
||||
|
||||
def get_db(db_path: str = "data.db") -> sqlite3.Connection:
|
||||
conn = sqlite3.connect(db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
conn.execute("PRAGMA journal_mode=WAL")
|
||||
conn.execute("PRAGMA foreign_keys=ON")
|
||||
return conn
|
||||
|
||||
|
||||
def _db_path(preferred: str = None) -> str:
|
||||
"""Get the actual database path, preferring current_app config if available."""
|
||||
if preferred:
|
||||
return preferred
|
||||
try:
|
||||
from flask import current_app
|
||||
return current_app.config.get("DB_PATH", "data.db")
|
||||
except Exception:
|
||||
return "data.db"
|
||||
|
||||
|
||||
# ─── Users ───────────────────────────────────
|
||||
|
||||
def get_users(db_path: str = None) -> list:
|
||||
"""Return all users ordered by id."""
|
||||
conn = get_db(_db_path(db_path))
|
||||
rows = [dict(r) for r in conn.execute("SELECT * FROM users ORDER BY id").fetchall()]
|
||||
conn.close()
|
||||
return rows
|
||||
|
||||
|
||||
def get_active_user_ids(db_path: str = None) -> list:
|
||||
"""Return ids of all active (non-pending) users. Used by the scheduler."""
|
||||
conn = get_db(_db_path(db_path))
|
||||
rows = conn.execute("SELECT id FROM users WHERE status='active' ORDER BY id").fetchall()
|
||||
conn.close()
|
||||
return [r["id"] for r in rows]
|
||||
|
||||
|
||||
def get_user(user_id: int, db_path: str = None) -> dict | None:
|
||||
conn = get_db(_db_path(db_path))
|
||||
row = conn.execute("SELECT * FROM users WHERE id=?", (user_id,)).fetchone()
|
||||
conn.close()
|
||||
return dict(row) if row else None
|
||||
|
||||
|
||||
def user_exists(user_id: int, db_path: str = None) -> bool:
|
||||
conn = get_db(_db_path(db_path))
|
||||
row = conn.execute("SELECT 1 FROM users WHERE id=?", (user_id,)).fetchone()
|
||||
conn.close()
|
||||
return row is not None
|
||||
|
||||
|
||||
def is_admin_user(user_id: int, db_path: str = None) -> bool:
|
||||
"""Root is always admin; otherwise check the role column."""
|
||||
if user_id == 0:
|
||||
return True
|
||||
conn = get_db(_db_path(db_path))
|
||||
row = conn.execute("SELECT role FROM users WHERE id=?", (user_id,)).fetchone()
|
||||
conn.close()
|
||||
return bool(row and row["role"] == "admin")
|
||||
|
||||
|
||||
def create_user(remark: str = "", role: str = "user", is_temp: int = 0,
|
||||
temp_totp_secret: str = "", status: str = "active",
|
||||
db_path: str = None) -> int:
|
||||
"""Create a user. Returns the new user id.
|
||||
The remark defaults to the user's own id (the only place an id is shown).
|
||||
"""
|
||||
conn = get_db(_db_path(db_path))
|
||||
cur = conn.execute(
|
||||
"INSERT INTO users (remark, role, is_temp, temp_totp_secret, status) VALUES (?, ?, ?, ?, ?)",
|
||||
(remark, role, 1 if is_temp else 0, temp_totp_secret, status)
|
||||
)
|
||||
uid = cur.lastrowid
|
||||
if not remark:
|
||||
conn.execute("UPDATE users SET remark=? WHERE id=?", (str(uid), uid))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return uid
|
||||
|
||||
|
||||
def update_user(user_id: int, remark: str = None, role: str = None,
|
||||
is_temp: int = None, temp_totp_secret: str = None,
|
||||
status: str = None, theme: str = None, db_path: str = None):
|
||||
conn = get_db(_db_path(db_path))
|
||||
fields = []
|
||||
vals = []
|
||||
for key, val in [("remark", remark), ("role", role), ("is_temp", is_temp),
|
||||
("temp_totp_secret", temp_totp_secret), ("status", status),
|
||||
("theme", theme)]:
|
||||
if val is not None:
|
||||
fields.append(f"{key}=?")
|
||||
vals.append(val)
|
||||
if fields:
|
||||
vals.append(user_id)
|
||||
conn.execute(f"UPDATE users SET {', '.join(fields)} WHERE id=?", vals)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
def delete_user(user_id: int, db_path: str = None):
|
||||
"""Delete a user and ALL of their data. Root (id 0) cannot be deleted.
|
||||
|
||||
Deletion order respects foreign keys (children before parents).
|
||||
"""
|
||||
if user_id == 0:
|
||||
return
|
||||
conn = get_db(_db_path(db_path))
|
||||
# Children first so FK constraints never reject a delete.
|
||||
ordered = [
|
||||
"calendar_events", "calendar_sources", "reminders", "calendar_groups",
|
||||
"weather_cache", "weather_hourly_cache", "weather_minutely_cache",
|
||||
"weather_warning_cache", "weather_config", "clock_config",
|
||||
"alarms", "countdowns", "display_config", "pomodoro_state", "auth_config",
|
||||
]
|
||||
try:
|
||||
for table in ordered:
|
||||
conn.execute(f"DELETE FROM {table} WHERE user_id=?", (user_id,))
|
||||
conn.execute("DELETE FROM users WHERE id=?", (user_id,))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_user_session_version(user_id: int, db_path: str = None) -> int:
|
||||
conn = get_db(_db_path(db_path))
|
||||
row = conn.execute("SELECT session_version FROM users WHERE id=?", (user_id,)).fetchone()
|
||||
conn.close()
|
||||
return row["session_version"] if row else -1
|
||||
|
||||
|
||||
def bump_user_session_version(user_id: int, db_path: str = None) -> int:
|
||||
"""Invalidate all sessions of one user by incrementing their version."""
|
||||
conn = get_db(_db_path(db_path))
|
||||
conn.execute("UPDATE users SET session_version = session_version + 1 WHERE id=?", (user_id,))
|
||||
conn.commit()
|
||||
row = conn.execute("SELECT session_version FROM users WHERE id=?", (user_id,)).fetchone()
|
||||
conn.close()
|
||||
return row["session_version"] if row else -1
|
||||
|
||||
|
||||
def bump_all_session_versions(db_path: str = None) -> int:
|
||||
"""Invalidate every session (admin 'kick all' action)."""
|
||||
conn = get_db(_db_path(db_path))
|
||||
conn.execute("UPDATE users SET session_version = session_version + 1")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return 0
|
||||
|
||||
|
||||
def is_temp_account(user_id: int, db_path: str = None) -> bool:
|
||||
"""True if the account is a temp account (awaiting first login-method setup)."""
|
||||
conn = get_db(_db_path(db_path))
|
||||
row = conn.execute("SELECT is_temp FROM users WHERE id=?", (user_id,)).fetchone()
|
||||
conn.close()
|
||||
return bool(row and row["is_temp"])
|
||||
|
||||
|
||||
def get_temp_totp_secret(user_id: int, db_path: str = None) -> str:
|
||||
conn = get_db(_db_path(db_path))
|
||||
row = conn.execute("SELECT temp_totp_secret FROM users WHERE id=?", (user_id,)).fetchone()
|
||||
conn.close()
|
||||
return (row["temp_totp_secret"] or "") if row else ""
|
||||
|
||||
|
||||
def finalize_temp_account(user_id: int, db_path: str = None):
|
||||
"""Convert a temp account into a formal account:
|
||||
invalidates the temp TOTP and clears the temp flag.
|
||||
"""
|
||||
conn = get_db(_db_path(db_path))
|
||||
conn.execute(
|
||||
"UPDATE users SET is_temp=0, temp_totp_secret='' WHERE id=?",
|
||||
(user_id,)
|
||||
)
|
||||
conn.execute(
|
||||
"UPDATE auth_config SET must_configure_mfa=0 WHERE user_id=?",
|
||||
(user_id,)
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
# ─── Display config (per user) ──────────────
|
||||
|
||||
def _ensure_display_row(conn, user_id: int):
|
||||
row = conn.execute("SELECT id FROM display_config WHERE user_id=?", (user_id,)).fetchone()
|
||||
if row is None:
|
||||
conn.execute(
|
||||
"INSERT INTO display_config (user_id, mode) VALUES (?, 'default')",
|
||||
(user_id,)
|
||||
)
|
||||
|
||||
|
||||
def get_display_config(db_path: str = None, user_id: int = 0) -> dict:
|
||||
db = get_db(_db_path(db_path))
|
||||
row = db.execute(
|
||||
"SELECT mode, custom_html, custom_html_1, custom_html_2, custom_html_3 "
|
||||
"FROM display_config WHERE user_id=?",
|
||||
(user_id,)
|
||||
).fetchone()
|
||||
if row is None:
|
||||
db.execute(
|
||||
"INSERT INTO display_config (user_id, mode, custom_html, custom_html_1, custom_html_2, custom_html_3) "
|
||||
"VALUES (?, 'default', '', '', '', '')",
|
||||
(user_id,)
|
||||
)
|
||||
db.commit()
|
||||
cfg = {"mode": "default", "custom_html": "", "custom_html_1": "", "custom_html_2": "", "custom_html_3": ""}
|
||||
else:
|
||||
cfg = {
|
||||
"mode": row["mode"],
|
||||
"custom_html": row["custom_html"] or "",
|
||||
"custom_html_1": row["custom_html_1"] or "",
|
||||
"custom_html_2": row["custom_html_2"] or "",
|
||||
"custom_html_3": row["custom_html_3"] or "",
|
||||
}
|
||||
db.close()
|
||||
return cfg
|
||||
|
||||
|
||||
def set_display_mode(mode: str, db_path: str = None, user_id: int = 0) -> bool:
|
||||
if mode not in ("default", "custom"):
|
||||
return False
|
||||
db = get_db(_db_path(db_path))
|
||||
_ensure_display_row(db, user_id)
|
||||
db.execute("UPDATE display_config SET mode=? WHERE user_id=?", (mode, user_id))
|
||||
db.commit()
|
||||
db.close()
|
||||
return True
|
||||
|
||||
|
||||
def set_custom_html(html: str, db_path: str = None, user_id: int = 0) -> bool:
|
||||
db = get_db(_db_path(db_path))
|
||||
_ensure_display_row(db, user_id)
|
||||
db.execute("UPDATE display_config SET custom_html=? WHERE user_id=?", (html, user_id))
|
||||
db.commit()
|
||||
db.close()
|
||||
return True
|
||||
|
||||
|
||||
def set_custom_html_template(index: int, html: str, db_path: str = None, user_id: int = 0) -> bool:
|
||||
if index not in (1, 2, 3):
|
||||
return False
|
||||
db = get_db(_db_path(db_path))
|
||||
_ensure_display_row(db, user_id)
|
||||
db.execute(
|
||||
f"UPDATE display_config SET custom_html_{index}=? WHERE user_id=?",
|
||||
(html, user_id)
|
||||
)
|
||||
db.commit()
|
||||
db.close()
|
||||
return True
|
||||
|
||||
|
||||
def apply_custom_html(index: int, db_path: str = None, user_id: int = 0) -> bool:
|
||||
"""Copy template N (0=main custom_html, 1-3=custom_html_1-3) into custom_html and set mode to custom."""
|
||||
db = get_db(_db_path(db_path))
|
||||
row = db.execute(
|
||||
"SELECT custom_html, custom_html_1, custom_html_2, custom_html_3 "
|
||||
"FROM display_config WHERE user_id=?",
|
||||
(user_id,)
|
||||
).fetchone()
|
||||
if row is None:
|
||||
db.close()
|
||||
return False
|
||||
if index == 0:
|
||||
html = row["custom_html"] or ""
|
||||
elif index == 1:
|
||||
html = row["custom_html_1"] or ""
|
||||
elif index == 2:
|
||||
html = row["custom_html_2"] or ""
|
||||
elif index == 3:
|
||||
html = row["custom_html_3"] or ""
|
||||
else:
|
||||
db.close()
|
||||
return False
|
||||
db.execute("UPDATE display_config SET mode='custom', custom_html=? WHERE user_id=?", (html, user_id))
|
||||
db.commit()
|
||||
db.close()
|
||||
return True
|
||||
|
||||
|
||||
# ─── Auth / MFA helpers (per user) ──────────
|
||||
|
||||
def get_auth_config(db_path: str = None, user_id: int = 0) -> dict:
|
||||
"""Return the full auth_config row for a user, with defaults for missing fields."""
|
||||
conn = get_db(_db_path(db_path))
|
||||
row = conn.execute("SELECT * FROM auth_config WHERE user_id = ?", (user_id,)).fetchone()
|
||||
if row is None:
|
||||
conn.execute(
|
||||
"""INSERT INTO auth_config (user_id, passkey_credential_id, passkey_public_key,
|
||||
totp_secret, totp_enabled, recovery_codes, temp_password, must_configure_mfa)
|
||||
VALUES (?, '', '', '', 0, '', '', 1)""",
|
||||
(user_id,)
|
||||
)
|
||||
conn.commit()
|
||||
cfg = {
|
||||
"user_id": user_id,
|
||||
"passkey_credential_id": "",
|
||||
"passkey_public_key": "",
|
||||
"totp_secret": "",
|
||||
"totp_enabled": 0,
|
||||
"recovery_codes": "",
|
||||
"temp_password": "",
|
||||
"must_configure_mfa": 1,
|
||||
}
|
||||
else:
|
||||
cfg = {
|
||||
"user_id": row["user_id"],
|
||||
"passkey_credential_id": row["passkey_credential_id"] or "",
|
||||
"passkey_public_key": row["passkey_public_key"] or "",
|
||||
"totp_secret": row["totp_secret"] or "",
|
||||
"totp_enabled": bool(row["totp_enabled"]),
|
||||
"recovery_codes": row["recovery_codes"] or "",
|
||||
"temp_password": row["temp_password"] or "",
|
||||
"must_configure_mfa": bool(row["must_configure_mfa"]),
|
||||
}
|
||||
conn.close()
|
||||
return cfg
|
||||
|
||||
|
||||
def update_auth_config(user_id: int = 0, **kwargs):
|
||||
"""Update auth_config fields. Keys match DB column names."""
|
||||
db_path = _db_path(kwargs.pop("_db_path", None))
|
||||
conn = get_db(db_path)
|
||||
row = conn.execute("SELECT user_id FROM auth_config WHERE user_id = ?", (user_id,)).fetchone()
|
||||
fields = []
|
||||
vals = []
|
||||
for key, val in kwargs.items():
|
||||
if key in ("passkey_credential_id", "passkey_public_key", "totp_secret",
|
||||
"recovery_codes", "temp_password", "must_configure_mfa", "totp_enabled"):
|
||||
fields.append(f"{key}=?")
|
||||
vals.append(val)
|
||||
if not fields:
|
||||
conn.close()
|
||||
return
|
||||
if row is None:
|
||||
cols = ["user_id"] + [f.split("=")[0] for f in fields]
|
||||
col_str = ", ".join(cols)
|
||||
ph = ", ".join("?" * len(cols))
|
||||
conn.execute(f"INSERT INTO auth_config ({col_str}) VALUES ({ph})", (user_id, *vals))
|
||||
else:
|
||||
vals.append(user_id)
|
||||
conn.execute(f"UPDATE auth_config SET {', '.join(fields)} WHERE user_id=?", vals)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_temp_password(db_path: str = None, user_id: int = 0) -> str:
|
||||
cfg = get_auth_config(db_path, user_id)
|
||||
return cfg["temp_password"]
|
||||
|
||||
|
||||
def set_temp_password(password: str, db_path: str = None, user_id: int = 0):
|
||||
update_auth_config(user_id, temp_password=password, _db_path=db_path)
|
||||
|
||||
|
||||
def clear_temp_password(db_path: str = None, user_id: int = 0):
|
||||
update_auth_config(user_id, temp_password="", must_configure_mfa=0, _db_path=db_path)
|
||||
|
||||
|
||||
def get_recovery_codes(db_path: str = None, user_id: int = 0) -> list:
|
||||
cfg = get_auth_config(db_path, user_id)
|
||||
raw = cfg["recovery_codes"]
|
||||
if not raw:
|
||||
return []
|
||||
return raw.split(",")
|
||||
|
||||
|
||||
def set_recovery_codes(codes: list, db_path: str = None, user_id: int = 0):
|
||||
update_auth_config(user_id, recovery_codes=",".join(codes), _db_path=db_path)
|
||||
|
||||
|
||||
def has_mfa_configured(db_path: str = None, user_id: int = 0) -> bool:
|
||||
cfg = get_auth_config(db_path, user_id)
|
||||
return bool(cfg["passkey_credential_id"]) or bool(cfg["totp_enabled"])
|
||||
|
||||
|
||||
def find_user_by_credential_id(credential_id: str, db_path: str = None) -> int | None:
|
||||
"""Find the user who owns a given WebAuthn credential id (for login)."""
|
||||
conn = get_db(_db_path(db_path))
|
||||
row = conn.execute(
|
||||
"SELECT a.user_id FROM auth_config a "
|
||||
"JOIN users u ON u.id = a.user_id "
|
||||
"WHERE a.passkey_credential_id = ? AND u.status = 'active'",
|
||||
(credential_id,)
|
||||
).fetchone()
|
||||
conn.close()
|
||||
return row["user_id"] if row else None
|
||||
|
||||
|
||||
# ─── DB init / migration ─────────────────────
|
||||
|
||||
def _ensure_defaults(conn):
|
||||
"""Insert default rows for config tables and the Root user when missing.
|
||||
Generates a random 6-digit temp password when Root's auth_config is created
|
||||
(or when a migrated DB has no MFA configured yet).
|
||||
"""
|
||||
import secrets
|
||||
import string
|
||||
|
||||
# Root user (id 0)
|
||||
root = conn.execute("SELECT id FROM users WHERE id=0").fetchone()
|
||||
if root is None:
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO users (id, remark, role, status) VALUES (0, 'Root', 'admin', 'active')"
|
||||
)
|
||||
|
||||
# weather_config for Root
|
||||
count = conn.execute("SELECT COUNT(*) FROM weather_config WHERE user_id=0").fetchone()[0]
|
||||
if count == 0:
|
||||
conn.execute("INSERT INTO weather_config (user_id, city, lat, lon) "
|
||||
"VALUES (0, 'Beijing', 39.9042, 116.4074)")
|
||||
|
||||
# clock_config for Root
|
||||
count = conn.execute("SELECT COUNT(*) FROM clock_config WHERE user_id=0").fetchone()[0]
|
||||
if count == 0:
|
||||
conn.execute("INSERT INTO clock_config (user_id, city, timezone, is_primary) "
|
||||
"VALUES (0, 'Beijing', 'Asia/Shanghai', 1)")
|
||||
|
||||
# auth_config for Root
|
||||
row = conn.execute("SELECT * FROM auth_config WHERE user_id=0").fetchone()
|
||||
mfa_configured = bool(row and (row["passkey_credential_id"] or row["totp_enabled"]))
|
||||
if row is None:
|
||||
temp_pw = "".join(secrets.choice(string.digits) for _ in range(6))
|
||||
conn.execute(
|
||||
"INSERT INTO auth_config (user_id, passkey_credential_id, passkey_public_key, "
|
||||
"totp_secret, totp_enabled, recovery_codes, temp_password, must_configure_mfa) "
|
||||
"VALUES (0, '', '', '', 0, '', ?, 1)",
|
||||
(temp_pw,)
|
||||
)
|
||||
_EMITTED_PASSWORDS.append(temp_pw)
|
||||
print(f"\n{'='*50}", file=sys.stderr)
|
||||
print(f" 🔐 Root 初始临时密码: {temp_pw}", file=sys.stderr)
|
||||
print(f" ⚠️ 请使用此密码登录 Root 并配置双因素认证", file=sys.stderr)
|
||||
print(f"{'='*50}\n", file=sys.stderr)
|
||||
else:
|
||||
# Migrated DB. If Root already has MFA, no temp password is needed.
|
||||
if mfa_configured:
|
||||
if row["temp_password"] or row["must_configure_mfa"]:
|
||||
conn.execute(
|
||||
"UPDATE auth_config SET temp_password='', must_configure_mfa=0 WHERE user_id=0"
|
||||
)
|
||||
elif (not row["temp_password"]) and row["must_configure_mfa"]:
|
||||
# Migrated DB without MFA — generate a fresh temp password.
|
||||
temp_pw = "".join(secrets.choice(string.digits) for _ in range(6))
|
||||
conn.execute(
|
||||
"UPDATE auth_config SET temp_password=? WHERE user_id=0",
|
||||
(temp_pw,)
|
||||
)
|
||||
_EMITTED_PASSWORDS.append(temp_pw)
|
||||
print(f"\n{'='*50}", file=sys.stderr)
|
||||
print(f" 🔐 迁移已存在数据库 — Root 临时密码: {temp_pw}", file=sys.stderr)
|
||||
print(f" ⚠️ 请使用此密码登录 Root 并配置双因素认证", file=sys.stderr)
|
||||
print(f"{'='*50}\n", file=sys.stderr)
|
||||
|
||||
conn.commit()
|
||||
|
||||
|
||||
# Stores passwords that were just generated, for the startup script to print.
|
||||
_EMITTED_PASSWORDS: list = []
|
||||
|
||||
|
||||
def _table_columns(conn, table_name):
|
||||
return [row["name"] for row in conn.execute(f"PRAGMA table_info({table_name})")]
|
||||
|
||||
|
||||
def _migrate_auth_config(conn_new, conn_old):
|
||||
"""Special-case migration for auth_config: the legacy table is keyed by
|
||||
`id` (single user, id=1), the new table is keyed by `user_id`.
|
||||
The legacy single-user row belongs to Root (user 0).
|
||||
"""
|
||||
old_cols = _table_columns(conn_old, "auth_config")
|
||||
new_cols = _table_columns(conn_new, "auth_config")
|
||||
if not old_cols:
|
||||
return
|
||||
rows = conn_old.execute("SELECT * FROM auth_config").fetchall()
|
||||
for row in rows:
|
||||
d = dict(row)
|
||||
if "user_id" in old_cols:
|
||||
uid = d.get("user_id") or 0
|
||||
else:
|
||||
# Legacy single-user DB: the id=1 row belongs to Root (user 0).
|
||||
old_id = d.get("id", 1)
|
||||
uid = 0 if old_id == 1 else (old_id or 0)
|
||||
common = [c for c in new_cols if c in old_cols and c not in ("id", "user_id")]
|
||||
if not common:
|
||||
continue
|
||||
col_str = ", ".join(common)
|
||||
placeholders = ", ".join("?" * len(common))
|
||||
conn_new.execute(
|
||||
f"INSERT OR IGNORE INTO auth_config (user_id, {col_str}) VALUES (?, {placeholders})",
|
||||
(uid, *[d[c] for c in common])
|
||||
)
|
||||
|
||||
|
||||
def init_db(db_path: str = "data.db"):
|
||||
"""Initialize the database.
|
||||
|
||||
- No database file: create it from the latest SCHEMA and create Root.
|
||||
- Existing database: rebuild it from the latest SCHEMA, migrate compatible
|
||||
data (legacy single-user data becomes Root's), fill missing columns with
|
||||
SCHEMA defaults, then replace the old file (keeping a backup).
|
||||
"""
|
||||
if not os.path.exists(db_path):
|
||||
conn = get_db(db_path)
|
||||
conn.executescript(SCHEMA)
|
||||
_ensure_defaults(conn)
|
||||
conn.close()
|
||||
return
|
||||
|
||||
# Migrate by rebuilding: create a fresh DB with the latest schema,
|
||||
# copy compatible data from the old DB, then replace the old DB.
|
||||
new_db_path = db_path + ".new"
|
||||
backup_path = db_path + ".backup"
|
||||
|
||||
# Clean up any stale temporary database from a previous failed run.
|
||||
if os.path.exists(new_db_path):
|
||||
os.remove(new_db_path)
|
||||
|
||||
conn_new = get_db(new_db_path)
|
||||
conn_new.executescript(SCHEMA)
|
||||
|
||||
conn_old = get_db(db_path)
|
||||
# Disable FK enforcement while copying; rows are copied table by table.
|
||||
conn_old.execute("PRAGMA foreign_keys=OFF")
|
||||
conn_new.execute("PRAGMA foreign_keys=OFF")
|
||||
|
||||
tables = conn_old.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
|
||||
).fetchall()
|
||||
|
||||
for (table_name,) in tables:
|
||||
# Tables that no longer exist in the new schema are dropped (e.g. camera_config).
|
||||
exists_new = conn_new.execute(
|
||||
"SELECT 1 FROM sqlite_master WHERE type='table' AND name=?",
|
||||
(table_name,)
|
||||
).fetchone()
|
||||
if not exists_new:
|
||||
continue
|
||||
|
||||
if table_name == "auth_config":
|
||||
_migrate_auth_config(conn_new, conn_old)
|
||||
continue
|
||||
|
||||
old_cols = _table_columns(conn_old, table_name)
|
||||
new_cols = _table_columns(conn_new, table_name)
|
||||
common_cols = [c for c in new_cols if c in old_cols]
|
||||
if not common_cols:
|
||||
continue
|
||||
|
||||
col_str = ", ".join(common_cols)
|
||||
placeholders = ", ".join("?" * len(common_cols))
|
||||
sql = f"INSERT OR IGNORE INTO {table_name} ({col_str}) VALUES ({placeholders})"
|
||||
|
||||
for row in conn_old.execute(f"SELECT {col_str} FROM {table_name}"):
|
||||
try:
|
||||
conn_new.execute(sql, tuple(row))
|
||||
except Exception:
|
||||
# Ignore rows that cannot be migrated (e.g. constraint violations).
|
||||
pass
|
||||
|
||||
conn_new.commit()
|
||||
|
||||
# Now fill defaults for any tables that are still empty + Root bootstrap.
|
||||
_ensure_defaults(conn_new)
|
||||
|
||||
conn_old.close()
|
||||
conn_new.close()
|
||||
|
||||
# Replace old DB with the new one, keeping a backup.
|
||||
if os.path.exists(backup_path):
|
||||
os.remove(backup_path)
|
||||
os.rename(db_path, backup_path)
|
||||
os.rename(new_db_path, db_path)
|
||||
Reference in New Issue
Block a user