UNPKG

@keycloakify/keycloak-admin-ui

Version:
31 lines 1.42 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { useEffect, useState } from "react"; import { useRealm } from "../../context/realm-context/RealmContext"; import { useWhoAmI } from "../../context/whoami/WhoAmI"; import { createNamedContext, useRequiredContext, } from "../../ui-shared"; export const AccessContext = createNamedContext("AccessContext", undefined); export const useAccess = () => useRequiredContext(AccessContext); export const AccessContextProvider = ({ children }) => { const { whoAmI } = useWhoAmI(); const { realm } = useRealm(); const [access, setAccess] = useState([]); useEffect(() => { if (whoAmI.getRealmAccess()[realm]) { setAccess(whoAmI.getRealmAccess()[realm]); } }, [whoAmI, realm]); const hasAccess = (...types) => { return types.every((type) => type === "anyone" || (typeof type === "function" && type({ hasAll: hasAccess, hasAny: hasSomeAccess })) || access.includes(type)); }; const hasSomeAccess = (...types) => { return types.some((type) => type === "anyone" || (typeof type === "function" && type({ hasAll: hasAccess, hasAny: hasSomeAccess })) || access.includes(type)); }; return (_jsx(AccessContext.Provider, { value: { hasAccess, hasSomeAccess }, children: children })); }; //# sourceMappingURL=Access.js.map