@anievo-id/rbac-evolution-react 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -73,6 +73,7 @@ function createRbacChecker(snapshot, options = {}) {
73
73
  roles,
74
74
  permissions,
75
75
  isSuperAdmin,
76
+ loading: options.loading ?? false,
76
77
  can,
77
78
  cannot: (permission) => !can(permission),
78
79
  canAny: (...list) => flatten(list).some(can),
@@ -94,13 +95,13 @@ var import_react = require("react");
94
95
  var import_jsx_runtime = require("react/jsx-runtime");
95
96
  var RbacContext = (0, import_react.createContext)(null);
96
97
  RbacContext.displayName = "RbacContext";
97
- function RbacProvider({ snapshot, wildcard = false, superAdminRoles, children }) {
98
+ function RbacProvider({ snapshot, wildcard = false, superAdminRoles, loading = false, children }) {
98
99
  const superKey = superAdminRoles?.join("\0") ?? "";
99
100
  const checker = (0, import_react.useMemo)(
100
- () => createRbacChecker(snapshot, superAdminRoles ? { wildcard, superAdminRoles } : { wildcard }),
101
+ () => createRbacChecker(snapshot, superAdminRoles ? { wildcard, superAdminRoles, loading } : { wildcard, loading }),
101
102
  // `superKey` keeps inline arrays (`superAdminRoles={['admin']}`) from recomputing every render.
102
103
  // eslint-disable-next-line react-hooks/exhaustive-deps
103
- [snapshot, wildcard, superKey]
104
+ [snapshot, wildcard, superKey, loading]
104
105
  );
105
106
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RbacContext.Provider, { value: checker, children });
106
107
  }
@@ -144,16 +145,19 @@ function checkRoles(rbac, p) {
144
145
  return (p.role === void 0 || rbac.hasRole(p.role)) && (p.anyOf === void 0 || rbac.hasAnyRole(p.anyOf)) && (p.allOf === void 0 || rbac.hasAllRoles(p.allOf));
145
146
  }
146
147
  function Can(props) {
147
- const allowed = checkPermissions(useRbac(), props);
148
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: allowed ? props.children : props.fallback });
148
+ const rbac = useRbac();
149
+ if (rbac.loading) return null;
150
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: checkPermissions(rbac, props) ? props.children : props.fallback });
149
151
  }
150
152
  function Cannot(props) {
151
- const allowed = checkPermissions(useRbac(), props);
152
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: allowed ? props.fallback : props.children });
153
+ const rbac = useRbac();
154
+ if (rbac.loading) return null;
155
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: checkPermissions(rbac, props) ? props.fallback : props.children });
153
156
  }
154
157
  function HasRole(props) {
155
- const allowed = checkRoles(useRbac(), props);
156
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: allowed ? props.children : props.fallback });
158
+ const rbac = useRbac();
159
+ if (rbac.loading) return null;
160
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: checkRoles(rbac, props) ? props.children : props.fallback });
157
161
  }
158
162
  // Annotate the CommonJS export names for ESM import in node:
