feat: управление пользователями в админке

- Заблокировать/разблокировать (blocked поле + проверка при логине)
- Удалить (с confirm, нельзя удалить себя)
- Изменить роль (admin ↔ user)
- Ручная верификация email
- Миграция: ALTER TABLE users ADD blocked
- UI: кнопки действий с цветными статусами

Тест: register → verify → block(401) → unblock(200) → delete ✅

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-27 16:15:07 +10:00
co-authored by Claude Opus 4.6
parent ee3d3e8c24
commit 288cbe17c5
4 changed files with 119 additions and 17 deletions
+29 -1
View File
@@ -25,6 +25,7 @@ CREATE TABLE IF NOT EXISTS users (
password_hash TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'user',
verified INTEGER NOT NULL DEFAULT 0,
blocked INTEGER NOT NULL DEFAULT 0,
verify_code TEXT,
verify_expires REAL,
created_at REAL NOT NULL
@@ -51,6 +52,11 @@ def _conn() -> sqlite3.Connection:
def init_db():
with _conn() as conn:
conn.executescript(_CREATE)
# Migration: add blocked column if missing
try:
conn.execute("SELECT blocked FROM users LIMIT 1")
except sqlite3.OperationalError:
conn.execute("ALTER TABLE users ADD COLUMN blocked INTEGER NOT NULL DEFAULT 0")
def save_message(username: str, tab_id: str, role: str, content: str):
@@ -121,11 +127,33 @@ def verify_user(email: str, code: str) -> bool:
def list_users() -> list[dict]:
with _conn() as conn:
rows = conn.execute(
"SELECT id, email, role, verified, created_at FROM users ORDER BY created_at DESC"
"SELECT id, email, role, verified, blocked, created_at FROM users ORDER BY created_at DESC"
).fetchall()
return [dict(r) for r in rows]
def block_user(email: str, blocked: bool = True):
with _conn() as conn:
conn.execute("UPDATE users SET blocked = ? WHERE email = ?", (1 if blocked else 0, email.lower().strip()))
def delete_user(email: str):
with _conn() as conn:
conn.execute("DELETE FROM users WHERE email = ?", (email.lower().strip(),))
def set_user_role(email: str, role: str):
if role not in ("admin", "user"):
return
with _conn() as conn:
conn.execute("UPDATE users SET role = ? WHERE email = ?", (role, email.lower().strip()))
def admin_verify_user(email: str):
with _conn() as conn:
conn.execute("UPDATE users SET verified = 1, verify_code = NULL WHERE email = ?", (email.lower().strip(),))
# === Invites ===
def create_invite(token: str, created_by: str, expires_hours: int = 72) -> int: