Mejoras de seguridad y nueva tabla de turnos para ayudantes
- Agregado sistema de protección CSRF con tokens - Creada clase Session para gestión centralizada de sesiones - Mejorado manejo de errores en Database (sin die()) - Refactorizado Auth para usar nueva clase Session - Agregada validación CSRF a formularios de login y admin - Agregada validación de roles en modelo User - Mejorada vista de ayudante con tabla de horarios por semana - Agregada tabla de Turnos de Ayudantes con fechas en columnas
This commit is contained in:
@@ -7,6 +7,7 @@ require_once BASE_PATH . '/src/Auth.php';
|
||||
require_once BASE_PATH . '/src/User.php';
|
||||
require_once BASE_PATH . '/src/DiasHorarios.php';
|
||||
require_once BASE_PATH . '/src/Asignacion.php';
|
||||
require_once BASE_PATH . '/src/CSRF.php';
|
||||
|
||||
$auth = new Auth();
|
||||
$auth->requireAdmin();
|
||||
@@ -19,52 +20,57 @@ $message = '';
|
||||
$messageType = '';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$action = $_POST['action'] ?? '';
|
||||
if (!CSRF::isValidRequest()) {
|
||||
$message = 'Error de validación del formulario';
|
||||
$messageType = 'danger';
|
||||
} else {
|
||||
$action = $_POST['action'] ?? '';
|
||||
|
||||
if ($action === 'asignar') {
|
||||
$userId = $_POST['user_id'] ?? 0;
|
||||
$semana = $_POST['semana'] ?? '';
|
||||
if ($action === 'asignar') {
|
||||
$userId = $_POST['user_id'] ?? 0;
|
||||
$semana = $_POST['semana'] ?? '';
|
||||
|
||||
if ($userId && $semana) {
|
||||
$asignacionModel->asignar($userId, $semana);
|
||||
$message = 'Turno asignado correctamente';
|
||||
$messageType = 'success';
|
||||
}
|
||||
} elseif ($action === 'rotar') {
|
||||
$semana = $_POST['semana'] ?? '';
|
||||
$asignacionActual = $asignacionModel->getAsignacionPorSemana($semana);
|
||||
|
||||
if ($asignacionActual) {
|
||||
$proximaPersona = $asignacionModel->getProximaPersona($asignacionActual['user_id']);
|
||||
if ($proximaPersona) {
|
||||
$asignacionModel->asignar($proximaPersona['id'], $semana);
|
||||
$message = 'Turno rotado a: ' . htmlspecialchars($proximaPersona['nombre']);
|
||||
if ($userId && $semana) {
|
||||
$asignacionModel->asignar($userId, $semana);
|
||||
$message = 'Turno asignado correctamente';
|
||||
$messageType = 'success';
|
||||
}
|
||||
}
|
||||
} elseif ($action === 'asignar_masivo') {
|
||||
$userIds = $_POST['user_ids'] ?? [];
|
||||
$semanaInicio = $_POST['semana_inicio'] ?? '';
|
||||
$rotacionAutomatica = isset($_POST['rotacion_automatica']) ? true : false;
|
||||
|
||||
if (!empty($userIds) && $semanaInicio) {
|
||||
$resultado = $asignacionModel->asignarMasivo($userIds, $semanaInicio, $rotacionAutomatica);
|
||||
} elseif ($action === 'rotar') {
|
||||
$semana = $_POST['semana'] ?? '';
|
||||
$asignacionActual = $asignacionModel->getAsignacionPorSemana($semana);
|
||||
|
||||
if ($resultado['success'] > 0) {
|
||||
$message = "Se asignaron {$resultado['success']} turnos correctamente";
|
||||
if ($rotacionAutomatica) {
|
||||
$message .= " con rotación automática para la siguiente semana";
|
||||
if ($asignacionActual) {
|
||||
$proximaPersona = $asignacionModel->getProximaPersona($asignacionActual['user_id']);
|
||||
if ($proximaPersona) {
|
||||
$asignacionModel->asignar($proximaPersona['id'], $semana);
|
||||
$message = 'Turno rotado a: ' . htmlspecialchars($proximaPersona['nombre']);
|
||||
$messageType = 'success';
|
||||
}
|
||||
$messageType = 'success';
|
||||
}
|
||||
|
||||
if (!empty($resultado['errors'])) {
|
||||
$message .= "<br>Errores: " . implode('<br>', $resultado['errors']);
|
||||
$messageType = 'warning';
|
||||
} elseif ($action === 'asignar_masivo') {
|
||||
$userIds = $_POST['user_ids'] ?? [];
|
||||
$semanaInicio = $_POST['semana_inicio'] ?? '';
|
||||
$rotacionAutomatica = isset($_POST['rotacion_automatica']) ? true : false;
|
||||
|
||||
if (!empty($userIds) && $semanaInicio) {
|
||||
$resultado = $asignacionModel->asignarMasivo($userIds, $semanaInicio, $rotacionAutomatica);
|
||||
|
||||
if ($resultado['success'] > 0) {
|
||||
$message = "Se asignaron {$resultado['success']} turnos correctamente";
|
||||
if ($rotacionAutomatica) {
|
||||
$message .= " con rotación automática para la siguiente semana";
|
||||
}
|
||||
$messageType = 'success';
|
||||
}
|
||||
|
||||
if (!empty($resultado['errors'])) {
|
||||
$message .= "<br>Errores: " . implode('<br>', $resultado['errors']);
|
||||
$messageType = 'warning';
|
||||
}
|
||||
} else {
|
||||
$message = 'Debes seleccionar al menos un ayudante y una semana';
|
||||
$messageType = 'danger';
|
||||
}
|
||||
} else {
|
||||
$message = 'Debes seleccionar al menos un ayudante y una semana';
|
||||
$messageType = 'danger';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -167,6 +173,7 @@ $pageTitle = 'Asignación de Turnos';
|
||||
</div>
|
||||
|
||||
<form method="POST" class="d-flex gap-2">
|
||||
<?= CSRF::getTokenField() ?>
|
||||
<input type="hidden" name="action" value="rotar">
|
||||
<input type="hidden" name="semana" value="<?= $currentWeekStart ?>">
|
||||
<button type="submit" class="btn btn-outline-primary">
|
||||
@@ -178,6 +185,7 @@ $pageTitle = 'Asignación de Turnos';
|
||||
|
||||
<form method="POST">
|
||||
<input type="hidden" name="action" value="asignar">
|
||||
<?= CSRF::getTokenField() ?>
|
||||
<input type="hidden" name="semana" value="<?= $currentWeekStart ?>">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Asignar a:</label>
|
||||
@@ -260,6 +268,7 @@ $pageTitle = 'Asignación de Turnos';
|
||||
</div>
|
||||
|
||||
<form method="POST" class="d-flex gap-2">
|
||||
<?= CSRF::getTokenField() ?>
|
||||
<input type="hidden" name="action" value="asignar">
|
||||
<input type="hidden" name="semana" value="<?= $semanaVer ?>">
|
||||
<select class="form-select" name="user_id" style="max-width: 250px;">
|
||||
@@ -302,6 +311,7 @@ No hay asignación para la semana <?= $posicionSinAsignar ?> de 4 (<?= date('d/m
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form method="POST" id="asignacionMasivaForm">
|
||||
<?= CSRF::getTokenField() ?>
|
||||
<input type="hidden" name="action" value="asignar_masivo">
|
||||
|
||||
<div class="row mb-3">
|
||||
@@ -401,6 +411,7 @@ No hay asignación para la semana <?= $posicionSinAsignar ?> de 4 (<?= date('d/m
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<form method="POST" class="h-100 d-flex flex-column justify-content-center">
|
||||
<?= CSRF::getTokenField() ?>
|
||||
<input type="hidden" name="action" value="rotacion_automatica">
|
||||
<button type="submit" class="btn btn-warning w-100">
|
||||
<i class="fas fa-sync"></i> Generar Rotación Automática
|
||||
@@ -429,6 +440,7 @@ No hay asignación para la semana <?= $posicionSinAsignar ?> de 4 (<?= date('d/m
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form method="POST" id="reordenarForm">
|
||||
<?= CSRF::getTokenField() ?>
|
||||
<input type="hidden" name="action" value="reordenar">
|
||||
|
||||
<p class="text-muted">
|
||||
|
||||
@@ -5,6 +5,7 @@ if (!defined('BASE_PATH')) {
|
||||
require_once BASE_PATH . '/config/config.php';
|
||||
require_once BASE_PATH . '/src/Auth.php';
|
||||
require_once BASE_PATH . '/src/DiasHorarios.php';
|
||||
require_once BASE_PATH . '/src/CSRF.php';
|
||||
|
||||
$auth = new Auth();
|
||||
$auth->requireAdmin();
|
||||
@@ -24,18 +25,23 @@ $diasNombres = [
|
||||
];
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$dia = $_POST['dia'] ?? '';
|
||||
$hora_apertura = $_POST["hora_apertura_$dia"] ?? '';
|
||||
$hora_cierre = $_POST["hora_cierre_$dia"] ?? '';
|
||||
$activo = isset($_POST["activo_$dia"]) ? 1 : 0;
|
||||
|
||||
if (empty($dia) || empty($hora_apertura) || empty($hora_cierre)) {
|
||||
$message = 'Todos los campos son obligatorios';
|
||||
if (!CSRF::isValidRequest()) {
|
||||
$message = 'Error de validación del formulario';
|
||||
$messageType = 'danger';
|
||||
} else {
|
||||
$horariosModel->update($dia, compact('hora_apertura', 'hora_cierre', 'activo'));
|
||||
$message = 'Horario actualizado correctamente';
|
||||
$messageType = 'success';
|
||||
$dia = $_POST['dia'] ?? '';
|
||||
$hora_apertura = $_POST["hora_apertura_$dia"] ?? '';
|
||||
$hora_cierre = $_POST["hora_cierre_$dia"] ?? '';
|
||||
$activo = isset($_POST["activo_$dia"]) ? 1 : 0;
|
||||
|
||||
if (empty($dia) || empty($hora_apertura) || empty($hora_cierre)) {
|
||||
$message = 'Todos los campos son obligatorios';
|
||||
$messageType = 'danger';
|
||||
} else {
|
||||
$horariosModel->update($dia, compact('hora_apertura', 'hora_cierre', 'activo'));
|
||||
$message = 'Horario actualizado correctamente';
|
||||
$messageType = 'success';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +70,7 @@ $pageTitle = 'Configuración de Horarios';
|
||||
<div class="card shadow-sm">
|
||||
<div class="card-body">
|
||||
<form method="POST" id="horariosForm">
|
||||
<?= CSRF::getTokenField() ?>
|
||||
<input type="hidden" name="dia" id="selectedDia" value="">
|
||||
|
||||
<div class="table-responsive">
|
||||
|
||||
@@ -5,6 +5,8 @@ if (!defined('BASE_PATH')) {
|
||||
require_once BASE_PATH . '/config/config.php';
|
||||
require_once BASE_PATH . '/src/Auth.php';
|
||||
require_once BASE_PATH . '/src/User.php';
|
||||
require_once BASE_PATH . '/src/CSRF.php';
|
||||
require_once BASE_PATH . '/src/Session.php';
|
||||
|
||||
$auth = new Auth();
|
||||
$auth->requireAdmin();
|
||||
@@ -14,59 +16,64 @@ $message = '';
|
||||
$messageType = '';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$action = $_POST['action'] ?? '';
|
||||
if (!CSRF::isValidRequest()) {
|
||||
$message = 'Error de validación del formulario';
|
||||
$messageType = 'danger';
|
||||
} else {
|
||||
$action = $_POST['action'] ?? '';
|
||||
|
||||
if ($action === 'create') {
|
||||
$nombre = trim($_POST['nombre'] ?? '');
|
||||
$email = trim($_POST['email'] ?? '');
|
||||
$username = trim($_POST['username'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
$rol = $_POST['rol'] ?? 'ayudante';
|
||||
if ($action === 'create') {
|
||||
$nombre = trim($_POST['nombre'] ?? '');
|
||||
$email = trim($_POST['email'] ?? '');
|
||||
$username = trim($_POST['username'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
$rol = $_POST['rol'] ?? 'ayudante';
|
||||
|
||||
if (empty($nombre) || empty($email) || empty($password)) {
|
||||
$message = 'Todos los campos son obligatorios';
|
||||
$messageType = 'danger';
|
||||
} elseif ($userModel->getByEmail($email)) {
|
||||
$message = 'El email ya está registrado';
|
||||
$messageType = 'danger';
|
||||
} elseif ($username && $userModel->usernameExists($username)) {
|
||||
$message = 'El username ya está en uso';
|
||||
$messageType = 'danger';
|
||||
} else {
|
||||
$userModel->create(compact('nombre', 'email', 'username', 'password', 'rol'));
|
||||
$message = 'Usuario creado exitosamente';
|
||||
$messageType = 'success';
|
||||
}
|
||||
} elseif ($action === 'update') {
|
||||
$id = $_POST['id'] ?? 0;
|
||||
$nombre = trim($_POST['nombre'] ?? '');
|
||||
$email = trim($_POST['email'] ?? '');
|
||||
$username = trim($_POST['username'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
$rol = $_POST['rol'] ?? 'ayudante';
|
||||
|
||||
if (empty($nombre) || empty($email)) {
|
||||
$message = 'Nombre y email son obligatorios';
|
||||
$messageType = 'danger';
|
||||
} elseif ($userModel->usernameExists($username, $id)) {
|
||||
$message = 'El username ya está en uso';
|
||||
$messageType = 'danger';
|
||||
} else {
|
||||
$userModel->update($id, compact('nombre', 'email', 'username', 'password', 'rol'));
|
||||
$message = 'Usuario actualizado exitosamente';
|
||||
$messageType = 'success';
|
||||
}
|
||||
} elseif ($action === 'toggle') {
|
||||
$id = $_POST['id'] ?? 0;
|
||||
$user = $userModel->getById($id);
|
||||
if ($user) {
|
||||
if ($user['activo']) {
|
||||
$userModel->deactivate($id);
|
||||
if (empty($nombre) || empty($email) || empty($password)) {
|
||||
$message = 'Todos los campos son obligatorios';
|
||||
$messageType = 'danger';
|
||||
} elseif ($userModel->getByEmail($email)) {
|
||||
$message = 'El email ya está registrado';
|
||||
$messageType = 'danger';
|
||||
} elseif ($username && $userModel->usernameExists($username)) {
|
||||
$message = 'El username ya está en uso';
|
||||
$messageType = 'danger';
|
||||
} else {
|
||||
$userModel->activate($id);
|
||||
$userModel->create(compact('nombre', 'email', 'username', 'password', 'rol'));
|
||||
$message = 'Usuario creado exitosamente';
|
||||
$messageType = 'success';
|
||||
}
|
||||
} elseif ($action === 'update') {
|
||||
$id = $_POST['id'] ?? 0;
|
||||
$nombre = trim($_POST['nombre'] ?? '');
|
||||
$email = trim($_POST['email'] ?? '');
|
||||
$username = trim($_POST['username'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
$rol = $_POST['rol'] ?? 'ayudante';
|
||||
|
||||
if (empty($nombre) || empty($email)) {
|
||||
$message = 'Nombre y email son obligatorios';
|
||||
$messageType = 'danger';
|
||||
} elseif ($userModel->usernameExists($username, $id)) {
|
||||
$message = 'El username ya está en uso';
|
||||
$messageType = 'danger';
|
||||
} else {
|
||||
$userModel->update($id, compact('nombre', 'email', 'username', 'password', 'rol'));
|
||||
$message = 'Usuario actualizado exitosamente';
|
||||
$messageType = 'success';
|
||||
}
|
||||
} elseif ($action === 'toggle') {
|
||||
$id = $_POST['id'] ?? 0;
|
||||
$user = $userModel->getById($id);
|
||||
if ($user) {
|
||||
if ($user['activo']) {
|
||||
$userModel->deactivate($id);
|
||||
} else {
|
||||
$userModel->activate($id);
|
||||
}
|
||||
$message = 'Estado actualizado';
|
||||
$messageType = 'success';
|
||||
}
|
||||
$message = 'Estado actualizado';
|
||||
$messageType = 'success';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -133,15 +140,16 @@ $pageTitle = 'Gestión de Usuarios';
|
||||
onclick="editUser(<?= $u['id'] ?>, '<?= htmlspecialchars($u['nombre']) ?>', '<?= htmlspecialchars($u['email']) ?>', '<?= htmlspecialchars($u['username'] ?? '') ?>', '<?= $u['rol'] ?>')">
|
||||
Editar
|
||||
</button>
|
||||
<?php if ($u['id'] != $_SESSION['user_id']): ?>
|
||||
<form method="POST" class="d-inline">
|
||||
<input type="hidden" name="action" value="toggle">
|
||||
<input type="hidden" name="id" value="<?= $u['id'] ?>">
|
||||
<button type="submit" class="btn btn-sm btn-<?= $u['activo'] ? 'outline-warning' : 'outline-success' ?>">
|
||||
<?= $u['activo'] ? 'Desactivar' : 'Activar' ?>
|
||||
</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
<?php if ($u['id'] != Session::get('user_id')): ?>
|
||||
<form method="POST" class="d-inline">
|
||||
<?= CSRF::getTokenField() ?>
|
||||
<input type="hidden" name="action" value="toggle">
|
||||
<input type="hidden" name="id" value="<?= $u['id'] ?>">
|
||||
<button type="submit" class="btn btn-sm btn-<?= $u['activo'] ? 'outline-warning' : 'outline-success' ?>">
|
||||
<?= $u['activo'] ? 'Desactivar' : 'Activar' ?>
|
||||
</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
@@ -160,6 +168,7 @@ $pageTitle = 'Gestión de Usuarios';
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
<form method="POST" id="userForm">
|
||||
<?= CSRF::getTokenField() ?>
|
||||
<input type="hidden" name="action" value="create" id="formAction">
|
||||
<input type="hidden" name="id" value="" id="userId">
|
||||
<div class="modal-body">
|
||||
|
||||
@@ -3,6 +3,7 @@ require_once __DIR__ . '/../src/Auth.php';
|
||||
require_once __DIR__ . '/../src/User.php';
|
||||
require_once __DIR__ . '/../src/DiasHorarios.php';
|
||||
require_once __DIR__ . '/../src/Asignacion.php';
|
||||
require_once __DIR__ . '/../src/Database.php';
|
||||
|
||||
$auth = new Auth();
|
||||
$auth->requireAuth();
|
||||
@@ -15,18 +16,17 @@ if ($auth->isAdmin()) {
|
||||
$user = $auth->getCurrentUser();
|
||||
$horariosModel = new DiasHorarios();
|
||||
$asignacionModel = new Asignacion();
|
||||
$db = Database::getInstance()->getConnection();
|
||||
|
||||
$horarios = $horariosModel->getActivos();
|
||||
$asignacionActual = $asignacionModel->getAsignacionActual();
|
||||
|
||||
// Obtener todas las asignaciones de las próximas semanas
|
||||
$semanasFuturas = [];
|
||||
|
||||
// Encontrar el domingo de esta semana
|
||||
$hoy = new DateTime();
|
||||
$diaSemana = (int)$hoy->format('w'); // 0 = domingo, 6 = sábado
|
||||
$diaSemana = (int)$hoy->format('w');
|
||||
$domingoEstaSemana = clone $hoy;
|
||||
$domingoEstaSemana->modify('-' . $diaSemana . ' days'); // Restar días para llegar al domingo
|
||||
$domingoEstaSemana->modify('-' . $diaSemana . ' days');
|
||||
|
||||
for ($i = 0; $i <= 4; $i++) {
|
||||
$semanaDomingo = clone $domingoEstaSemana;
|
||||
@@ -37,7 +37,7 @@ for ($i = 0; $i <= 4; $i++) {
|
||||
|
||||
$semanasFuturas[] = [
|
||||
'inicio' => $semanaInicio,
|
||||
'fin' => date('Y-m-d', strtotime('+5 days', strtotime($semanaInicio))), // +5 días = domingo a viernes
|
||||
'fin' => date('Y-m-d', strtotime('+5 days', strtotime($semanaInicio))),
|
||||
'asignaciones' => $asignacionesSemana,
|
||||
'asignacion' => !empty($asignacionesSemana) ? $asignacionesSemana[0] : null
|
||||
];
|
||||
@@ -45,7 +45,6 @@ for ($i = 0; $i <= 4; $i++) {
|
||||
|
||||
$miTurno = $asignacionActual && $asignacionActual['id'] == $user['id'];
|
||||
|
||||
// También verificar si el usuario tiene turno en las próximas semanas
|
||||
$misAsignacionesFuturas = [];
|
||||
foreach ($semanasFuturas as $semana) {
|
||||
foreach ($semana['asignaciones'] as $asignacion) {
|
||||
@@ -57,6 +56,12 @@ foreach ($semanasFuturas as $semana) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$userModel = new User();
|
||||
$ayudantes = $userModel->getAyudantesActivos();
|
||||
|
||||
$domingo = new DateTime();
|
||||
$domingo->modify('-' . (int)$domingo->format('w') . ' days');
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
@@ -81,21 +86,18 @@ foreach ($semanasFuturas as $semana) {
|
||||
<h2 class="mb-4">Mis Turnos</h2>
|
||||
|
||||
<?php
|
||||
// Verificar si tiene turno esta semana (hoy está entre domingo y viernes de la semana actual)
|
||||
$hoy = new DateTime();
|
||||
$diaSemana = (int)$hoy->format('w'); // 0 = domingo, 6 = sábado
|
||||
$domingoActual = clone $hoy;
|
||||
$domingoActual->modify('-' . $diaSemana . ' days'); // Restar días para llegar al domingo
|
||||
$viernesActual = clone $domingoActual;
|
||||
$viernesActual->modify('+5 days');
|
||||
|
||||
$asignacionEstaSemana = $asignacionModel->getAsignacionPorSemana($domingoActual->format('Y-m-d'));
|
||||
$tengoTurnoEstaSemana = $asignacionEstaSemana && $asignacionEstaSemana['id'] == $user['id'];
|
||||
|
||||
if ($tengoTurnoEstaSemana):
|
||||
?>
|
||||
$hoy = new DateTime();
|
||||
$diaSemana = (int)$hoy->format('w');
|
||||
$domingoActual = clone $hoy;
|
||||
$domingoActual->modify('-' . $diaSemana . ' days');
|
||||
|
||||
$asignacionEstaSemana = $asignacionModel->getAsignacionPorSemana($domingoActual->format('Y-m-d'));
|
||||
$tengoTurnoEstaSemana = $asignacionEstaSemana && $asignacionEstaSemana['id'] == $user['id'];
|
||||
|
||||
if ($tengoTurnoEstaSemana):
|
||||
?>
|
||||
<div class="alert alert-success mb-4">
|
||||
<strong>¡Tienes turno esta semana!</strong><br>
|
||||
<strong>Tienes turno esta semana!</strong><br>
|
||||
Del <?= date('d/m/y', strtotime($asignacionEstaSemana['semana_inicio'])) ?>
|
||||
al <?= date('d/m/y', strtotime($asignacionEstaSemana['semana_fin'])) ?>
|
||||
</div>
|
||||
@@ -103,125 +105,161 @@ foreach ($semanasFuturas as $semana) {
|
||||
<div class="alert alert-secondary mb-4">
|
||||
<strong>Turno esta semana:</strong> <?= htmlspecialchars($asignacionEstaSemana['nombre']) ?><br>
|
||||
<?php if (!empty($misAsignacionesFuturas)): ?>
|
||||
Tu próximo turno: <?= date('d/m/y', strtotime($misAsignacionesFuturas[0]['semana']['inicio'])) ?>
|
||||
Tu proximo turno: <?= date('d/m/y', strtotime($misAsignacionesFuturas[0]['semana']['inicio'])) ?>
|
||||
al <?= date('d/m/y', strtotime($misAsignacionesFuturas[0]['semana']['fin'])) ?>
|
||||
<?php else: ?>
|
||||
Tu próximo turno será en las próximas semanas.
|
||||
Tu proximo turno sera en las proximas semanas.
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php elseif (!empty($misAsignacionesFuturas)): ?>
|
||||
<div class="alert alert-info mb-4">
|
||||
<strong>Próximo turno:</strong><br>
|
||||
<strong>Proximo turno:</strong><br>
|
||||
Del <?= date('d/m/y', strtotime($misAsignacionesFuturas[0]['semana']['inicio'])) ?>
|
||||
al <?= date('d/m/y', strtotime($misAsignacionesFuturas[0]['semana']['fin'])) ?>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="alert alert-warning mb-4">
|
||||
No hay turnos asignados para las próximas semanas.
|
||||
No hay turnos asignados para las proximas semanas.
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="card shadow-sm">
|
||||
<div class="card-header bg-primary text-white">
|
||||
<h5 class="mb-0">Horarios de Apertura del Contenedor</h5>
|
||||
<div class="card-header bg-warning text-dark">
|
||||
<h5 class="mb-0">Horarios por Semana</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<?php
|
||||
$diasNombres = [
|
||||
'domingo' => 'Domingo',
|
||||
'lunes' => 'Lunes',
|
||||
'martes' => 'Martes',
|
||||
'miercoles' => 'Miercoles',
|
||||
'jueves' => 'Jueves',
|
||||
'viernes' => 'Viernes',
|
||||
'sabado' => 'Sabado'
|
||||
];
|
||||
$diasOrden = ['domingo', 'lunes', 'martes', 'miercoles', 'jueves', 'viernes', 'sabado'];
|
||||
?>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover mb-0">
|
||||
<thead>
|
||||
<table class="table table-bordered table-sm mb-0" style="min-width: 600px;">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Día</th>
|
||||
<th>Hora Apertura</th>
|
||||
<th>Hora Cierre</th>
|
||||
<th class="text-center" style="min-width: 120px;">Semana</th>
|
||||
<?php foreach ($diasOrden as $dia): ?>
|
||||
<th class="text-center"><?= $diasNombres[$dia] ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($horarios as $h): ?>
|
||||
<tr class="<?= $miTurno ? 'table-primary' : '' ?>">
|
||||
<td><strong><?= ucfirst($h['dia_semana']) ?></strong></td>
|
||||
<td><?= date('H:i', strtotime($h['hora_apertura'])) ?></td>
|
||||
<td><?= date('H:i', strtotime($h['hora_cierre'])) ?></td>
|
||||
<?php foreach ($semanasFuturas as $index => $semana): ?>
|
||||
<?php
|
||||
$esMiTurno = !empty($semana['asignaciones']) && in_array($user['id'], array_column($semana['asignaciones'], 'id'));
|
||||
$rowClass = $esMiTurno ? 'table-success' : '';
|
||||
?>
|
||||
<tr class="<?= $rowClass ?>">
|
||||
<td class="text-center align-middle">
|
||||
<strong><?= date('d/m', strtotime($semana['inicio'])) ?></strong>
|
||||
<?php if ($index === 0): ?>
|
||||
<span class="badge bg-primary ms-1">Actual</span>
|
||||
<?php endif; ?>
|
||||
<?php if ($esMiTurno): ?>
|
||||
<span class="badge bg-success ms-1">Tu turno</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<?php foreach ($diasOrden as $dia): ?>
|
||||
<?php
|
||||
$horarioDia = null;
|
||||
foreach ($horarios as $h) {
|
||||
if ($h['dia_semana'] === $dia) {
|
||||
$horarioDia = $h;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$esActivo = $horarioDia && $horarioDia['activo'];
|
||||
$claseDia = '';
|
||||
if ($esMiTurno && $esActivo) {
|
||||
$claseDia = 'bg-success text-white';
|
||||
} elseif (!$esActivo) {
|
||||
$claseDia = 'text-muted';
|
||||
}
|
||||
?>
|
||||
<td class="text-center <?= $claseDia ?>" style="min-width: 100px;">
|
||||
<?php if ($esActivo): ?>
|
||||
<small><?= date('H:i', strtotime($horarioDia['hora_apertura'])) ?></small><br>
|
||||
<small><?= date('H:i', strtotime($horarioDia['hora_cierre'])) ?></small>
|
||||
<?php else: ?>
|
||||
<small class="text-muted">Cerrado</small>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Tabla de Asignaciones de Turnos -->
|
||||
<div class="card mt-4 shadow-sm">
|
||||
<div class="card-header bg-success text-white">
|
||||
<h5 class="mb-0">Calendario de Turnos</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover mb-0">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Semana</th>
|
||||
<th>Período</th>
|
||||
<th>Asignado a</th>
|
||||
<th>Estado</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($semanasFuturas as $index => $semana): ?>
|
||||
<tr class="<?= !empty($semana['asignaciones']) && in_array($user['id'], array_column($semana['asignaciones'], 'id')) ? 'table-success' : '' ?>">
|
||||
<td>
|
||||
<strong>Semana <?= date('d/m/y', strtotime($semana['inicio'])) ?></strong>
|
||||
<?php if ($index === 0): ?>
|
||||
<span class="badge bg-primary ms-1">Actual</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td>
|
||||
<?= date('d/m/y', strtotime($semana['inicio'])) ?> (Dom) -
|
||||
<?= date('d/m/y', strtotime($semana['fin'])) ?> (Vie)
|
||||
</td>
|
||||
<td>
|
||||
<?php if (!empty($semana['asignaciones'])): ?>
|
||||
<?php foreach ($semana['asignaciones'] as $asignacion): ?>
|
||||
<div class="mb-1">
|
||||
<?= htmlspecialchars($asignacion['nombre']) ?>
|
||||
<?php if ($asignacion['id'] == $user['id']): ?>
|
||||
<span class="badge bg-success ms-1">Tú</span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
<?php else: ?>
|
||||
<span class="text-muted">Sin asignar</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td>
|
||||
<?php if (!empty($semana['asignaciones']) && in_array($user['id'], array_column($semana['asignaciones'], 'id'))): ?>
|
||||
<span class="badge bg-success">Tu turno</span>
|
||||
<?php elseif (!empty($semana['asignaciones'])): ?>
|
||||
<span class="badge bg-secondary">
|
||||
<?= count($semana['asignaciones']) ?> asignado(s)
|
||||
</span>
|
||||
<?php else: ?>
|
||||
<span class="badge bg-warning">Pendiente</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="mt-2">
|
||||
<small class="text-muted">
|
||||
<span class="badge bg-success text-white">Verde</span> = Tu turno activo |
|
||||
<span class="badge bg-secondary text-white">Gris</span> = Dia cerrado |
|
||||
Horario: Apertura - Cierre
|
||||
</small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card mt-4 shadow-sm">
|
||||
<div class="card-header bg-info text-white">
|
||||
<h5 class="mb-0">Información</h5>
|
||||
<h5 class="mb-0">Turnos de Ayudantes</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<ul class="mb-0">
|
||||
<li>Los turnos se asignan de forma rotativa semanalmente.</li>
|
||||
<li>Cada semana inicia en lunes y termina en domingo.</li>
|
||||
<li>Recuerda estar atento a tu turno para abrir y cerrar el contenedor.</li>
|
||||
<li>Las filas en verde indican tus turnos asignados.</li>
|
||||
</ul>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover mb-0">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Ayudante</th>
|
||||
<th class="text-center">Fecha 1</th>
|
||||
<th class="text-center">Fecha 2</th>
|
||||
<th class="text-center">Fecha 3</th>
|
||||
<th class="text-center">Fecha 4</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($ayudantes as $ayudante): ?>
|
||||
<?php
|
||||
$stmt = $db->prepare("
|
||||
SELECT semana_inicio, semana_fin
|
||||
FROM asignaciones_turnos
|
||||
WHERE user_id = ? AND semana_inicio >= CURDATE()
|
||||
ORDER BY semana_inicio
|
||||
LIMIT 4
|
||||
");
|
||||
$stmt->execute([$ayudante['id']]);
|
||||
$turnos = $stmt->fetchAll();
|
||||
?>
|
||||
<tr class="<?= $ayudante['id'] == $user['id'] ? 'table-success' : '' ?>">
|
||||
<td>
|
||||
<?= htmlspecialchars($ayudante['nombre']) ?>
|
||||
<?php if ($ayudante['id'] == $user['id']): ?>
|
||||
<span class="badge bg-success ms-1">Tu</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<?php for ($i = 0; $i < 4; $i++): ?>
|
||||
<td class="text-center">
|
||||
<?php if (isset($turnos[$i])): ?>
|
||||
<span class="badge bg-primary">
|
||||
<?= date('d/m/Y', strtotime($turnos[$i]['semana_inicio'])) ?> -
|
||||
<?= date('d/m/Y', strtotime($turnos[$i]['semana_fin'])) ?>
|
||||
</span>
|
||||
<?php else: ?>
|
||||
<span class="text-muted">-</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<?php endfor; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -3,6 +3,7 @@ ini_set('display_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
require_once __DIR__ . '/../src/Auth.php';
|
||||
require_once __DIR__ . '/../src/CSRF.php';
|
||||
|
||||
$auth = new Auth();
|
||||
|
||||
@@ -19,22 +20,25 @@ $error = '';
|
||||
$loginInput = '';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$loginInput = trim($_POST['login'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
if (empty($loginInput) || empty($password)) {
|
||||
$error = 'Por favor ingresa usuario/email y contraseña';
|
||||
if (!CSRF::isValidRequest()) {
|
||||
$error = 'Error de validación del formulario';
|
||||
} else {
|
||||
if ($auth->login($loginInput, $password)) {
|
||||
session_write_close();
|
||||
if ($auth->isAdmin()) {
|
||||
header('Location: /admin/index.php');
|
||||
} else {
|
||||
header('Location: /ayudante.php');
|
||||
}
|
||||
exit;
|
||||
$loginInput = trim($_POST['login'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
if (empty($loginInput) || empty($password)) {
|
||||
$error = 'Por favor ingresa usuario/email y contraseña';
|
||||
} else {
|
||||
$error = 'Usuario/email o contraseña incorrectos';
|
||||
if ($auth->login($loginInput, $password)) {
|
||||
if ($auth->isAdmin()) {
|
||||
header('Location: /admin/index.php');
|
||||
} else {
|
||||
header('Location: /ayudante.php');
|
||||
}
|
||||
exit;
|
||||
} else {
|
||||
$error = 'Usuario/email o contraseña incorrectos';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,6 +65,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
<?php endif; ?>
|
||||
|
||||
<form method="POST">
|
||||
<?= CSRF::getTokenField() ?>
|
||||
<div class="mb-3">
|
||||
<label for="login" class="form-label">Usuario o Email</label>
|
||||
<input type="text" class="form-control" id="login" name="login"
|
||||
|
||||
64
src/Auth.php
64
src/Auth.php
@@ -2,52 +2,49 @@
|
||||
|
||||
require_once __DIR__ . '/Database.php';
|
||||
require_once __DIR__ . '/User.php';
|
||||
|
||||
$sessionPath = dirname(__DIR__) . '/sessions';
|
||||
if (!is_dir($sessionPath)) {
|
||||
mkdir($sessionPath, 0733, true);
|
||||
}
|
||||
session_save_path($sessionPath);
|
||||
session_name('contenedor_session');
|
||||
require_once __DIR__ . '/Session.php';
|
||||
|
||||
class Auth {
|
||||
private $db;
|
||||
private $userModel;
|
||||
|
||||
public function __construct() {
|
||||
$this->db = Database::getInstance()->getConnection();
|
||||
$this->userModel = new User();
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
Session::init();
|
||||
}
|
||||
|
||||
public function login($login, $password) {
|
||||
$user = $this->userModel->findByLogin($login);
|
||||
|
||||
if ($user && password_verify($password, $user['password'])) {
|
||||
$_SESSION['user_id'] = $user['id'];
|
||||
$_SESSION['user_name'] = $user['nombre'];
|
||||
$_SESSION['user_rol'] = $user['rol'];
|
||||
$_SESSION['logged_in'] = true;
|
||||
session_write_close();
|
||||
Session::set('user_id', $user['id']);
|
||||
Session::set('user_name', $user['nombre']);
|
||||
Session::set('user_rol', $user['rol']);
|
||||
Session::set('logged_in', true);
|
||||
Session::set('login_time', time());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function logout() {
|
||||
session_destroy();
|
||||
$_SESSION = [];
|
||||
Session::destroy();
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isLoggedIn() {
|
||||
return isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true;
|
||||
return Session::get('logged_in') === true;
|
||||
}
|
||||
|
||||
public function isAdmin() {
|
||||
return isset($_SESSION['user_rol']) && $_SESSION['user_rol'] === 'admin';
|
||||
return Session::get('user_rol') === 'admin';
|
||||
}
|
||||
|
||||
public function isAyudante() {
|
||||
return Session::get('user_rol') === 'ayudante';
|
||||
}
|
||||
|
||||
public function hasRole($role) {
|
||||
return Session::get('user_rol') === $role;
|
||||
}
|
||||
|
||||
public function getCurrentUser() {
|
||||
@@ -55,23 +52,32 @@ class Auth {
|
||||
return null;
|
||||
}
|
||||
return [
|
||||
'id' => $_SESSION['user_id'],
|
||||
'nombre' => $_SESSION['user_name'],
|
||||
'rol' => $_SESSION['user_rol']
|
||||
'id' => Session::get('user_id'),
|
||||
'nombre' => Session::get('user_name'),
|
||||
'rol' => Session::get('user_rol')
|
||||
];
|
||||
}
|
||||
|
||||
public function requireAuth() {
|
||||
public function requireAuth($redirectUrl = '/login.php') {
|
||||
if (!$this->isLoggedIn()) {
|
||||
header('Location: /login.php');
|
||||
header('Location: ' . $redirectUrl);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
public function requireAdmin() {
|
||||
$this->requireAuth();
|
||||
public function requireAdmin($redirectUrl = '/ayudante.php') {
|
||||
$this->requireAuth($redirectUrl);
|
||||
if (!$this->isAdmin()) {
|
||||
header('Location: /ayudante.php');
|
||||
header('Location: ' . $redirectUrl);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
public function requireRole($roles, $redirectUrl = '/') {
|
||||
$this->requireAuth($redirectUrl);
|
||||
$userRole = Session::get('user_rol');
|
||||
if (!in_array($userRole, (array)$roles)) {
|
||||
header('Location: ' . $redirectUrl);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
65
src/CSRF.php
Normal file
65
src/CSRF.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
class CSRF {
|
||||
private static $tokenName = 'csrf_token';
|
||||
private static $tokenLifetime = 3600;
|
||||
|
||||
public static function generateToken() {
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
$token = bin2hex(random_bytes(32));
|
||||
$_SESSION[self::$tokenName] = [
|
||||
'token' => $token,
|
||||
'expires' => time() + self::$tokenLifetime
|
||||
];
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
public static function getToken() {
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
if (isset($_SESSION[self::$tokenName])) {
|
||||
$data = $_SESSION[self::$tokenName];
|
||||
if ($data['expires'] > time()) {
|
||||
return $data['token'];
|
||||
}
|
||||
unset($_SESSION[self::$tokenName]);
|
||||
}
|
||||
|
||||
return self::generateToken();
|
||||
}
|
||||
|
||||
public static function validateToken($token) {
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
if (!isset($_SESSION[self::$tokenName]) || empty($token)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = $_SESSION[self::$tokenName];
|
||||
unset($_SESSION[self::$tokenName]);
|
||||
|
||||
return hash_equals($data['token'], $token);
|
||||
}
|
||||
|
||||
public static function getTokenField() {
|
||||
return '<input type="hidden" name="csrf_token" value="' . self::getToken() . '">';
|
||||
}
|
||||
|
||||
public static function isValidRequest() {
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
if ($method === 'GET' || $method === 'HEAD') {
|
||||
return true;
|
||||
}
|
||||
|
||||
$token = $_POST['csrf_token'] ?? $_SERVER['HTTP_X_CSRF_TOKEN'] ?? null;
|
||||
return self::validateToken($token);
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,8 @@ class Database {
|
||||
]
|
||||
);
|
||||
} catch (PDOException $e) {
|
||||
die("Error de conexión a la base de datos: " . $e->getMessage());
|
||||
error_log("Error de conexión a la base de datos: " . $e->getMessage());
|
||||
throw new Exception("Error de conexión a la base de datos");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
54
src/Session.php
Normal file
54
src/Session.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
class Session {
|
||||
private static $sessionPath;
|
||||
|
||||
public static function init() {
|
||||
$sessionPath = dirname(__DIR__) . '/sessions';
|
||||
if (!is_dir($sessionPath)) {
|
||||
mkdir($sessionPath, 0733, true);
|
||||
}
|
||||
session_save_path($sessionPath);
|
||||
session_name('contenedor_session');
|
||||
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
}
|
||||
|
||||
public static function set($key, $value) {
|
||||
self::init();
|
||||
$_SESSION[$key] = $value;
|
||||
}
|
||||
|
||||
public static function get($key, $default = null) {
|
||||
self::init();
|
||||
return $_SESSION[$key] ?? $default;
|
||||
}
|
||||
|
||||
public static function has($key) {
|
||||
self::init();
|
||||
return isset($_SESSION[$key]);
|
||||
}
|
||||
|
||||
public static function remove($key) {
|
||||
self::init();
|
||||
unset($_SESSION[$key]);
|
||||
}
|
||||
|
||||
public static function destroy() {
|
||||
self::init();
|
||||
session_destroy();
|
||||
$_SESSION = [];
|
||||
}
|
||||
|
||||
public static function regenerate($deleteOldSession = false) {
|
||||
self::init();
|
||||
session_regenerate_id($deleteOldSession);
|
||||
}
|
||||
|
||||
public static function getId() {
|
||||
self::init();
|
||||
return session_id();
|
||||
}
|
||||
}
|
||||
16
src/User.php
16
src/User.php
@@ -125,4 +125,20 @@ class User {
|
||||
$stmt = $this->db->query("SELECT * FROM users WHERE rol = 'ayudante' AND activo = 1 ORDER BY nombre");
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public function isValidRole($role) {
|
||||
return in_array($role, ['admin', 'ayudante']);
|
||||
}
|
||||
|
||||
public function hasRole($userId, $role) {
|
||||
$stmt = $this->db->prepare("SELECT rol FROM users WHERE id = ?");
|
||||
$stmt->execute([$userId]);
|
||||
$user = $stmt->fetch();
|
||||
return $user && $user['rol'] === $role;
|
||||
}
|
||||
|
||||
public function getAdmins() {
|
||||
$stmt = $this->db->query("SELECT * FROM users WHERE rol = 'admin' AND activo = 1 ORDER BY nombre");
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user