feat: administration — users, roles, stats, audit
DB: - login_history table (user_id, ip, user_agent, timestamp) - Migration v2.5: alter users + create login_history Auth: - Premier utilisateur = admin (is_admin=1) - _log_login() après chaque connexion (register, local-login, OAuth) Backend (app/routers/admin.py): - admin_required middleware (vérifie is_admin) - GET /api/admin/users → liste users + stats par user - POST /api/admin/users → créer user - PUT /api/admin/users/:id → modifier (name, email, password, admin, active) - DELETE /api/admin/users/:id → supprimer + cascade - GET /api/admin/stats → totaux globaux - GET /api/admin/audit → historique login Frontend (settings.html): - Nav Admin visible seulement si userIsAdmin - Users & Roles: stats cards, create/edit/delete users, table complète - Audit Log: historique login avec date, IP, user-agent - CSS professionnel: table-wrap, admin-table, stat-card, role-badge, status-dot
This commit is contained in:
@@ -37,6 +37,14 @@ def init_db():
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS login_history (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id),
|
||||
ip_address TEXT DEFAULT '',
|
||||
user_agent TEXT DEFAULT '',
|
||||
logged_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_tokens (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
gitea_user_id INTEGER NOT NULL UNIQUE,
|
||||
@@ -356,6 +364,21 @@ def init_db():
|
||||
conn.execute(f"ALTER TABLE users ADD COLUMN {col} {'TEXT' if col in ('password_hash','last_login','locked_until') else 'INTEGER NOT NULL DEFAULT ' + ('1' if col=='is_active' else '0')}")
|
||||
except sqlite3.OperationalError:
|
||||
pass
|
||||
# v2.5: Login history
|
||||
try:
|
||||
conn.execute("ALTER TABLE users ADD COLUMN is_admin INTEGER NOT NULL DEFAULT 0")
|
||||
except sqlite3.OperationalError:
|
||||
pass
|
||||
try:
|
||||
conn.execute("""CREATE TABLE IF NOT EXISTS login_history (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id),
|
||||
ip_address TEXT DEFAULT '',
|
||||
user_agent TEXT DEFAULT '',
|
||||
logged_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)""")
|
||||
except sqlite3.OperationalError:
|
||||
pass
|
||||
conn.commit()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user