Refactor v5.0: Architettura modulare backend/frontend e PWA v6

This commit is contained in:
2026-10-24 17:58:56 +02:00
parent 86899aebb9
commit 21f971b205
18 changed files with 2992 additions and 2583 deletions
View File
+417
View File
@@ -0,0 +1,417 @@
import os
import re
import json
import time
import sqlite3
import subprocess
from contextlib import closing
from flask import Blueprint, request, jsonify, send_from_directory
from core.config import (
BASE_DIR, CLIENTS_PATH, DB_PATH, config, read_config, write_config, logger
)
from core.database import db_connect, audit
import core.state as state
from services.mqtt_service import mqtt_publish
from services.push_service import node_allowed
from routes.auth_routes import get_auth, json_body, err
api_bp = Blueprint('api', __name__)
# --- REGEX & COSTANTI COMANDI ---
CID_RE = re.compile(r'^[a-z0-9][a-z0-9_\-]{0,63}$')
SERVICE_RE = re.compile(r'^[A-Za-z0-9][A-Za-z0-9_.@\-]{0,63}$')
PROFILE_KEY_RE = re.compile(r'^[A-Za-z0-9_\-]{1,32}$')
TIME_RE = re.compile(r'^([01]\d|2[0-3]):[0-5]\d$')
SIMPLE_COMMANDS = {'TG:ON', 'TG:OFF'}
PRIVILEGED_COMMANDS = {'REBOOT', 'RESET_HAT', 'UPDATE_AGENT'}
SERVICE_ACTIONS = {'start', 'stop', 'restart'}
def known_profile_keys(cid=None):
keys = {'A', 'B'}
healths = [state.device_health.get(cid)] if cid else list(state.device_health.values())
for h in healths:
profiles = (h or {}).get('profiles') or {}
if isinstance(profiles, dict):
keys.update(str(k) for k in profiles.keys() if PROFILE_KEY_RE.match(str(k)))
return keys - PRIVILEGED_COMMANDS - SIMPLE_COMMANDS
# --- ROTTE DATI GENERALI & LOGS ---
@api_bp.route('/api/clients')
def get_clients():
if os.path.exists(CLIENTS_PATH):
with open(CLIENTS_PATH, 'r') as f:
return jsonify(json.load(f))
return jsonify([])
@api_bp.route('/api/logs')
def get_logs():
conn = sqlite3.connect(DB_PATH, timeout=10)
c = conn.cursor()
c.execute("""SELECT timestamp, client_id, protocol, source_id, target, slot, duration, ber, source_ext, rssi, source_conn, ysf_mode
FROM radio_logs ORDER BY id DESC LIMIT 60""")
logs = c.fetchall()
conn.close()
return jsonify(logs)
@api_bp.route('/api/states', methods=['GET'])
def get_states():
clean_versions = {}
for c, d in state.node_versions.items():
clean_versions[c] = {k: v for k, v in d.items() if not k.startswith('_')}
enriched_gateway_states = dict(state.gateway_states)
for cid, confs in state.device_configs.items():
if 'dmrgateway' in confs:
dmr_conf = confs['dmrgateway']
if isinstance(dmr_conf, dict) and 'raw_text' in dmr_conf:
raw = dmr_conf['raw_text']
active_dmr_nets = []
for i in range(1, 6):
section_name = f"[DMR Network {i}]"
if section_name in raw:
block = raw.split(section_name)[1].split('[')[0]
if re.search(r'^Enabled\s*=\s*1', block, re.MULTILINE | re.IGNORECASE):
m_name = re.search(r'^Name\s*=\s*(.+)', block, re.MULTILINE | re.IGNORECASE)
net_name = m_name.group(1).strip() if m_name else f"Net {i}"
active_dmr_nets.append(net_name)
if active_dmr_nets:
if cid not in enriched_gateway_states:
enriched_gateway_states[cid] = {}
enriched_gateway_states[cid]['DMR_NETS'] = active_dmr_nets
return jsonify({
"states": state.client_states,
"telemetry": state.client_telemetry,
"health": state.device_health,
"info": state.node_info,
"general": state.node_general,
"last_seen": state.seen_ages(),
"uptime": int(time.time() - state.START_TIME),
"versions": clean_versions,
"gateway_states": enriched_gateway_states,
"warnings": state.active_warnings
})
@api_bp.route('/api/stats')
def get_stats():
node = request.args.get('node', 'all').lower()
conn = sqlite3.connect(DB_PATH, timeout=10)
c = conn.cursor()
node_filter = ""
params = []
if node != 'all':
node_filter = " AND LOWER(client_id) = ?"
params.append(node)
c.execute(f"""SELECT target, COUNT(*) as cnt FROM radio_logs
WHERE target NOT IN ('---', 'NET', '')
AND timestamp >= date('now', 'localtime') AND timestamp < date('now', 'localtime', '+1 day'){node_filter}
GROUP BY target ORDER BY cnt DESC LIMIT 5""", params)
top_tgs = [{"target": row[0], "count": row[1]} for row in c.fetchall()]
c.execute(f"""SELECT source_id, COUNT(*) as cnt FROM radio_logs
WHERE source_id NOT LIKE '🌐%'
AND timestamp >= date('now', 'localtime') AND timestamp < date('now', 'localtime', '+1 day'){node_filter}
GROUP BY source_id ORDER BY cnt DESC LIMIT 5""", params)
top_calls = [{"call": row[0], "count": row[1]} for row in c.fetchall()]
c.execute(f"""SELECT AVG(duration) FROM radio_logs
WHERE duration > 0.5
AND timestamp >= date('now', 'localtime') AND timestamp < date('now', 'localtime', '+1 day'){node_filter}""", params)
avg_dur = c.fetchone()[0]
avg_dur = round(avg_dur, 1) if avg_dur else 0
c.execute(f"SELECT COUNT(*) FROM radio_logs WHERE timestamp >= date('now', 'localtime') AND timestamp < date('now', 'localtime', '+1 day'){node_filter}", params)
today_tx = c.fetchone()[0]
conn.close()
return jsonify({
"top_tgs": top_tgs,
"top_calls": top_calls,
"avg_duration": avg_dur,
"today_tx": today_tx
})
# --- COMANDI NODO & RETE ---
@api_bp.route('/api/command', methods=['POST'])
def cmd():
auth = get_auth()
if not auth:
return err("Non autenticato", 403)
d = json_body()
cid = str(d.get('clientId', '')).strip().lower()
cmd_type = str(d.get('type', '')).strip()
if not CID_RE.match(cid):
return err("ID nodo non valido")
if not node_allowed(auth['role'], auth['allowed_nodes'], cid):
return err("Permesso negato per questo nodo.", 403)
if cmd_type in PRIVILEGED_COMMANDS:
if auth['role'] != 'admin':
return err("Solo gli amministratori possono eseguire questo comando.", 403)
elif cmd_type in SIMPLE_COMMANDS:
pass
elif cmd_type in known_profile_keys(cid):
pass
else:
logger.warning(f"Comando non consentito da {auth['username']}: {cmd_type[:40]!r} -> {cid}")
return err("Comando sconosciuto")
if not mqtt_publish(f"servizi/{cid}/cmnd", cmd_type):
return err("Broker MQTT non raggiungibile - comando non inviato", 503)
audit(auth['username'], cid, cmd_type)
state.client_telemetry[cid] = {"ts1": "🔄 Inviato...", "ts2": "🔄 Inviato...", "alt": ""}
return jsonify({"success": True})
@api_bp.route('/api/global_command', methods=['POST'])
def global_cmd():
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Azione riservata agli amministratori!", 403)
cmd_type = str(json_body().get('type', '')).strip()
if cmd_type not in known_profile_keys():
return err("Profilo sconosciuto")
clients_list = []
if os.path.exists(CLIENTS_PATH):
with open(CLIENTS_PATH, 'r') as f:
clients_list = json.load(f)
sent, failed = 0, 0
for client in clients_list:
cid = str(client.get('id', '')).lower()
if not CID_RE.match(cid):
continue
if mqtt_publish(f"servizi/{cid}/cmnd", cmd_type):
audit(auth['username'], cid, f"GLOBAL_OVERRIDE_{cmd_type}")
sent += 1
else:
failed += 1
if failed and not sent:
return err("Broker MQTT non raggiungibile", 503)
return jsonify({"success": True, "sent": sent, "failed": failed})
@api_bp.route('/api/service_control', methods=['POST'])
def service_control():
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Non autorizzato", 403)
d = json_body()
cid = str(d.get('clientId', '')).strip().lower()
action = str(d.get('action', '')).strip().lower()
service = str(d.get('service', '')).strip()
if not CID_RE.match(cid):
return err("ID nodo non valido")
if action not in SERVICE_ACTIONS:
return err("Azione servizio non valida")
if not SERVICE_RE.match(service):
return err("Nome servizio non valido")
if not mqtt_publish(f"devices/{cid}/control", f"{action}:{service}"):
return err("Broker MQTT non raggiungibile", 503)
audit(auth['username'], cid, f"SVC_{action.upper()}_{service}")
return jsonify({"success": True})
@api_bp.route('/api/update_nodes', methods=['POST'])
def update_nodes():
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Non autorizzato", 403)
if not mqtt_publish("devices/control/request", "update"):
return err("Broker MQTT non raggiungibile", 503)
logger.info(f"📢 REQ CONFIG inviata a tutta la flotta da {auth['username']}")
audit(auth['username'], '*', 'REQ_CONFIG_ALL')
return jsonify({"success": True})
# --- CONFIGURAZIONI & FILE .INI ---
@api_bp.route('/api/ui_config', methods=['GET'])
def get_ui_config():
try:
cfg = read_config()
ui_cfg = cfg.get("ui", {
"profileA_Name": "PROFILE A",
"profileA_Color": "#3498db",
"profileB_Name": "PROFILE B",
"profileB_Color": "#9b59b6"
})
return jsonify(ui_cfg)
except Exception as e:
logger.error(f"ui_config error: {e}")
return jsonify({"error": "Configurazione non disponibile"}), 500
@api_bp.route('/api/config', methods=['GET'])
def get_config_api():
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Non autorizzato", 403)
cfg = read_config()
return jsonify({
"update_schedule": cfg.get("update_schedule", "03:00"),
"url_dmr": cfg.get("id_urls", {}).get("dmr", ""),
"url_nxdn": cfg.get("id_urls", {}).get("nxdn", ""),
"log_max_mb": cfg.get("log_max_mb", 50),
"log_backup_count": cfg.get("log_backup_count", 10)
})
@api_bp.route('/api/config', methods=['POST'])
def save_config_api():
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Non autorizzato", 403)
d = json_body()
schedule = str(d.get("update_schedule", "03:00")).strip()
url_dmr = str(d.get("url_dmr", "")).strip()
url_nxdn = str(d.get("url_nxdn", "")).strip()
try:
log_max_mb = int(d.get("log_max_mb", 50))
log_backup_count = int(d.get("log_backup_count", 10))
except ValueError:
return err("Parametri di rotazione log non validi")
if not TIME_RE.match(schedule):
return err("Formato orario non valido (usa HH:MM)")
for u in (url_dmr, url_nxdn):
if not u.lower().startswith(('http://', 'https://')):
return err("Gli URL devono iniziare con http:// o https://")
cfg = read_config()
cfg["update_schedule"] = schedule
cfg.setdefault("id_urls", {})
cfg["id_urls"]["dmr"] = url_dmr
cfg["id_urls"]["nxdn"] = url_nxdn
cfg["log_max_mb"] = log_max_mb
cfg["log_backup_count"] = log_backup_count
write_config(cfg)
audit(auth['username'], '-', 'EDIT_SERVER_CONFIG')
return jsonify({"success": True})
@api_bp.route('/api/config_file/<cid>/<service>', methods=['GET'])
def get_config_file(cid, service):
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Non autorizzato", 403)
cid = cid.lower()
service = service.lower()
if not CID_RE.match(cid) or not SERVICE_RE.match(service):
return err("Parametri non validi")
config_data = state.device_configs.get(cid, {}).get(service)
if not config_data:
return err("Configurazione non ancora ricevuta. Invia un comando di richiesta aggiornamento.", 404)
return jsonify({"success": True, "data": config_data})
@api_bp.route('/api/config_file', methods=['POST'])
def save_config_file():
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Non autorizzato", 403)
d = json_body()
cid = str(d.get('clientId', '')).strip().lower()
service = str(d.get('service', '')).strip().lower()
new_config = d.get('config_data')
if not CID_RE.match(cid) or not SERVICE_RE.match(service):
return err("Parametri non validi")
if not isinstance(new_config, dict) or not isinstance(new_config.get('raw_text'), str):
return err("Payload configurazione non valido")
if len(new_config['raw_text']) > 65536:
return err("File di configurazione troppo grande (max 64 KB)")
if not mqtt_publish(f"devices/{cid}/config_set/{service}", json.dumps(new_config)):
return err("Broker MQTT non raggiungibile", 503)
audit(auth['username'], cid, f"EDIT_CONFIG_{service.upper()}")
return jsonify({"success": True})
# --- AGGIORNAMENTO SERVER OTA ---
@api_bp.route('/api/server_update/check', methods=['GET'])
def check_server_update():
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Non autorizzato", 403)
try:
subprocess.run(['git', 'fetch'], timeout=10, cwd=BASE_DIR)
local_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=BASE_DIR).decode('utf-8').strip()
remote_hash = subprocess.check_output(['git', 'rev-parse', '@{u}'], cwd=BASE_DIR).decode('utf-8').strip()
update_available = local_hash != remote_hash
return jsonify({"success": True, "update_available": update_available})
except Exception as e:
logger.error(f"Errore controllo aggiornamenti server: {e}")
return jsonify({"success": False, "error": "Impossibile verificare aggiornamenti"})
@api_bp.route('/api/server_update/apply', methods=['POST'])
def apply_server_update():
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Non autorizzato", 403)
try:
audit(auth['username'], 'SERVER', 'SERVER_OTA_UPDATE')
subprocess.run(['git', 'pull', '--ff-only'], timeout=30, cwd=BASE_DIR, check=True)
def restart_server():
time.sleep(2)
os._exit(1)
import threading
threading.Thread(target=restart_server, daemon=True).start()
return jsonify({"success": True})
except subprocess.CalledProcessError as e:
logger.error(f"Errore durante git pull: {e}")
return err("Conflitto o errore durante il git pull.")
except Exception as e:
logger.error(f"Errore aggiornamento server: {e}")
return err(str(e))
# --- NOTIFICHE WEBPUSH VAPID ---
@api_bp.route('/api/vapid_public_key')
def get_vapid_key():
return jsonify({"public_key": config.get('webpush', {}).get('vapid_public_key', '')})
@api_bp.route('/api/subscribe', methods=['POST'])
def subscribe_push():
auth = get_auth()
if not auth:
return err("Non autorizzato", 403)
sub_data = json_body()
if not sub_data.get('endpoint'):
return err("Sottoscrizione non valida")
with closing(db_connect()) as conn:
conn.execute("""INSERT INTO push_subscriptions (username, subscription) VALUES (?, ?)
ON CONFLICT(subscription) DO UPDATE SET username = excluded.username""",
(auth['username'], json.dumps(sub_data)))
conn.commit()
return jsonify({"success": True})
# --- ASSET PWA / OFFLINE ---
@api_bp.route('/manifest.json')
def serve_manifest():
return send_from_directory(BASE_DIR, 'manifest.json')
@api_bp.route('/sw.js')
def serve_sw():
resp = send_from_directory(BASE_DIR, 'sw.js', mimetype='application/javascript')
resp.headers['Cache-Control'] = 'no-cache'
resp.headers['Service-Worker-Allowed'] = '/'
return resp
@api_bp.route('/icon-512.png')
def serve_icon():
return send_from_directory(BASE_DIR, 'icon-512.png')
+252
View File
@@ -0,0 +1,252 @@
import re
import time
import hashlib
import threading
import sqlite3
from contextlib import closing
from flask import Blueprint, request, session, jsonify
from werkzeug.security import generate_password_hash, check_password_hash
from core.config import config, logger
from core.database import db_connect, audit
auth_bp = Blueprint('auth', __name__)
# --- COSTANTI & REGEX VALIDAZIONE ---
VALID_ROLES = ('admin', 'operator')
USERNAME_RE = re.compile(r'^[A-Za-z0-9_.@\-]{3,32}$')
MIN_PASSWORD_LEN = 8
def json_body():
d = request.get_json(silent=True)
return d if isinstance(d, dict) else {}
def err(msg, code=400):
return jsonify({"success": False, "error": msg}), code
def pw_fingerprint(password_hash):
return hashlib.sha256(password_hash.encode()).hexdigest()[:16]
def get_auth():
username = session.get('user')
if not session.get('logged_in') or not username:
return None
with closing(db_connect(row=True)) as conn:
row = conn.execute("SELECT username, password_hash, role, allowed_nodes FROM users WHERE username = ?",
(username,)).fetchone()
if not row or session.get('pv') != pw_fingerprint(row['password_hash']):
session.clear()
return None
return {"username": row['username'], "role": row['role'], "allowed_nodes": row['allowed_nodes'] or ''}
def count_admins(cursor, exclude_id=None):
if exclude_id is None:
cursor.execute("SELECT COUNT(*) FROM users WHERE role = 'admin'")
else:
cursor.execute("SELECT COUNT(*) FROM users WHERE role = 'admin' AND id != ?", (exclude_id,))
return cursor.fetchone()[0]
def normalize_nodes(value):
value = (value or '').strip().lower()
if not value or value == 'all':
return 'all'
return ','.join(x.strip() for x in value.split(',') if x.strip())
# --- RATE LIMITING / BRUTE FORCE PROTECTION ---
LOGIN_MAX_FAILS = 5
LOGIN_WINDOW_SEC = 300
login_attempts = {}
login_lock = threading.Lock()
def client_ip():
if config.get('web', {}).get('trust_proxy'):
fwd = request.headers.get('X-Forwarded-For', '')
if fwd:
return fwd.split(',')[0].strip()
return request.remote_addr or 'unknown'
def login_blocked(ip):
now = time.time()
with login_lock:
fails = [x for x in login_attempts.get(ip, []) if now - x < LOGIN_WINDOW_SEC]
if fails:
login_attempts[ip] = fails
else:
login_attempts.pop(ip, None)
return len(fails) >= LOGIN_MAX_FAILS
def login_register_fail(ip):
with login_lock:
login_attempts.setdefault(ip, []).append(time.time())
def login_reset(ip):
with login_lock:
login_attempts.pop(ip, None)
# --- ROTTE AUTENTICAZIONE ---
@auth_bp.route('/api/login', methods=['POST'])
def login():
d = json_body()
username, password = d.get('user'), d.get('pass')
if not isinstance(username, str) or not isinstance(password, str) or not username or not password:
return err("Credenziali mancanti", 400)
ip = client_ip()
if login_blocked(ip):
logger.warning(f"Login bloccato (troppi tentativi) da {ip}")
return err("Troppi tentativi falliti. Riprova tra qualche minuto.", 429)
with closing(db_connect(row=True)) as conn:
user = conn.execute("SELECT * FROM users WHERE username = ?", (username,)).fetchone()
if user and check_password_hash(user['password_hash'], password):
login_reset(ip)
session.clear()
session.permanent = True
session['logged_in'] = True
session['user'] = user['username']
session['pv'] = pw_fingerprint(user['password_hash'])
logger.info(f"Login OK: {user['username']} da {ip}")
return jsonify({"success": True, "role": user['role'], "allowed_nodes": user['allowed_nodes']})
login_register_fail(ip)
logger.warning(f"Login fallito per {username[:32]!r} da {ip}")
return jsonify({"success": False}), 401
@auth_bp.route('/api/logout', methods=['POST'])
def logout():
session.clear()
return jsonify({"success": True})
@auth_bp.route('/api/me')
def me():
auth = get_auth()
if not auth:
return jsonify({"logged_in": False})
return jsonify({"logged_in": True, "user": auth['username'], "role": auth['role'],
"allowed_nodes": auth['allowed_nodes']})
@auth_bp.route('/api/change_password', methods=['POST'])
def change_password():
auth = get_auth()
if not auth:
return err("Non autenticato", 403)
d = json_body()
new_pass = d.get('new_password')
user_to_change = d.get('username') or auth['username']
if auth['role'] != 'admin' and auth['username'] != user_to_change:
return err("Non autorizzato", 403)
if not new_pass or not isinstance(new_pass, str):
return err("La password non può essere vuota")
if len(new_pass) < MIN_PASSWORD_LEN:
return err(f"La password deve contenere almeno {MIN_PASSWORD_LEN} caratteri")
with closing(db_connect()) as conn:
cur = conn.execute("UPDATE users SET password_hash = ? WHERE username = ?",
(generate_password_hash(new_pass), user_to_change))
conn.commit()
if cur.rowcount == 0:
return err("Utente non trovato", 404)
audit(auth['username'], '-', f"PASSWORD_CHANGE_{user_to_change}")
return jsonify({"success": True})
# --- GESTIONE UTENTI (ADMIN ONLY) ---
@auth_bp.route('/api/users', methods=['GET'])
def get_users():
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Non autorizzato", 403)
with closing(db_connect(row=True)) as conn:
users = [dict(r) for r in conn.execute("SELECT id, username, role, allowed_nodes FROM users")]
return jsonify(users)
@auth_bp.route('/api/users', methods=['POST'])
def add_user():
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Non autorizzato", 403)
d = json_body()
username = str(d.get('username', '')).strip()
password = d.get('password')
role = d.get('role', 'operator')
allowed = normalize_nodes(d.get('allowed_nodes', 'all'))
if not username or not password:
return err("Dati mancanti")
if not USERNAME_RE.match(username):
return err("Username non valido (3-32 caratteri: lettere, numeri, . _ @ -)")
if not isinstance(password, str) or len(password) < MIN_PASSWORD_LEN:
return err(f"La password deve contenere almeno {MIN_PASSWORD_LEN} caratteri")
if role not in VALID_ROLES:
return err("Ruolo non valido")
try:
with closing(db_connect()) as conn:
conn.execute("INSERT INTO users (username, password_hash, role, allowed_nodes) VALUES (?,?,?,?)",
(username, generate_password_hash(password), role, allowed))
conn.commit()
except sqlite3.IntegrityError:
return err("Username già esistente")
audit(auth['username'], '-', f"USER_ADD_{username}_{role}")
return jsonify({"success": True})
@auth_bp.route('/api/users/<int:user_id>', methods=['PUT'])
def update_user(user_id):
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Non autorizzato", 403)
d = json_body()
password = d.get('password')
if password is not None and not isinstance(password, str):
return err("Password non valida")
with closing(db_connect()) as conn:
c = conn.cursor()
c.execute("SELECT username, role, allowed_nodes FROM users WHERE id = ?", (user_id,))
existing = c.fetchone()
if not existing:
return err("Utente non trovato", 404)
role = d.get('role', existing[1])
allowed = normalize_nodes(d.get('allowed_nodes', existing[2]))
if role not in VALID_ROLES:
return err("Ruolo non valido")
if existing[1] == 'admin' and role != 'admin' and count_admins(c, exclude_id=user_id) == 0:
return err("Impossibile degradare l'ultimo amministratore rimasto!")
if password and password.strip() != "":
if len(password) < MIN_PASSWORD_LEN:
return err(f"La password deve contenere almeno {MIN_PASSWORD_LEN} caratteri")
c.execute("UPDATE users SET password_hash=?, role=?, allowed_nodes=? WHERE id=?",
(generate_password_hash(password), role, allowed, user_id))
else:
c.execute("UPDATE users SET role=?, allowed_nodes=? WHERE id=?", (role, allowed, user_id))
conn.commit()
audit(auth['username'], '-', f"USER_EDIT_{existing[0]}")
return jsonify({"success": True})
@auth_bp.route('/api/users/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
auth = get_auth()
if not auth or auth['role'] != 'admin':
return err("Non autorizzato", 403)
with closing(db_connect()) as conn:
c = conn.cursor()
c.execute("SELECT username, role FROM users WHERE id = ?", (user_id,))
u = c.fetchone()
if not u:
return err("Utente non trovato", 404)
if u[0] == auth['username']:
return err("Non puoi eliminare il tuo stesso account!")
if u[1] == 'admin' and count_admins(c, exclude_id=user_id) == 0:
return err("Impossibile eliminare l'ultimo amministratore rimasto!")
c.execute("DELETE FROM users WHERE id = ?", (user_id,))
c.execute("DELETE FROM push_subscriptions WHERE username = ?", (u[0],))
conn.commit()
audit(auth['username'], '-', f"USER_DEL_{u[0]}")
return jsonify({"success": True})