r"""Gemeinsame DB-Zugriffe (Benutzer + Admin-Bootstrap).""" from db import get_connection from config import Config from auth import hash_password def get_user_by_username(username: str) -> dict | None: with get_connection() as conn: with conn.cursor() as cur: cur.execute("SELECT * FROM users WHERE username = %s", (username,)) return cur.fetchone() def get_user_by_id(user_id: int) -> dict | None: with get_connection() as conn: with conn.cursor() as cur: cur.execute("SELECT * FROM users WHERE id = %s", (user_id,)) return cur.fetchone() def create_user(username: str, password: str, email: str = "", is_admin: bool = False) -> None: salt, password_hash = hash_password(password) with get_connection() as conn: with conn.cursor() as cur: cur.execute( "INSERT INTO users (username, email, salt, password_hash, is_admin) " "VALUES (%s, %s, %s, %s, %s)", (username, email, salt, password_hash, 1 if is_admin else 0), ) def ensure_admin(app): """Legt beim ersten Start den Admin aus den Env-Variablen an, falls keiner existiert.""" with app.app_context(): with get_connection() as conn: with conn.cursor() as cur: cur.execute("SELECT COUNT(*) AS c FROM users WHERE is_admin = 1") count = cur.fetchone()["c"] if count == 0: create_user(Config.ADMIN_USERNAME, Config.ADMIN_PASSWORD, is_admin=True) print(f"Admin-Benutzer '{Config.ADMIN_USERNAME}' angelegt.")