prepare("SELECT id, username, role, telegram_chat_id FROM users WHERE id = ?"); $stmt->execute([$edit_id]); $user_to_edit = $stmt->fetch(PDO::FETCH_ASSOC); if ($user_to_edit) { $edit_mode = true; $edit_username = $user_to_edit['username']; $edit_role = $user_to_edit['role']; $edit_telegram_chat_id = $user_to_edit['telegram_chat_id']; } else { $error = "Usuario no encontrado."; } } // Handle POST actions if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Action: Create or Update User if (isset($_POST['save_user'])) { $username = $_POST['username']; $role = $_POST['role']; $telegram_chat_id = trim($_POST['telegram_chat_id']); $is_edit = isset($_POST['edit_id']); if (empty($username) || empty($role)) { $error = "El nombre de usuario y el rol son obligatorios."; } elseif (!empty($telegram_chat_id) && !is_numeric($telegram_chat_id)) { $error = "El ID de Chat de Telegram debe ser un número."; } else { $chat_id_to_save = empty($telegram_chat_id) ? null : $telegram_chat_id; try { if ($is_edit) { $edit_id = $_POST['edit_id']; $details = 'Admin ' . $_SESSION['username'] . ' updated user: ' . $username . ' (ID: ' . $edit_id . ').'; if (!empty($_POST['password'])) { $hashedPassword = password_hash($_POST['password'], PASSWORD_DEFAULT); $stmt = $pdo->prepare("UPDATE users SET username = ?, password = ?, role = ?, telegram_chat_id = ? WHERE id = ?"); $stmt->execute([$username, $hashedPassword, $role, $chat_id_to_save, $edit_id]); $details .= ' Password was changed.'; } else { $stmt = $pdo->prepare("UPDATE users SET username = ?, role = ?, telegram_chat_id = ? WHERE id = ?"); $stmt->execute([$username, $role, $chat_id_to_save, $edit_id]); } log_activity($_SESSION['user_id'], 'User Updated', $details); header('Location: users.php?success=updated'); exit(); } else { if (empty($_POST['password'])) { $error = "La contraseña es obligatoria para nuevos usuarios."; } else { $hashedPassword = password_hash($_POST['password'], PASSWORD_DEFAULT); $stmt = $pdo->prepare("INSERT INTO users (username, password, role, telegram_chat_id) VALUES (?, ?, ?, ?)"); $stmt->execute([$username, $hashedPassword, $role, $chat_id_to_save]); $new_user_id = $pdo->lastInsertId(); log_activity($_SESSION['user_id'], 'User Created', 'Admin ' . $_SESSION['username'] . ' created new user: ' . $username . ' (ID: ' . $new_user_id . ')'); header('Location: users.php?success=created'); exit(); } } } catch (PDOException $e) { $error = ($e->errorInfo[1] == 1062) ? "El nombre de usuario ya existe." : "Error al guardar el usuario: " . $e->getMessage(); if ($is_edit) { $edit_mode = true; $edit_id = $_POST['edit_id']; $edit_username = $username; $edit_role = $role; $edit_telegram_chat_id = $telegram_chat_id; } } } } // ... (Otras acciones POST como eliminar, etc. se mantienen aquí) } // Fetch all users to display $users = $pdo->query("SELECT id, username, role, created_at, telegram_chat_id FROM users ORDER BY username ASC")->fetchAll(PDO::FETCH_ASSOC); require_once __DIR__ . '/../templates/header.php'; ?>

Administrar Usuarios

>
Lista de Usuarios
ID Usuario Rol ID Chat Telegram Creado en Acciones
No vinculado