| 1 | import { consumeJson } from "../lib/js/consumeJson.js" |
| 2 | import { exportaAHtml } from "../lib/js/exportaAHtml.js" |
| 3 | import { muestraError } from "../lib/js/muestraError.js" |
| 4 | import { MiNav } from "./custom/mi-nav.js" |
| 5 | import { Sesion } from "./Sesion.js" |
| 6 | |
| 7 | const SERVICIO = 'srv/sesion-actual.php' |
| 8 | const URL_DE_PROTECCION = 'index.html' |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | export async function protege(nav, rolIdsPermitidos) { |
| 15 | try { |
| 16 | |
| 17 | const respuesta = await consumeJson(SERVICIO) |
| 18 | const sesion = new Sesion(respuesta.body) |
| 19 | |
| 20 | if (nav) { |
| 21 | nav.sesion = sesion |
| 22 | } |
| 23 | |
| 24 | if (rolIdsPermitidos === undefined) { |
| 25 | |
| 26 | return sesion |
| 27 | |
| 28 | } else { |
| 29 | |
| 30 | const rolIds = sesion.rolIds |
| 31 | |
| 32 | for (const rolId of rolIdsPermitidos) { |
| 33 | if (rolIds.has(rolId)) { |
| 34 | return sesion |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if (location.href !== URL_DE_PROTECCION) { |
| 39 | location.href = URL_DE_PROTECCION |
| 40 | } |
| 41 | |
| 42 | throw new Error("No autorizado.") |
| 43 | |
| 44 | } |
| 45 | |
| 46 | } catch (error) { |
| 47 | muestraError(error) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | exportaAHtml(protege) |