convex-cms 0.0.1
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/cli/commands/admin.d.ts +16 -0
- package/dist/cli/commands/admin.d.ts.map +1 -0
- package/dist/cli/commands/admin.js +88 -0
- package/dist/cli/commands/admin.js.map +1 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +18 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/utils/detectConvexUrl.d.ts +13 -0
- package/dist/cli/utils/detectConvexUrl.d.ts.map +1 -0
- package/dist/cli/utils/detectConvexUrl.js +48 -0
- package/dist/cli/utils/detectConvexUrl.js.map +1 -0
- package/dist/cli/utils/openBrowser.d.ts +7 -0
- package/dist/cli/utils/openBrowser.d.ts.map +1 -0
- package/dist/cli/utils/openBrowser.js +17 -0
- package/dist/cli/utils/openBrowser.js.map +1 -0
- package/dist/client/admin-config.d.ts +126 -0
- package/dist/client/admin-config.d.ts.map +1 -0
- package/dist/client/admin-config.js +117 -0
- package/dist/client/admin-config.js.map +1 -0
- package/dist/client/adminApi.d.ts +2273 -0
- package/dist/client/adminApi.d.ts.map +1 -0
- package/dist/client/adminApi.js +716 -0
- package/dist/client/adminApi.js.map +1 -0
- package/dist/client/agentTools.d.ts +933 -0
- package/dist/client/agentTools.d.ts.map +1 -0
- package/dist/client/agentTools.js +1004 -0
- package/dist/client/agentTools.js.map +1 -0
- package/dist/client/argTypes.d.ts +212 -0
- package/dist/client/argTypes.d.ts.map +1 -0
- package/dist/client/argTypes.js +5 -0
- package/dist/client/argTypes.js.map +1 -0
- package/dist/client/field-types.d.ts +55 -0
- package/dist/client/field-types.d.ts.map +1 -0
- package/dist/client/field-types.js +152 -0
- package/dist/client/field-types.js.map +1 -0
- package/dist/client/index.d.ts +189 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +668 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/queryBuilder.d.ts +765 -0
- package/dist/client/queryBuilder.d.ts.map +1 -0
- package/dist/client/queryBuilder.js +970 -0
- package/dist/client/queryBuilder.js.map +1 -0
- package/dist/client/schema/codegen.d.ts +128 -0
- package/dist/client/schema/codegen.d.ts.map +1 -0
- package/dist/client/schema/codegen.js +318 -0
- package/dist/client/schema/codegen.js.map +1 -0
- package/dist/client/schema/defineContentType.d.ts +221 -0
- package/dist/client/schema/defineContentType.d.ts.map +1 -0
- package/dist/client/schema/defineContentType.js +380 -0
- package/dist/client/schema/defineContentType.js.map +1 -0
- package/dist/client/schema/index.d.ts +85 -0
- package/dist/client/schema/index.d.ts.map +1 -0
- package/dist/client/schema/index.js +92 -0
- package/dist/client/schema/index.js.map +1 -0
- package/dist/client/schema/schemaDrift.d.ts +199 -0
- package/dist/client/schema/schemaDrift.d.ts.map +1 -0
- package/dist/client/schema/schemaDrift.js +340 -0
- package/dist/client/schema/schemaDrift.js.map +1 -0
- package/dist/client/schema/typedClient.d.ts +401 -0
- package/dist/client/schema/typedClient.d.ts.map +1 -0
- package/dist/client/schema/typedClient.js +269 -0
- package/dist/client/schema/typedClient.js.map +1 -0
- package/dist/client/schema/types.d.ts +477 -0
- package/dist/client/schema/types.d.ts.map +1 -0
- package/dist/client/schema/types.js +39 -0
- package/dist/client/schema/types.js.map +1 -0
- package/dist/client/types.d.ts +449 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +149 -0
- package/dist/client/types.js.map +1 -0
- package/dist/client/workflows.d.ts +51 -0
- package/dist/client/workflows.d.ts.map +1 -0
- package/dist/client/workflows.js +103 -0
- package/dist/client/workflows.js.map +1 -0
- package/dist/client/wrapper.d.ts +2198 -0
- package/dist/client/wrapper.d.ts.map +1 -0
- package/dist/client/wrapper.js +2651 -0
- package/dist/client/wrapper.js.map +1 -0
- package/dist/component/_generated/api.d.ts +124 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +4321 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/auditLog.d.ts +410 -0
- package/dist/component/auditLog.d.ts.map +1 -0
- package/dist/component/auditLog.js +607 -0
- package/dist/component/auditLog.js.map +1 -0
- package/dist/component/authorization.d.ts +323 -0
- package/dist/component/authorization.d.ts.map +1 -0
- package/dist/component/authorization.js +464 -0
- package/dist/component/authorization.js.map +1 -0
- package/dist/component/authorizationHooks.d.ts +184 -0
- package/dist/component/authorizationHooks.d.ts.map +1 -0
- package/dist/component/authorizationHooks.js +521 -0
- package/dist/component/authorizationHooks.js.map +1 -0
- package/dist/component/bulkOperations.d.ts +200 -0
- package/dist/component/bulkOperations.d.ts.map +1 -0
- package/dist/component/bulkOperations.js +568 -0
- package/dist/component/bulkOperations.js.map +1 -0
- package/dist/component/contentEntries.d.ts +719 -0
- package/dist/component/contentEntries.d.ts.map +1 -0
- package/dist/component/contentEntries.js +1617 -0
- package/dist/component/contentEntries.js.map +1 -0
- package/dist/component/contentEntryMutations.d.ts +505 -0
- package/dist/component/contentEntryMutations.d.ts.map +1 -0
- package/dist/component/contentEntryMutations.js +1009 -0
- package/dist/component/contentEntryMutations.js.map +1 -0
- package/dist/component/contentEntryValidation.d.ts +115 -0
- package/dist/component/contentEntryValidation.d.ts.map +1 -0
- package/dist/component/contentEntryValidation.js +546 -0
- package/dist/component/contentEntryValidation.js.map +1 -0
- package/dist/component/contentLock.d.ts +328 -0
- package/dist/component/contentLock.d.ts.map +1 -0
- package/dist/component/contentLock.js +471 -0
- package/dist/component/contentLock.js.map +1 -0
- package/dist/component/contentTypeMigration.d.ts +411 -0
- package/dist/component/contentTypeMigration.d.ts.map +1 -0
- package/dist/component/contentTypeMigration.js +805 -0
- package/dist/component/contentTypeMigration.js.map +1 -0
- package/dist/component/contentTypeMutations.d.ts +975 -0
- package/dist/component/contentTypeMutations.d.ts.map +1 -0
- package/dist/component/contentTypeMutations.js +768 -0
- package/dist/component/contentTypeMutations.js.map +1 -0
- package/dist/component/contentTypes.d.ts +538 -0
- package/dist/component/contentTypes.d.ts.map +1 -0
- package/dist/component/contentTypes.js +304 -0
- package/dist/component/contentTypes.js.map +1 -0
- package/dist/component/convex.config.d.ts +42 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +43 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/documentTypes.d.ts +186 -0
- package/dist/component/documentTypes.d.ts.map +1 -0
- package/dist/component/documentTypes.js +23 -0
- package/dist/component/documentTypes.js.map +1 -0
- package/dist/component/eventEmitter.d.ts +281 -0
- package/dist/component/eventEmitter.d.ts.map +1 -0
- package/dist/component/eventEmitter.js +300 -0
- package/dist/component/eventEmitter.js.map +1 -0
- package/dist/component/exportImport.d.ts +1120 -0
- package/dist/component/exportImport.d.ts.map +1 -0
- package/dist/component/exportImport.js +931 -0
- package/dist/component/exportImport.js.map +1 -0
- package/dist/component/index.d.ts +28 -0
- package/dist/component/index.d.ts.map +1 -0
- package/dist/component/index.js +142 -0
- package/dist/component/index.js.map +1 -0
- package/dist/component/lib/deepReferenceResolver.d.ts +252 -0
- package/dist/component/lib/deepReferenceResolver.d.ts.map +1 -0
- package/dist/component/lib/deepReferenceResolver.js +601 -0
- package/dist/component/lib/deepReferenceResolver.js.map +1 -0
- package/dist/component/lib/errors.d.ts +306 -0
- package/dist/component/lib/errors.d.ts.map +1 -0
- package/dist/component/lib/errors.js +407 -0
- package/dist/component/lib/errors.js.map +1 -0
- package/dist/component/lib/index.d.ts +10 -0
- package/dist/component/lib/index.d.ts.map +1 -0
- package/dist/component/lib/index.js +33 -0
- package/dist/component/lib/index.js.map +1 -0
- package/dist/component/lib/mediaReferenceResolver.d.ts +217 -0
- package/dist/component/lib/mediaReferenceResolver.d.ts.map +1 -0
- package/dist/component/lib/mediaReferenceResolver.js +326 -0
- package/dist/component/lib/mediaReferenceResolver.js.map +1 -0
- package/dist/component/lib/metadataExtractor.d.ts +245 -0
- package/dist/component/lib/metadataExtractor.d.ts.map +1 -0
- package/dist/component/lib/metadataExtractor.js +548 -0
- package/dist/component/lib/metadataExtractor.js.map +1 -0
- package/dist/component/lib/mutationAuth.d.ts +95 -0
- package/dist/component/lib/mutationAuth.d.ts.map +1 -0
- package/dist/component/lib/mutationAuth.js +146 -0
- package/dist/component/lib/mutationAuth.js.map +1 -0
- package/dist/component/lib/queries.d.ts +17 -0
- package/dist/component/lib/queries.d.ts.map +1 -0
- package/dist/component/lib/queries.js +49 -0
- package/dist/component/lib/queries.js.map +1 -0
- package/dist/component/lib/ragContentChunker.d.ts +423 -0
- package/dist/component/lib/ragContentChunker.d.ts.map +1 -0
- package/dist/component/lib/ragContentChunker.js +897 -0
- package/dist/component/lib/ragContentChunker.js.map +1 -0
- package/dist/component/lib/referenceResolver.d.ts +175 -0
- package/dist/component/lib/referenceResolver.d.ts.map +1 -0
- package/dist/component/lib/referenceResolver.js +293 -0
- package/dist/component/lib/referenceResolver.js.map +1 -0
- package/dist/component/lib/slugGenerator.d.ts +71 -0
- package/dist/component/lib/slugGenerator.d.ts.map +1 -0
- package/dist/component/lib/slugGenerator.js +207 -0
- package/dist/component/lib/slugGenerator.js.map +1 -0
- package/dist/component/lib/slugUniqueness.d.ts +131 -0
- package/dist/component/lib/slugUniqueness.d.ts.map +1 -0
- package/dist/component/lib/slugUniqueness.js +229 -0
- package/dist/component/lib/slugUniqueness.js.map +1 -0
- package/dist/component/lib/softDelete.d.ts +18 -0
- package/dist/component/lib/softDelete.d.ts.map +1 -0
- package/dist/component/lib/softDelete.js +29 -0
- package/dist/component/lib/softDelete.js.map +1 -0
- package/dist/component/localeFallbackChain.d.ts +410 -0
- package/dist/component/localeFallbackChain.d.ts.map +1 -0
- package/dist/component/localeFallbackChain.js +467 -0
- package/dist/component/localeFallbackChain.js.map +1 -0
- package/dist/component/localeFields.d.ts +508 -0
- package/dist/component/localeFields.d.ts.map +1 -0
- package/dist/component/localeFields.js +592 -0
- package/dist/component/localeFields.js.map +1 -0
- package/dist/component/mediaAssetMutations.d.ts +235 -0
- package/dist/component/mediaAssetMutations.d.ts.map +1 -0
- package/dist/component/mediaAssetMutations.js +558 -0
- package/dist/component/mediaAssetMutations.js.map +1 -0
- package/dist/component/mediaAssets.d.ts +168 -0
- package/dist/component/mediaAssets.d.ts.map +1 -0
- package/dist/component/mediaAssets.js +618 -0
- package/dist/component/mediaAssets.js.map +1 -0
- package/dist/component/mediaFolderMutations.d.ts +642 -0
- package/dist/component/mediaFolderMutations.d.ts.map +1 -0
- package/dist/component/mediaFolderMutations.js +849 -0
- package/dist/component/mediaFolderMutations.js.map +1 -0
- package/dist/component/mediaUploadMutations.d.ts +136 -0
- package/dist/component/mediaUploadMutations.d.ts.map +1 -0
- package/dist/component/mediaUploadMutations.js +205 -0
- package/dist/component/mediaUploadMutations.js.map +1 -0
- package/dist/component/mediaVariantMutations.d.ts +468 -0
- package/dist/component/mediaVariantMutations.d.ts.map +1 -0
- package/dist/component/mediaVariantMutations.js +737 -0
- package/dist/component/mediaVariantMutations.js.map +1 -0
- package/dist/component/mediaVariants.d.ts +525 -0
- package/dist/component/mediaVariants.d.ts.map +1 -0
- package/dist/component/mediaVariants.js +661 -0
- package/dist/component/mediaVariants.js.map +1 -0
- package/dist/component/ragContentIndexer.d.ts +595 -0
- package/dist/component/ragContentIndexer.d.ts.map +1 -0
- package/dist/component/ragContentIndexer.js +794 -0
- package/dist/component/ragContentIndexer.js.map +1 -0
- package/dist/component/rateLimitHooks.d.ts +266 -0
- package/dist/component/rateLimitHooks.d.ts.map +1 -0
- package/dist/component/rateLimitHooks.js +412 -0
- package/dist/component/rateLimitHooks.js.map +1 -0
- package/dist/component/roles.d.ts +649 -0
- package/dist/component/roles.d.ts.map +1 -0
- package/dist/component/roles.js +884 -0
- package/dist/component/roles.js.map +1 -0
- package/dist/component/scheduledPublish.d.ts +182 -0
- package/dist/component/scheduledPublish.d.ts.map +1 -0
- package/dist/component/scheduledPublish.js +304 -0
- package/dist/component/scheduledPublish.js.map +1 -0
- package/dist/component/schema.d.ts +4114 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +469 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/taxonomies.d.ts +476 -0
- package/dist/component/taxonomies.d.ts.map +1 -0
- package/dist/component/taxonomies.js +785 -0
- package/dist/component/taxonomies.js.map +1 -0
- package/dist/component/taxonomyMutations.d.ts +206 -0
- package/dist/component/taxonomyMutations.d.ts.map +1 -0
- package/dist/component/taxonomyMutations.js +1001 -0
- package/dist/component/taxonomyMutations.js.map +1 -0
- package/dist/component/trash.d.ts +265 -0
- package/dist/component/trash.d.ts.map +1 -0
- package/dist/component/trash.js +621 -0
- package/dist/component/trash.js.map +1 -0
- package/dist/component/types.d.ts +4 -0
- package/dist/component/types.d.ts.map +1 -0
- package/dist/component/types.js +2 -0
- package/dist/component/types.js.map +1 -0
- package/dist/component/userContext.d.ts +508 -0
- package/dist/component/userContext.d.ts.map +1 -0
- package/dist/component/userContext.js +615 -0
- package/dist/component/userContext.js.map +1 -0
- package/dist/component/validation.d.ts +387 -0
- package/dist/component/validation.d.ts.map +1 -0
- package/dist/component/validation.js +1052 -0
- package/dist/component/validation.js.map +1 -0
- package/dist/component/validators.d.ts +4645 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +641 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/component/versionMutations.d.ts +216 -0
- package/dist/component/versionMutations.d.ts.map +1 -0
- package/dist/component/versionMutations.js +321 -0
- package/dist/component/versionMutations.js.map +1 -0
- package/dist/component/webhookTrigger.d.ts +770 -0
- package/dist/component/webhookTrigger.d.ts.map +1 -0
- package/dist/component/webhookTrigger.js +1413 -0
- package/dist/component/webhookTrigger.js.map +1 -0
- package/dist/react/index.d.ts +316 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +558 -0
- package/dist/react/index.js.map +1 -0
- package/dist/test.d.ts +2230 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +1107 -0
- package/dist/test.js.map +1 -0
- package/package.json +95 -0
- package/src/cli/commands/admin.ts +104 -0
- package/src/cli/index.ts +21 -0
- package/src/cli/utils/detectConvexUrl.ts +54 -0
- package/src/cli/utils/openBrowser.ts +16 -0
- package/src/client/admin-config.ts +138 -0
- package/src/client/adminApi.ts +942 -0
- package/src/client/agentTools.ts +1311 -0
- package/src/client/argTypes.ts +316 -0
- package/src/client/field-types.ts +187 -0
- package/src/client/index.ts +1301 -0
- package/src/client/queryBuilder.ts +1100 -0
- package/src/client/schema/codegen.ts +500 -0
- package/src/client/schema/defineContentType.ts +501 -0
- package/src/client/schema/index.ts +169 -0
- package/src/client/schema/schemaDrift.ts +574 -0
- package/src/client/schema/typedClient.ts +688 -0
- package/src/client/schema/types.ts +666 -0
- package/src/client/types.ts +723 -0
- package/src/client/workflows.ts +141 -0
- package/src/client/wrapper.ts +4304 -0
- package/src/component/_generated/api.ts +140 -0
- package/src/component/_generated/component.ts +5029 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +156 -0
- package/src/component/authorization.ts +647 -0
- package/src/component/authorizationHooks.ts +668 -0
- package/src/component/bulkOperations.ts +687 -0
- package/src/component/contentEntries.ts +1976 -0
- package/src/component/contentEntryMutations.ts +1223 -0
- package/src/component/contentEntryValidation.ts +707 -0
- package/src/component/contentLock.ts +550 -0
- package/src/component/contentTypeMigration.ts +1064 -0
- package/src/component/contentTypeMutations.ts +969 -0
- package/src/component/contentTypes.ts +346 -0
- package/src/component/convex.config.ts +44 -0
- package/src/component/documentTypes.ts +240 -0
- package/src/component/eventEmitter.ts +485 -0
- package/src/component/exportImport.ts +1169 -0
- package/src/component/index.ts +491 -0
- package/src/component/lib/deepReferenceResolver.ts +999 -0
- package/src/component/lib/errors.ts +816 -0
- package/src/component/lib/index.ts +145 -0
- package/src/component/lib/mediaReferenceResolver.ts +495 -0
- package/src/component/lib/metadataExtractor.ts +792 -0
- package/src/component/lib/mutationAuth.ts +199 -0
- package/src/component/lib/queries.ts +79 -0
- package/src/component/lib/ragContentChunker.ts +1371 -0
- package/src/component/lib/referenceResolver.ts +430 -0
- package/src/component/lib/slugGenerator.ts +262 -0
- package/src/component/lib/slugUniqueness.ts +333 -0
- package/src/component/lib/softDelete.ts +44 -0
- package/src/component/localeFallbackChain.ts +673 -0
- package/src/component/localeFields.ts +896 -0
- package/src/component/mediaAssetMutations.ts +725 -0
- package/src/component/mediaAssets.ts +932 -0
- package/src/component/mediaFolderMutations.ts +1046 -0
- package/src/component/mediaUploadMutations.ts +224 -0
- package/src/component/mediaVariantMutations.ts +900 -0
- package/src/component/mediaVariants.ts +793 -0
- package/src/component/ragContentIndexer.ts +1067 -0
- package/src/component/rateLimitHooks.ts +572 -0
- package/src/component/roles.ts +1360 -0
- package/src/component/scheduledPublish.ts +358 -0
- package/src/component/schema.ts +617 -0
- package/src/component/taxonomies.ts +949 -0
- package/src/component/taxonomyMutations.ts +1210 -0
- package/src/component/trash.ts +724 -0
- package/src/component/userContext.ts +898 -0
- package/src/component/validation.ts +1388 -0
- package/src/component/validators.ts +949 -0
- package/src/component/versionMutations.ts +392 -0
- package/src/component/webhookTrigger.ts +1922 -0
- package/src/react/index.ts +898 -0
- package/src/test.ts +1580 -0
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authorization Module
|
|
3
|
+
*
|
|
4
|
+
* Core permission checking logic that evaluates user roles against requested
|
|
5
|
+
* actions and resources. This module is called internally by all CMS operations
|
|
6
|
+
* to enforce access control.
|
|
7
|
+
*
|
|
8
|
+
* Key concepts:
|
|
9
|
+
* - Users are mapped to roles via the getUserRole hook (configured in ComponentConfig)
|
|
10
|
+
* - Roles have permissions that define what actions can be performed on resources
|
|
11
|
+
* - Permissions can be scoped to "all" (any resource) or "own" (resources created by the user)
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { requirePermission, checkPermission, UnauthorizedError } from './authorization';
|
|
16
|
+
*
|
|
17
|
+
* // In a mutation handler:
|
|
18
|
+
* const userRole = await getUserRole(userId);
|
|
19
|
+
* await requirePermission({
|
|
20
|
+
* userId,
|
|
21
|
+
* role: userRole,
|
|
22
|
+
* resource: 'contentEntries',
|
|
23
|
+
* action: 'update',
|
|
24
|
+
* resourceOwnerId: entry.createdBy,
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Throws UnauthorizedError if permission denied
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
import { type Resource, type Action, type OwnershipScope, type RoleDefinition } from "./roles.js";
|
|
31
|
+
/**
|
|
32
|
+
* Error codes for authorization failures.
|
|
33
|
+
* These provide machine-readable error classification.
|
|
34
|
+
*/
|
|
35
|
+
export type AuthorizationErrorCode = "NO_ROLE" | "UNKNOWN_ROLE" | "PERMISSION_DENIED" | "OWNERSHIP_REQUIRED" | "INVALID_RESOURCE" | "INVALID_ACTION";
|
|
36
|
+
/**
|
|
37
|
+
* Error thrown when a user lacks permission to perform an action.
|
|
38
|
+
*
|
|
39
|
+
* Includes detailed context about the failed authorization check,
|
|
40
|
+
* making it easy to understand why access was denied.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* try {
|
|
45
|
+
* await requirePermission({ ... });
|
|
46
|
+
* } catch (error) {
|
|
47
|
+
* if (error instanceof UnauthorizedError) {
|
|
48
|
+
* console.log(error.code); // 'PERMISSION_DENIED'
|
|
49
|
+
* console.log(error.resource); // 'contentEntries'
|
|
50
|
+
* console.log(error.action); // 'delete'
|
|
51
|
+
* console.log(error.message); // Human-readable message
|
|
52
|
+
* }
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare class UnauthorizedError extends Error {
|
|
57
|
+
/** Machine-readable error code for classification */
|
|
58
|
+
readonly code: AuthorizationErrorCode;
|
|
59
|
+
/** The resource being accessed (if applicable) */
|
|
60
|
+
readonly resource?: Resource;
|
|
61
|
+
/** The action being attempted (if applicable) */
|
|
62
|
+
readonly action?: Action;
|
|
63
|
+
/** The user's role (if known) */
|
|
64
|
+
readonly role?: string;
|
|
65
|
+
/** The user ID (if provided) */
|
|
66
|
+
readonly userId?: string;
|
|
67
|
+
/** The scope that was required but not granted */
|
|
68
|
+
readonly requiredScope?: OwnershipScope;
|
|
69
|
+
constructor(message: string, options: {
|
|
70
|
+
code: AuthorizationErrorCode;
|
|
71
|
+
resource?: Resource;
|
|
72
|
+
action?: Action;
|
|
73
|
+
role?: string;
|
|
74
|
+
userId?: string;
|
|
75
|
+
requiredScope?: OwnershipScope;
|
|
76
|
+
});
|
|
77
|
+
/**
|
|
78
|
+
* Create a JSON-serializable representation of the error.
|
|
79
|
+
* Useful for logging or API responses.
|
|
80
|
+
*/
|
|
81
|
+
toJSON(): Record<string, unknown>;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Options for checking a user's permission to perform an action.
|
|
85
|
+
*/
|
|
86
|
+
export interface PermissionCheckOptions {
|
|
87
|
+
/** The user ID performing the action (for error messages and ownership checks) */
|
|
88
|
+
userId?: string;
|
|
89
|
+
/** The role name to check permissions for (null means no role assigned) */
|
|
90
|
+
role: string | null;
|
|
91
|
+
/** The resource type being accessed */
|
|
92
|
+
resource: Resource;
|
|
93
|
+
/** The action being performed on the resource */
|
|
94
|
+
action: Action;
|
|
95
|
+
/**
|
|
96
|
+
* The ID of the user who owns the resource.
|
|
97
|
+
* Required when the user's role only has "own" scope permission.
|
|
98
|
+
* If not provided and "own" scope is needed, the check will fail.
|
|
99
|
+
*/
|
|
100
|
+
resourceOwnerId?: string;
|
|
101
|
+
/**
|
|
102
|
+
* Custom roles to check in addition to default roles.
|
|
103
|
+
* Use this to support organization-specific role definitions.
|
|
104
|
+
*/
|
|
105
|
+
customRoles?: Record<string, RoleDefinition>;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Result of a permission check that passed.
|
|
109
|
+
*/
|
|
110
|
+
export interface PermissionGranted {
|
|
111
|
+
allowed: true;
|
|
112
|
+
/** The scope that was granted (how the permission was satisfied) */
|
|
113
|
+
grantedScope: OwnershipScope;
|
|
114
|
+
/** Whether ownership was verified (true if resourceOwnerId matched userId) */
|
|
115
|
+
ownershipVerified: boolean;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Result of a permission check that failed.
|
|
119
|
+
*/
|
|
120
|
+
export interface PermissionDenied {
|
|
121
|
+
allowed: false;
|
|
122
|
+
/** The reason the permission was denied */
|
|
123
|
+
reason: string;
|
|
124
|
+
/** Machine-readable error code */
|
|
125
|
+
code: AuthorizationErrorCode;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Result of a permission check.
|
|
129
|
+
*/
|
|
130
|
+
export type PermissionCheckResult = PermissionGranted | PermissionDenied;
|
|
131
|
+
/**
|
|
132
|
+
* Check if a user has permission to perform an action on a resource.
|
|
133
|
+
*
|
|
134
|
+
* This is the core permission evaluation function. It returns a result object
|
|
135
|
+
* indicating whether the permission was granted or denied, with details about
|
|
136
|
+
* why.
|
|
137
|
+
*
|
|
138
|
+
* The function checks permissions in the following order:
|
|
139
|
+
* 1. Validates that the user has a role assigned
|
|
140
|
+
* 2. Validates that the role exists (in default or custom roles)
|
|
141
|
+
* 3. Checks if the role has the required permission
|
|
142
|
+
* 4. For "own" scope permissions, validates ownership if resourceOwnerId is provided
|
|
143
|
+
*
|
|
144
|
+
* @param options - The permission check configuration
|
|
145
|
+
* @returns Result indicating whether permission was granted or denied
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* // Check if an editor can update any content entry
|
|
150
|
+
* const result = checkPermission({
|
|
151
|
+
* role: 'editor',
|
|
152
|
+
* resource: 'contentEntries',
|
|
153
|
+
* action: 'update',
|
|
154
|
+
* });
|
|
155
|
+
* if (result.allowed) {
|
|
156
|
+
* console.log('Permission granted with scope:', result.grantedScope);
|
|
157
|
+
* }
|
|
158
|
+
*
|
|
159
|
+
* // Check if an author can update their own content entry
|
|
160
|
+
* const result = checkPermission({
|
|
161
|
+
* userId: 'user123',
|
|
162
|
+
* role: 'author',
|
|
163
|
+
* resource: 'contentEntries',
|
|
164
|
+
* action: 'update',
|
|
165
|
+
* resourceOwnerId: 'user123', // Same as userId - ownership verified
|
|
166
|
+
* });
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
export declare function checkPermission(options: PermissionCheckOptions): PermissionCheckResult;
|
|
170
|
+
/**
|
|
171
|
+
* Require that a user has permission to perform an action.
|
|
172
|
+
*
|
|
173
|
+
* This is the throwing version of `checkPermission`. It's designed to be used
|
|
174
|
+
* at the start of mutation/query handlers to enforce access control. If the
|
|
175
|
+
* permission check fails, it throws an UnauthorizedError with a descriptive
|
|
176
|
+
* message.
|
|
177
|
+
*
|
|
178
|
+
* @param options - The permission check configuration
|
|
179
|
+
* @returns The granted permission details (if allowed)
|
|
180
|
+
* @throws UnauthorizedError if the permission is denied
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* // In a content entry update mutation:
|
|
185
|
+
* export const updateEntry = mutation({
|
|
186
|
+
* args: { id: v.id("contentEntries"), data: v.any() },
|
|
187
|
+
* handler: async (ctx, { id, data }) => {
|
|
188
|
+
* const entry = await ctx.db.get(id);
|
|
189
|
+
* if (!entry) throw new Error("Entry not found");
|
|
190
|
+
*
|
|
191
|
+
* // Check authorization before proceeding
|
|
192
|
+
* const userRole = await getUserRole(ctx.auth.userId);
|
|
193
|
+
* await requirePermission({
|
|
194
|
+
* userId: ctx.auth.userId,
|
|
195
|
+
* role: userRole,
|
|
196
|
+
* resource: 'contentEntries',
|
|
197
|
+
* action: 'update',
|
|
198
|
+
* resourceOwnerId: entry.createdBy,
|
|
199
|
+
* });
|
|
200
|
+
*
|
|
201
|
+
* // If we get here, the user is authorized
|
|
202
|
+
* await ctx.db.patch(id, data);
|
|
203
|
+
* },
|
|
204
|
+
* });
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
export declare function requirePermission(options: PermissionCheckOptions): PermissionGranted;
|
|
208
|
+
/**
|
|
209
|
+
* Check if a user owns a resource.
|
|
210
|
+
*
|
|
211
|
+
* This is a simple helper for ownership checks without full permission validation.
|
|
212
|
+
* Use this when you've already verified the permission and just need to check
|
|
213
|
+
* ownership for scope enforcement.
|
|
214
|
+
*
|
|
215
|
+
* @param userId - The ID of the user performing the action
|
|
216
|
+
* @param resourceOwnerId - The ID of the user who created/owns the resource
|
|
217
|
+
* @returns True if the user owns the resource
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* // Check ownership before allowing a delete
|
|
222
|
+
* if (!isResourceOwner(currentUserId, entry.createdBy)) {
|
|
223
|
+
* throw new UnauthorizedError(
|
|
224
|
+
* 'You can only delete your own content entries',
|
|
225
|
+
* { code: 'OWNERSHIP_REQUIRED', resource: 'contentEntries', action: 'delete' }
|
|
226
|
+
* );
|
|
227
|
+
* }
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
export declare function isResourceOwner(userId: string | undefined, resourceOwnerId: string | undefined): boolean;
|
|
231
|
+
/**
|
|
232
|
+
* Require that a user owns a resource.
|
|
233
|
+
*
|
|
234
|
+
* Throws an UnauthorizedError if the user doesn't own the resource.
|
|
235
|
+
* Use this when you need to enforce "own" scope on a resource.
|
|
236
|
+
*
|
|
237
|
+
* @param userId - The ID of the user performing the action
|
|
238
|
+
* @param resourceOwnerId - The ID of the user who created/owns the resource
|
|
239
|
+
* @param options - Additional context for the error message
|
|
240
|
+
* @throws UnauthorizedError if the user doesn't own the resource
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* // Require ownership before allowing update
|
|
245
|
+
* requireResourceOwnership(currentUserId, entry.createdBy, {
|
|
246
|
+
* resource: 'contentEntries',
|
|
247
|
+
* action: 'update',
|
|
248
|
+
* role: userRole,
|
|
249
|
+
* });
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
export declare function requireResourceOwnership(userId: string | undefined, resourceOwnerId: string | undefined, options: {
|
|
253
|
+
resource: Resource;
|
|
254
|
+
action: Action;
|
|
255
|
+
role?: string;
|
|
256
|
+
}): void;
|
|
257
|
+
/**
|
|
258
|
+
* Context for performing authorization checks within a request.
|
|
259
|
+
* This can be built up at the start of a handler and reused for multiple checks.
|
|
260
|
+
*/
|
|
261
|
+
export interface AuthorizationContext {
|
|
262
|
+
/** The user's ID */
|
|
263
|
+
userId: string;
|
|
264
|
+
/** The user's CMS role */
|
|
265
|
+
role: string;
|
|
266
|
+
/** Optional custom roles to check */
|
|
267
|
+
customRoles?: Record<string, RoleDefinition>;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Create an authorization context for a user.
|
|
271
|
+
*
|
|
272
|
+
* This is a convenience function for building the context object used by
|
|
273
|
+
* authorization functions. It validates that the user has a role assigned.
|
|
274
|
+
*
|
|
275
|
+
* @param userId - The user's ID
|
|
276
|
+
* @param role - The user's role (from getUserRole hook)
|
|
277
|
+
* @param customRoles - Optional custom role definitions
|
|
278
|
+
* @returns Authorization context for permission checks
|
|
279
|
+
* @throws UnauthorizedError if the user has no role assigned
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* // At the start of a mutation handler:
|
|
284
|
+
* const userRole = await getUserRole({ userId });
|
|
285
|
+
* const authCtx = createAuthContext(userId, userRole);
|
|
286
|
+
*
|
|
287
|
+
* // Later, check permissions with the context:
|
|
288
|
+
* requirePermission({
|
|
289
|
+
* ...authCtx,
|
|
290
|
+
* resource: 'contentEntries',
|
|
291
|
+
* action: 'create',
|
|
292
|
+
* });
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
export declare function createAuthContext(userId: string, role: string | null, customRoles?: Record<string, RoleDefinition>): AuthorizationContext;
|
|
296
|
+
/**
|
|
297
|
+
* Check if a user can perform an action using an authorization context.
|
|
298
|
+
*
|
|
299
|
+
* This is a convenience wrapper around checkPermission that uses a pre-built
|
|
300
|
+
* authorization context.
|
|
301
|
+
*
|
|
302
|
+
* @param authCtx - The authorization context
|
|
303
|
+
* @param resource - The resource type being accessed
|
|
304
|
+
* @param action - The action being performed
|
|
305
|
+
* @param resourceOwnerId - Optional owner ID for ownership validation
|
|
306
|
+
* @returns Permission check result
|
|
307
|
+
*/
|
|
308
|
+
export declare function canPerform(authCtx: AuthorizationContext, resource: Resource, action: Action, resourceOwnerId?: string): PermissionCheckResult;
|
|
309
|
+
/**
|
|
310
|
+
* Require that a user can perform an action using an authorization context.
|
|
311
|
+
*
|
|
312
|
+
* This is a convenience wrapper around requirePermission that uses a pre-built
|
|
313
|
+
* authorization context.
|
|
314
|
+
*
|
|
315
|
+
* @param authCtx - The authorization context
|
|
316
|
+
* @param resource - The resource type being accessed
|
|
317
|
+
* @param action - The action being performed
|
|
318
|
+
* @param resourceOwnerId - Optional owner ID for ownership validation
|
|
319
|
+
* @returns The granted permission details
|
|
320
|
+
* @throws UnauthorizedError if permission is denied
|
|
321
|
+
*/
|
|
322
|
+
export declare function mustPerform(authCtx: AuthorizationContext, resource: Resource, action: Action, resourceOwnerId?: string): PermissionGranted;
|
|
323
|
+
//# sourceMappingURL=authorization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authorization.d.ts","sourceRoot":"","s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