159
163
  0 && (module.exports = {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/checker.ts","../src/context.tsx","../src/components.tsx"],"sourcesContent":["/**\n * @anievo-id/rbac-evolution-react\n *\n * Client-side UI helpers. They only show/hide UI — always enforce authorization on the server.\n */\n\nexport { createRbacChecker } from './checker'\nexport type { RbacChecker, RbacCheckerOptions } from './checker'\n\nexport {\n RbacProvider,\n useCan,\n useCanAll,\n useCanAny,\n useCannot,\n useHasAllRoles,\n useHasAnyRole,\n useHasRole,\n useRbac,\n} from './context'\nexport type { RbacProviderProps } from './context'\n\nexport { Can, Cannot, HasRole } from './components'\nexport type { CannotProps, CanProps, HasRoleProps, PermissionCondition, RoleCondition } from './components'\n","import { wildcardImplies } from '@anievo-id/rbac-evolution'\nimport type { Many, RbacSnapshot } from '@anievo-id/rbac-evolution'\n\nexport interface RbacCheckerOptions {\n /** Treat granted permissions as wildcard patterns (`articles.*`). Must match the server's `wildcard` setting. */\n wildcard?: boolean\n /** Roles that pass every permission check (like spatie's `Gate::before` super-admin pattern). */\n superAdminRoles?: readonly string[]\n}\n\n/** Synchronous checks against a {@link RbacSnapshot}. */\nexport interface RbacChecker {\n readonly roles: readonly string[]\n readonly permissions: readonly string[]\n /** `true` if the subject has one of the configured `superAdminRoles`. */\n readonly isSuperAdmin: boolean\n can(permission: string): boolean\n cannot(permission: string): boolean\n /** `true` if ANY of the permissions is granted. `false` for an empty list. */\n canAny(...permissions: Many<string>[]): boolean\n /** `true` if ALL of the permissions are granted. `false` for an empty list (deny by default, like the Vue package). */\n canAll(...permissions: Many<string>[]): boolean\n /** `true` if the subject has the role — or ANY of the roles when given an array. */\n hasRole(roles: Many<string>): boolean\n hasAnyRole(...roles: Many<string>[]): boolean\n hasAllRoles(...roles: Many<string>[]): boolean\n}\n\nconst EMPTY: RbacSnapshot = { roles: [], permissions: [] }\n\nfunction flatten(items: readonly Many<string>[]): string[] {\n const out: string[] = []\n for (const item of items) {\n if (typeof item === 'string') out.push(item)\n else out.push(...item)\n }\n return out\n}\n\nfunction safeImplies(granted: string, required: string): boolean {\n try {\n return wildcardImplies(granted, required)\n } catch {\n return false\n }\n}\n\n/**\n * Builds a pure, synchronous checker for a snapshot (as returned by `rbac.for(user).snapshot()`).\n * Used by `RbacProvider`, but usable on its own too.\n */\nexport function createRbacChecker(\n snapshot: RbacSnapshot | null | undefined,\n options: RbacCheckerOptions = {},\n): RbacChecker {\n const { roles, permissions } = snapshot ?? EMPTY\n const roleSet = new Set(roles)\n const permissionSet = new Set(permissions)\n const wildcard = options.wildcard ?? false\n const isSuperAdmin = (options.superAdminRoles ?? []).some((r) => roleSet.has(r))\n\n const can = (permission: string): boolean => {\n if (isSuperAdmin) return true\n const name = permission.trim()\n if (name === '') return false\n if (permissionSet.has(name)) return true\n return wildcard && permissions.some((granted) => safeImplies(granted, name))\n }\n const hasOne = (role: string): boolean => roleSet.has(role.trim())\n\n return {\n roles,\n permissions,\n isSuperAdmin,\n can,\n cannot: (permission) => !can(permission),\n canAny: (...list) => flatten(list).some(can),\n canAll: (...list) => {\n const wanted = flatten(list)\n return wanted.length > 0 && wanted.every(can)\n },\n hasRole: (list) => flatten([list]).some(hasOne),\n hasAnyRole: (...list) => flatten(list).some(hasOne),\n hasAllRoles: (...list) => {\n const wanted = flatten(list)\n return wanted.length > 0 && wanted.every(hasOne)\n },\n }\n}\n","import { createContext, useContext, useMemo } from 'react'\nimport type { ReactNode } from 'react'\nimport type { Many, RbacSnapshot } from '@anievo-id/rbac-evolution'\nimport { createRbacChecker } from './checker'\nimport type { RbacChecker } from './checker'\n\nconst RbacContext = createContext<RbacChecker | null>(null)\nRbacContext.displayName = 'RbacContext'\n\nexport interface RbacProviderProps {\n /** Roles & permissions of the current user, e.g. from `rbac.for(user).snapshot()` on the server. `null` = guest. */\n snapshot: RbacSnapshot | null | undefined\n /** Enable wildcard matching (`articles.*`). Must match the server's `wildcard` setting. Default: `false`. */\n wildcard?: boolean\n /** Roles that pass every `can` check (super admin). */\n superAdminRoles?: readonly string[]\n children?: ReactNode\n}\n\n/**\n * Provides a user's roles & permissions to `useCan()`, `<Can>`, ...\n *\n * UI only: this hides and shows elements. Always enforce authorization on the server.\n */\nexport function RbacProvider({ snapshot, wildcard = false, superAdminRoles, children }: RbacProviderProps) {\n const superKey = superAdminRoles?.join('\\u0000') ?? ''\n const checker = useMemo(\n () => createRbacChecker(snapshot, superAdminRoles ? { wildcard, superAdminRoles } : { wildcard }),\n // `superKey` keeps inline arrays (`superAdminRoles={['admin']}`) from recomputing every render.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [snapshot, wildcard, superKey],\n )\n return <RbacContext.Provider value={checker}>{children}</RbacContext.Provider>\n}\n\n/** Returns the checker of the nearest `RbacProvider`. Throws when there is none. */\nexport function useRbac(): RbacChecker {\n const checker = useContext(RbacContext)\n if (!checker) {\n throw new Error('useRbac() must be used inside <RbacProvider>.')\n }\n return checker\n}\n\nexport function useCan(permission: string): boolean {\n return useRbac().can(permission)\n}\n\nexport function useCannot(permission: string): boolean {\n return useRbac().cannot(permission)\n}\n\nexport function useCanAny(...permissions: Many<string>[]): boolean {\n return useRbac().canAny(...permissions)\n}\n\nexport function useCanAll(...permissions: Many<string>[]): boolean {\n return useRbac().canAll(...permissions)\n}\n\n/** `true` if the user has the role — or ANY of the roles when given an array. */\nexport function useHasRole(role: Many<string>): boolean {\n return useRbac().hasRole(role)\n}\n\nexport function useHasAnyRole(...roles: Many<string>[]): boolean {\n return useRbac().hasAnyRole(...roles)\n}\n\nexport function useHasAllRoles(...roles: Many<string>[]): boolean {\n return useRbac().hasAllRoles(...roles)\n}\n","import type { ReactNode } from 'react'\nimport type { RbacChecker } from './checker'\nimport { useRbac } from './context'\n\n/** Exactly one of `permission`, `anyOf` or `allOf`. */\nexport type PermissionCondition =\n | { permission: string; anyOf?: never; allOf?: never }\n | { permission?: never; anyOf: readonly string[]; allOf?: never }\n | { permission?: never; anyOf?: never; allOf: readonly string[] }\n\n/** Exactly one of `role`, `anyOf` or `allOf`. */\nexport type RoleCondition =\n | { role: string; anyOf?: never; allOf?: never }\n | { role?: never; anyOf: readonly string[]; allOf?: never }\n | { role?: never; anyOf?: never; allOf: readonly string[] }\n\ninterface GateProps {\n children?: ReactNode\n /** Rendered when the check fails. Default: nothing. */\n fallback?: ReactNode\n}\n\nexport type CanProps = PermissionCondition & GateProps\nexport type CannotProps = PermissionCondition & GateProps\nexport type HasRoleProps = RoleCondition & GateProps\n\ninterface LooseCondition {\n anyOf?: readonly string[] | undefined\n allOf?: readonly string[] | undefined\n}\n\n// Conditions are ANDed; no condition at all is denied (deny by default, also for untyped JS callers).\nfunction checkPermissions(rbac: RbacChecker, p: LooseCondition & { permission?: string | undefined }): boolean {\n if (p.permission === undefined && p.anyOf === undefined && p.allOf === undefined) return false\n return (\n (p.permission === undefined || rbac.can(p.permission)) &&\n (p.anyOf === undefined || rbac.canAny(p.anyOf)) &&\n (p.allOf === undefined || rbac.canAll(p.allOf))\n )\n}\n\nfunction checkRoles(rbac: RbacChecker, p: LooseCondition & { role?: string | undefined }): boolean {\n if (p.role === undefined && p.anyOf === undefined && p.allOf === undefined) return false\n return (\n (p.role === undefined || rbac.hasRole(p.role)) &&\n (p.anyOf === undefined || rbac.hasAnyRole(p.anyOf)) &&\n (p.allOf === undefined || rbac.hasAllRoles(p.allOf))\n )\n}\n\n/** Renders `children` if the user has the permission(s) — like spatie's `@can` / `@canany`. */\nexport function Can(props: CanProps) {\n const allowed = checkPermissions(useRbac(), props)\n return <>{allowed ? props.children : props.fallback}</>\n}\n\n/** Renders `children` if the user does NOT have the permission(s) — like spatie's `@cannot`. */\nexport function Cannot(props: CannotProps) {\n const allowed = checkPermissions(useRbac(), props)\n return <>{allowed ? props.fallback : props.children}</>\n}\n\n/** Renders `children` if the user has the role(s) — like spatie's `@role` / `@hasanyrole` / `@hasallroles`. */\nexport function HasRole(props: HasRoleProps) {\n const allowed = checkRoles(useRbac(), props)\n return <>{allowed ? props.children : props.fallback}</>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,4BAAgC;AA4BhC,IAAM,QAAsB,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC,EAAE;AAEzD,SAAS,QAAQ,OAA0C;AACzD,QAAM,MAAgB,CAAC;AACvB,aAAW,QAAQ,OAAO;AACxB,QAAI,OAAO,SAAS,SAAU,KAAI,KAAK,IAAI;AAAA,QACtC,KAAI,KAAK,GAAG,IAAI;AAAA,EACvB;AACA,SAAO;AACT;AAEA,SAAS,YAAY,SAAiB,UAA2B;AAC/D,MAAI;AACF,eAAO,uCAAgB,SAAS,QAAQ;AAAA,EAC1C,QAAQ;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O,EAAE,KAAK,OAC5C,EAAE,UAAU,UAAa,KAAK,OAAO,EAAE,KAAK;AAEjD;AAEA,SAAS,WAAW,MAAmB,GAA4D;AACjG,MAAI,EAAE,SAAS,UAAa,EAAE,UAAU,UAAa,EAAE,UAAU,OAAW,QAAO;AACnF,UACG,EAAE,SAAS,UAAa,KAAK,QAAQ,EAAE,IAAI,OAC3C,EAAE,UAAU,UAAa,KAAK,WAAW,EAAE,KAAK,OAChD,EAAE,UAAU,UAAa,KAAK,YAAY,EAAE,KAAK;AAEtD;AAGO,SAAS,IAAI,OAAiB;AACnC,QAAM,UAAU,iBAAiB,QAAQ,GAAG,KAAK;AACjD,SAAO,6EAAG,oBAAU,MAAM,WAAW,MAAM,UAAS;AACtD;AAGO,SAAS,OAAO,OAAoB;AACzC,QAAM,UAAU,iBAAiB,QAAQ,GAAG,KAAK;AACjD,SAAO,6EAAG,oBAAU,MAAM,WAAW,MAAM,UAAS;AACtD;AAGO,SAAS,QAAQ,OAAqB;AAC3C,QAAM,UAAU,WAAW,QAAQ,GAAG,KAAK;AAC3C,SAAO,6EAAG,oBAAU,MAAM,WAAW,MAAM,UAAS;AACtD;","names":["import_jsx_runtime"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/checker.ts","../src/context.tsx","../src/components.tsx"],"sourcesContent":["/**\n * @anievo-id/rbac-evolution-react\n *\n * Client-side UI helpers. They only show/hide UI — always enforce authorization on the server.\n */\n\nexport { createRbacChecker } from './checker'\nexport type { RbacChecker, RbacCheckerOptions } from './checker'\n\nexport {\n RbacProvider,\n useCan,\n useCanAll,\n useCanAny,\n useCannot,\n useHasAllRoles,\n useHasAnyRole,\n useHasRole,\n useRbac,\n} from './context'\nexport type { RbacProviderProps } from './context'\n\nexport { Can, Cannot, HasRole } from './components'\nexport type { CannotProps, CanProps, HasRoleProps, PermissionCondition, RoleCondition } from './components'\n","import { wildcardImplies } from '@anievo-id/rbac-evolution'\nimport type { Many, RbacSnapshot } from '@anievo-id/rbac-evolution'\n\nexport interface RbacCheckerOptions {\n /** Treat granted permissions as wildcard patterns (`articles.*`). Must match the server's `wildcard` setting. */\n wildcard?: boolean\n /** Roles that pass every permission check (like spatie's `Gate::before` super-admin pattern). */\n superAdminRoles?: readonly string[]\n /** The snapshot is still being fetched: `<Can>`, `<Cannot>` and `<HasRole>` render nothing. Default: `false`. */\n loading?: boolean\n}\n\n/** Synchronous checks against a {@link RbacSnapshot}. */\nexport interface RbacChecker {\n readonly roles: readonly string[]\n readonly permissions: readonly string[]\n /** `true` if the subject has one of the configured `superAdminRoles`. */\n readonly isSuperAdmin: boolean\n /** `true` while the snapshot is still being fetched (the `loading` option). Checks return `false` meanwhile. */\n readonly loading: boolean\n can(permission: string): boolean\n cannot(permission: string): boolean\n /** `true` if ANY of the permissions is granted. `false` for an empty list. */\n canAny(...permissions: Many<string>[]): boolean\n /** `true` if ALL of the permissions are granted. `false` for an empty list (deny by default, like the Vue package). */\n canAll(...permissions: Many<string>[]): boolean\n /** `true` if the subject has the role — or ANY of the roles when given an array. */\n hasRole(roles: Many<string>): boolean\n hasAnyRole(...roles: Many<string>[]): boolean\n hasAllRoles(...roles: Many<string>[]): boolean\n}\n\nconst EMPTY: RbacSnapshot = { roles: [], permissions: [] }\n\nfunction flatten(items: readonly Many<string>[]): string[] {\n const out: string[] = []\n for (const item of items) {\n if (typeof item === 'string') out.push(item)\n else out.push(...item)\n }\n return out\n}\n\nfunction safeImplies(granted: string, required: string): boolean {\n try {\n return wildcardImplies(granted, required)\n } catch {\n return false\n }\n}\n\n/**\n * Builds a pure, synchronous checker for a snapshot (as returned by `rbac.for(user).snapshot()`).\n * Used by `RbacProvider`, but usable on its own too.\n */\nexport function createRbacChecker(\n snapshot: RbacSnapshot | null | undefined,\n options: RbacCheckerOptions = {},\n): RbacChecker {\n const { roles, permissions } = snapshot ?? EMPTY\n const roleSet = new Set(roles)\n const permissionSet = new Set(permissions)\n const wildcard = options.wildcard ?? false\n const isSuperAdmin = (options.superAdminRoles ?? []).some((r) => roleSet.has(r))\n\n const can = (permission: string): boolean => {\n if (isSuperAdmin) return true\n const name = permission.trim()\n if (name === '') return false\n if (permissionSet.has(name)) return true\n return wildcard && permissions.some((granted) => safeImplies(granted, name))\n }\n const hasOne = (role: string): boolean => roleSet.has(role.trim())\n\n return {\n roles,\n permissions,\n isSuperAdmin,\n loading: options.loading ?? false,\n can,\n cannot: (permission) => !can(permission),\n canAny: (...list) => flatten(list).some(can),\n canAll: (...list) => {\n const wanted = flatten(list)\n return wanted.length > 0 && wanted.every(can)\n },\n hasRole: (list) => flatten([list]).some(hasOne),\n hasAnyRole: (...list) => flatten(list).some(hasOne),\n hasAllRoles: (...list) => {\n const wanted = flatten(list)\n return wanted.length > 0 && wanted.every(hasOne)\n },\n }\n}\n","import { createContext, useContext, useMemo } from 'react'\nimport type { ReactNode } from 'react'\nimport type { Many, RbacSnapshot } from '@anievo-id/rbac-evolution'\nimport { createRbacChecker } from './checker'\nimport type { RbacChecker } from './checker'\n\nconst RbacContext = createContext<RbacChecker | null>(null)\nRbacContext.displayName = 'RbacContext'\n\nexport interface RbacProviderProps {\n /** Roles & permissions of the current user, e.g. from `rbac.for(user).snapshot()` on the server. `null` = guest. */\n snapshot: RbacSnapshot | null | undefined\n /** Enable wildcard matching (`articles.*`). Must match the server's `wildcard` setting. Default: `false`. */\n wildcard?: boolean\n /** Roles that pass every `can` check (super admin). */\n superAdminRoles?: readonly string[]\n /**\n * The snapshot is still being fetched. `<Can>`, `<Cannot>` and `<HasRole>` then render nothing\n * (not even `fallback`), so nothing flashes before the real permissions arrive. Default: `false`.\n */\n loading?: boolean\n children?: ReactNode\n}\n\n/**\n * Provides a user's roles & permissions to `useCan()`, `<Can>`, ...\n *\n * UI only: this hides and shows elements. Always enforce authorization on the server.\n */\nexport function RbacProvider({ snapshot, wildcard = false, superAdminRoles, loading = false, children }: RbacProviderProps) {\n const superKey = superAdminRoles?.join('\\u0000') ?? ''\n const checker = useMemo(\n () => createRbacChecker(snapshot, superAdminRoles ? { wildcard, superAdminRoles, loading } : { wildcard, loading }),\n // `superKey` keeps inline arrays (`superAdminRoles={['admin']}`) from recomputing every render.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [snapshot, wildcard, superKey, loading],\n )\n return <RbacContext.Provider value={checker}>{children}</RbacContext.Provider>\n}\n\n/** Returns the checker of the nearest `RbacProvider`. Throws when there is none. */\nexport function useRbac(): RbacChecker {\n const checker = useContext(RbacContext)\n if (!checker) {\n throw new Error('useRbac() must be used inside <RbacProvider>.')\n }\n return checker\n}\n\nexport function useCan(permission: string): boolean {\n return useRbac().can(permission)\n}\n\nexport function useCannot(permission: string): boolean {\n return useRbac().cannot(permission)\n}\n\nexport function useCanAny(...permissions: Many<string>[]): boolean {\n return useRbac().canAny(...permissions)\n}\n\nexport function useCanAll(...permissions: Many<string>[]): boolean {\n return useRbac().canAll(...permissions)\n}\n\n/** `true` if the user has the role — or ANY of the roles when given an array. */\nexport function useHasRole(role: Many<string>): boolean {\n return useRbac().hasRole(role)\n}\n\nexport function useHasAnyRole(...roles: Many<string>[]): boolean {\n return useRbac().hasAnyRole(...roles)\n}\n\nexport function useHasAllRoles(...roles: Many<string>[]): boolean {\n return useRbac().hasAllRoles(...roles)\n}\n","import type { ReactNode } from 'react'\nimport type { RbacChecker } from './checker'\nimport { useRbac } from './context'\n\n/** Exactly one of `permission`, `anyOf` or `allOf`. */\nexport type PermissionCondition =\n | { permission: string; anyOf?: never; allOf?: never }\n | { permission?: never; anyOf: readonly string[]; allOf?: never }\n | { permission?: never; anyOf?: never; allOf: readonly string[] }\n\n/** Exactly one of `role`, `anyOf` or `allOf`. */\nexport type RoleCondition =\n | { role: string; anyOf?: never; allOf?: never }\n | { role?: never; anyOf: readonly string[]; allOf?: never }\n | { role?: never; anyOf?: never; allOf: readonly string[] }\n\ninterface GateProps {\n children?: ReactNode\n /** Rendered when the check fails. Default: nothing. */\n fallback?: ReactNode\n}\n\nexport type CanProps = PermissionCondition & GateProps\nexport type CannotProps = PermissionCondition & GateProps\nexport type HasRoleProps = RoleCondition & GateProps\n\ninterface LooseCondition {\n anyOf?: readonly string[] | undefined\n allOf?: readonly string[] | undefined\n}\n\n// Conditions are ANDed; no condition at all is denied (deny by default, also for untyped JS callers).\nfunction checkPermissions(rbac: RbacChecker, p: LooseCondition & { permission?: string | undefined }): boolean {\n if (p.permission === undefined && p.anyOf === undefined && p.allOf === undefined) return false\n return (\n (p.permission === undefined || rbac.can(p.permission)) &&\n (p.anyOf === undefined || rbac.canAny(p.anyOf)) &&\n (p.allOf === undefined || rbac.canAll(p.allOf))\n )\n}\n\nfunction checkRoles(rbac: RbacChecker, p: LooseCondition & { role?: string | undefined }): boolean {\n if (p.role === undefined && p.anyOf === undefined && p.allOf === undefined) return false\n return (\n (p.role === undefined || rbac.hasRole(p.role)) &&\n (p.anyOf === undefined || rbac.hasAnyRole(p.anyOf)) &&\n (p.allOf === undefined || rbac.hasAllRoles(p.allOf))\n )\n}\n\n/** Renders `children` if the user has the permission(s) — like spatie's `@can` / `@canany`. */\nexport function Can(props: CanProps) {\n const rbac = useRbac()\n if (rbac.loading) return null\n return <>{checkPermissions(rbac, props) ? props.children : props.fallback}</>\n}\n\n/** Renders `children` if the user does NOT have the permission(s) — like spatie's `@cannot`. */\nexport function Cannot(props: CannotProps) {\n const rbac = useRbac()\n if (rbac.loading) return null\n return <>{checkPermissions(rbac, props) ? props.fallback : props.children}</>\n}\n\n/** Renders `children` if the user has the role(s) — like spatie's `@role` / `@hasanyrole` / `@hasallroles`. */\nexport function HasRole(props: HasRoleProps) {\n const rbac = useRbac()\n if (rbac.loading) return null\n return <>{checkRoles(rbac, props) ? props.children : props.fallback}</>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,4BAAgC;AAgChC,IAAM,QAAsB,EAAE,OAAO,CAAC,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7,6 +7,8 @@ interface RbacCheckerOptions {
7
7
  wildcard?: boolean;
8
8
  /** Roles that pass every permission check (like spatie's `Gate::before` super-admin pattern). */
9
9
  superAdminRoles?: readonly string[];
10
+ /** The snapshot is still being fetched: `<Can>`, `<Cannot>` and `<HasRole>` render nothing. Default: `false`. */
11
+ loading?: boolean;
10
12
  }
11
13
  /** Synchronous checks against a {@link RbacSnapshot}. */
12
14
  interface RbacChecker {
@@ -14,6 +16,8 @@ interface RbacChecker {
14
16
  readonly permissions: readonly string[];
15
17
  /** `true` if the subject has one of the configured `superAdminRoles`. */
16
18
  readonly isSuperAdmin: boolean;
19
+ /** `true` while the snapshot is still being fetched (the `loading` option). Checks return `false` meanwhile. */
20
+ readonly loading: boolean;
17
21
  can(permission: string): boolean;
18
22
  cannot(permission: string): boolean;
19
23
  /** `true` if ANY of the permissions is granted. `false` for an empty list. */
@@ -38,6 +42,11 @@ interface RbacProviderProps {
38
42
  wildcard?: boolean;
39
43
  /** Roles that pass every `can` check (super admin). */
40
44
  superAdminRoles?: readonly string[];
45
+ /**
46
+ * The snapshot is still being fetched. `<Can>`, `<Cannot>` and `<HasRole>` then render nothing
47
+ * (not even `fallback`), so nothing flashes before the real permissions arrive. Default: `false`.
48
+ */
49
+ loading?: boolean;
41
50
  children?: ReactNode;
42
51
  }
43
52
  /**
@@ -45,7 +54,7 @@ interface RbacProviderProps {
45
54
  *
46
55
  * UI only: this hides and shows elements. Always enforce authorization on the server.
47
56
  */
48
- declare function RbacProvider({ snapshot, wildcard, superAdminRoles, children }: RbacProviderProps): react.JSX.Element;
57
+ declare function RbacProvider({ snapshot, wildcard, superAdminRoles, loading, children }: RbacProviderProps): react.JSX.Element;
49
58
  /** Returns the checker of the nearest `RbacProvider`. Throws when there is none. */
50
59
  declare function useRbac(): RbacChecker;
51
60
  declare function useCan(permission: string): boolean;
@@ -94,10 +103,10 @@ type CanProps = PermissionCondition & GateProps;
94
103
  type CannotProps = PermissionCondition & GateProps;
95
104
  type HasRoleProps = RoleCondition & GateProps;
96
105
  /** Renders `children` if the user has the permission(s) — like spatie's `@can` / `@canany`. */
97
- declare function Can(props: CanProps): react.JSX.Element;
106
+ declare function Can(props: CanProps): react.JSX.Element | null;
98
107
  /** Renders `children` if the user does NOT have the permission(s) — like spatie's `@cannot`. */
99
- declare function Cannot(props: CannotProps): react.JSX.Element;
108
+ declare function Cannot(props: CannotProps): react.JSX.Element | null;
100
109
  /** Renders `children` if the user has the role(s) — like spatie's `@role` / `@hasanyrole` / `@hasallroles`. */
101
- declare function HasRole(props: HasRoleProps): react.JSX.Element;
110
+ declare function HasRole(props: HasRoleProps): react.JSX.Element | null;
102
111
 
103
112
  export { Can, type CanProps, Cannot, type CannotProps, HasRole, type HasRoleProps, type PermissionCondition, type RbacChecker, type RbacCheckerOptions, RbacProvider, type RbacProviderProps, type RoleCondition, createRbacChecker, useCan, useCanAll, useCanAny, useCannot, useHasAllRoles, useHasAnyRole, useHasRole, useRbac };
package/dist/index.d.ts CHANGED
@@ -7,6 +7,8 @@ interface RbacCheckerOptions {
7
7
  wildcard?: boolean;
8
8
  /** Roles that pass every permission check (like spatie's `Gate::before` super-admin pattern). */
9
9
  superAdminRoles?: readonly string[];
10
+ /** The snapshot is still being fetched: `<Can>`, `<Cannot>` and `<HasRole>` render nothing. Default: `false`. */
11
+ loading?: boolean;
10
12
  }
11
13
  /** Synchronous checks against a {@link RbacSnapshot}. */
12
14
  interface RbacChecker {
@@ -14,6 +16,8 @@ interface RbacChecker {
14
16
  readonly permissions: readonly string[];
15
17
  /** `true` if the subject has one of the configured `superAdminRoles`. */
16
18
  readonly isSuperAdmin: boolean;
19
+ /** `true` while the snapshot is still being fetched (the `loading` option). Checks return `false` meanwhile. */
20
+ readonly loading: boolean;
17
21
  can(permission: string): boolean;
18
22
  cannot(permission: string): boolean;
19
23
  /** `true` if ANY of the permissions is granted. `false` for an empty list. */
@@ -38,6 +42,11 @@ interface RbacProviderProps {
38
42
  wildcard?: boolean;
39
43
  /** Roles that pass every `can` check (super admin). */
40
44
  superAdminRoles?: readonly string[];
45
+ /**
46
+ * The snapshot is still being fetched. `<Can>`, `<Cannot>` and `<HasRole>` then render nothing
47
+ * (not even `fallback`), so nothing flashes before the real permissions arrive. Default: `false`.
48
+ */
49
+ loading?: boolean;
41
50
  children?: ReactNode;
42
51
  }
43
52
  /**
@@ -45,7 +54,7 @@ interface RbacProviderProps {
45
54
  *
46
55
  * UI only: this hides and shows elements. Always enforce authorization on the server.
47
56
  */
48
- declare function RbacProvider({ snapshot, wildcard, superAdminRoles, children }: RbacProviderProps): react.JSX.Element;
57
+ declare function RbacProvider({ snapshot, wildcard, superAdminRoles, loading, children }: RbacProviderProps): react.JSX.Element;
49
58
  /** Returns the checker of the nearest `RbacProvider`. Throws when there is none. */
50
59
  declare function useRbac(): RbacChecker;
51
60
  declare function useCan(permission: string): boolean;
@@ -94,10 +103,10 @@ type CanProps = PermissionCondition & GateProps;
94
103
  type CannotProps = PermissionCondition & GateProps;
95
104
  type HasRoleProps = RoleCondition & GateProps;
96
105
  /** Renders `children` if the user has the permission(s) — like spatie's `@can` / `@canany`. */
97
- declare function Can(props: CanProps): react.JSX.Element;
106
+ declare function Can(props: CanProps): react.JSX.Element | null;
98
107
  /** Renders `children` if the user does NOT have the permission(s) — like spatie's `@cannot`. */
99
- declare function Cannot(props: CannotProps): react.JSX.Element;
108
+ declare function Cannot(props: CannotProps): react.JSX.Element | null;
100
109
  /** Renders `children` if the user has the role(s) — like spatie's `@role` / `@hasanyrole` / `@hasallroles`. */
101
- declare function HasRole(props: HasRoleProps): react.JSX.Element;
110
+ declare function HasRole(props: HasRoleProps): react.JSX.Element | null;
102
111
 
103
112
  export { Can, type CanProps, Cannot, type CannotProps, HasRole, type HasRoleProps, type PermissionCondition, type RbacChecker, type RbacCheckerOptions, RbacProvider, type RbacProviderProps, type RoleCondition, createRbacChecker, useCan, useCanAll, useCanAny, useCannot, useHasAllRoles, useHasAnyRole, useHasRole, useRbac };
package/dist/index.js CHANGED
@@ -36,6 +36,7 @@ function createRbacChecker(snapshot, options = {}) {
36
36
  roles,
37
37
  permissions,
38
38
  isSuperAdmin,
39
+ loading: options.loading ?? false,
39
40
  can,
40
41
  cannot: (permission) => !can(permission),
41
42
  canAny: (...list) => flatten(list).some(can),
@@ -57,13 +58,13 @@ import { createContext, useContext, useMemo } from "react";
57
58
  import { jsx } from "react/jsx-runtime";
58
59
  var RbacContext = createContext(null);
59
60
  RbacContext.displayName = "RbacContext";
60
- function RbacProvider({ snapshot, wildcard = false, superAdminRoles, children }) {
61
+ function RbacProvider({ snapshot, wildcard = false, superAdminRoles, loading = false, children }) {
61
62
  const superKey = superAdminRoles?.join("\0") ?? "";
62
63
  const checker = useMemo(
63
- () => createRbacChecker(snapshot, superAdminRoles ? { wildcard, superAdminRoles } : { wildcard }),
64
+ () => createRbacChecker(snapshot, superAdminRoles ? { wildcard, superAdminRoles, loading } : { wildcard, loading }),
64
65
  // `superKey` keeps inline arrays (`superAdminRoles={['admin']}`) from recomputing every render.
65
66
  // eslint-disable-next-line react-hooks/exhaustive-deps
66
- [snapshot, wildcard, superKey]
67
+ [snapshot, wildcard, superKey, loading]
67
68
  );
68
69
  return /* @__PURE__ */ jsx(RbacContext.Provider, { value: checker, children });
69
70
  }
@@ -107,16 +108,19 @@ function checkRoles(rbac, p) {
107
108
  return (p.role === void 0 || rbac.hasRole(p.role)) && (p.anyOf === void 0 || rbac.hasAnyRole(p.anyOf)) && (p.allOf === void 0 || rbac.hasAllRoles(p.allOf));
108
109
  }
109
110
  function Can(props) {
110
- const allowed = checkPermissions(useRbac(), props);
111
- return /* @__PURE__ */ jsx2(Fragment, { children: allowed ? props.children : props.fallback });
111
+ const rbac = useRbac();
112
+ if (rbac.loading) return null;
113
+ return /* @__PURE__ */ jsx2(Fragment, { children: checkPermissions(rbac, props) ? props.children : props.fallback });
112
114
  }
113
115
  function Cannot(props) {
114
- const allowed = checkPermissions(useRbac(), props);
115
- return /* @__PURE__ */ jsx2(Fragment, { children: allowed ? props.fallback : props.children });
116
+ const rbac = useRbac();
117
+ if (rbac.loading) return null;
118
+ return /* @__PURE__ */ jsx2(Fragment, { children: checkPermissions(rbac, props) ? props.fallback : props.children });
116
119
  }
117
120
  function HasRole(props) {
118
- const allowed = checkRoles(useRbac(), props);
119
- return /* @__PURE__ */ jsx2(Fragment, { children: allowed ? props.children : props.fallback });
121
+ const rbac = useRbac();
122
+ if (rbac.loading) return null;
123
+ return /* @__PURE__ */ jsx2(Fragment, { children: checkRoles(rbac, props) ? props.children : props.fallback });
120
124
  }
121
125
  export {
122
126
  Can,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/checker.ts","../src/context.tsx","../src/components.tsx"],"sourcesContent":["import { wildcardImplies } from '@anievo-id/rbac-evolution'\nimport type { Many, RbacSnapshot } from '@anievo-id/rbac-evolution'\n\nexport interface RbacCheckerOptions {\n /** Treat granted permissions as wildcard patterns (`articles.*`). Must match the server's `wildcard` setting. */\n wildcard?: boolean\n /** Roles that pass every permission check (like spatie's `Gate::before` super-admin pattern). */\n superAdminRoles?: readonly string[]\n}\n\n/** Synchronous checks against a {@link RbacSnapshot}. */\nexport interface RbacChecker {\n readonly roles: readonly string[]\n readonly permissions: readonly string[]\n /** `true` if the subject has one of the configured `superAdminRoles`. */\n readonly isSuperAdmin: boolean\n can(permission: string): boolean\n cannot(permission: string): boolean\n /** `true` if ANY of the permissions is granted. `false` for an empty list. */\n canAny(...permissions: Many<string>[]): boolean\n /** `true` if ALL of the permissions are granted. `false` for an empty list (deny by default, like the Vue package). */\n canAll(...permissions: Many<string>[]): boolean\n /** `true` if the subject has the role — or ANY of the roles when given an array. */\n hasRole(roles: Many<string>): boolean\n hasAnyRole(...roles: Many<string>[]): boolean\n hasAllRoles(...roles: Many<string>[]): boolean\n}\n\nconst EMPTY: RbacSnapshot = { roles: [], permissions: [] }\n\nfunction flatten(items: readonly Many<string>[]): string[] {\n const out: string[] = []\n for (const item of items) {\n if (typeof item === 'string') out.push(item)\n else out.push(...item)\n }\n return out\n}\n\nfunction safeImplies(granted: string, required: string): boolean {\n try {\n return wildcardImplies(granted, required)\n } catch {\n return false\n }\n}\n\n/**\n * Builds a pure, synchronous checker for a snapshot (as returned by `rbac.for(user).snapshot()`).\n * Used by `RbacProvider`, but usable on its own too.\n */\nexport function createRbacChecker(\n snapshot: RbacSnapshot | null | undefined,\n options: RbacCheckerOptions = {},\n): RbacChecker {\n const { roles, permissions } = snapshot ?? EMPTY\n const roleSet = new Set(roles)\n const permissionSet = new Set(permissions)\n const wildcard = options.wildcard ?? false\n const isSuperAdmin = (options.superAdminRoles ?? []).some((r) => roleSet.has(r))\n\n const can = (permission: string): boolean => {\n if (isSuperAdmin) return true\n const name = permission.trim()\n if (name === '') return false\n if (permissionSet.has(name)) return true\n return wildcard && permissions.some((granted) => safeImplies(granted, name))\n }\n const hasOne = (role: string): boolean => roleSet.has(role.trim())\n\n return {\n roles,\n permissions,\n isSuperAdmin,\n can,\n cannot: (permission) => !can(permission),\n canAny: (...list) => flatten(list).some(can),\n canAll: (...list) => {\n const wanted = flatten(list)\n return wanted.length > 0 && wanted.every(can)\n },\n hasRole: (list) => flatten([list]).some(hasOne),\n hasAnyRole: (...list) => flatten(list).some(hasOne),\n hasAllRoles: (...list) => {\n const wanted = flatten(list)\n return wanted.length > 0 && wanted.every(hasOne)\n },\n }\n}\n","import { createContext, useContext, useMemo } from 'react'\nimport type { ReactNode } from 'react'\nimport type { Many, RbacSnapshot } from '@anievo-id/rbac-evolution'\nimport { createRbacChecker } from './checker'\nimport type { RbacChecker } from './checker'\n\nconst RbacContext = createContext<RbacChecker | null>(null)\nRbacContext.displayName = 'RbacContext'\n\nexport interface RbacProviderProps {\n /** Roles & permissions of the current user, e.g. from `rbac.for(user).snapshot()` on the server. `null` = guest. */\n snapshot: RbacSnapshot | null | undefined\n /** Enable wildcard matching (`articles.*`). Must match the server's `wildcard` setting. Default: `false`. */\n wildcard?: boolean\n /** Roles that pass every `can` check (super admin). */\n superAdminRoles?: readonly string[]\n children?: ReactNode\n}\n\n/**\n * Provides a user's roles & permissions to `useCan()`, `<Can>`, ...\n *\n * UI only: this hides and shows elements. Always enforce authorization on the server.\n */\nexport function RbacProvider({ snapshot, wildcard = false, superAdminRoles, children }: RbacProviderProps) {\n const superKey = superAdminRoles?.join('\\u0000') ?? ''\n const checker = useMemo(\n () => createRbacChecker(snapshot, superAdminRoles ? { wildcard, superAdminRoles } : { wildcard }),\n // `superKey` keeps inline arrays (`superAdminRoles={['admin']}`) from recomputing every render.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [snapshot, wildcard, superKey],\n )\n return <RbacContext.Provider value={checker}>{children}</RbacContext.Provider>\n}\n\n/** Returns the checker of the nearest `RbacProvider`. Throws when there is none. */\nexport function useRbac(): RbacChecker {\n const checker = useContext(RbacContext)\n if (!checker) {\n throw new Error('useRbac() must be used inside <RbacProvider>.')\n }\n return checker\n}\n\nexport function useCan(permission: string): boolean {\n return useRbac().can(permission)\n}\n\nexport function useCannot(permission: string): boolean {\n return useRbac().cannot(permission)\n}\n\nexport function useCanAny(...permissions: Many<string>[]): boolean {\n return useRbac().canAny(...permissions)\n}\n\nexport function useCanAll(...permissions: Many<string>[]): boolean {\n return useRbac().canAll(...permissions)\n}\n\n/** `true` if the user has the role — or ANY of the roles when given an array. */\nexport function useHasRole(role: Many<string>): boolean {\n return useRbac().hasRole(role)\n}\n\nexport function useHasAnyRole(...roles: Many<string>[]): boolean {\n return useRbac().hasAnyRole(...roles)\n}\n\nexport function useHasAllRoles(...roles: Many<string>[]): boolean {\n return useRbac().hasAllRoles(...roles)\n}\n","import type { ReactNode } from 'react'\nimport type { RbacChecker } from './checker'\nimport { useRbac } from './context'\n\n/** Exactly one of `permission`, `anyOf` or `allOf`. */\nexport type PermissionCondition =\n | { permission: string; anyOf?: never; allOf?: never }\n | { permission?: never; anyOf: readonly string[]; allOf?: never }\n | { permission?: never; anyOf?: never; allOf: readonly string[] }\n\n/** Exactly one of `role`, `anyOf` or `allOf`. */\nexport type RoleCondition =\n | { role: string; anyOf?: never; allOf?: never }\n | { role?: never; anyOf: readonly string[]; allOf?: never }\n | { role?: never; anyOf?: never; allOf: readonly string[] }\n\ninterface GateProps {\n children?: ReactNode\n /** Rendered when the check fails. Default: nothing. */\n fallback?: ReactNode\n}\n\nexport type CanProps = PermissionCondition & GateProps\nexport type CannotProps = PermissionCondition & GateProps\nexport type HasRoleProps = RoleCondition & GateProps\n\ninterface LooseCondition {\n anyOf?: readonly string[] | undefined\n allOf?: readonly string[] | undefined\n}\n\n// Conditions are ANDed; no condition at all is denied (deny by default, also for untyped JS callers).\nfunction checkPermissions(rbac: RbacChecker, p: LooseCondition & { permission?: string | undefined }): boolean {\n if (p.permission === undefined && p.anyOf === undefined && p.allOf === undefined) return false\n return (\n (p.permission === undefined || rbac.can(p.permission)) &&\n (p.anyOf === undefined || rbac.canAny(p.anyOf)) &&\n (p.allOf === undefined || rbac.canAll(p.allOf))\n )\n}\n\nfunction checkRoles(rbac: RbacChecker, p: LooseCondition & { role?: string | undefined }): boolean {\n if (p.role === undefined && p.anyOf === undefined && p.allOf === undefined) return false\n return (\n (p.role === undefined || rbac.hasRole(p.role)) &&\n (p.anyOf === undefined || rbac.hasAnyRole(p.anyOf)) &&\n (p.allOf === undefined || rbac.hasAllRoles(p.allOf))\n )\n}\n\n/** Renders `children` if the user has the permission(s) — like spatie's `@can` / `@canany`. */\nexport function Can(props: CanProps) {\n const allowed = checkPermissions(useRbac(), props)\n return <>{allowed ? props.children : props.fallback}</>\n}\n\n/** Renders `children` if the user does NOT have the permission(s) — like spatie's `@cannot`. */\nexport function Cannot(props: CannotProps) {\n const allowed = checkPermissions(useRbac(), props)\n return <>{allowed ? props.fallback : props.children}</>\n}\n\n/** Renders `children` if the user has the role(s) — like spatie's `@role` / `@hasanyrole` / `@hasallroles`. */\nexport function HasRole(props: HasRoleProps) {\n const allowed = checkRoles(useRbac(), props)\n return <>{allowed ? props.children : props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"],"sourcesContent":["import { wildcardImplies } from '@anievo-id/rbac-evolution'\nimport type { Many, RbacSnapshot } from '@anievo-id/rbac-evolution'\n\nexport interface RbacCheckerOptions {\n /** Treat granted permissions as wildcard patterns (`articles.*`). Must match the server's `wildcard` setting. */\n wildcard?: boolean\n /** Roles that pass every permission check (like spatie's `Gate::before` super-admin pattern). */\n superAdminRoles?: readonly string[]\n /** The snapshot is still being fetched: `<Can>`, `<Cannot>` and `<HasRole>` render nothing. Default: `false`. */\n loading?: boolean\n}\n\n/** Synchronous checks against a {@link RbacSnapshot}. */\nexport interface RbacChecker {\n readonly roles: readonly string[]\n readonly permissions: readonly string[]\n /** `true` if the subject has one of the configured `superAdminRoles`. */\n readonly isSuperAdmin: boolean\n /** `true` while the snapshot is still being fetched (the `loading` option). Checks return `false` meanwhile. */\n readonly loading: boolean\n can(permission: string): boolean\n cannot(permission: string): boolean\n /** `true` if ANY of the permissions is granted. `false` for an empty list. */\n canAny(...permissions: Many<string>[]): boolean\n /** `true` if ALL of the permissions are granted. `false` for an empty list (deny by default, like the Vue package). */\n canAll(...permissions: Many<string>[]): boolean\n /** `true` if the subject has the role — or ANY of the roles when given an array. */\n hasRole(roles: Many<string>): boolean\n hasAnyRole(...roles: Many<string>[]): boolean\n hasAllRoles(...roles: Many<string>[]): boolean\n}\n\nconst EMPTY: RbacSnapshot = { roles: [], permissions: [] }\n\nfunction flatten(items: readonly Many<string>[]): string[] {\n const out: string[] = []\n for (const item of items) {\n if (typeof item === 'string') out.push(item)\n else out.push(...item)\n }\n return out\n}\n\nfunction safeImplies(granted: string, required: string): boolean {\n try {\n return wildcardImplies(granted, required)\n } catch {\n return false\n }\n}\n\n/**\n * Builds a pure, synchronous checker for a snapshot (as returned by `rbac.for(user).snapshot()`).\n * Used by `RbacProvider`, but usable on its own too.\n */\nexport function createRbacChecker(\n snapshot: RbacSnapshot | null | undefined,\n options: RbacCheckerOptions = {},\n): RbacChecker {\n const { roles, permissions } = snapshot ?? EMPTY\n const roleSet = new Set(roles)\n const permissionSet = new Set(permissions)\n const wildcard = options.wildcard ?? false\n const isSuperAdmin = (options.superAdminRoles ?? []).some((r) => roleSet.has(r))\n\n const can = (permission: string): boolean => {\n if (isSuperAdmin) return true\n const name = permission.trim()\n if (name === '') return false\n if (permissionSet.has(name)) return true\n return wildcard && permissions.some((granted) => safeImplies(granted, name))\n }\n const hasOne = (role: string): boolean => roleSet.has(role.trim())\n\n return {\n roles,\n permissions,\n isSuperAdmin,\n loading: options.loading ?? false,\n can,\n cannot: (permission) => !can(permission),\n canAny: (...list) => flatten(list).some(can),\n canAll: (...list) => {\n const wanted = flatten(list)\n return wanted.length > 0 && wanted.every(can)\n },\n hasRole: (list) => flatten([list]).some(hasOne),\n hasAnyRole: (...list) => flatten(list).some(hasOne),\n hasAllRoles: (...list) => {\n const wanted = flatten(list)\n return wanted.length > 0 && wanted.every(hasOne)\n },\n }\n}\n","import { createContext, useContext, useMemo } from 'react'\nimport type { ReactNode } from 'react'\nimport type { Many, RbacSnapshot } from '@anievo-id/rbac-evolution'\nimport { createRbacChecker } from './checker'\nimport type { RbacChecker } from './checker'\n\nconst RbacContext = createContext<RbacChecker | null>(null)\nRbacContext.displayName = 'RbacContext'\n\nexport interface RbacProviderProps {\n /** Roles & permissions of the current user, e.g. from `rbac.for(user).snapshot()` on the server. `null` = guest. */\n snapshot: RbacSnapshot | null | undefined\n /** Enable wildcard matching (`articles.*`). Must match the server's `wildcard` setting. Default: `false`. */\n wildcard?: boolean\n /** Roles that pass every `can` check (super admin). */\n superAdminRoles?: readonly string[]\n /**\n * The snapshot is still being fetched. `<Can>`, `<Cannot>` and `<HasRole>` then render nothing\n * (not even `fallback`), so nothing flashes before the real permissions arrive. Default: `false`.\n */\n loading?: boolean\n children?: ReactNode\n}\n\n/**\n * Provides a user's roles & permissions to `useCan()`, `<Can>`, ...\n *\n * UI only: this hides and shows elements. Always enforce authorization on the server.\n */\nexport function RbacProvider({ snapshot, wildcard = false, superAdminRoles, loading = false, children }: RbacProviderProps) {\n const superKey = superAdminRoles?.join('\\u0000') ?? ''\n const checker = useMemo(\n () => createRbacChecker(snapshot, superAdminRoles ? { wildcard, superAdminRoles, loading } : { wildcard, loading }),\n // `superKey` keeps inline arrays (`superAdminRoles={['admin']}`) from recomputing every render.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [snapshot, wildcard, superKey, loading],\n )\n return <RbacContext.Provider value={checker}>{children}</RbacContext.Provider>\n}\n\n/** Returns the checker of the nearest `RbacProvider`. Throws when there is none. */\nexport function useRbac(): RbacChecker {\n const checker = useContext(RbacContext)\n if (!checker) {\n throw new Error('useRbac() must be used inside <RbacProvider>.')\n }\n return checker\n}\n\nexport function useCan(permission: string): boolean {\n return useRbac().can(permission)\n}\n\nexport function useCannot(permission: string): boolean {\n return useRbac().cannot(permission)\n}\n\nexport function useCanAny(...permissions: Many<string>[]): boolean {\n return useRbac().canAny(...permissions)\n}\n\nexport function useCanAll(...permissions: Many<string>[]): boolean {\n return useRbac().canAll(...permissions)\n}\n\n/** `true` if the user has the role — or ANY of the roles when given an array. */\nexport function useHasRole(role: Many<string>): boolean {\n return useRbac().hasRole(role)\n}\n\nexport function useHasAnyRole(...roles: Many<string>[]): boolean {\n return useRbac().hasAnyRole(...roles)\n}\n\nexport function useHasAllRoles(...roles: Many<string>[]): boolean {\n return useRbac().hasAllRoles(...roles)\n}\n","import type { ReactNode } from 'react'\nimport type { RbacChecker } from './checker'\nimport { useRbac } from './context'\n\n/** Exactly one of `permission`, `anyOf` or `allOf`. */\nexport type PermissionCondition =\n | { permission: string; anyOf?: never; allOf?: never }\n | { permission?: never; anyOf: readonly string[]; allOf?: never }\n | { permission?: never; anyOf?: never; allOf: readonly string[] }\n\n/** Exactly one of `role`, `anyOf` or `allOf`. */\nexport type RoleCondition =\n | { role: string; anyOf?: never; allOf?: never }\n | { role?: never; anyOf: readonly string[]; allOf?: never }\n | { role?: never; anyOf?: never; allOf: readonly string[] }\n\ninterface GateProps {\n children?: ReactNode\n /** Rendered when the check fails. Default: nothing. */\n fallback?: ReactNode\n}\n\nexport type CanProps = PermissionCondition & GateProps\nexport type CannotProps = PermissionCondition & GateProps\nexport type HasRoleProps = RoleCondition & GateProps\n\ninterface LooseCondition {\n anyOf?: readonly string[] | undefined\n allOf?: readonly string[] | undefined\n}\n\n// Conditions are ANDed; no condition at all is denied (deny by default, also for untyped JS callers).\nfunction checkPermissions(rbac: RbacChecker, p: LooseCondition & { permission?: string | undefined }): boolean {\n if (p.permission === undefined && p.anyOf === undefined && p.allOf === undefined) return false\n return (\n (p.permission === undefined || rbac.can(p.permission)) &&\n (p.anyOf === undefined || rbac.canAny(p.anyOf)) &&\n (p.allOf === undefined || rbac.canAll(p.allOf))\n )\n}\n\nfunction checkRoles(rbac: RbacChecker, p: LooseCondition & { role?: string | undefined }): boolean {\n if (p.role === undefined && p.anyOf === undefined && p.allOf === undefined) return false\n return (\n (p.role === undefined || rbac.hasRole(p.role)) &&\n (p.anyOf === undefined || rbac.hasAnyRole(p.anyOf)) &&\n (p.allOf === undefined || rbac.hasAllRoles(p.allOf))\n )\n}\n\n/** Renders `children` if the user has the permission(s) — like spatie's `@can` / `@canany`. */\nexport function Can(props: CanProps) {\n const rbac = useRbac()\n if (rbac.loading) return null\n return <>{checkPermissions(rbac, props) ? props.children : props.fallback}</>\n}\n\n/** Renders `children` if the user does NOT have the permission(s) — like spatie's `@cannot`. */\nexport function Cannot(props: CannotProps) {\n const rbac = useRbac()\n if (rbac.loading) return null\n return <>{checkPermissions(rbac, props) ? props.fallback : props.children}</>\n}\n\n/** Renders `children` if the user has the role(s) — like spatie's `@role` / `@hasanyrole` / `@hasallroles`. */\nexport function HasRole(props: HasRoleProps) {\n const rbac = useRbac()\n if (rbac.loading) return null\n return <>{checkRoles(rbac, props) ? props.children : props.fallback}</>\n}\n"],"mappings":";;;AAAA,SAAS,uBAAuB;AAgChC,IAAM,QAAsB,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC,EAAE;AAEzD,S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@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anievo-id/rbac-evolution-react",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "React bindings for rbac-evolution: <Can>, useCan(), useHasRole().",
5
5
  "license": "MIT",
6
6
  "author": "AniEvo-ID (https://github.com/AniEvo-ID)",
@@ -46,7 +46,7 @@
46
46
  "access": "public"
47
47
  },
48
48
  "dependencies": {
49
- "@anievo-id/rbac-evolution": "^0.4.0"
49
+ "@anievo-id/rbac-evolution": "^0.5.0"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "react": ">=18"