uikit/dist/permission.js
Mohamadzadeh 4730057b3f
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Successful in 52s
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Successful in 7s
uikit-v1.2.5-chakra-default-loading
2026-10-26 14:49:25 +03:30

137 lines
4.9 KiB
JavaScript

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