12]); try { db()->prepare('INSERT INTO admin_users (username, role, account_id, password) VALUES (?, ?, ?, ?)') ->execute([$username, $role, $account_id, $hash]); flash('success', "Utilisateur {$username} créé."); } catch (Exception $e) { $error = "L'utilisateur « {$username} » existe déjà."; } } if (!$error) { header('Location: /admin/users.php'); exit; } } if ($_POST['action'] === 'delete') { $id = (int)$_POST['user_id']; if ($id === (int)$_SESSION['admin_id']) { flash('danger', 'Impossible de supprimer votre propre compte.'); } else { db()->prepare('DELETE FROM admin_users WHERE id = ?')->execute([$id]); flash('success', 'Utilisateur supprimé.'); } header('Location: /admin/users.php'); exit; } if ($_POST['action'] === 'edit') { $id = (int)$_POST['user_id']; $role = $_POST['role'] === 'admin' ? 'admin' : 'user'; $account_id = ($role === 'user' && !empty($_POST['account_id'])) ? (int)$_POST['account_id'] : null; $newPassword = trim($_POST['new_password'] ?? ''); db()->prepare('UPDATE admin_users SET role = ?, account_id = ? WHERE id = ?') ->execute([$role, $account_id, $id]); if (strlen($newPassword) >= 6) { $hash = password_hash($newPassword, PASSWORD_BCRYPT, ['cost' => 12]); db()->prepare('UPDATE admin_users SET password = ? WHERE id = ?')->execute([$hash, $id]); } flash('success', 'Utilisateur mis à jour.'); header('Location: /admin/users.php'); exit; } } $users = db()->query('SELECT u.*, a.username as account_name FROM admin_users u LEFT JOIN accounts a ON u.account_id = a.id ORDER BY u.role, u.username')->fetchAll(); $accounts = db()->query('SELECT id, username, email FROM accounts WHERE active = 1 ORDER BY username')->fetchAll(); html_head('Utilisateurs'); html_navbar('users'); ?>
| Utilisateur | Rôle | Compte associé | Actions |
|---|---|---|---|
| = htmlspecialchars($u['username']) ?> | admin user | = $u['account_name'] ? htmlspecialchars($u['account_name']) : 'tous' ?> |
|