fix: detect device by token (cookie) before IP fallback in sidebar menu

For recepcionista and bacteriologo roles, module.php now checks the
turnero_token cookie first — same order as recepcion.php — so token-
registered devices show only their assigned desk/lugar in the menu.
IP fallback now requires token IS NULL to avoid ambiguous matches.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-09 13:27:21 -05:00
co-authored by Claude Sonnet 4.6
parent dfff945066
commit 417b53528b
+36 -13
View File
@@ -34,16 +34,28 @@ if ($_trIsRecep) {
$_trLinks[] = ['name' => 'Verificar Paciente', 'icon' => 'fas fa-id-card', 'route' => '/erp.php?m=turnero&v=verificar_paciente'];
$_trLinks[] = ['name' => 'Pantalla TV', 'icon' => 'fas fa-tv', 'route' => '/erp.php?m=turnero&v=display_global'];
// Detectar si la IP está asignada a un desk de recepción
// Detectar escritorio de recepción: primero por token, luego por IP
$_trRecepIpDesk = null;
try {
$_trRecepIpRow = $_trPdo->prepare(
"SELECT d.lugar_id FROM turnero_dispositivos d
JOIN turnero_lugares l ON l.id = d.lugar_id
WHERE d.ip = ? AND d.activo = 1 AND l.tipo = 'recepcion' LIMIT 1"
);
$_trRecepIpRow->execute([$_trClientIp]);
$_trRecepIpDesk = $_trRecepIpRow->fetchColumn() ?: null;
$_trDevToken = trim($_COOKIE['turnero_token'] ?? '');
if ($_trDevToken) {
$_trTokRow = $_trPdo->prepare(
"SELECT d.lugar_id FROM turnero_dispositivos d
JOIN turnero_lugares l ON l.id = d.lugar_id
WHERE d.token = ? AND d.activo = 1 AND l.tipo = 'recepcion' LIMIT 1"
);
$_trTokRow->execute([$_trDevToken]);
$_trRecepIpDesk = $_trTokRow->fetchColumn() ?: null;
}
if (!$_trRecepIpDesk) {
$_trRecepIpRow = $_trPdo->prepare(
"SELECT d.lugar_id FROM turnero_dispositivos d
JOIN turnero_lugares l ON l.id = d.lugar_id
WHERE d.ip = ? AND d.token IS NULL AND d.activo = 1 AND l.tipo = 'recepcion' LIMIT 1"
);
$_trRecepIpRow->execute([$_trClientIp]);
$_trRecepIpDesk = $_trRecepIpRow->fetchColumn() ?: null;
}
} catch (\Throwable $_) {}
if ($_trRecepIpDesk) {
@@ -73,13 +85,24 @@ if ($_trIsRecep) {
// ── Bacteriólogo: sin dashboard ni historial; vista según IP ──
} elseif ($_trIsBacte) {
// Detectar lugar de muestras: primero por token, luego por IP
$_trIpLugar = null;
try {
$_trIpRow = $_trPdo->prepare(
"SELECT lugar_id FROM turnero_dispositivos WHERE ip = ? AND activo = 1 LIMIT 1"
);
$_trIpRow->execute([$_trClientIp]);
$_trIpLugar = $_trIpRow->fetchColumn() ?: null;
$_trDevToken = trim($_COOKIE['turnero_token'] ?? '');
if ($_trDevToken) {
$_trTokRow2 = $_trPdo->prepare(
"SELECT lugar_id FROM turnero_dispositivos WHERE token = ? AND activo = 1 LIMIT 1"
);
$_trTokRow2->execute([$_trDevToken]);
$_trIpLugar = $_trTokRow2->fetchColumn() ?: null;
}
if (!$_trIpLugar) {
$_trIpRow = $_trPdo->prepare(
"SELECT lugar_id FROM turnero_dispositivos WHERE ip = ? AND token IS NULL AND activo = 1 LIMIT 1"
);
$_trIpRow->execute([$_trClientIp]);
$_trIpLugar = $_trIpRow->fetchColumn() ?: null;
}
} catch (\Throwable $_) {}
if ($_trIpLugar) {