From fd142bb151e7c11200658d4a7b5d4caf6e8d60a5 Mon Sep 17 00:00:00 2001 From: Mohamadzadeh <7796733@gmail.com> Date: Sat, 20 Jun 2026 13:39:40 +0330 Subject: [PATCH] update PanelAccessGuard --- dist/core.js | 78 ++++++++++++++++++++--------------------- src/core/permission.jsx | 6 ++-- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/dist/core.js b/dist/core.js index 5ba17b1..83fb15f 100644 --- a/dist/core.js +++ b/dist/core.js @@ -5,18 +5,18 @@ import { createContext as P, useState as y, useRef as U, useCallback as b, useEf import { useQueryClient as Q, useMutation as j, useQuery as Y, QueryClient as $, QueryClientProvider as _ } from "@tanstack/react-query"; import { extendTheme as ee, withDefaultColorScheme as te, ChakraProvider as re, Spinner as ne } from "@chakra-ui/react"; import { Global as oe } from "@emotion/react"; -let w = null, n = null, B = null, W = !1; -const ie = P(null), se = ({ url: e, realm: t, clientId: r }) => (n || (n = new H({ +let w = null, o = null, B = null, W = !1; +const ie = P(null), se = ({ url: e, realm: t, clientId: r }) => (o || (o = new H({ url: e, realm: t, clientId: r -})), n), ue = (e) => (w = e, w), ae = () => W, C = () => !!(n != null && n.authenticated), X = () => (n == null ? void 0 : n.token) ?? null, M = async (e = 30) => n ? n.updateToken(e) : !1, L = (e) => n == null ? void 0 : n.logout(e), Ue = (e) => n == null ? void 0 : n.login(e), V = ({ config: e, initOptions: t = {} } = {}) => { +})), o), ue = (e) => (w = e, w), ae = () => W, C = () => !!(o != null && o.authenticated), X = () => (o == null ? void 0 : o.token) ?? null, M = async (e = 30) => o ? o.updateToken(e) : !1, L = (e) => o == null ? void 0 : o.logout(e), Ue = (e) => o == null ? void 0 : o.login(e), V = ({ config: e, initOptions: t = {} } = {}) => { const r = e ?? w; if (!(r != null && r.url) || !(r != null && r.realm) || !(r != null && r.clientId)) throw new Error("Keycloak config is required."); ue(r); - const o = se(r); - return B || (B = o.init({ + const n = se(r); + return B || (B = n.init({ onLoad: "login-required", checkLoginIframe: !1, flow: "implicit", @@ -28,22 +28,22 @@ const ie = P(null), se = ({ url: e, realm: t, clientId: r }) => (n || (n = new H children: e, config: t, initOptions: r, - loading: o = null + loading: n = null }) => { const [d, h] = y(!1), [a, i] = y(!1), [s, m] = y(null), u = U(!1), l = b(() => { - n && (i(!!n.authenticated), m(n.token ?? null)); + o && (i(!!o.authenticated), m(o.token ?? null)); }, []); K(() => (u.current = !0, V({ config: t, initOptions: r }).then((c) => { - u.current && (i(!!c), m((n == null ? void 0 : n.token) ?? null), h(!0)); + u.current && (i(!!c), m((o == null ? void 0 : o.token) ?? null), h(!0)); }).catch(() => { u.current && (i(!1), m(null), h(!0)); }), () => { u.current = !1; }), [t, r]); - const E = b(() => n == null ? void 0 : n.login(), []), F = b((c) => n == null ? void 0 : n.logout(c), []), S = b( + const E = b(() => o == null ? void 0 : o.login(), []), F = b((c) => o == null ? void 0 : o.logout(c), []), S = b( async (c = 30) => { - if (!n) return !1; - const p = await n.updateToken(c); + if (!o) return !1; + const p = await o.updateToken(c); return l(), p; }, [l] @@ -58,17 +58,17 @@ const ie = P(null), se = ({ url: e, realm: t, clientId: r }) => (n || (n = new H }), [d, a, s, E, F, S] ); - return d ? /* @__PURE__ */ Z(ie.Provider, { value: q, children: e }) : o; + return d ? /* @__PURE__ */ Z(ie.Provider, { value: q, children: e }) : n; }; let x = null; const ce = (e) => { const { api: t } = e, r = J.create({ baseURL: t.baseUrl - }), o = async () => { + }), n = async () => { (!ae() || !C()) && await V(); }, d = !!e.keycloak, h = async (i) => { if (!d) return i; - await o(); + await n(); let s = X(); if (s) { try { @@ -132,8 +132,8 @@ function me(e, t) { Object.entries(t).forEach(([d, h]) => { h != null && h !== "" && r.append(d, h); }); - const o = r.toString(); - return o ? `${e}?${o}` : e; + const n = r.toString(); + return n ? `${e}?${n}` : e; } function qe(e, t) { return typeof e == "function" ? e(t == null ? void 0 : t.id, t) : e; @@ -149,7 +149,7 @@ function Ce({ key: e, url: t, actions: r = {}, - axiosInstance: o = N, + axiosInstance: n = N, fetcher: d = Fe }) { const h = O(e), a = Se(e); @@ -176,10 +176,10 @@ function Ce({ mutationKey: [...h, u], mutationFn: async (S = {}) => { const q = F.method || "post", c = qe(F.url || t, S), p = F.body ? F.body(S) : F.sendRawPayload ? S : pe(S); - return (q === "delete" ? await o.delete( + return (q === "delete" ? await n.delete( c, p && Object.keys(p).length > 0 ? { data: p } : void 0 - ) : await o[q](c, p)).data; + ) : await n[q](c, p)).data; }, onSuccess: (...S) => { var A; @@ -211,7 +211,7 @@ const R = P(null), g = "permissions", Ze = (e) => (e == null ? void 0 : e.storag user: e = null, children: t, config: r, - loading: o = /* @__PURE__ */ Z("div", { children: "Loading permissions..." }), + loading: n = /* @__PURE__ */ Z("div", { children: "Loading permissions..." }), errorFallback: d = /* @__PURE__ */ Z("div", { children: "Error loading permissions" }) }) => { const h = Ze(r), [a, i] = y( @@ -237,7 +237,7 @@ const R = P(null), g = "permissions", Ze = (e) => (e == null ? void 0 : e.storag }), [e, F, S, l, m, u] ); - return u && !l ? d : m && !l ? o : /* @__PURE__ */ Z(R.Provider, { value: q, children: t }); + return u && !l ? d : m && !l ? n : /* @__PURE__ */ Z(R.Provider, { value: q, children: t }); }, ye = (e = g) => { if (!(typeof window > "u")) try { @@ -260,7 +260,7 @@ const R = P(null), g = "permissions", Ze = (e) => (e == null ? void 0 : e.storag } }, Te = (e) => e ? new Set(e.map((t) => t.name)) : /* @__PURE__ */ new Set(), Xe = (e, t = "") => { if (!t) return !1; - if (e.has(t) || [...e].some((o) => o.startsWith(t))) return !0; + if (e.has(t) || [...e].some((n) => n.startsWith(t))) return !0; let r = t; for (; r.includes(":"); ) if (r = r.slice(0, r.lastIndexOf(":")), e.has(`${r}:*`)) return !0; @@ -273,15 +273,15 @@ const R = P(null), g = "permissions", Ze = (e) => (e == null ? void 0 : e.storag throw new Error("Permission config requires url and clientId."); const t = X(), r = new URL(e.url); r.searchParams.set("client_id", e.clientId); - const o = await fetch(r.toString(), { + const n = await fetch(r.toString(), { method: "GET", headers: { ...t ? { Authorization: `Bearer ${t}` } : {} } }); - if (!o.ok) + if (!n.ok) throw new Error("Failed to load permissions"); - return o.json(); + return n.json(); }, f = () => { const e = k(R); if (!e) @@ -299,22 +299,22 @@ const R = P(null), g = "permissions", Ze = (e) => (e == null ? void 0 : e.storag children: t, fallback: r = /* @__PURE__ */ Z("div", { children: "You are not allowed to do this" }) }) => { - const { can: o } = f(); - return o(e) ? t : r; + const { can: n } = f(); + return n(e) ? t : r; }, Ne = ({ children: e }) => { const { can: t, permissions: r } = f(); return r && t("admin") ? e : null; -}, Re = ({ children: e }) => { - const { can: t, isLoading: r } = f(); +}, Re = ({ children: e, forceNoAccess: t = !1 }) => { + const { can: r, isLoading: n } = f(); return K(() => { - r || t("panel_access") || L(); - }, [r, t]), e; + n || (t || !r("panel_access")) && L(); + }, [n, r, t]), e; }; function ge() { const [e, t] = y(!1), r = U(null); return K(() => { if (process.env.NODE_ENV !== "production") return; - let o; + let n; const d = async () => { const a = await fetch(`/version.json?t=${Date.now()}`, { cache: "no-store" @@ -326,10 +326,10 @@ function ge() { }; return (async () => { try { - r.current = await d(), o = setInterval(async () => { + r.current = await d(), n = setInterval(async () => { try { const a = await d(); - r.current && a !== r.current && (t(!0), clearInterval(o)); + r.current && a !== r.current && (t(!0), clearInterval(n)); } catch (a) { console.error("Update check failed:", a); } @@ -338,7 +338,7 @@ function ge() { console.error("Initial version fetch failed:", a); } })(), () => { - o && clearInterval(o); + n && clearInterval(n); }; }, []), e ? /* @__PURE__ */ Z( "div", @@ -408,7 +408,7 @@ function Ie({ children: e, apiBaseUrl: t, keycloakClientId: r, - permissionClientId: o, + permissionClientId: n, loading: d = /* @__PURE__ */ Z(ne, {}), updateChecker: h = !1, updateCheckerProps: a, @@ -418,7 +418,7 @@ function Ie({ keycloakEnabled: u, permissionEnabled: l }) { - const [E, F] = y(!1), S = u ?? !!r, q = l ?? !!o, c = z( + const [E, F] = y(!1), S = u ?? !!r, q = l ?? !!n, c = z( () => ({ api: { baseUrl: t || "" @@ -430,7 +430,7 @@ function Ie({ } : null, permission: q ? { url: m, - clientId: o + clientId: n } : null }), [ @@ -441,7 +441,7 @@ function Ie({ s, r, m, - o + n ] ); if (K(() => { diff --git a/src/core/permission.jsx b/src/core/permission.jsx index 578584a..1fa6c89 100644 --- a/src/core/permission.jsx +++ b/src/core/permission.jsx @@ -222,16 +222,16 @@ export const AdminGuard = ({ children }) => { return can("admin") ? children : null; }; -export const PanelAccessGuard = ({ children }) => { +export const PanelAccessGuard = ({ children, forceNoAccess = false }) => { const { can, isLoading } = usePermission(); useEffect(() => { if (isLoading) return; - if (!can("panel_access")) { + if (forceNoAccess || !can("panel_access")) { logoutKeycloak(); } - }, [isLoading, can]); + }, [isLoading, can, forceNoAccess]); return children; };