@anievo-id/rbac-evolution-react 0.2.0 → 0.3.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
@@ -76,10 +76,16 @@ function createRbacChecker(snapshot, options = {}) {
76
76
  can,
77
77
  cannot: (permission) => !can(permission),
78
78
  canAny: (...list) => flatten(list).some(can),
79
- canAll: (...list) => flatten(list).every(can),
79
+ canAll: (...list) => {
80
+ const wanted = flatten(list);
81
+ return wanted.length > 0 && wanted.every(can);
82
+ },
80
83
  hasRole: (list) => flatten([list]).some(hasOne),
81
84
  hasAnyRole: (...list) => flatten(list).some(hasOne),
82
- hasAllRoles: (...list) => flatten(list).every(hasOne)
85
+ hasAllRoles: (...list) => {
86
+ const wanted = flatten(list);
87
+ return wanted.length > 0 && wanted.every(hasOne);
88
+ }
83
89
  };
84
90
  }
85
91
 
@@ -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. `true` for an empty list. */\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) => flatten(list).every(can),\n hasRole: (list) => flatten([list]).some(hasOne),\n hasAnyRole: (...list) => flatten(list).some(hasOne),\n hasAllRoles: (...list) => flatten(list).every(hasOne),\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;AACN,WAAO;AAAA,EACT;AACF;AAMO,SAAS,kBACd,UACA,UAA8B,CAAC,GAClB;AACb,QAAM,EAAE,OAAO,YAAY,IAAI,YAAY;AAC3C,QAAM,UAAU,IAAI,IAAI,KAAK;AAC7B,QAAM,gBAAgB,IAAI,IAAI,WAAW;AACzC,QAAM,WAAW,QAAQ,YAAY;AACrC,QAAM,gBAAgB,QAAQ,mBAAmB,CAAC,G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iB,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}\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;AACN,WAAO;AAAA,EACT;AACF;AAMO,SAAS,kBACd,UACA,UAA8B,CAAC,GAClB;AACb,QAAM,EAAE,OAAO,YAAY,IAAI,YAAY;AAC3C,QAAM,UAAU,IAAI,IAAI,KAAK;AAC7B,QAAM,gBAAgB,IAAI,IAAI,WAAW;AACzC,QAAM,WAAW,QAAQ,YAAY;AACrC,QAAM,gBAAgB,QAAQ,mBAAmB,CAAC,GAAG,KAAK,CAAC,MAAM,QAAQ,IAAI,CAAC,CAAC;AAE/E,QAAM,MAAM,CAAC,eAAgC;AAC3C,QAAI,aAAc,QAAO;AACzB,UAAM,OAAO,WAAW,KAAK;AAC7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AK;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"]}
package/dist/index.d.cts CHANGED
@@ -18,7 +18,7 @@ interface RbacChecker {
18
18
  cannot(permission: string): boolean;
19
19
  /** `true` if ANY of the permissions is granted. `false` for an empty list. */
20
20
  canAny(...permissions: Many<string>[]): boolean;
21
- /** `true` if ALL of the permissions are granted. `true` for an empty list. */
21
+ /** `true` if ALL of the permissions are granted. `false` for an empty list (deny by default, like the Vue package). */
22
22
  canAll(...permissions: Many<string>[]): boolean;
23
23
  /** `true` if the subject has the role — or ANY of the roles when given an array. */
24
24
  hasRole(roles: Many<string>): boolean;
package/dist/index.d.ts CHANGED
@@ -18,7 +18,7 @@ interface RbacChecker {
18
18
  cannot(permission: string): boolean;
19
19
  /** `true` if ANY of the permissions is granted. `false` for an empty list. */
20
20
  canAny(...permissions: Many<string>[]): boolean;
21
- /** `true` if ALL of the permissions are granted. `true` for an empty list. */
21
+ /** `true` if ALL of the permissions are granted. `false` for an empty list (deny by default, like the Vue package). */
22
22
  canAll(...permissions: Many<string>[]): boolean;
23
23
  /** `true` if the subject has the role — or ANY of the roles when given an array. */
24
24
  hasRole(roles: Many<string>): boolean;
package/dist/index.js CHANGED
@@ -39,10 +39,16 @@ function createRbacChecker(snapshot, options = {}) {
39
39
  can,
40
40
  cannot: (permission) => !can(permission),
41
41
  canAny: (...list) => flatten(list).some(can),
42
- canAll: (...list) => flatten(list).every(can),
42
+ canAll: (...list) => {
43
+ const wanted = flatten(list);
44
+ return wanted.length > 0 && wanted.every(can);
45
+ },
43
46
  hasRole: (list) => flatten([list]).some(hasOne),
44
47
  hasAnyRole: (...list) => flatten(list).some(hasOne),
45
- hasAllRoles: (...list) => flatten(list).every(hasOne)
48
+ hasAllRoles: (...list) => {
49
+ const wanted = flatten(list);
50
+ return wanted.length > 0 && wanted.every(hasOne);
51
+ }
46
52
  };
47
53
  }
48
54
 
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. `true` for an empty list. */\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) => flatten(list).every(can),\n hasRole: (list) => flatten([list]).some(hasOne),\n hasAnyRole: (...list) => flatten(list).some(hasOne),\n hasAllRoles: (...list) => flatten(list).every(hasOne),\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,SAAS,uBAAuB;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,K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eAAe,UAAa,KAAK,IAAI,EAAE,UAAU,OACnD,EAAE,UAAU,UAAa,KAAK,OAAO,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,gBAAAA,KAAA,YAAG,oBAAU,MAAM,WAAW,MAAM,UAAS;AACtD;AAGO,SAAS,OAAO,OAAoB;AACzC,QAAM,UAAU,iBAAiB,QAAQ,GAAG,KAAK;AACjD,SAAO,gBAAAA,KAAA,YAAG,oBAAU,MAAM,WAAW,MAAM,UAAS;AACtD;AAGO,SAAS,QAAQ,OAAqB;AAC3C,QAAM,UAAU,WAAW,QAAQ,GAAG,KAAK;AAC3C,SAAO,gBAAAA,KAAA,YAAG,oBAAU,MAAM,WAAW,MAAM,UAAS;AACtD;","names":["jsx"]}
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.fallback}</>\n}\n"],"mappings":";;;AAAA,SAAS,uBAAuB;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,WAAO,gBAAgB,SAAS,QAAQ;AAAA,EAC1C,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAMO,SAAS,kBACd,UACA,UAA8B,CAAC,GAClB;AACb,QAAM,EAAE,OAAO,YAAY,IAAI,YAAY;AAC3C,QAAM,UAAU,IAAI,IAAI,KAAK;AAC7B,QAAM,gBAAgB,IAAI,IAAI,WAAW;AACzC,QAAM,WAAW,QAAQ,YAAY;AACrC,QAAM,gBAAgB,QAAQ,mBAAmB,CAAC,GAAG,KAAK,CAAC,MAAM,QAAQ,IAAI,CAAC,CAAC;AAE/E,QAAM,MAAM,CAAC,eAAgC;AAC3C,QAAI,aAAc,QAAO;AACzB,UAAM,OAAO,WAAW,KA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AiB,QAAQ,GAAG,KAAK;AACjD,SAAO,gBAAAA,KAAA,YAAG,oBAAU,MAAM,WAAW,MAAM,UAAS;AACtD;AAGO,SAAS,OAAO,OAAoB;AACzC,QAAM,UAAU,iBAAiB,QAAQ,GAAG,KAAK;AACjD,SAAO,gBAAAA,KAAA,YAAG,oBAAU,MAAM,WAAW,MAAM,UAAS;AACtD;AAGO,SAAS,QAAQ,OAAqB;AAC3C,QAAM,UAAU,WAAW,QAAQ,GAAG,KAAK;AAC3C,SAAO,gBAAAA,KAAA,YAAG,oBAAU,MAAM,WAAW,MAAM,UAAS;AACtD;","names":["jsx"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@anievo-id/rbac-evolution-react",
3
- "version": "0.2.0",
4
- "description": "React bindings for rbac-evolution: <Can>, useCan(), useRole().",
3
+ "version": "0.3.0",
4
+ "description": "React bindings for rbac-evolution: <Can>, useCan(), useHasRole().",
5
5
  "license": "MIT",
6
6
  "author": "AniEvo-ID (https://github.com/AniEvo-ID)",
7
7
  "homepage": "https://github.com/AniEvo-ID/rbac-evolution/tree/main/packages/react#readme",
@@ -46,7 +46,7 @@
46
46
  "access": "public"
47
47
  },
48
48
  "dependencies": {
49
- "@anievo-id/rbac-evolution": "^0.2.0"
49
+ "@anievo-id/rbac-evolution": "^0.3.0"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "react": ">=18"