137 lines
4.8 KiB
JavaScript
137 lines
4.8 KiB
JavaScript
"use client";
|
|
import { jsx as L } from "react/jsx-runtime";
|
|
import { useState as U, useRef as B, useEffect as F, useCallback as y, useMemo as z } from "react";
|
|
import { d as K } from "./chunks/feature-registry-B2iuEiEx.js";
|
|
import { u as H } from "./chunks/provider-runtime-B6XIrDzf.js";
|
|
import { PermissionContext as N, usePermission as $ } from "./core-runtime.js";
|
|
function Q(r) {
|
|
return new Set((Array.isArray(r) ? r : []).map((n) => n.name));
|
|
}
|
|
function V(r, n = "") {
|
|
if (!n) return !1;
|
|
if (r.has(n) || [...r].some((s) => s.startsWith(n))) return !0;
|
|
let e = n;
|
|
for (; e.includes(":"); )
|
|
if (e = e.slice(0, e.lastIndexOf(":")), r.has(`${e}:*`)) return !0;
|
|
return !1;
|
|
}
|
|
function X(r = "permissions") {
|
|
if (!(typeof window > "u"))
|
|
try {
|
|
const n = window.localStorage.getItem(r), e = n ? JSON.parse(n) : void 0;
|
|
return Array.isArray(e) ? e : void 0;
|
|
} catch {
|
|
return;
|
|
}
|
|
}
|
|
function Z(r = "permissions", n) {
|
|
if (!(typeof window > "u"))
|
|
try {
|
|
window.localStorage.setItem(r, JSON.stringify(n));
|
|
} catch {
|
|
}
|
|
}
|
|
function k(r = "permissions") {
|
|
if (!(typeof window > "u"))
|
|
try {
|
|
window.localStorage.removeItem(r);
|
|
} catch {
|
|
}
|
|
}
|
|
function nn({
|
|
children: r,
|
|
clientId: n,
|
|
url: e = "https://api.d.aiengines.ir/user_api/v1/permissions/list",
|
|
user: s = null,
|
|
getToken: u,
|
|
storageKey: d,
|
|
loading: l = /* @__PURE__ */ L("div", { children: "Loading permissions..." }),
|
|
errorFallback: f = /* @__PURE__ */ L("div", { children: "Error loading permissions" }),
|
|
enabled: x = !0
|
|
}) {
|
|
var I, J;
|
|
if (!n) throw new Error("<Permission /> requires clientId.");
|
|
const t = H(), M = ((J = (I = t == null ? void 0 : t.instance) == null ? void 0 : I.tokenParsed) == null ? void 0 : J.sub) ?? (s == null ? void 0 : s.id) ?? "anonymous", i = d ?? `permissions:${n}:${M}`, [W, Y] = U(0), [G, p] = U({ key: null, permissions: null, isLoading: !0, error: null }), v = B(u);
|
|
v.current = u;
|
|
const O = t == null ? void 0 : t.token, w = t == null ? void 0 : t.instance, R = t == null ? void 0 : t.authenticated, h = t == null ? void 0 : t.updateToken;
|
|
F(() => {
|
|
const o = X(i) ?? null;
|
|
if (!x) {
|
|
p({ key: i, permissions: [], isLoading: !1, error: null });
|
|
return;
|
|
}
|
|
let A = !0;
|
|
const b = new AbortController();
|
|
return p({ key: i, permissions: o, isLoading: !o, error: null }), (async () => {
|
|
try {
|
|
const S = typeof window > "u" ? "http://localhost" : window.location.origin, T = new URL(e, S);
|
|
T.searchParams.set("client_id", n);
|
|
let m = null;
|
|
if (R) {
|
|
try {
|
|
await (h == null ? void 0 : h(5));
|
|
} catch {
|
|
}
|
|
m = (w == null ? void 0 : w.token) || O || null;
|
|
}
|
|
!m && v.current && (m = await v.current() || null);
|
|
const C = await fetch(T, {
|
|
signal: b.signal,
|
|
headers: m ? { Authorization: `Bearer ${m}` } : {}
|
|
});
|
|
if (!C.ok) throw new Error(`Permission HTTP ${C.status}`);
|
|
const E = await C.json();
|
|
if (!Array.isArray(E)) throw new Error("Permission response must be an array.");
|
|
if (!A) return;
|
|
Z(i, E), p({ key: i, permissions: E, isLoading: !1, error: null });
|
|
} catch (S) {
|
|
A && !b.signal.aborted && p({ key: i, permissions: o, isLoading: !1, error: S });
|
|
}
|
|
})(), () => {
|
|
A = !1, b.abort();
|
|
};
|
|
}, [e, n, i, O, w, x, R, h, W]);
|
|
const g = y(() => Y((o) => o + 1), []), _ = y(() => {
|
|
k(i), g();
|
|
}, [i, g]), a = G.key === i ? G : { permissions: null, isLoading: !0, error: null }, c = a.permissions, j = z(() => Q(c), [c]), P = y((o) => V(j, o), [j]), q = y((o) => !P(o), [P]), D = z(() => ({
|
|
user: s,
|
|
permissions: c,
|
|
can: P,
|
|
cannot: q,
|
|
isLoading: a.isLoading,
|
|
error: a.error,
|
|
refresh: g,
|
|
clear: _
|
|
}), [s, c, P, q, a.isLoading, a.error, g, _]);
|
|
return a.isLoading && !c ? l : a.error && !c ? f : /* @__PURE__ */ L(N.Provider, { value: D, children: r });
|
|
}
|
|
const an = K("Permission", 60, nn);
|
|
function cn({ perm: r, children: n, fallback: e = /* @__PURE__ */ L("div", { children: "You are not allowed to do this" }) }) {
|
|
const { can: s } = $();
|
|
return s(r) ? n : e;
|
|
}
|
|
function un({ children: r }) {
|
|
const { permissions: n, can: e } = $();
|
|
return n && e("admin") ? r : null;
|
|
}
|
|
function dn({ children: r, forceNoAccess: n = !1, onNoAccess: e }) {
|
|
const { can: s, isLoading: u } = $(), d = H(), l = B(e);
|
|
l.current = e;
|
|
const f = d == null ? void 0 : d.logout;
|
|
return F(() => {
|
|
u || !n && s("panel_access") || (l.current ? l.current() : f && f());
|
|
}, [u, n, s, f]), r;
|
|
}
|
|
export {
|
|
un as AdminGuard,
|
|
cn as Can,
|
|
dn as PanelAccessGuard,
|
|
an as Permission,
|
|
V as checkPermission,
|
|
k as clearCachedPermissions,
|
|
Q as createPermissionSet,
|
|
X as getCachedPermissions,
|
|
Z as setCachedPermissions,
|
|
$ as usePermission
|
|
};
|