Files
lastwar/admin/users.php

139 lines
5.6 KiB
PHP
Executable File

<?php
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/session_check.php';
require_once __DIR__ . '/../includes/i18n.php';
require_once __DIR__ . '/../includes/auth.php';
requireAdmin();
$pageTitle = t('Gestión de Usuarios');
$users = getAllUsers();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
if ($action === 'create') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
$role = $_POST['role'] ?? 'user';
if ($username && $password) {
$userId = registerUser($username, $password, $role);
if ($userId) {
logActivity(getCurrentUserId(), 'create_user', "Usuario creado: $username");
header('Location: users.php');
exit;
} else {
$error = t('El usuario ya existe');
}
}
} elseif ($action === 'delete') {
$userId = (int) $_POST['user_id'];
if ($userId !== getCurrentUserId()) {
deleteUser($userId);
logActivity(getCurrentUserId(), 'delete_user', "Usuario eliminado ID: $userId");
header('Location: users.php');
exit;
} else {
$error = t('No puedes eliminarte a ti mismo');
}
}
}
require_once __DIR__ . '/../templates/header.php';
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h2><i class="bi bi-people"></i> <?= t('Gestión de Usuarios') ?></h2>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#userModal">
<i class="bi bi-plus-circle"></i> <?= t('Nuevo Usuario') ?>
</button>
</div>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<div class="card border-0 shadow-sm">
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th><?= t('ID') ?></th>
<th><?= t('Usuario') ?></th>
<th><?= t('Rol') ?></th>
<th>Telegram</th>
<th><?= t('Creado') ?></th>
<th><?= t('Acciones') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= $user['id'] ?></td>
<td><?= htmlspecialchars($user['username']) ?></td>
<td>
<span class="badge bg-<?= $user['role'] === 'admin' ? 'danger' : 'primary' ?>">
<?= $user['role'] === 'admin' ? t('Administrador') : t('Usuario') ?>
</span>
</td>
<td><?= $user['telegram_chat_id'] ? htmlspecialchars($user['telegram_chat_id']) : '-' ?></td>
<td><?= date('d/m/Y', strtotime($user['created_at'])) ?></td>
<td>
<?php if ($user['id'] !== getCurrentUserId()): ?>
<form method="POST" onsubmit="return confirm('<?= t('¿Eliminar este usuario?') ?>');" class="d-inline">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="user_id" value="<?= $user['id'] ?>">
<button type="submit" class="btn btn-outline-danger btn-sm">
<i class="bi bi-trash"></i>
</button>
</form>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<div class="modal fade" id="userModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<input type="hidden" name="action" value="create">
<div class="modal-header">
<h5 class="modal-title"><?= t('Nuevo Usuario') ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label"><?= t('Usuario') ?></label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label"><?= t('Contraseña') ?></label>
<input type="password" name="password" class="form-control" required minlength="6">
</div>
<div class="mb-3">
<label class="form-label"><?= t('Rol') ?></label>
<select name="role" class="form-select">
<option value="user"><?= t('Usuario') ?></option>
<option value="admin"><?= t('Administrador') ?></option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary"><?= t('Crear') ?></button>
</div>
</form>
</div>
</div>
</div>
<?php require_once __DIR__ . '/../templates/footer.php'; ?>