@xtandard/permissions 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +364 -2
- package/bin/xtandard-permissions.mjs +3 -0
- package/dist/better-auth-BEmX8qyz.mjs +94 -0
- package/dist/better-auth-BEmX8qyz.mjs.map +1 -0
- package/dist/better-auth-DQET1sXa.d.mts +75 -0
- package/dist/better-auth-MR7dBtpo.cjs +111 -0
- package/dist/better-auth-MR7dBtpo.cjs.map +1 -0
- package/dist/better-auth-vgRYgjYK.d.cts +75 -0
- package/dist/catalog-DA_d1SdC.d.cts +153 -0
- package/dist/catalog-DJcJ3ycq.d.mts +153 -0
- package/dist/catalog.cjs +198 -0
- package/dist/catalog.cjs.map +1 -0
- package/dist/catalog.d.cts +3 -0
- package/dist/catalog.d.mts +3 -0
- package/dist/catalog.mjs +141 -0
- package/dist/catalog.mjs.map +1 -0
- package/dist/chunk-DakpK96I.cjs +43 -0
- package/dist/cli.cjs +554 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +28 -0
- package/dist/cli.d.mts +28 -0
- package/dist/cli.mjs +550 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/contract-BAisPHBw.d.cts +78 -0
- package/dist/contract-BganM0fx.mjs +79 -0
- package/dist/contract-BganM0fx.mjs.map +1 -0
- package/dist/contract-Bpy8yPrS.cjs +70 -0
- package/dist/contract-Bpy8yPrS.cjs.map +1 -0
- package/dist/contract-Brnu9XXo.cjs +114 -0
- package/dist/contract-Brnu9XXo.cjs.map +1 -0
- package/dist/contract-CNgMXr2u.d.cts +46 -0
- package/dist/contract-CNgMXr2u.d.mts +46 -0
- package/dist/contract-CXPMGuHk.d.cts +391 -0
- package/dist/contract-D6j-yrBL.d.mts +78 -0
- package/dist/contract-DTdTXZtJ.mjs +47 -0
- package/dist/contract-DTdTXZtJ.mjs.map +1 -0
- package/dist/contract-DhS23Pcp.d.mts +391 -0
- package/dist/contract-RMkmERhT.d.cts +2 -0
- package/dist/contract-RMkmERhT.d.mts +2 -0
- package/dist/create-fetch-handler-B0twltQL.mjs +673 -0
- package/dist/create-fetch-handler-B0twltQL.mjs.map +1 -0
- package/dist/create-fetch-handler-B711wb64.d.mts +77 -0
- package/dist/create-fetch-handler-BHT4vSZs.cjs +690 -0
- package/dist/create-fetch-handler-BHT4vSZs.cjs.map +1 -0
- package/dist/create-fetch-handler-DxhvXuBA.d.cts +77 -0
- package/dist/elysia-shared-BFcWzfc9.cjs +101 -0
- package/dist/elysia-shared-BFcWzfc9.cjs.map +1 -0
- package/dist/elysia-shared-BwUYg0Ln.mjs +78 -0
- package/dist/elysia-shared-BwUYg0Ln.mjs.map +1 -0
- package/dist/elysia-shared-C2LnDOj3.d.cts +58 -0
- package/dist/elysia-shared-WuDr0jco.d.mts +58 -0
- package/dist/engine--C7zo-4M.cjs +667 -0
- package/dist/engine--C7zo-4M.cjs.map +1 -0
- package/dist/engine-BTgwbHeO.d.cts +336 -0
- package/dist/engine-C3xHfi3q.d.mts +336 -0
- package/dist/engine-CR75K253.mjs +654 -0
- package/dist/engine-CR75K253.mjs.map +1 -0
- package/dist/entry-auth-basic.cjs +40 -0
- package/dist/entry-auth-basic.cjs.map +1 -0
- package/dist/entry-auth-basic.d.cts +24 -0
- package/dist/entry-auth-basic.d.mts +24 -0
- package/dist/entry-auth-basic.mjs +26 -0
- package/dist/entry-auth-basic.mjs.map +1 -0
- package/dist/entry-auth-better-auth.cjs +15 -0
- package/dist/entry-auth-better-auth.d.cts +5 -0
- package/dist/entry-auth-better-auth.d.mts +5 -0
- package/dist/entry-auth-better-auth.mjs +4 -0
- package/dist/entry-auth-delegated.cjs +8 -0
- package/dist/entry-auth-delegated.d.cts +2 -0
- package/dist/entry-auth-delegated.d.mts +2 -0
- package/dist/entry-auth-delegated.mjs +2 -0
- package/dist/entry-auth-jwt.cjs +120 -0
- package/dist/entry-auth-jwt.cjs.map +1 -0
- package/dist/entry-auth-jwt.d.cts +71 -0
- package/dist/entry-auth-jwt.d.mts +71 -0
- package/dist/entry-auth-jwt.mjs +109 -0
- package/dist/entry-auth-jwt.mjs.map +1 -0
- package/dist/entry-auth-none.cjs +14 -0
- package/dist/entry-auth-none.d.cts +2 -0
- package/dist/entry-auth-none.d.mts +2 -0
- package/dist/entry-auth-none.mjs +2 -0
- package/dist/entry-auth-session.cjs +86 -0
- package/dist/entry-auth-session.cjs.map +1 -0
- package/dist/entry-auth-session.d.cts +25 -0
- package/dist/entry-auth-session.d.mts +25 -0
- package/dist/entry-auth-session.mjs +76 -0
- package/dist/entry-auth-session.mjs.map +1 -0
- package/dist/entry-authorization-delegated.cjs +31 -0
- package/dist/entry-authorization-delegated.cjs.map +1 -0
- package/dist/entry-authorization-delegated.d.cts +27 -0
- package/dist/entry-authorization-delegated.d.mts +27 -0
- package/dist/entry-authorization-delegated.mjs +30 -0
- package/dist/entry-authorization-delegated.mjs.map +1 -0
- package/dist/entry-authorization-none.cjs +29 -0
- package/dist/entry-authorization-none.cjs.map +1 -0
- package/dist/entry-authorization-none.d.cts +17 -0
- package/dist/entry-authorization-none.d.mts +17 -0
- package/dist/entry-authorization-none.mjs +28 -0
- package/dist/entry-authorization-none.mjs.map +1 -0
- package/dist/entry-authorization-permissions.cjs +4 -0
- package/dist/entry-authorization-permissions.d.cts +49 -0
- package/dist/entry-authorization-permissions.d.mts +49 -0
- package/dist/entry-authorization-permissions.mjs +2 -0
- package/dist/entry-authorization-roles.cjs +9 -0
- package/dist/entry-authorization-roles.d.cts +37 -0
- package/dist/entry-authorization-roles.d.mts +37 -0
- package/dist/entry-authorization-roles.mjs +3 -0
- package/dist/entry-better-auth-client.cjs +31 -0
- package/dist/entry-better-auth-client.cjs.map +1 -0
- package/dist/entry-better-auth-client.d.cts +63 -0
- package/dist/entry-better-auth-client.d.mts +63 -0
- package/dist/entry-better-auth-client.mjs +29 -0
- package/dist/entry-better-auth-client.mjs.map +1 -0
- package/dist/entry-better-auth.cjs +289 -0
- package/dist/entry-better-auth.cjs.map +1 -0
- package/dist/entry-better-auth.d.cts +3 -0
- package/dist/entry-better-auth.d.mts +3 -0
- package/dist/entry-better-auth.mjs +284 -0
- package/dist/entry-better-auth.mjs.map +1 -0
- package/dist/entry-bridges-audit.cjs +106 -0
- package/dist/entry-bridges-audit.cjs.map +1 -0
- package/dist/entry-bridges-audit.d.cts +58 -0
- package/dist/entry-bridges-audit.d.mts +58 -0
- package/dist/entry-bridges-audit.mjs +105 -0
- package/dist/entry-bridges-audit.mjs.map +1 -0
- package/dist/entry-bridges-webhooks.cjs +90 -0
- package/dist/entry-bridges-webhooks.cjs.map +1 -0
- package/dist/entry-bridges-webhooks.d.cts +46 -0
- package/dist/entry-bridges-webhooks.d.mts +46 -0
- package/dist/entry-bridges-webhooks.mjs +88 -0
- package/dist/entry-bridges-webhooks.mjs.map +1 -0
- package/dist/entry-bun.cjs +33 -0
- package/dist/entry-bun.cjs.map +1 -0
- package/dist/entry-bun.d.cts +31 -0
- package/dist/entry-bun.d.mts +31 -0
- package/dist/entry-bun.mjs +30 -0
- package/dist/entry-bun.mjs.map +1 -0
- package/dist/entry-drizzle-mysql.cjs +34 -0
- package/dist/entry-drizzle-mysql.cjs.map +1 -0
- package/dist/entry-drizzle-mysql.d.cts +20 -0
- package/dist/entry-drizzle-mysql.d.mts +20 -0
- package/dist/entry-drizzle-mysql.mjs +33 -0
- package/dist/entry-drizzle-mysql.mjs.map +1 -0
- package/dist/entry-drizzle-pg.cjs +44 -0
- package/dist/entry-drizzle-pg.cjs.map +1 -0
- package/dist/entry-drizzle-pg.d.cts +20 -0
- package/dist/entry-drizzle-pg.d.mts +20 -0
- package/dist/entry-drizzle-pg.mjs +43 -0
- package/dist/entry-drizzle-pg.mjs.map +1 -0
- package/dist/entry-drizzle-sqlite.cjs +34 -0
- package/dist/entry-drizzle-sqlite.cjs.map +1 -0
- package/dist/entry-drizzle-sqlite.d.cts +15 -0
- package/dist/entry-drizzle-sqlite.d.mts +15 -0
- package/dist/entry-drizzle-sqlite.mjs +33 -0
- package/dist/entry-drizzle-sqlite.mjs.map +1 -0
- package/dist/entry-elysia.cjs +117 -0
- package/dist/entry-elysia.cjs.map +1 -0
- package/dist/entry-elysia.d.cts +446 -0
- package/dist/entry-elysia.d.mts +446 -0
- package/dist/entry-elysia.mjs +113 -0
- package/dist/entry-elysia.mjs.map +1 -0
- package/dist/entry-elysia2.cjs +117 -0
- package/dist/entry-elysia2.cjs.map +1 -0
- package/dist/entry-elysia2.d.cts +571 -0
- package/dist/entry-elysia2.d.mts +571 -0
- package/dist/entry-elysia2.mjs +113 -0
- package/dist/entry-elysia2.mjs.map +1 -0
- package/dist/entry-express.cjs +101 -0
- package/dist/entry-express.cjs.map +1 -0
- package/dist/entry-express.d.cts +36 -0
- package/dist/entry-express.d.mts +36 -0
- package/dist/entry-express.mjs +97 -0
- package/dist/entry-express.mjs.map +1 -0
- package/dist/entry-hono.cjs +63 -0
- package/dist/entry-hono.cjs.map +1 -0
- package/dist/entry-hono.d.cts +40 -0
- package/dist/entry-hono.d.mts +40 -0
- package/dist/entry-hono.mjs +60 -0
- package/dist/entry-hono.mjs.map +1 -0
- package/dist/entry-hooks-log.cjs +39 -0
- package/dist/entry-hooks-log.cjs.map +1 -0
- package/dist/entry-hooks-log.d.cts +19 -0
- package/dist/entry-hooks-log.d.mts +19 -0
- package/dist/entry-hooks-log.mjs +36 -0
- package/dist/entry-hooks-log.mjs.map +1 -0
- package/dist/entry-next.cjs +67 -0
- package/dist/entry-next.cjs.map +1 -0
- package/dist/entry-next.d.cts +60 -0
- package/dist/entry-next.d.mts +60 -0
- package/dist/entry-next.mjs +57 -0
- package/dist/entry-next.mjs.map +1 -0
- package/dist/entry-react.cjs +401 -0
- package/dist/entry-react.cjs.map +1 -0
- package/dist/entry-react.d.cts +206 -0
- package/dist/entry-react.d.mts +206 -0
- package/dist/entry-react.mjs +388 -0
- package/dist/entry-react.mjs.map +1 -0
- package/dist/entry-storage-cloudflare-kv.cjs +8 -0
- package/dist/entry-storage-cloudflare-kv.d.cts +2 -0
- package/dist/entry-storage-cloudflare-kv.d.mts +2 -0
- package/dist/entry-storage-cloudflare-kv.mjs +2 -0
- package/dist/entry-storage-drizzle.cjs +71 -0
- package/dist/entry-storage-drizzle.cjs.map +1 -0
- package/dist/entry-storage-drizzle.d.cts +27 -0
- package/dist/entry-storage-drizzle.d.mts +27 -0
- package/dist/entry-storage-drizzle.mjs +68 -0
- package/dist/entry-storage-drizzle.mjs.map +1 -0
- package/dist/entry-storage-file.cjs +54 -0
- package/dist/entry-storage-file.cjs.map +1 -0
- package/dist/entry-storage-file.d.cts +24 -0
- package/dist/entry-storage-file.d.mts +24 -0
- package/dist/entry-storage-file.mjs +46 -0
- package/dist/entry-storage-file.mjs.map +1 -0
- package/dist/entry-storage-libsql.cjs +64 -0
- package/dist/entry-storage-libsql.cjs.map +1 -0
- package/dist/entry-storage-libsql.d.cts +28 -0
- package/dist/entry-storage-libsql.d.mts +28 -0
- package/dist/entry-storage-libsql.mjs +61 -0
- package/dist/entry-storage-libsql.mjs.map +1 -0
- package/dist/entry-storage-memory.cjs +3 -0
- package/dist/entry-storage-memory.d.cts +25 -0
- package/dist/entry-storage-memory.d.mts +25 -0
- package/dist/entry-storage-memory.mjs +2 -0
- package/dist/entry-storage-mongodb.cjs +61 -0
- package/dist/entry-storage-mongodb.cjs.map +1 -0
- package/dist/entry-storage-mongodb.d.cts +25 -0
- package/dist/entry-storage-mongodb.d.mts +25 -0
- package/dist/entry-storage-mongodb.mjs +58 -0
- package/dist/entry-storage-mongodb.mjs.map +1 -0
- package/dist/entry-storage-postgres.cjs +62 -0
- package/dist/entry-storage-postgres.cjs.map +1 -0
- package/dist/entry-storage-postgres.d.cts +29 -0
- package/dist/entry-storage-postgres.d.mts +29 -0
- package/dist/entry-storage-postgres.mjs +58 -0
- package/dist/entry-storage-postgres.mjs.map +1 -0
- package/dist/entry-storage-redis.cjs +76 -0
- package/dist/entry-storage-redis.cjs.map +1 -0
- package/dist/entry-storage-redis.d.cts +29 -0
- package/dist/entry-storage-redis.d.mts +29 -0
- package/dist/entry-storage-redis.mjs +72 -0
- package/dist/entry-storage-redis.mjs.map +1 -0
- package/dist/entry-storage-sqlite.cjs +46 -0
- package/dist/entry-storage-sqlite.cjs.map +1 -0
- package/dist/entry-storage-sqlite.d.cts +22 -0
- package/dist/entry-storage-sqlite.d.mts +22 -0
- package/dist/entry-storage-sqlite.mjs +43 -0
- package/dist/entry-storage-sqlite.mjs.map +1 -0
- package/dist/entry-storage-unstorage.cjs +8 -0
- package/dist/entry-storage-unstorage.d.cts +2 -0
- package/dist/entry-storage-unstorage.d.mts +2 -0
- package/dist/entry-storage-unstorage.mjs +2 -0
- package/dist/error-codes-9MRMSSMQ.cjs +49 -0
- package/dist/error-codes-9MRMSSMQ.cjs.map +1 -0
- package/dist/error-codes-CCKuDDu7.mjs +44 -0
- package/dist/error-codes-CCKuDDu7.mjs.map +1 -0
- package/dist/errors-CG_OLEEv.mjs +64 -0
- package/dist/errors-CG_OLEEv.mjs.map +1 -0
- package/dist/errors-CjpymzFj.cjs +105 -0
- package/dist/errors-CjpymzFj.cjs.map +1 -0
- package/dist/events-AE4Ytqmz.d.cts +6 -0
- package/dist/events-AE4Ytqmz.d.mts +6 -0
- package/dist/events-DYZShrYB.mjs +15 -0
- package/dist/events-DYZShrYB.mjs.map +1 -0
- package/dist/events-FJ2K0wly.cjs +26 -0
- package/dist/events-FJ2K0wly.cjs.map +1 -0
- package/dist/guard-Bj8zk23F.mjs +141 -0
- package/dist/guard-Bj8zk23F.mjs.map +1 -0
- package/dist/guard-DvaoUgjk.cjs +188 -0
- package/dist/guard-DvaoUgjk.cjs.map +1 -0
- package/dist/guard-VEK0W8ug.d.cts +62 -0
- package/dist/guard-k7A71SWC.d.mts +62 -0
- package/dist/index-BVVgDSdq.d.cts +1 -0
- package/dist/index-BVVgDSdq.d.mts +1 -0
- package/dist/index.cjs +243 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +213 -0
- package/dist/index.d.mts +213 -0
- package/dist/index.mjs +60 -0
- package/dist/index.mjs.map +1 -0
- package/dist/kit-51JtMxsn.d.mts +62 -0
- package/dist/kit-BM3uosfV.mjs +80 -0
- package/dist/kit-BM3uosfV.mjs.map +1 -0
- package/dist/kit-BrqnCcDw.cjs +97 -0
- package/dist/kit-BrqnCcDw.cjs.map +1 -0
- package/dist/kit-DLF7DZrp.d.cts +62 -0
- package/dist/memory-BbBbbY8-.mjs +32 -0
- package/dist/memory-BbBbbY8-.mjs.map +1 -0
- package/dist/memory-knijqKw_.cjs +39 -0
- package/dist/memory-knijqKw_.cjs.map +1 -0
- package/dist/permissions-CeV6M3v7.mjs +62 -0
- package/dist/permissions-CeV6M3v7.mjs.map +1 -0
- package/dist/permissions-DDEkHNvN.cjs +73 -0
- package/dist/permissions-DDEkHNvN.cjs.map +1 -0
- package/dist/policy-BQmlo2lu.mjs +3 -0
- package/dist/policy-CGA6QD0a.d.mts +4 -0
- package/dist/policy-DFq_SYR3.d.cts +4 -0
- package/dist/react-embed.js +18735 -0
- package/dist/react.css +1 -0
- package/dist/relationships-BLYEJ9tc.cjs +907 -0
- package/dist/relationships-BLYEJ9tc.cjs.map +1 -0
- package/dist/relationships-BfLonEMI.mjs +756 -0
- package/dist/relationships-BfLonEMI.mjs.map +1 -0
- package/dist/roles-DBk4i8b5.mjs +69 -0
- package/dist/roles-DBk4i8b5.mjs.map +1 -0
- package/dist/roles-a8B5uZ35.cjs +104 -0
- package/dist/roles-a8B5uZ35.cjs.map +1 -0
- package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/dist/routes-B2LrYpT0.cjs +1055 -0
- package/dist/routes-B2LrYpT0.cjs.map +1 -0
- package/dist/routes-Cx-kl4PY.mjs +1007 -0
- package/dist/routes-Cx-kl4PY.mjs.map +1 -0
- package/dist/server-Bn643UOy.d.mts +886 -0
- package/dist/server-YdH25OEA.d.cts +886 -0
- package/dist/table-DA98mVVT.d.cts +2 -0
- package/dist/table-DA98mVVT.d.mts +2 -0
- package/dist/testing.cjs +59 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +37 -0
- package/dist/testing.d.mts +37 -0
- package/dist/testing.mjs +57 -0
- package/dist/testing.mjs.map +1 -0
- package/dist/types-elysia2/adapters/elysia-shared.d.ts +71 -0
- package/dist/types-elysia2/adapters/elysia2.d.ts +432 -0
- package/dist/types-elysia2/adapters/guard.d.ts +140 -0
- package/dist/types-elysia2/auth/contract.d.ts +9 -0
- package/dist/types-elysia2/auth-provider/contract.d.ts +85 -0
- package/dist/types-elysia2/auth-provider/kit.d.ts +62 -0
- package/dist/types-elysia2/authorization/contract.d.ts +53 -0
- package/dist/types-elysia2/authorization/permissions.d.ts +54 -0
- package/dist/types-elysia2/catalog.d.ts +197 -0
- package/dist/types-elysia2/engine.d.ts +233 -0
- package/dist/types-elysia2/entry-elysia2.d.ts +9 -0
- package/dist/types-elysia2/errors.d.ts +55 -0
- package/dist/types-elysia2/hooks/contract.d.ts +100 -0
- package/dist/types-elysia2/keys.d.ts +78 -0
- package/dist/types-elysia2/policy.d.ts +13 -0
- package/dist/types-elysia2/relationships.d.ts +162 -0
- package/dist/types-elysia2/schema.d.ts +315 -0
- package/dist/types-elysia2/server/cors.d.ts +8 -0
- package/dist/types-elysia2/server/create-fetch-handler.d.ts +87 -0
- package/dist/types-elysia2/server/openapi.d.ts +29 -0
- package/dist/types-elysia2/server/routes.d.ts +143 -0
- package/dist/types-elysia2/server/validation.d.ts +205 -0
- package/dist/types-elysia2/storage/contract.d.ts +18 -0
- package/dist/types-react/catalog.d.ts +197 -0
- package/dist/types-react/engine.d.ts +233 -0
- package/dist/types-react/entry-react-embed.d.ts +20 -0
- package/dist/types-react/errors.d.ts +55 -0
- package/dist/types-react/hooks/contract.d.ts +100 -0
- package/dist/types-react/keys.d.ts +78 -0
- package/dist/types-react/policy.d.ts +13 -0
- package/dist/types-react/react-embed.d.ts +37 -0
- package/dist/types-react/relationships.d.ts +162 -0
- package/dist/types-react/schema.d.ts +315 -0
- package/dist/types-react/storage/contract.d.ts +18 -0
- package/dist/types-react/ui/App.d.ts +28 -0
- package/dist/types-react/ui/api.d.ts +73 -0
- package/dist/types-react/ui/components/perm-bits.d.ts +68 -0
- package/dist/types-react/ui/lib/queries.d.ts +46 -0
- package/dist/types-react/ui/theme.d.ts +10 -0
- package/dist/types-react/ui/types.d.ts +118 -0
- package/dist/types-react/ui/views/CatalogView.d.ts +3 -0
- package/dist/types-react/ui/views/HistoryView.d.ts +6 -0
- package/dist/types-react/ui/views/PlaygroundView.d.ts +14 -0
- package/dist/types-react/ui/views/RelationshipsView.d.ts +6 -0
- package/dist/types-react/ui/views/SubjectDetailView.d.ts +7 -0
- package/dist/types-react/ui/views/SubjectsView.d.ts +6 -0
- package/dist/ui/apple-touch-icon.png +0 -0
- package/dist/ui/assets/index-DCPZRo-Z.css +1 -0
- package/dist/ui/assets/index-DdvxpphI.js +104 -0
- package/dist/ui/favicon.ico +0 -0
- package/dist/ui/favicon.svg +15 -0
- package/dist/ui/index.html +20 -0
- package/package.json +587 -4
package/dist/catalog.cjs
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _xtandard_lib_policy_rebac = require("@xtandard/lib/policy/rebac");
|
|
3
|
+
let _xtandard_lib_policy_permix = require("@xtandard/lib/policy/permix");
|
|
4
|
+
//#region src/catalog.ts
|
|
5
|
+
/**
|
|
6
|
+
* The catalog: roles, assignable permissions and per-role rules, declared once
|
|
7
|
+
* in code and shared by the server, the browser and the admin panel (ADR 0002).
|
|
8
|
+
*
|
|
9
|
+
* `definePermissions()` wraps `defineAuthorization` from
|
|
10
|
+
* `@xtandard/lib/policy/permix` (which owns role parsing, the assignable list,
|
|
11
|
+
* the sync-only rule helpers, `createAbility` and `explain`) and adds what the
|
|
12
|
+
* product needs on top: `managerRoles`, labels for the panel, the
|
|
13
|
+
* `directGrants` switch and `effectiveRole()`. The rule user is fixed to
|
|
14
|
+
* {@link PermissionsSubject}, whose `role` the engine sets to the subject's
|
|
15
|
+
* effective role.
|
|
16
|
+
*
|
|
17
|
+
* Depends only on `@xtandard/lib/policy/permix` (and its `permix` peer), so
|
|
18
|
+
* browsers and edge runtimes can import it.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { definePermissions } from "@xtandard/permissions/catalog";
|
|
23
|
+
*
|
|
24
|
+
* type Definition = { post: ["read", { name: "publish"; type: Post }, "delete"] };
|
|
25
|
+
*
|
|
26
|
+
* export const permissions = definePermissions<Definition>()({
|
|
27
|
+
* roles: ["guest", "member", "editor", "admin"], // least → most privileged
|
|
28
|
+
* guestRole: "guest",
|
|
29
|
+
* defaultRole: "member",
|
|
30
|
+
* managerRoles: ["admin"],
|
|
31
|
+
* assignable: ["post:moderate", "billing:refund"],
|
|
32
|
+
* labels: { roles: { admin: "Administrator" }, permissions: { "post:moderate": "Moderate posts" } },
|
|
33
|
+
* rules: {
|
|
34
|
+
* guest: () => ({ post: { read: true, publish: false, delete: false } }),
|
|
35
|
+
* member: ({ user, permissions }, { or, fieldEquals, hasPermission }) => ({
|
|
36
|
+
* post: {
|
|
37
|
+
* read: true,
|
|
38
|
+
* publish: or<Post>(fieldEquals(user!.id, { field: "authorId" }), () =>
|
|
39
|
+
* hasPermission(permissions, "post:moderate"),
|
|
40
|
+
* ),
|
|
41
|
+
* delete: false,
|
|
42
|
+
* },
|
|
43
|
+
* }),
|
|
44
|
+
* editor: () => ({ post: { read: true, publish: true, delete: false } }),
|
|
45
|
+
* admin: () => ({ post: { read: true, publish: true, delete: true } }),
|
|
46
|
+
* },
|
|
47
|
+
* });
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @module catalog
|
|
51
|
+
*/
|
|
52
|
+
const normalizeLabel = (key, input) => input === void 0 ? { label: key } : typeof input === "string" ? { label: input } : {
|
|
53
|
+
label: input.label,
|
|
54
|
+
...input.description ? { description: input.description } : {}
|
|
55
|
+
};
|
|
56
|
+
function labelMap(keys, inputs) {
|
|
57
|
+
const out = Object.create(null);
|
|
58
|
+
for (const key of keys) out[key] = normalizeLabel(key, inputs && Object.hasOwn(inputs, key) ? inputs[key] : void 0);
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
function buildCatalog(authorization, meta) {
|
|
62
|
+
const { roles, guestRole, defaultRole, assignable } = authorization.config;
|
|
63
|
+
if (roles.length === 0) throw new Error("definePermissions: roles must not be empty");
|
|
64
|
+
const managerRoles = meta.managerRoles ?? [roles[roles.length - 1]];
|
|
65
|
+
for (const role of managerRoles) {
|
|
66
|
+
if (!authorization.isRole(role)) throw new Error(`definePermissions: managerRoles "${role}" is not in roles`);
|
|
67
|
+
if (role === guestRole) throw new Error(`definePermissions: the guest role "${role}" can't be a manager role`);
|
|
68
|
+
}
|
|
69
|
+
const managers = new Set(managerRoles);
|
|
70
|
+
const rank = new Map(roles.map((role, index) => [role, index]));
|
|
71
|
+
const effectiveRole = (raw) => {
|
|
72
|
+
const candidates = typeof raw === "string" ? raw.split(",").map((s) => s.trim()) : Array.isArray(raw) ? raw : [];
|
|
73
|
+
let best;
|
|
74
|
+
for (const candidate of candidates) {
|
|
75
|
+
if (!authorization.isRole(candidate) || candidate === guestRole) continue;
|
|
76
|
+
if (best === void 0 || rank.get(candidate) > rank.get(best)) best = candidate;
|
|
77
|
+
}
|
|
78
|
+
return best ?? authorization.parseRole(void 0);
|
|
79
|
+
};
|
|
80
|
+
return Object.freeze({
|
|
81
|
+
authorization,
|
|
82
|
+
roles,
|
|
83
|
+
guestRole,
|
|
84
|
+
defaultRole,
|
|
85
|
+
assignable,
|
|
86
|
+
managerRoles: Object.freeze([...managerRoles]),
|
|
87
|
+
labels: Object.freeze({
|
|
88
|
+
roles: Object.freeze(labelMap(roles, meta.labels?.roles)),
|
|
89
|
+
permissions: Object.freeze(labelMap(assignable, meta.labels?.permissions))
|
|
90
|
+
}),
|
|
91
|
+
directGrants: meta.directGrants ?? false,
|
|
92
|
+
relations: meta.relations ? (0, _xtandard_lib_policy_rebac.defineRelations)(meta.relations) : null,
|
|
93
|
+
isRole: authorization.isRole,
|
|
94
|
+
isAssignable: authorization.isAssignable,
|
|
95
|
+
isManagerRole: (role) => typeof role === "string" && managers.has(role),
|
|
96
|
+
parseRole: authorization.parseRole,
|
|
97
|
+
effectiveRole
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Define the catalog. Curried like the lib's `defineAuthorization`: the permix definition and the
|
|
102
|
+
* rule context type go in the first call, the roles and permissions are inferred by the second:
|
|
103
|
+
* `definePermissions<Definition, Ctx>()({ ... })`.
|
|
104
|
+
*
|
|
105
|
+
* Rules are synchronous (a rule that returns a Promise denies and throws `AsyncRuleError`); load
|
|
106
|
+
* what they need first and pass it as `ctx` or as the checked data. A rule wrapped in the lib's
|
|
107
|
+
* `asyncRule()` is evaluated only by the engine's async `check()` / `can()`, never by the
|
|
108
|
+
* synchronous `ability()` (ADR 0005).
|
|
109
|
+
*
|
|
110
|
+
* @throws {Error} When `guestRole`, `defaultRole` or a `managerRoles` entry is not in `roles`, a
|
|
111
|
+
* role has no rules, or the guest role is a manager role.
|
|
112
|
+
*/
|
|
113
|
+
function definePermissions() {
|
|
114
|
+
return (config) => {
|
|
115
|
+
const { managerRoles, labels, directGrants, relations, ...authorizationConfig } = config;
|
|
116
|
+
return buildCatalog((0, _xtandard_lib_policy_permix.defineAuthorization)()({
|
|
117
|
+
...authorizationConfig,
|
|
118
|
+
getRole: (subject) => subject.role
|
|
119
|
+
}), {
|
|
120
|
+
...managerRoles ? { managerRoles } : {},
|
|
121
|
+
...labels ? { labels } : {},
|
|
122
|
+
...directGrants !== void 0 ? { directGrants } : {},
|
|
123
|
+
...relations ? { relations } : {}
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Wrap an existing lib `Authorization` (from `defineAuthorization`) as a catalog. Its config is
|
|
129
|
+
* re-defined with `getRole: (subject) => subject.role`, so its rules receive a
|
|
130
|
+
* {@link PermissionsSubject} (`{ id, role, name?, email?, attributes? }`) as `user`; rules that read
|
|
131
|
+
* other user fields must move them into `attributes` or `ctx`.
|
|
132
|
+
*/
|
|
133
|
+
function fromAuthorization(authorization, meta = {}) {
|
|
134
|
+
const { getRole: _ignored, ...config } = authorization.config;
|
|
135
|
+
return buildCatalog((0, _xtandard_lib_policy_permix.defineAuthorization)()({
|
|
136
|
+
...config,
|
|
137
|
+
getRole: (subject) => subject.role
|
|
138
|
+
}), meta);
|
|
139
|
+
}
|
|
140
|
+
//#endregion
|
|
141
|
+
Object.defineProperty(exports, "RelationDepthError", {
|
|
142
|
+
enumerable: true,
|
|
143
|
+
get: function() {
|
|
144
|
+
return _xtandard_lib_policy_rebac.RelationDepthError;
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
Object.defineProperty(exports, "RelationSchemaError", {
|
|
148
|
+
enumerable: true,
|
|
149
|
+
get: function() {
|
|
150
|
+
return _xtandard_lib_policy_rebac.RelationSchemaError;
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
Object.defineProperty(exports, "computed", {
|
|
154
|
+
enumerable: true,
|
|
155
|
+
get: function() {
|
|
156
|
+
return _xtandard_lib_policy_rebac.computed;
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
exports.definePermissions = definePermissions;
|
|
160
|
+
Object.defineProperty(exports, "defineRelations", {
|
|
161
|
+
enumerable: true,
|
|
162
|
+
get: function() {
|
|
163
|
+
return _xtandard_lib_policy_rebac.defineRelations;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
Object.defineProperty(exports, "direct", {
|
|
167
|
+
enumerable: true,
|
|
168
|
+
get: function() {
|
|
169
|
+
return _xtandard_lib_policy_rebac.direct;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
Object.defineProperty(exports, "exclusion", {
|
|
173
|
+
enumerable: true,
|
|
174
|
+
get: function() {
|
|
175
|
+
return _xtandard_lib_policy_rebac.exclusion;
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
Object.defineProperty(exports, "from", {
|
|
179
|
+
enumerable: true,
|
|
180
|
+
get: function() {
|
|
181
|
+
return _xtandard_lib_policy_rebac.from;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
exports.fromAuthorization = fromAuthorization;
|
|
185
|
+
Object.defineProperty(exports, "intersection", {
|
|
186
|
+
enumerable: true,
|
|
187
|
+
get: function() {
|
|
188
|
+
return _xtandard_lib_policy_rebac.intersection;
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
Object.defineProperty(exports, "union", {
|
|
192
|
+
enumerable: true,
|
|
193
|
+
get: function() {
|
|
194
|
+
return _xtandard_lib_policy_rebac.union;
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
//# sourceMappingURL=catalog.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog.cjs","names":[],"sources":["../src/catalog.ts"],"sourcesContent":["/**\n * The catalog: roles, assignable permissions and per-role rules, declared once\n * in code and shared by the server, the browser and the admin panel (ADR 0002).\n *\n * `definePermissions()` wraps `defineAuthorization` from\n * `@xtandard/lib/policy/permix` (which owns role parsing, the assignable list,\n * the sync-only rule helpers, `createAbility` and `explain`) and adds what the\n * product needs on top: `managerRoles`, labels for the panel, the\n * `directGrants` switch and `effectiveRole()`. The rule user is fixed to\n * {@link PermissionsSubject}, whose `role` the engine sets to the subject's\n * effective role.\n *\n * Depends only on `@xtandard/lib/policy/permix` (and its `permix` peer), so\n * browsers and edge runtimes can import it.\n *\n * @example\n * ```ts\n * import { definePermissions } from \"@xtandard/permissions/catalog\";\n *\n * type Definition = { post: [\"read\", { name: \"publish\"; type: Post }, \"delete\"] };\n *\n * export const permissions = definePermissions<Definition>()({\n * roles: [\"guest\", \"member\", \"editor\", \"admin\"], // least → most privileged\n * guestRole: \"guest\",\n * defaultRole: \"member\",\n * managerRoles: [\"admin\"],\n * assignable: [\"post:moderate\", \"billing:refund\"],\n * labels: { roles: { admin: \"Administrator\" }, permissions: { \"post:moderate\": \"Moderate posts\" } },\n * rules: {\n * guest: () => ({ post: { read: true, publish: false, delete: false } }),\n * member: ({ user, permissions }, { or, fieldEquals, hasPermission }) => ({\n * post: {\n * read: true,\n * publish: or<Post>(fieldEquals(user!.id, { field: \"authorId\" }), () =>\n * hasPermission(permissions, \"post:moderate\"),\n * ),\n * delete: false,\n * },\n * }),\n * editor: () => ({ post: { read: true, publish: true, delete: false } }),\n * admin: () => ({ post: { read: true, publish: true, delete: true } }),\n * },\n * });\n * ```\n *\n * @module catalog\n */\n\nimport {\n defineAuthorization,\n defineRelations,\n type Authorization,\n type AuthorizationConfig,\n type Definition,\n type RelationSchema,\n} from \"./policy.ts\";\n\n/** A catalog without relations (ReBAC off): no object type, so no relation query type-checks. */\nexport type NoRelations = Record<never, never>;\n\nexport {\n computed,\n defineRelations,\n direct,\n exclusion,\n from,\n intersection,\n RelationDepthError,\n RelationSchemaError,\n union,\n} from \"./policy.ts\";\nexport type {\n ObjectRef,\n RelationExpr,\n RelationSchema,\n RelationTuple,\n SubjectRef,\n} from \"./policy.ts\";\n\n/** The user the rules see: the engine passes the subject with its effective `role`. */\nexport interface PermissionsSubject {\n /** The subject id (a user id in 0.1). */\n id: string;\n /** The effective role (stored assignment, else the auth provider's, else `defaultRole`). */\n role: string;\n name?: string;\n email?: string;\n /** Extra facts from the auth provider (claims, profile fields). */\n attributes?: Record<string, unknown>;\n}\n\n/**\n * Who a check is for, as an auth provider reports it. `role` may be one role, several (the most\n * privileged known one wins, see {@link PermissionsCatalog.effectiveRole}) or missing\n * (`defaultRole`); a stored role assignment overrides it.\n */\nexport interface SubjectIdentity {\n id: string;\n role?: string | readonly string[] | null;\n name?: string;\n email?: string;\n attributes?: Record<string, unknown>;\n}\n\n/** A human label: a string, or a label with a description. */\nexport type LabelInput = string | { label: string; description?: string };\n\n/** A normalized label. */\nexport interface Label {\n label: string;\n description?: string;\n}\n\n/** What `definePermissions` adds on top of the lib's `AuthorizationConfig`. */\nexport interface CatalogMeta<\n R extends string,\n P extends string,\n S extends RelationSchema = NoRelations,\n> {\n /**\n * The relation schema (ReBAC, ADR 0013), built with `defineRelations` and the `direct` /\n * `computed` / `from` / `union` / `intersection` / `exclusion` builders. Validated again here.\n * Without it, relationship writes and checks throw `RelationSchemaError`.\n */\n relations?: S;\n /**\n * Roles that may grant / revoke / assign roles by default (an auth provider can override it).\n * Default: the most privileged role (the last of `roles`).\n */\n managerRoles?: readonly R[];\n /** Labels and descriptions for the admin panel. Missing entries default to the key. */\n labels?: {\n roles?: Partial<Record<R, LabelInput>>;\n permissions?: Partial<Record<P, LabelInput>>;\n };\n /**\n * When `true`, a non-expired grant whose name equals the checked path allows it outright\n * (`reason: \"grant\"`), for catalogs whose assignable names are paths. Default `false`: grants\n * only matter through the rules (ADR 0005).\n */\n directGrants?: boolean;\n}\n\n/**\n * The configuration of {@link definePermissions}: the lib's `AuthorizationConfig` (roles ordered\n * **least → most privileged**, `guestRole`, `defaultRole`, `assignable`, `rules`) without\n * `getRole` (the engine sets the role), plus {@link CatalogMeta}.\n */\nexport type PermissionsConfig<\n D extends Definition,\n R extends string,\n P extends string,\n X,\n S extends RelationSchema = NoRelations,\n> = Omit<AuthorizationConfig<D, R, P, PermissionsSubject, X>, \"getRole\"> &\n CatalogMeta<NoInfer<R>, NoInfer<P>, S>;\n\n/** A catalog: the wrapped lib `Authorization` plus the product metadata. */\nexport interface PermissionsCatalog<\n D extends Definition = Definition,\n R extends string = string,\n P extends string = string,\n X = undefined,\n S extends RelationSchema = RelationSchema,\n> {\n /** The lib authorization (`createAbility`, `createRules`, `parseRole`, …). */\n readonly authorization: Authorization<D, R, P, PermissionsSubject, X>;\n /** Every role, least → most privileged. */\n readonly roles: readonly R[];\n /** The role of a request without a subject. */\n readonly guestRole: R;\n /** The role of a signed-in subject whose role is missing or unknown. */\n readonly defaultRole: R;\n /** The assignable permissions. */\n readonly assignable: readonly P[];\n /** Roles that may manage permissions by default. */\n readonly managerRoles: readonly R[];\n /** Labels for every role and permission (defaulting to the key). */\n readonly labels: {\n readonly roles: Readonly<Record<R, Label>>;\n readonly permissions: Readonly<Record<P, Label>>;\n };\n /** See {@link CatalogMeta.directGrants}. */\n readonly directGrants: boolean;\n /** The relation schema (ReBAC), `null` when the catalog declares none. */\n readonly relations: S | null;\n /** True when `value` is one of `roles`. */\n isRole(value: unknown): value is R;\n /** True when `value` is one of `assignable`. */\n isAssignable(value: unknown): value is P;\n /** True when `role` is one of `managerRoles`. */\n isManagerRole(role: unknown): boolean;\n /** A signed-in subject's role: `value` when it is a known non-guest role, else `defaultRole`. */\n parseRole(value: unknown): R;\n /**\n * The effective role of a signed-in subject from what an auth provider reports: one role, several\n * (an array, or a comma-separated string as better-auth's admin plugin stores them) → the most\n * privileged known one; none known (or missing) → `defaultRole`. Never the guest role.\n */\n effectiveRole(raw: string | readonly string[] | null | undefined): R;\n}\n\n/**\n * The permission type of a catalog: the `assignable` union, or `string` for a roles-only catalog\n * (`assignable: []` infers `never`, which no generic `AnyPermissionsCatalog` slot accepts because\n * rules are contravariant in it). Every permission is still rejected at runtime there.\n */\nexport type PermissionUnion<P extends string> = [P] extends [never] ? string : P;\n\n/** Any catalog (for generic constraints). */\n// oxlint-disable-next-line typescript/no-explicit-any -- variance: rules are contravariant in the role type.\nexport type AnyPermissionsCatalog = PermissionsCatalog<any, any, any, any, any>;\n\n/** The permix definition of a catalog. */\n// oxlint-disable-next-line typescript/no-explicit-any -- variance, see AnyPermissionsCatalog.\nexport type DefinitionOf<C> = C extends PermissionsCatalog<infer D, any, any, any, any> ? D : never;\n/** The role union of a catalog. */\nexport type RoleOf<C> = C extends { readonly roles: readonly (infer R)[] } ? R : never;\n/** The assignable-permission union of a catalog. */\nexport type PermissionOf<C> = C extends { readonly assignable: readonly (infer P)[] } ? P : never;\n/** The rule context (`ctx`) type of a catalog. */\n// oxlint-disable-next-line typescript/no-explicit-any -- variance, see AnyPermissionsCatalog.\nexport type CtxOf<C> = C extends PermissionsCatalog<any, any, any, infer X, any> ? X : never;\n/** The relation schema of a catalog. */\n// oxlint-disable-next-line typescript/no-explicit-any -- variance, see AnyPermissionsCatalog.\nexport type RelationsOf<C> = C extends PermissionsCatalog<any, any, any, any, infer S> ? S : never;\n\nconst normalizeLabel = (key: string, input: LabelInput | undefined): Label =>\n input === undefined\n ? { label: key }\n : typeof input === \"string\"\n ? { label: input }\n : { label: input.label, ...(input.description ? { description: input.description } : {}) };\n\nfunction labelMap<K extends string>(\n keys: readonly K[],\n inputs: Partial<Record<K, LabelInput>> | undefined,\n): Record<K, Label> {\n const out = Object.create(null) as Record<K, Label>;\n for (const key of keys) {\n out[key] = normalizeLabel(key, inputs && Object.hasOwn(inputs, key) ? inputs[key] : undefined);\n }\n return out;\n}\n\nfunction buildCatalog<\n D extends Definition,\n R extends string,\n P extends string,\n X,\n S extends RelationSchema,\n>(\n authorization: Authorization<D, R, P, PermissionsSubject, X>,\n meta: CatalogMeta<R, P, S>,\n): PermissionsCatalog<D, R, P, X, S> {\n const { roles, guestRole, defaultRole, assignable } = authorization.config;\n if (roles.length === 0) throw new Error(\"definePermissions: roles must not be empty\");\n const managerRoles = meta.managerRoles ?? [roles[roles.length - 1]!];\n for (const role of managerRoles) {\n if (!authorization.isRole(role)) {\n throw new Error(`definePermissions: managerRoles \"${role}\" is not in roles`);\n }\n if (role === guestRole) {\n throw new Error(`definePermissions: the guest role \"${role}\" can't be a manager role`);\n }\n }\n const managers = new Set<string>(managerRoles);\n const rank = new Map<string, number>(roles.map((role, index) => [role, index]));\n\n const effectiveRole = (raw: string | readonly string[] | null | undefined): R => {\n const candidates =\n typeof raw === \"string\" ? raw.split(\",\").map((s) => s.trim()) : Array.isArray(raw) ? raw : [];\n let best: R | undefined;\n for (const candidate of candidates) {\n // parseRole maps unknown / guest values to defaultRole; only keep real matches.\n if (!authorization.isRole(candidate) || candidate === guestRole) continue;\n if (best === undefined || rank.get(candidate)! > rank.get(best)!) best = candidate;\n }\n return best ?? authorization.parseRole(undefined);\n };\n\n return Object.freeze({\n authorization,\n roles,\n guestRole,\n defaultRole,\n assignable,\n managerRoles: Object.freeze([...managerRoles]),\n labels: Object.freeze({\n roles: Object.freeze(labelMap(roles, meta.labels?.roles)),\n permissions: Object.freeze(labelMap(assignable, meta.labels?.permissions)),\n }),\n directGrants: meta.directGrants ?? false,\n relations: meta.relations ? defineRelations(meta.relations) : null,\n isRole: authorization.isRole,\n isAssignable: authorization.isAssignable,\n isManagerRole: (role: unknown) => typeof role === \"string\" && managers.has(role),\n parseRole: authorization.parseRole,\n effectiveRole,\n });\n}\n\n/**\n * Define the catalog. Curried like the lib's `defineAuthorization`: the permix definition and the\n * rule context type go in the first call, the roles and permissions are inferred by the second:\n * `definePermissions<Definition, Ctx>()({ ... })`.\n *\n * Rules are synchronous (a rule that returns a Promise denies and throws `AsyncRuleError`); load\n * what they need first and pass it as `ctx` or as the checked data. A rule wrapped in the lib's\n * `asyncRule()` is evaluated only by the engine's async `check()` / `can()`, never by the\n * synchronous `ability()` (ADR 0005).\n *\n * @throws {Error} When `guestRole`, `defaultRole` or a `managerRoles` entry is not in `roles`, a\n * role has no rules, or the guest role is a manager role.\n */\nexport function definePermissions<D extends Definition, X = undefined>() {\n return <\n const R extends string,\n const P extends string,\n const S extends RelationSchema = NoRelations,\n >(\n config: PermissionsConfig<D, R, P, X, S>,\n ): PermissionsCatalog<D, R, PermissionUnion<P>, X, S> => {\n const { managerRoles, labels, directGrants, relations, ...authorizationConfig } = config;\n const authorization = defineAuthorization<D, PermissionsSubject, X>()<R, PermissionUnion<P>>({\n ...authorizationConfig,\n getRole: (subject: PermissionsSubject) => subject.role,\n } as unknown as AuthorizationConfig<D, R, PermissionUnion<P>, PermissionsSubject, X>);\n return buildCatalog<D, R, PermissionUnion<P>, X, S>(authorization, {\n ...(managerRoles ? { managerRoles } : {}),\n ...(labels ? { labels: labels as CatalogMeta<R, PermissionUnion<P>>[\"labels\"] } : {}),\n ...(directGrants !== undefined ? { directGrants } : {}),\n ...(relations ? { relations } : {}),\n });\n };\n}\n\n/**\n * Wrap an existing lib `Authorization` (from `defineAuthorization`) as a catalog. Its config is\n * re-defined with `getRole: (subject) => subject.role`, so its rules receive a\n * {@link PermissionsSubject} (`{ id, role, name?, email?, attributes? }`) as `user`; rules that read\n * other user fields must move them into `attributes` or `ctx`.\n */\nexport function fromAuthorization<\n D extends Definition,\n R extends string,\n P extends string,\n X,\n const S extends RelationSchema = NoRelations,\n>(\n // oxlint-disable-next-line typescript/no-explicit-any -- any user type: the rules are re-bound to PermissionsSubject.\n authorization: Authorization<D, R, P, any, X>,\n meta: CatalogMeta<NoInfer<R>, NoInfer<P>, S> = {},\n): PermissionsCatalog<D, R, P, X, S> {\n const { getRole: _ignored, ...config } = authorization.config;\n const rebound = defineAuthorization<D, PermissionsSubject, X>()<R, P>({\n ...(config as Omit<AuthorizationConfig<D, R, P, PermissionsSubject, X>, \"getRole\">),\n getRole: (subject) => subject.role,\n });\n return buildCatalog(rebound, meta);\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { A as intersection, C as computed, D as from, E as exclusion, M as union, T as direct, b as SubjectRef, c as ObjectRef, f as RelationExpr, h as RelationTuple, m as RelationSchemaError, p as RelationSchema, u as RelationDepthError, w as defineRelations } from "./policy-DFq_SYR3.cjs";
|
|
2
|
+
import { _ as fromAuthorization, a as Label, c as PermissionOf, d as PermissionsConfig, f as PermissionsSubject, g as definePermissions, h as SubjectIdentity, i as DefinitionOf, l as PermissionUnion, m as RoleOf, n as CatalogMeta, o as LabelInput, p as RelationsOf, r as CtxOf, s as NoRelations, t as AnyPermissionsCatalog, u as PermissionsCatalog } from "./catalog-DA_d1SdC.cjs";
|
|
3
|
+
export { AnyPermissionsCatalog, CatalogMeta, CtxOf, DefinitionOf, Label, LabelInput, NoRelations, type ObjectRef, PermissionOf, PermissionUnion, PermissionsCatalog, PermissionsConfig, PermissionsSubject, RelationDepthError, type RelationExpr, type RelationSchema, RelationSchemaError, type RelationTuple, RelationsOf, RoleOf, SubjectIdentity, type SubjectRef, computed, definePermissions, defineRelations, direct, exclusion, from, fromAuthorization, intersection, union };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { A as intersection, C as computed, D as from, E as exclusion, M as union, T as direct, b as SubjectRef, c as ObjectRef, f as RelationExpr, h as RelationTuple, m as RelationSchemaError, p as RelationSchema, u as RelationDepthError, w as defineRelations } from "./policy-CGA6QD0a.mjs";
|
|
2
|
+
import { _ as fromAuthorization, a as Label, c as PermissionOf, d as PermissionsConfig, f as PermissionsSubject, g as definePermissions, h as SubjectIdentity, i as DefinitionOf, l as PermissionUnion, m as RoleOf, n as CatalogMeta, o as LabelInput, p as RelationsOf, r as CtxOf, s as NoRelations, t as AnyPermissionsCatalog, u as PermissionsCatalog } from "./catalog-DJcJ3ycq.mjs";
|
|
3
|
+
export { AnyPermissionsCatalog, CatalogMeta, CtxOf, DefinitionOf, Label, LabelInput, NoRelations, type ObjectRef, PermissionOf, PermissionUnion, PermissionsCatalog, PermissionsConfig, PermissionsSubject, RelationDepthError, type RelationExpr, type RelationSchema, RelationSchemaError, type RelationTuple, RelationsOf, RoleOf, SubjectIdentity, type SubjectRef, computed, definePermissions, defineRelations, direct, exclusion, from, fromAuthorization, intersection, union };
|
package/dist/catalog.mjs
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { a as computed, b as union, c as defineAuthorization, d as exclusion, f as from, g as intersection, l as defineRelations, n as RelationDepthError, r as RelationSchemaError, u as direct } from "./policy-BQmlo2lu.mjs";
|
|
2
|
+
//#region src/catalog.ts
|
|
3
|
+
/**
|
|
4
|
+
* The catalog: roles, assignable permissions and per-role rules, declared once
|
|
5
|
+
* in code and shared by the server, the browser and the admin panel (ADR 0002).
|
|
6
|
+
*
|
|
7
|
+
* `definePermissions()` wraps `defineAuthorization` from
|
|
8
|
+
* `@xtandard/lib/policy/permix` (which owns role parsing, the assignable list,
|
|
9
|
+
* the sync-only rule helpers, `createAbility` and `explain`) and adds what the
|
|
10
|
+
* product needs on top: `managerRoles`, labels for the panel, the
|
|
11
|
+
* `directGrants` switch and `effectiveRole()`. The rule user is fixed to
|
|
12
|
+
* {@link PermissionsSubject}, whose `role` the engine sets to the subject's
|
|
13
|
+
* effective role.
|
|
14
|
+
*
|
|
15
|
+
* Depends only on `@xtandard/lib/policy/permix` (and its `permix` peer), so
|
|
16
|
+
* browsers and edge runtimes can import it.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { definePermissions } from "@xtandard/permissions/catalog";
|
|
21
|
+
*
|
|
22
|
+
* type Definition = { post: ["read", { name: "publish"; type: Post }, "delete"] };
|
|
23
|
+
*
|
|
24
|
+
* export const permissions = definePermissions<Definition>()({
|
|
25
|
+
* roles: ["guest", "member", "editor", "admin"], // least → most privileged
|
|
26
|
+
* guestRole: "guest",
|
|
27
|
+
* defaultRole: "member",
|
|
28
|
+
* managerRoles: ["admin"],
|
|
29
|
+
* assignable: ["post:moderate", "billing:refund"],
|
|
30
|
+
* labels: { roles: { admin: "Administrator" }, permissions: { "post:moderate": "Moderate posts" } },
|
|
31
|
+
* rules: {
|
|
32
|
+
* guest: () => ({ post: { read: true, publish: false, delete: false } }),
|
|
33
|
+
* member: ({ user, permissions }, { or, fieldEquals, hasPermission }) => ({
|
|
34
|
+
* post: {
|
|
35
|
+
* read: true,
|
|
36
|
+
* publish: or<Post>(fieldEquals(user!.id, { field: "authorId" }), () =>
|
|
37
|
+
* hasPermission(permissions, "post:moderate"),
|
|
38
|
+
* ),
|
|
39
|
+
* delete: false,
|
|
40
|
+
* },
|
|
41
|
+
* }),
|
|
42
|
+
* editor: () => ({ post: { read: true, publish: true, delete: false } }),
|
|
43
|
+
* admin: () => ({ post: { read: true, publish: true, delete: true } }),
|
|
44
|
+
* },
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @module catalog
|
|
49
|
+
*/
|
|
50
|
+
const normalizeLabel = (key, input) => input === void 0 ? { label: key } : typeof input === "string" ? { label: input } : {
|
|
51
|
+
label: input.label,
|
|
52
|
+
...input.description ? { description: input.description } : {}
|
|
53
|
+
};
|
|
54
|
+
function labelMap(keys, inputs) {
|
|
55
|
+
const out = Object.create(null);
|
|
56
|
+
for (const key of keys) out[key] = normalizeLabel(key, inputs && Object.hasOwn(inputs, key) ? inputs[key] : void 0);
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
function buildCatalog(authorization, meta) {
|
|
60
|
+
const { roles, guestRole, defaultRole, assignable } = authorization.config;
|
|
61
|
+
if (roles.length === 0) throw new Error("definePermissions: roles must not be empty");
|
|
62
|
+
const managerRoles = meta.managerRoles ?? [roles[roles.length - 1]];
|
|
63
|
+
for (const role of managerRoles) {
|
|
64
|
+
if (!authorization.isRole(role)) throw new Error(`definePermissions: managerRoles "${role}" is not in roles`);
|
|
65
|
+
if (role === guestRole) throw new Error(`definePermissions: the guest role "${role}" can't be a manager role`);
|
|
66
|
+
}
|
|
67
|
+
const managers = new Set(managerRoles);
|
|
68
|
+
const rank = new Map(roles.map((role, index) => [role, index]));
|
|
69
|
+
const effectiveRole = (raw) => {
|
|
70
|
+
const candidates = typeof raw === "string" ? raw.split(",").map((s) => s.trim()) : Array.isArray(raw) ? raw : [];
|
|
71
|
+
let best;
|
|
72
|
+
for (const candidate of candidates) {
|
|
73
|
+
if (!authorization.isRole(candidate) || candidate === guestRole) continue;
|
|
74
|
+
if (best === void 0 || rank.get(candidate) > rank.get(best)) best = candidate;
|
|
75
|
+
}
|
|
76
|
+
return best ?? authorization.parseRole(void 0);
|
|
77
|
+
};
|
|
78
|
+
return Object.freeze({
|
|
79
|
+
authorization,
|
|
80
|
+
roles,
|
|
81
|
+
guestRole,
|
|
82
|
+
defaultRole,
|
|
83
|
+
assignable,
|
|
84
|
+
managerRoles: Object.freeze([...managerRoles]),
|
|
85
|
+
labels: Object.freeze({
|
|
86
|
+
roles: Object.freeze(labelMap(roles, meta.labels?.roles)),
|
|
87
|
+
permissions: Object.freeze(labelMap(assignable, meta.labels?.permissions))
|
|
88
|
+
}),
|
|
89
|
+
directGrants: meta.directGrants ?? false,
|
|
90
|
+
relations: meta.relations ? defineRelations(meta.relations) : null,
|
|
91
|
+
isRole: authorization.isRole,
|
|
92
|
+
isAssignable: authorization.isAssignable,
|
|
93
|
+
isManagerRole: (role) => typeof role === "string" && managers.has(role),
|
|
94
|
+
parseRole: authorization.parseRole,
|
|
95
|
+
effectiveRole
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Define the catalog. Curried like the lib's `defineAuthorization`: the permix definition and the
|
|
100
|
+
* rule context type go in the first call, the roles and permissions are inferred by the second:
|
|
101
|
+
* `definePermissions<Definition, Ctx>()({ ... })`.
|
|
102
|
+
*
|
|
103
|
+
* Rules are synchronous (a rule that returns a Promise denies and throws `AsyncRuleError`); load
|
|
104
|
+
* what they need first and pass it as `ctx` or as the checked data. A rule wrapped in the lib's
|
|
105
|
+
* `asyncRule()` is evaluated only by the engine's async `check()` / `can()`, never by the
|
|
106
|
+
* synchronous `ability()` (ADR 0005).
|
|
107
|
+
*
|
|
108
|
+
* @throws {Error} When `guestRole`, `defaultRole` or a `managerRoles` entry is not in `roles`, a
|
|
109
|
+
* role has no rules, or the guest role is a manager role.
|
|
110
|
+
*/
|
|
111
|
+
function definePermissions() {
|
|
112
|
+
return (config) => {
|
|
113
|
+
const { managerRoles, labels, directGrants, relations, ...authorizationConfig } = config;
|
|
114
|
+
return buildCatalog(defineAuthorization()({
|
|
115
|
+
...authorizationConfig,
|
|
116
|
+
getRole: (subject) => subject.role
|
|
117
|
+
}), {
|
|
118
|
+
...managerRoles ? { managerRoles } : {},
|
|
119
|
+
...labels ? { labels } : {},
|
|
120
|
+
...directGrants !== void 0 ? { directGrants } : {},
|
|
121
|
+
...relations ? { relations } : {}
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Wrap an existing lib `Authorization` (from `defineAuthorization`) as a catalog. Its config is
|
|
127
|
+
* re-defined with `getRole: (subject) => subject.role`, so its rules receive a
|
|
128
|
+
* {@link PermissionsSubject} (`{ id, role, name?, email?, attributes? }`) as `user`; rules that read
|
|
129
|
+
* other user fields must move them into `attributes` or `ctx`.
|
|
130
|
+
*/
|
|
131
|
+
function fromAuthorization(authorization, meta = {}) {
|
|
132
|
+
const { getRole: _ignored, ...config } = authorization.config;
|
|
133
|
+
return buildCatalog(defineAuthorization()({
|
|
134
|
+
...config,
|
|
135
|
+
getRole: (subject) => subject.role
|
|
136
|
+
}), meta);
|
|
137
|
+
}
|
|
138
|
+
//#endregion
|
|
139
|
+
export { RelationDepthError, RelationSchemaError, computed, definePermissions, defineRelations, direct, exclusion, from, fromAuthorization, intersection, union };
|
|
140
|
+
|
|
141
|
+
//# sourceMappingURL=catalog.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog.mjs","names":[],"sources":["../src/catalog.ts"],"sourcesContent":["/**\n * The catalog: roles, assignable permissions and per-role rules, declared once\n * in code and shared by the server, the browser and the admin panel (ADR 0002).\n *\n * `definePermissions()` wraps `defineAuthorization` from\n * `@xtandard/lib/policy/permix` (which owns role parsing, the assignable list,\n * the sync-only rule helpers, `createAbility` and `explain`) and adds what the\n * product needs on top: `managerRoles`, labels for the panel, the\n * `directGrants` switch and `effectiveRole()`. The rule user is fixed to\n * {@link PermissionsSubject}, whose `role` the engine sets to the subject's\n * effective role.\n *\n * Depends only on `@xtandard/lib/policy/permix` (and its `permix` peer), so\n * browsers and edge runtimes can import it.\n *\n * @example\n * ```ts\n * import { definePermissions } from \"@xtandard/permissions/catalog\";\n *\n * type Definition = { post: [\"read\", { name: \"publish\"; type: Post }, \"delete\"] };\n *\n * export const permissions = definePermissions<Definition>()({\n * roles: [\"guest\", \"member\", \"editor\", \"admin\"], // least → most privileged\n * guestRole: \"guest\",\n * defaultRole: \"member\",\n * managerRoles: [\"admin\"],\n * assignable: [\"post:moderate\", \"billing:refund\"],\n * labels: { roles: { admin: \"Administrator\" }, permissions: { \"post:moderate\": \"Moderate posts\" } },\n * rules: {\n * guest: () => ({ post: { read: true, publish: false, delete: false } }),\n * member: ({ user, permissions }, { or, fieldEquals, hasPermission }) => ({\n * post: {\n * read: true,\n * publish: or<Post>(fieldEquals(user!.id, { field: \"authorId\" }), () =>\n * hasPermission(permissions, \"post:moderate\"),\n * ),\n * delete: false,\n * },\n * }),\n * editor: () => ({ post: { read: true, publish: true, delete: false } }),\n * admin: () => ({ post: { read: true, publish: true, delete: true } }),\n * },\n * });\n * ```\n *\n * @module catalog\n */\n\nimport {\n defineAuthorization,\n defineRelations,\n type Authorization,\n type AuthorizationConfig,\n type Definition,\n type RelationSchema,\n} from \"./policy.ts\";\n\n/** A catalog without relations (ReBAC off): no object type, so no relation query type-checks. */\nexport type NoRelations = Record<never, never>;\n\nexport {\n computed,\n defineRelations,\n direct,\n exclusion,\n from,\n intersection,\n RelationDepthError,\n RelationSchemaError,\n union,\n} from \"./policy.ts\";\nexport type {\n ObjectRef,\n RelationExpr,\n RelationSchema,\n RelationTuple,\n SubjectRef,\n} from \"./policy.ts\";\n\n/** The user the rules see: the engine passes the subject with its effective `role`. */\nexport interface PermissionsSubject {\n /** The subject id (a user id in 0.1). */\n id: string;\n /** The effective role (stored assignment, else the auth provider's, else `defaultRole`). */\n role: string;\n name?: string;\n email?: string;\n /** Extra facts from the auth provider (claims, profile fields). */\n attributes?: Record<string, unknown>;\n}\n\n/**\n * Who a check is for, as an auth provider reports it. `role` may be one role, several (the most\n * privileged known one wins, see {@link PermissionsCatalog.effectiveRole}) or missing\n * (`defaultRole`); a stored role assignment overrides it.\n */\nexport interface SubjectIdentity {\n id: string;\n role?: string | readonly string[] | null;\n name?: string;\n email?: string;\n attributes?: Record<string, unknown>;\n}\n\n/** A human label: a string, or a label with a description. */\nexport type LabelInput = string | { label: string; description?: string };\n\n/** A normalized label. */\nexport interface Label {\n label: string;\n description?: string;\n}\n\n/** What `definePermissions` adds on top of the lib's `AuthorizationConfig`. */\nexport interface CatalogMeta<\n R extends string,\n P extends string,\n S extends RelationSchema = NoRelations,\n> {\n /**\n * The relation schema (ReBAC, ADR 0013), built with `defineRelations` and the `direct` /\n * `computed` / `from` / `union` / `intersection` / `exclusion` builders. Validated again here.\n * Without it, relationship writes and checks throw `RelationSchemaError`.\n */\n relations?: S;\n /**\n * Roles that may grant / revoke / assign roles by default (an auth provider can override it).\n * Default: the most privileged role (the last of `roles`).\n */\n managerRoles?: readonly R[];\n /** Labels and descriptions for the admin panel. Missing entries default to the key. */\n labels?: {\n roles?: Partial<Record<R, LabelInput>>;\n permissions?: Partial<Record<P, LabelInput>>;\n };\n /**\n * When `true`, a non-expired grant whose name equals the checked path allows it outright\n * (`reason: \"grant\"`), for catalogs whose assignable names are paths. Default `false`: grants\n * only matter through the rules (ADR 0005).\n */\n directGrants?: boolean;\n}\n\n/**\n * The configuration of {@link definePermissions}: the lib's `AuthorizationConfig` (roles ordered\n * **least → most privileged**, `guestRole`, `defaultRole`, `assignable`, `rules`) without\n * `getRole` (the engine sets the role), plus {@link CatalogMeta}.\n */\nexport type PermissionsConfig<\n D extends Definition,\n R extends string,\n P extends string,\n X,\n S extends RelationSchema = NoRelations,\n> = Omit<AuthorizationConfig<D, R, P, PermissionsSubject, X>, \"getRole\"> &\n CatalogMeta<NoInfer<R>, NoInfer<P>, S>;\n\n/** A catalog: the wrapped lib `Authorization` plus the product metadata. */\nexport interface PermissionsCatalog<\n D extends Definition = Definition,\n R extends string = string,\n P extends string = string,\n X = undefined,\n S extends RelationSchema = RelationSchema,\n> {\n /** The lib authorization (`createAbility`, `createRules`, `parseRole`, …). */\n readonly authorization: Authorization<D, R, P, PermissionsSubject, X>;\n /** Every role, least → most privileged. */\n readonly roles: readonly R[];\n /** The role of a request without a subject. */\n readonly guestRole: R;\n /** The role of a signed-in subject whose role is missing or unknown. */\n readonly defaultRole: R;\n /** The assignable permissions. */\n readonly assignable: readonly P[];\n /** Roles that may manage permissions by default. */\n readonly managerRoles: readonly R[];\n /** Labels for every role and permission (defaulting to the key). */\n readonly labels: {\n readonly roles: Readonly<Record<R, Label>>;\n readonly permissions: Readonly<Record<P, Label>>;\n };\n /** See {@link CatalogMeta.directGrants}. */\n readonly directGrants: boolean;\n /** The relation schema (ReBAC), `null` when the catalog declares none. */\n readonly relations: S | null;\n /** True when `value` is one of `roles`. */\n isRole(value: unknown): value is R;\n /** True when `value` is one of `assignable`. */\n isAssignable(value: unknown): value is P;\n /** True when `role` is one of `managerRoles`. */\n isManagerRole(role: unknown): boolean;\n /** A signed-in subject's role: `value` when it is a known non-guest role, else `defaultRole`. */\n parseRole(value: unknown): R;\n /**\n * The effective role of a signed-in subject from what an auth provider reports: one role, several\n * (an array, or a comma-separated string as better-auth's admin plugin stores them) → the most\n * privileged known one; none known (or missing) → `defaultRole`. Never the guest role.\n */\n effectiveRole(raw: string | readonly string[] | null | undefined): R;\n}\n\n/**\n * The permission type of a catalog: the `assignable` union, or `string` for a roles-only catalog\n * (`assignable: []` infers `never`, which no generic `AnyPermissionsCatalog` slot accepts because\n * rules are contravariant in it). Every permission is still rejected at runtime there.\n */\nexport type PermissionUnion<P extends string> = [P] extends [never] ? string : P;\n\n/** Any catalog (for generic constraints). */\n// oxlint-disable-next-line typescript/no-explicit-any -- variance: rules are contravariant in the role type.\nexport type AnyPermissionsCatalog = PermissionsCatalog<any, any, any, any, any>;\n\n/** The permix definition of a catalog. */\n// oxlint-disable-next-line typescript/no-explicit-any -- variance, see AnyPermissionsCatalog.\nexport type DefinitionOf<C> = C extends PermissionsCatalog<infer D, any, any, any, any> ? D : never;\n/** The role union of a catalog. */\nexport type RoleOf<C> = C extends { readonly roles: readonly (infer R)[] } ? R : never;\n/** The assignable-permission union of a catalog. */\nexport type PermissionOf<C> = C extends { readonly assignable: readonly (infer P)[] } ? P : never;\n/** The rule context (`ctx`) type of a catalog. */\n// oxlint-disable-next-line typescript/no-explicit-any -- variance, see AnyPermissionsCatalog.\nexport type CtxOf<C> = C extends PermissionsCatalog<any, any, any, infer X, any> ? X : never;\n/** The relation schema of a catalog. */\n// oxlint-disable-next-line typescript/no-explicit-any -- variance, see AnyPermissionsCatalog.\nexport type RelationsOf<C> = C extends PermissionsCatalog<any, any, any, any, infer S> ? S : never;\n\nconst normalizeLabel = (key: string, input: LabelInput | undefined): Label =>\n input === undefined\n ? { label: key }\n : typeof input === \"string\"\n ? { label: input }\n : { label: input.label, ...(input.description ? { description: input.description } : {}) };\n\nfunction labelMap<K extends string>(\n keys: readonly K[],\n inputs: Partial<Record<K, LabelInput>> | undefined,\n): Record<K, Label> {\n const out = Object.create(null) as Record<K, Label>;\n for (const key of keys) {\n out[key] = normalizeLabel(key, inputs && Object.hasOwn(inputs, key) ? inputs[key] : undefined);\n }\n return out;\n}\n\nfunction buildCatalog<\n D extends Definition,\n R extends string,\n P extends string,\n X,\n S extends RelationSchema,\n>(\n authorization: Authorization<D, R, P, PermissionsSubject, X>,\n meta: CatalogMeta<R, P, S>,\n): PermissionsCatalog<D, R, P, X, S> {\n const { roles, guestRole, defaultRole, assignable } = authorization.config;\n if (roles.length === 0) throw new Error(\"definePermissions: roles must not be empty\");\n const managerRoles = meta.managerRoles ?? [roles[roles.length - 1]!];\n for (const role of managerRoles) {\n if (!authorization.isRole(role)) {\n throw new Error(`definePermissions: managerRoles \"${role}\" is not in roles`);\n }\n if (role === guestRole) {\n throw new Error(`definePermissions: the guest role \"${role}\" can't be a manager role`);\n }\n }\n const managers = new Set<string>(managerRoles);\n const rank = new Map<string, number>(roles.map((role, index) => [role, index]));\n\n const effectiveRole = (raw: string | readonly string[] | null | undefined): R => {\n const candidates =\n typeof raw === \"string\" ? raw.split(\",\").map((s) => s.trim()) : Array.isArray(raw) ? raw : [];\n let best: R | undefined;\n for (const candidate of candidates) {\n // parseRole maps unknown / guest values to defaultRole; only keep real matches.\n if (!authorization.isRole(candidate) || candidate === guestRole) continue;\n if (best === undefined || rank.get(candidate)! > rank.get(best)!) best = candidate;\n }\n return best ?? authorization.parseRole(undefined);\n };\n\n return Object.freeze({\n authorization,\n roles,\n guestRole,\n defaultRole,\n assignable,\n managerRoles: Object.freeze([...managerRoles]),\n labels: Object.freeze({\n roles: Object.freeze(labelMap(roles, meta.labels?.roles)),\n permissions: Object.freeze(labelMap(assignable, meta.labels?.permissions)),\n }),\n directGrants: meta.directGrants ?? false,\n relations: meta.relations ? defineRelations(meta.relations) : null,\n isRole: authorization.isRole,\n isAssignable: authorization.isAssignable,\n isManagerRole: (role: unknown) => typeof role === \"string\" && managers.has(role),\n parseRole: authorization.parseRole,\n effectiveRole,\n });\n}\n\n/**\n * Define the catalog. Curried like the lib's `defineAuthorization`: the permix definition and the\n * rule context type go in the first call, the roles and permissions are inferred by the second:\n * `definePermissions<Definition, Ctx>()({ ... })`.\n *\n * Rules are synchronous (a rule that returns a Promise denies and throws `AsyncRuleError`); load\n * what they need first and pass it as `ctx` or as the checked data. A rule wrapped in the lib's\n * `asyncRule()` is evaluated only by the engine's async `check()` / `can()`, never by the\n * synchronous `ability()` (ADR 0005).\n *\n * @throws {Error} When `guestRole`, `defaultRole` or a `managerRoles` entry is not in `roles`, a\n * role has no rules, or the guest role is a manager role.\n */\nexport function definePermissions<D extends Definition, X = undefined>() {\n return <\n const R extends string,\n const P extends string,\n const S extends RelationSchema = NoRelations,\n >(\n config: PermissionsConfig<D, R, P, X, S>,\n ): PermissionsCatalog<D, R, PermissionUnion<P>, X, S> => {\n const { managerRoles, labels, directGrants, relations, ...authorizationConfig } = config;\n const authorization = defineAuthorization<D, PermissionsSubject, X>()<R, PermissionUnion<P>>({\n ...authorizationConfig,\n getRole: (subject: PermissionsSubject) => subject.role,\n } as unknown as AuthorizationConfig<D, R, PermissionUnion<P>, PermissionsSubject, X>);\n return buildCatalog<D, R, PermissionUnion<P>, X, S>(authorization, {\n ...(managerRoles ? { managerRoles } : {}),\n ...(labels ? { labels: labels as CatalogMeta<R, PermissionUnion<P>>[\"labels\"] } : {}),\n ...(directGrants !== undefined ? { directGrants } : {}),\n ...(relations ? { relations } : {}),\n });\n };\n}\n\n/**\n * Wrap an existing lib `Authorization` (from `defineAuthorization`) as a catalog. Its config is\n * re-defined with `getRole: (subject) => subject.role`, so its rules receive a\n * {@link PermissionsSubject} (`{ id, role, name?, email?, attributes? }`) as `user`; rules that read\n * other user fields must move them into `attributes` or `ctx`.\n */\nexport function fromAuthorization<\n D extends Definition,\n R extends string,\n P extends string,\n X,\n const S extends RelationSchema = NoRelations,\n>(\n // oxlint-disable-next-line typescript/no-explicit-any -- any user type: the rules are re-bound to PermissionsSubject.\n authorization: Authorization<D, R, P, any, X>,\n meta: CatalogMeta<NoInfer<R>, NoInfer<P>, S> = {},\n): PermissionsCatalog<D, R, P, X, S> {\n const { getRole: _ignored, ...config } = authorization.config;\n const rebound = defineAuthorization<D, PermissionsSubject, X>()<R, P>({\n ...(config as Omit<AuthorizationConfig<D, R, P, PermissionsSubject, X>, \"getRole\">),\n getRole: (subject) => subject.role,\n });\n return buildCatalog(rebound, meta);\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __exportAll = (all, no_symbols) => {
|
|
9
|
+
let target = {};
|
|
10
|
+
for (var name in all) __defProp(target, name, {
|
|
11
|
+
get: all[name],
|
|
12
|
+
enumerable: true
|
|
13
|
+
});
|
|
14
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
15
|
+
return target;
|
|
16
|
+
};
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
19
|
+
key = keys[i];
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
21
|
+
get: ((k) => from[k]).bind(null, key),
|
|
22
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
+
value: mod,
|
|
29
|
+
enumerable: true
|
|
30
|
+
}) : target, mod));
|
|
31
|
+
//#endregion
|
|
32
|
+
Object.defineProperty(exports, "__exportAll", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function() {
|
|
35
|
+
return __exportAll;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(exports, "__toESM", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function() {
|
|
41
|
+
return __toESM;
|
|
42
|
+
}
|
|
43
|
+
});
|