turnero: chequeo IP en vivo en cada carga de página

En lugar de confiar solo en la sesión (seteada al login), recepcion.php
y lugar.php ahora consultan turnero_dispositivos por IP en cada request.
Esto cubre sesiones preexistentes y evita que aparezca el selector cuando
la IP ya tiene un lugar asignado.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-09 10:25:22 -05:00
co-authored by Claude Sonnet 4.6
parent 003d16b6a2
commit 9afadb9a6f
2 changed files with 45 additions and 29 deletions
+22 -19
View File
@@ -10,28 +10,31 @@ if (!isUserLoggedIn()) {
}
// Tablet asignada → forzar su lugar, bloquear cualquier otro
// Chequeo en vivo por IP (no solo sesión, para sesiones preexistentes)
$lugarForzado = 0;
if (!empty($_SESSION['turnero_dispositivo'])) {
$_d = $_SESSION['turnero_dispositivo'];
$_forzado = (int)$_d['lugar_id'];
if ($_d['tipo'] === 'recepcion') {
header('Location: ' . BASE_URL . 'erp.php?m=turnero&v=recepcion&desk_id=' . $_forzado); exit;
}
if ((int)($_GET['lugar_id'] ?? 0) !== $_forzado) {
header('Location: ' . BASE_URL . 'erp.php?m=turnero&v=lugar&lugar_id=' . $_forzado); exit;
}
$lugarForzado = $_forzado;
}
// Restricción a nivel de usuario (turnero_lugar_id en admin_users)
if (!$lugarForzado) {
$_userLugar = (int)($_SESSION['admin_user']['turnero_lugar_id'] ?? 0);
if ($_userLugar) {
if ((int)($_GET['lugar_id'] ?? 0) !== $_userLugar) {
header('Location: ' . BASE_URL . 'erp.php?m=turnero&v=lugar&lugar_id=' . $_userLugar); exit;
try {
$_clientIp = trim(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'] ?? '')[0]);
$_dispPdo = Database::getInstance()->getConnection();
$_dispStmt = $_dispPdo->prepare(
"SELECT td.lugar_id, td.nombre, tl.tipo
FROM turnero_dispositivos td
JOIN turnero_lugares tl ON tl.id = td.lugar_id
WHERE td.ip = ? AND td.activo = 1 LIMIT 1"
);
$_dispStmt->execute([$_clientIp]);
$_dispRow = $_dispStmt->fetch(PDO::FETCH_ASSOC);
if ($_dispRow) {
$_SESSION['turnero_dispositivo'] = $_dispRow;
$_forzado = (int)$_dispRow['lugar_id'];
if ($_dispRow['tipo'] === 'recepcion') {
header('Location: ' . BASE_URL . 'erp.php?m=turnero&v=recepcion&desk_id=' . $_forzado); exit;
}
$lugarForzado = $_userLugar;
if ((int)($_GET['lugar_id'] ?? 0) !== $_forzado) {
header('Location: ' . BASE_URL . 'erp.php?m=turnero&v=lugar&lugar_id=' . $_forzado); exit;
}
$lugarForzado = $_forzado;
}
}
} catch (\Throwable $_) {}
try {
$pdo = Database::getInstance()->getConnection();
+23 -10
View File
@@ -10,18 +10,31 @@ if (!isUserLoggedIn()) {
}
// Tablet asignada → forzar su escritorio, bloquear cualquier otro
// Chequeo en vivo por IP (no solo sesión, para sesiones preexistentes)
$_recepForzado = 0;
if (!empty($_SESSION['turnero_dispositivo'])) {
$_d = $_SESSION['turnero_dispositivo'];
$_forzado = (int)$_d['lugar_id'];
if ($_d['tipo'] !== 'recepcion') {
header('Location: ' . BASE_URL . 'erp.php?m=turnero&v=lugar&lugar_id=' . $_forzado); exit;
try {
$_clientIp = trim(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'] ?? '')[0]);
$_dispPdo = Database::getInstance()->getConnection();
$_dispStmt = $_dispPdo->prepare(
"SELECT td.lugar_id, td.nombre, tl.tipo
FROM turnero_dispositivos td
JOIN turnero_lugares tl ON tl.id = td.lugar_id
WHERE td.ip = ? AND td.activo = 1 LIMIT 1"
);
$_dispStmt->execute([$_clientIp]);
$_dispRow = $_dispStmt->fetch(PDO::FETCH_ASSOC);
if ($_dispRow) {
$_SESSION['turnero_dispositivo'] = $_dispRow; // mantener sesión actualizada
$_forzado = (int)$_dispRow['lugar_id'];
if ($_dispRow['tipo'] !== 'recepcion') {
header('Location: ' . BASE_URL . 'erp.php?m=turnero&v=lugar&lugar_id=' . $_forzado); exit;
}
if ((int)($_GET['desk_id'] ?? 0) !== $_forzado) {
header('Location: ' . BASE_URL . 'erp.php?m=turnero&v=recepcion&desk_id=' . $_forzado); exit;
}
$_recepForzado = $_forzado;
}
if ((int)($_GET['desk_id'] ?? 0) !== $_forzado) {
header('Location: ' . BASE_URL . 'erp.php?m=turnero&v=recepcion&desk_id=' . $_forzado); exit;
}
$_recepForzado = $_forzado;
}
} catch (\Throwable $_) {}
// Restricción a nivel de usuario (turnero_lugar_id en admin_users)
if (!$_recepForzado) {
$_userDesk = (int)($_SESSION['admin_user']['turnero_lugar_id'] ?? 0);