new file: .gitignore new file: Dockerfile new file: README.md new file: app.py new file: auth.py new file: config.py new file: db.py new file: db_helpers.py new file: docker-compose.yml new file: requirements.txt new file: routes/__init__.py new file: routes/admin_routes.py new file: routes/auth_routes.py new file: routes/journal_routes.py new file: static/css/style.css new file: static/js/main.js new file: templates/admin/dashboard.html new file: templates/admin/users.html new file: templates/base.html new file: templates/day.html new file: templates/future.html new file: templates/index.html new file: templates/login.html new file: templates/week.html
42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
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.")
|