136 lines
4.5 KiB
PHP
136 lines
4.5 KiB
PHP
<?php
|
|
// admin_user_action.php — Azioni admin sugli utenti
|
|
// Gestisce: cambio ruolo, cambio stato (attiva/blocca), reset password, elimina.
|
|
// Protetto: solo role_id = ROLE_ADMIN.
|
|
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
if (session_status() === PHP_SESSION_NONE) session_start();
|
|
include('include/headscript.php');
|
|
|
|
$dbHandler = DBHandlerSelect::getInstance();
|
|
$pdo = $dbHandler->getConnection();
|
|
|
|
if (!isset($iduserlogin)) {
|
|
header("Location: login.php");
|
|
exit;
|
|
}
|
|
// Solo admin
|
|
if ((int)$kindofrole !== ROLE_ADMIN) {
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
|
|
function setFlash(string $type, string $text): void
|
|
{
|
|
$_SESSION['flash'] = ['type' => $type, 'text' => $text];
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
setFlash('danger', 'Richiesta non valida.');
|
|
header("Location: admin_users.php");
|
|
exit;
|
|
}
|
|
|
|
$target_id = (int)($_POST['user_id'] ?? 0);
|
|
$action = trim($_POST['action'] ?? '');
|
|
|
|
// Query string di ritorno (per mantenere ricerca/filtri)
|
|
$ret = trim($_POST['ret'] ?? '');
|
|
function back(string $ret): void
|
|
{
|
|
header("Location: admin_users.php" . ($ret !== '' ? '?' . $ret : ''));
|
|
exit;
|
|
}
|
|
|
|
if ($target_id <= 0 || $action === '') {
|
|
setFlash('danger', 'Dati azione mancanti.');
|
|
back($ret);
|
|
}
|
|
|
|
// Sicurezza: l'admin non può agire su SE STESSO per azioni distruttive
|
|
// (evita di auto-bloccarsi, declassarsi o eliminarsi restando fuori dal pannello).
|
|
$isSelf = ($target_id === (int)$iduserlogin);
|
|
|
|
try {
|
|
// Verifica che l'utente esista
|
|
$u = $pdo->prepare("SELECT id, role_id, status FROM auth_users WHERE id = ?");
|
|
$u->execute([$target_id]);
|
|
$target = $u->fetch(PDO::FETCH_ASSOC);
|
|
if (!$target) {
|
|
setFlash('danger', 'Utente non trovato.');
|
|
back($ret);
|
|
}
|
|
|
|
// ===== Cambio ruolo =====
|
|
if ($action === 'set_role') {
|
|
if ($isSelf) {
|
|
setFlash('warning', 'Non puoi cambiare il ruolo del tuo stesso account.');
|
|
back($ret);
|
|
}
|
|
$new_role = (int)($_POST['role_id'] ?? 0);
|
|
if (!in_array($new_role, [ROLE_ADMIN, ROLE_OWNER, ROLE_USER], true)) {
|
|
setFlash('danger', 'Ruolo non valido.');
|
|
back($ret);
|
|
}
|
|
$upd = $pdo->prepare("UPDATE auth_users SET role_id = ? WHERE id = ?");
|
|
$upd->execute([$new_role, $target_id]);
|
|
setFlash('success', 'Ruolo aggiornato.');
|
|
back($ret);
|
|
}
|
|
|
|
// ===== Cambio stato (attiva / blocca) =====
|
|
if ($action === 'set_status') {
|
|
if ($isSelf) {
|
|
setFlash('warning', 'Non puoi cambiare lo stato del tuo stesso account.');
|
|
back($ret);
|
|
}
|
|
$new_status = trim($_POST['status'] ?? '');
|
|
// Valori usati dall'app: "Active" = attivo, "banned" = bloccato
|
|
if (!in_array($new_status, ['Active', 'banned'], true)) {
|
|
setFlash('danger', 'Stato non valido.');
|
|
back($ret);
|
|
}
|
|
$upd = $pdo->prepare("UPDATE auth_users SET status = ? WHERE id = ?");
|
|
$upd->execute([$new_status, $target_id]);
|
|
setFlash('success', $new_status === 'Active' ? 'Utente attivato.' : 'Utente bloccato.');
|
|
back($ret);
|
|
}
|
|
|
|
// ===== Reset password =====
|
|
if ($action === 'reset_password') {
|
|
$new_pw = (string)($_POST['new_password'] ?? '');
|
|
if (strlen($new_pw) < 8) {
|
|
setFlash('danger', 'La password deve avere almeno 8 caratteri.');
|
|
back($ret);
|
|
}
|
|
// bcrypt, compatibile col login Laravel
|
|
$hash = password_hash($new_pw, PASSWORD_BCRYPT);
|
|
$upd = $pdo->prepare("UPDATE auth_users SET password = ? WHERE id = ?");
|
|
$upd->execute([$hash, $target_id]);
|
|
setFlash('success', 'Password reimpostata. Comunicala all\'utente in modo sicuro.');
|
|
back($ret);
|
|
}
|
|
|
|
// ===== Elimina =====
|
|
if ($action === 'delete') {
|
|
if ($isSelf) {
|
|
setFlash('warning', 'Non puoi eliminare il tuo stesso account.');
|
|
back($ret);
|
|
}
|
|
$del = $pdo->prepare("DELETE FROM auth_users WHERE id = ?");
|
|
$del->execute([$target_id]);
|
|
setFlash('success', 'Utente eliminato.');
|
|
back($ret);
|
|
}
|
|
|
|
setFlash('danger', 'Azione non riconosciuta.');
|
|
back($ret);
|
|
} catch (Throwable $e) {
|
|
error_log('admin_user_action: ' . $e->getMessage());
|
|
// Errore tipico: eliminare un owner con saloni collegati (FK). Lo spieghiamo.
|
|
setFlash('danger', 'Operazione non riuscita. L\'utente potrebbe avere dati collegati (es. saloni) che ne impediscono l\'eliminazione.');
|
|
back($ret);
|
|
}
|