1 | import { invocaServicio } from "../lib/js/invocaServicio.js" |
2 | import { Sesion } from "./Sesion.js" |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | export async function protege(servicio, rolIdsPermitidos, urlDeProtección) { |
10 | const respuesta = await invocaServicio('srv/srvSesion.php') |
11 | const sesion = new Sesion(respuesta.body) |
12 | if (rolIdsPermitidos === undefined) { |
13 | return sesion |
14 | } else { |
15 | const rolIds = sesion.rolIds |
16 | for (const rolId of rolIdsPermitidos) { |
17 | if (rolIds.has(rolId)) { |
18 | return sesion |
19 | } |
20 | } |
21 | if (urlDeProtección !== undefined) { |
22 | location.href = urlDeProtección |
23 | } |
24 | throw new Error("No autorizado.") |
25 | } |
26 | } |
27 | |
28 | |
29 | window["protege"] = protege |