| 1 | // Permisos por ámbito: nada corre sin consentimiento explícito del usuario. |
| 2 | import { t } from './i18n.js'; |
| 3 | |
| 4 | const KEY = 'elffuss.grants'; |
| 5 | const granted = new Set(JSON.parse(localStorage.getItem(KEY) || '[]')); |
| 6 | let asker = async () => false; // la UI lo sustituye por el modal real |
| 7 | |
| 8 | // label/desc son getters localizados: se resuelven al leerse (idioma del |
| 9 | // navegador), no al importar el módulo, así el chrome sale en el idioma correcto. |
| 10 | export const SCOPES = { |
| 11 | fs: { icon: '📁', get label() { return t('scFsL'); }, get desc() { return t('scFsD'); } }, |
| 12 | apps: { icon: '🎨', get label() { return t('scAppsL'); }, get desc() { return t('scAppsD'); } }, |
| 13 | vault: { icon: '🔐', get label() { return t('scVaultL'); }, get desc() { return t('scVaultD'); } }, |
| 14 | tasks: { icon: '⏰', get label() { return t('scTasksL'); }, get desc() { return t('scTasksD'); } }, |
| 15 | web: { icon: '🌐', get label() { return t('scWebL'); }, get desc() { return t('scWebD'); } }, |
| 16 | memory: { icon: '🧠', get label() { return t('scMemL'); }, get desc() { return t('scMemD'); } }, |
| 17 | }; |
| 18 | |
| 19 | export function setAsker(fn) { asker = fn; } |
| 20 | export function grants() { return [...granted]; } |
| 21 | export function has(scope) { return granted.has(scope); } |
| 22 | |
| 23 | // Las peticiones se hacen EN FILA, una detrás de otra. El diálogo es uno: con |
| 24 | // varias a la vez —los perfiles del cerebro CEO piensan en paralelo y pueden |
| 25 | // pedir un ámbito al mismo tiempo— solo la última quedaba enganchada a sus |
| 26 | // botones y las demás esperaban una respuesta que no iba a llegar nunca. En |
| 27 | // fila, cada una recibe la suya; y la que espera turno ya no pregunta si |
| 28 | // mientras tanto ese ámbito se concedió. |
| 29 | let fila = Promise.resolve(); |
| 30 | export async function require(scope, detail = '') { |
| 31 | if (granted.has(scope)) return true; |
| 32 | const turno = fila.then(async () => { |
| 33 | if (granted.has(scope)) return true; |
| 34 | const ok = await asker(scope, SCOPES[scope], detail); |
| 35 | if (!ok) throw new Error(t('permDenied', { label: SCOPES[scope]?.label || scope })); |
| 36 | granted.add(scope); |
| 37 | localStorage.setItem(KEY, JSON.stringify([...granted])); |
| 38 | return true; |
| 39 | }); |
| 40 | fila = turno.catch(() => {}); |
| 41 | return turno; |
| 42 | } |
| 43 | |
| 44 | export function revoke(scope) { |
| 45 | granted.delete(scope); |
| 46 | localStorage.setItem(KEY, JSON.stringify([...granted])); |
| 47 | } |