182 lines
8.6 KiB
PHP
182 lines
8.6 KiB
PHP
<?php
|
|
$adminSection = 'usuarios';
|
|
$pageTitle = 'Usuarios';
|
|
$pageHeading = 'Usuarios de acceso';
|
|
require_once __DIR__ . '/../includes/admin_bootstrap.php';
|
|
require_once __DIR__ . '/../config/database.php';
|
|
|
|
$db = getDB();
|
|
$current = getCurrentUser();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$action = $_POST['action'] ?? '';
|
|
|
|
if ($action === 'create') {
|
|
$usuario = trim($_POST['usuario'] ?? '');
|
|
$password = $_POST['password'] ?? '';
|
|
$nombre = trim($_POST['nombre'] ?? '');
|
|
|
|
if ($usuario === '' || strlen($password) < 6) {
|
|
flash('danger', 'Usuario requerido y contraseña mínimo 6 caracteres.');
|
|
} else {
|
|
try {
|
|
$hash = password_hash($password, PASSWORD_DEFAULT);
|
|
$db->prepare('INSERT INTO usuarios (usuario, password_hash, nombre) VALUES (?, ?, ?)')
|
|
->execute([$usuario, $hash, $nombre !== '' ? $nombre : null]);
|
|
flash('success', 'Usuario agregado correctamente.');
|
|
} catch (PDOException $e) {
|
|
flash('danger', 'Ese nombre de usuario ya existe.');
|
|
}
|
|
}
|
|
} elseif ($action === 'update') {
|
|
$id = (int) ($_POST['id'] ?? 0);
|
|
$nombre = trim($_POST['nombre'] ?? '');
|
|
$activo = isset($_POST['activo']) ? 1 : 0;
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if ($id <= 0) {
|
|
flash('danger', 'Usuario no válido.');
|
|
} elseif ($id === $current['id'] && !$activo) {
|
|
flash('danger', 'No puede desactivar su propio usuario.');
|
|
} elseif ($activo === 0 && countActiveUsers($db) <= 1) {
|
|
flash('danger', 'Debe existir al menos un usuario activo.');
|
|
} else {
|
|
if ($password !== '') {
|
|
if (strlen($password) < 6) {
|
|
flash('danger', 'La contraseña debe tener al menos 6 caracteres.');
|
|
redirect(BASE_URL . '/admin/usuarios.php');
|
|
}
|
|
$hash = password_hash($password, PASSWORD_DEFAULT);
|
|
$db->prepare('UPDATE usuarios SET nombre = ?, activo = ?, password_hash = ? WHERE id = ?')
|
|
->execute([$nombre !== '' ? $nombre : null, $activo, $hash, $id]);
|
|
} else {
|
|
$db->prepare('UPDATE usuarios SET nombre = ?, activo = ? WHERE id = ?')
|
|
->execute([$nombre !== '' ? $nombre : null, $activo, $id]);
|
|
}
|
|
flash('success', 'Usuario actualizado.');
|
|
}
|
|
} elseif ($action === 'delete') {
|
|
$id = (int) ($_POST['id'] ?? 0);
|
|
if ($id === $current['id']) {
|
|
flash('danger', 'No puede eliminar su propio usuario.');
|
|
} elseif (countActiveUsers($db) <= 1) {
|
|
$stmt = $db->prepare('SELECT activo FROM usuarios WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
$target = $stmt->fetch();
|
|
if ($target && $target['activo']) {
|
|
flash('danger', 'No puede eliminar el último usuario activo.');
|
|
redirect(BASE_URL . '/admin/usuarios.php');
|
|
}
|
|
}
|
|
if ($id > 0) {
|
|
$db->prepare('DELETE FROM usuarios WHERE id = ?')->execute([$id]);
|
|
flash('success', 'Usuario eliminado.');
|
|
}
|
|
}
|
|
|
|
redirect(BASE_URL . '/admin/usuarios.php');
|
|
}
|
|
|
|
$usuarios = $db->query('SELECT * FROM usuarios ORDER BY usuario')->fetchAll();
|
|
$flash = getFlash();
|
|
require_once __DIR__ . '/../includes/admin_header.php';
|
|
?>
|
|
|
|
<?php if ($flash): ?>
|
|
<div class="alert alert-<?= h($flash['type']) ?> alert-dismissible fade show">
|
|
<?= h($flash['message']) ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="row g-4">
|
|
<div class="col-lg-4">
|
|
<div class="card">
|
|
<div class="card-header">Nuevo usuario</div>
|
|
<div class="card-body">
|
|
<form method="post">
|
|
<input type="hidden" name="action" value="create">
|
|
<div class="mb-3">
|
|
<label class="form-label">Usuario <span class="text-danger">*</span></label>
|
|
<input type="text" name="usuario" class="form-control" required maxlength="50"
|
|
pattern="[a-zA-Z0-9._-]+" title="Solo letras, números, punto, guion y guion bajo">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Contraseña <span class="text-danger">*</span></label>
|
|
<input type="password" name="password" class="form-control" required minlength="6">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Nombre completo</label>
|
|
<input type="text" name="nombre" class="form-control" maxlength="100">
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100">Agregar usuario</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-lg-8">
|
|
<div class="card">
|
|
<div class="card-header">Usuarios registrados (<?= count($usuarios) ?>)</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover mb-0">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>Usuario</th>
|
|
<th>Nombre</th>
|
|
<th>Activo</th>
|
|
<th>Nueva contraseña</th>
|
|
<th></th>
|
|
<th></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($usuarios as $u): ?>
|
|
<tr>
|
|
<form method="post">
|
|
<input type="hidden" name="action" value="update">
|
|
<input type="hidden" name="id" value="<?= (int) $u['id'] ?>">
|
|
<td>
|
|
<strong><?= h($u['usuario']) ?></strong>
|
|
<?php if ((int) $u['id'] === $current['id']): ?>
|
|
<span class="badge bg-info ms-1">Tú</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td>
|
|
<input type="text" name="nombre" class="form-control form-control-sm"
|
|
value="<?= h($u['nombre'] ?? '') ?>" maxlength="100">
|
|
</td>
|
|
<td>
|
|
<div class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" name="activo"
|
|
<?= $u['activo'] ? 'checked' : '' ?>
|
|
<?= (int) $u['id'] === $current['id'] ? 'disabled' : '' ?>>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<input type="password" name="password" class="form-control form-control-sm"
|
|
placeholder="Dejar vacío = sin cambio" minlength="6">
|
|
</td>
|
|
<td>
|
|
<button type="submit" class="btn btn-sm btn-primary">Guardar</button>
|
|
</td>
|
|
</form>
|
|
<td>
|
|
<?php if ((int) $u['id'] !== $current['id']): ?>
|
|
<form method="post" onsubmit="return confirm('¿Eliminar usuario <?= h($u['usuario']) ?>?');">
|
|
<input type="hidden" name="action" value="delete">
|
|
<input type="hidden" name="id" value="<?= (int) $u['id'] ?>">
|
|
<button type="submit" class="btn btn-sm btn-outline-danger">Eliminar</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/../includes/admin_footer.php'; ?>
|