update PanelAccessGuard

This commit is contained in:
Mohamadzadeh 2026-06-20 13:39:40 +03:30
parent cd1128332f
commit fd142bb151
2 changed files with 42 additions and 42 deletions

78
dist/core.js vendored
View File

@ -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 { 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 { extendTheme as ee, withDefaultColorScheme as te, ChakraProvider as re, Spinner as ne } from "@chakra-ui/react";
import { Global as oe } from "@emotion/react"; import { Global as oe } from "@emotion/react";
let w = null, n = null, B = null, W = !1; let w = null, o = null, B = null, W = !1;
const ie = P(null), se = ({ url: e, realm: t, clientId: r }) => (n || (n = new H({ const ie = P(null), se = ({ url: e, realm: t, clientId: r }) => (o || (o = new H({
url: e, url: e,
realm: t, realm: t,
clientId: r 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; const r = e ?? w;
if (!(r != null && r.url) || !(r != null && r.realm) || !(r != null && r.clientId)) if (!(r != null && r.url) || !(r != null && r.realm) || !(r != null && r.clientId))
throw new Error("Keycloak config is required."); throw new Error("Keycloak config is required.");
ue(r); ue(r);
const o = se(r); const n = se(r);
return B || (B = o.init({ return B || (B = n.init({
onLoad: "login-required", onLoad: "login-required",
checkLoginIframe: !1, checkLoginIframe: !1,
flow: "implicit", flow: "implicit",
@ -28,22 +28,22 @@ const ie = P(null), se = ({ url: e, realm: t, clientId: r }) => (n || (n = new H
children: e, children: e,
config: t, config: t,
initOptions: r, 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(() => { 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) => { 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(() => { }).catch(() => {
u.current && (i(!1), m(null), h(!0)); u.current && (i(!1), m(null), h(!0));
}), () => { }), () => {
u.current = !1; u.current = !1;
}), [t, r]); }), [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) => { async (c = 30) => {
if (!n) return !1; if (!o) return !1;
const p = await n.updateToken(c); const p = await o.updateToken(c);
return l(), p; return l(), p;
}, },
[l] [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] [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; let x = null;
const ce = (e) => { const ce = (e) => {
const { api: t } = e, r = J.create({ const { api: t } = e, r = J.create({
baseURL: t.baseUrl baseURL: t.baseUrl
}), o = async () => { }), n = async () => {
(!ae() || !C()) && await V(); (!ae() || !C()) && await V();
}, d = !!e.keycloak, h = async (i) => { }, d = !!e.keycloak, h = async (i) => {
if (!d) return i; if (!d) return i;
await o(); await n();
let s = X(); let s = X();
if (s) { if (s) {
try { try {
@ -132,8 +132,8 @@ function me(e, t) {
Object.entries(t).forEach(([d, h]) => { Object.entries(t).forEach(([d, h]) => {
h != null && h !== "" && r.append(d, h); h != null && h !== "" && r.append(d, h);
}); });
const o = r.toString(); const n = r.toString();
return o ? `${e}?${o}` : e; return n ? `${e}?${n}` : e;
} }
function qe(e, t) { function qe(e, t) {
return typeof e == "function" ? e(t == null ? void 0 : t.id, t) : e; return typeof e == "function" ? e(t == null ? void 0 : t.id, t) : e;
@ -149,7 +149,7 @@ function Ce({
key: e, key: e,
url: t, url: t,
actions: r = {}, actions: r = {},
axiosInstance: o = N, axiosInstance: n = N,
fetcher: d = Fe fetcher: d = Fe
}) { }) {
const h = O(e), a = Se(e); const h = O(e), a = Se(e);
@ -176,10 +176,10 @@ function Ce({
mutationKey: [...h, u], mutationKey: [...h, u],
mutationFn: async (S = {}) => { 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); 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, c,
p && Object.keys(p).length > 0 ? { data: p } : void 0 p && Object.keys(p).length > 0 ? { data: p } : void 0
) : await o[q](c, p)).data; ) : await n[q](c, p)).data;
}, },
onSuccess: (...S) => { onSuccess: (...S) => {
var A; var A;
@ -211,7 +211,7 @@ const R = P(null), g = "permissions", Ze = (e) => (e == null ? void 0 : e.storag
user: e = null, user: e = null,
children: t, children: t,
config: r, 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" }) errorFallback: d = /* @__PURE__ */ Z("div", { children: "Error loading permissions" })
}) => { }) => {
const h = Ze(r), [a, i] = y( 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] [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) => { }, ye = (e = g) => {
if (!(typeof window > "u")) if (!(typeof window > "u"))
try { 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 = "") => { }, Te = (e) => e ? new Set(e.map((t) => t.name)) : /* @__PURE__ */ new Set(), Xe = (e, t = "") => {
if (!t) return !1; 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; let r = t;
for (; r.includes(":"); ) for (; r.includes(":"); )
if (r = r.slice(0, r.lastIndexOf(":")), e.has(`${r}:*`)) return !0; 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."); throw new Error("Permission config requires url and clientId.");
const t = X(), r = new URL(e.url); const t = X(), r = new URL(e.url);
r.searchParams.set("client_id", e.clientId); r.searchParams.set("client_id", e.clientId);
const o = await fetch(r.toString(), { const n = await fetch(r.toString(), {
method: "GET", method: "GET",
headers: { headers: {
...t ? { Authorization: `Bearer ${t}` } : {} ...t ? { Authorization: `Bearer ${t}` } : {}
} }
}); });
if (!o.ok) if (!n.ok)
throw new Error("Failed to load permissions"); throw new Error("Failed to load permissions");
return o.json(); return n.json();
}, f = () => { }, f = () => {
const e = k(R); const e = k(R);
if (!e) if (!e)
@ -299,22 +299,22 @@ const R = P(null), g = "permissions", Ze = (e) => (e == null ? void 0 : e.storag
children: t, children: t,
fallback: r = /* @__PURE__ */ Z("div", { children: "You are not allowed to do this" }) fallback: r = /* @__PURE__ */ Z("div", { children: "You are not allowed to do this" })
}) => { }) => {
const { can: o } = f(); const { can: n } = f();
return o(e) ? t : r; return n(e) ? t : r;
}, Ne = ({ children: e }) => { }, Ne = ({ children: e }) => {
const { can: t, permissions: r } = f(); const { can: t, permissions: r } = f();
return r && t("admin") ? e : null; return r && t("admin") ? e : null;
}, Re = ({ children: e }) => { }, Re = ({ children: e, forceNoAccess: t = !1 }) => {
const { can: t, isLoading: r } = f(); const { can: r, isLoading: n } = f();
return K(() => { return K(() => {
r || t("panel_access") || L(); n || (t || !r("panel_access")) && L();
}, [r, t]), e; }, [n, r, t]), e;
}; };
function ge() { function ge() {
const [e, t] = y(!1), r = U(null); const [e, t] = y(!1), r = U(null);
return K(() => { return K(() => {
if (process.env.NODE_ENV !== "production") return; if (process.env.NODE_ENV !== "production") return;
let o; let n;
const d = async () => { const d = async () => {
const a = await fetch(`/version.json?t=${Date.now()}`, { const a = await fetch(`/version.json?t=${Date.now()}`, {
cache: "no-store" cache: "no-store"
@ -326,10 +326,10 @@ function ge() {
}; };
return (async () => { return (async () => {
try { try {
r.current = await d(), o = setInterval(async () => { r.current = await d(), n = setInterval(async () => {
try { try {
const a = await d(); const a = await d();
r.current && a !== r.current && (t(!0), clearInterval(o)); r.current && a !== r.current && (t(!0), clearInterval(n));
} catch (a) { } catch (a) {
console.error("Update check failed:", a); console.error("Update check failed:", a);
} }
@ -338,7 +338,7 @@ function ge() {
console.error("Initial version fetch failed:", a); console.error("Initial version fetch failed:", a);
} }
})(), () => { })(), () => {
o && clearInterval(o); n && clearInterval(n);
}; };
}, []), e ? /* @__PURE__ */ Z( }, []), e ? /* @__PURE__ */ Z(
"div", "div",
@ -408,7 +408,7 @@ function Ie({
children: e, children: e,
apiBaseUrl: t, apiBaseUrl: t,
keycloakClientId: r, keycloakClientId: r,
permissionClientId: o, permissionClientId: n,
loading: d = /* @__PURE__ */ Z(ne, {}), loading: d = /* @__PURE__ */ Z(ne, {}),
updateChecker: h = !1, updateChecker: h = !1,
updateCheckerProps: a, updateCheckerProps: a,
@ -418,7 +418,7 @@ function Ie({
keycloakEnabled: u, keycloakEnabled: u,
permissionEnabled: l 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: { api: {
baseUrl: t || "" baseUrl: t || ""
@ -430,7 +430,7 @@ function Ie({
} : null, } : null,
permission: q ? { permission: q ? {
url: m, url: m,
clientId: o clientId: n
} : null } : null
}), }),
[ [
@ -441,7 +441,7 @@ function Ie({
s, s,
r, r,
m, m,
o n
] ]
); );
if (K(() => { if (K(() => {

View File

@ -222,16 +222,16 @@ export const AdminGuard = ({ children }) => {
return can("admin") ? children : null; return can("admin") ? children : null;
}; };
export const PanelAccessGuard = ({ children }) => { export const PanelAccessGuard = ({ children, forceNoAccess = false }) => {
const { can, isLoading } = usePermission(); const { can, isLoading } = usePermission();
useEffect(() => { useEffect(() => {
if (isLoading) return; if (isLoading) return;
if (!can("panel_access")) { if (forceNoAccess || !can("panel_access")) {
logoutKeycloak(); logoutKeycloak();
} }
}, [isLoading, can]); }, [isLoading, can, forceNoAccess]);
return children; return children;
}; };