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