@payloadcms/plugin-multi-tenant 4.0.0-beta.6 → 4.0.0-beta.7
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/hooks/afterTenantDelete.d.ts +1 -1
- package/dist/hooks/afterTenantDelete.d.ts.map +1 -1
- package/dist/hooks/afterTenantDelete.js +17 -16
- package/dist/hooks/afterTenantDelete.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +286 -224
- package/dist/index.js.map +1 -1
- package/dist/utilities/addCollectionAccess.d.ts +4 -5
- package/dist/utilities/addCollectionAccess.d.ts.map +1 -1
- package/dist/utilities/addCollectionAccess.js +36 -102
- package/dist/utilities/addCollectionAccess.js.map +1 -1
- package/dist/utilities/addFilterOptionsToFields.d.ts +2 -3
- package/dist/utilities/addFilterOptionsToFields.d.ts.map +1 -1
- package/dist/utilities/addFilterOptionsToFields.js +37 -40
- package/dist/utilities/addFilterOptionsToFields.js.map +1 -1
- package/dist/utilities/collectionAccessKeys.d.ts +3 -0
- package/dist/utilities/collectionAccessKeys.d.ts.map +1 -0
- package/dist/utilities/collectionAccessKeys.js +11 -0
- package/dist/utilities/collectionAccessKeys.js.map +1 -0
- package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts +7 -0
- package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts.map +1 -0
- package/dist/utilities/combineBaseAccessWithTenantAccess.js +35 -0
- package/dist/utilities/combineBaseAccessWithTenantAccess.js.map +1 -0
- package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts +4 -0
- package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts.map +1 -0
- package/dist/utilities/combineCollectionAccessWithTenantAccess.js +26 -0
- package/dist/utilities/combineCollectionAccessWithTenantAccess.js.map +1 -0
- package/dist/utilities/getGlobalViewRedirect.d.ts.map +1 -1
- package/dist/utilities/getGlobalViewRedirect.js +24 -20
- package/dist/utilities/getGlobalViewRedirect.js.map +1 -1
- package/dist/utilities/getTenantAccessResult.d.ts +9 -0
- package/dist/utilities/getTenantAccessResult.d.ts.map +1 -0
- package/dist/utilities/getTenantAccessResult.js +46 -0
- package/dist/utilities/getTenantAccessResult.js.map +1 -0
- package/package.json +6 -6
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { AcceptedLanguages } from '@payloadcms/translations'\nimport type { CollectionConfig } from 'payload'\n\nimport { definePlugin } from 'payload'\nimport { hasAutosaveEnabled, parsePayloadComponent } from 'payload/shared'\n\nimport type { PluginDefaultTranslationsObject } from './translations/types.js'\nimport type { MultiTenantPluginConfig } from './types.js'\n\nimport { defaults } from './defaults.js'\nimport { getTenantOptionsEndpoint } from './endpoints/getTenantOptionsEndpoint.js'\nimport { tenantField } from './fields/tenantField/index.js'\nimport { tenantsArrayField } from './fields/tenantsArrayField/index.js'\nimport { filterDocumentsByTenants } from './filters/filterDocumentsByTenants.js'\nimport { addTenantCleanup } from './hooks/afterTenantDelete.js'\nimport { enforceTenantMembership } from './hooks/enforceTenantMembership.js'\nimport { translations } from './translations/index.js'\nimport { addCollectionAccess, type TenantAccessConfig } from './utilities/addCollectionAccess.js'\nimport { addFilterOptionsToFields } from './utilities/addFilterOptionsToFields.js'\nimport { combineFilters } from './utilities/combineFilters.js'\nimport { miniChalk } from './utilities/miniChalk.js'\n\nexport const multiTenantPlugin = definePlugin<MultiTenantPluginConfig>({\n slug: '@payloadcms/plugin-multi-tenant',\n plugin: ({ config: incomingConfig, options: pluginConfig }) => {\n if (pluginConfig.enabled === false) {\n return incomingConfig\n }\n\n /**\n * Set defaults\n */\n const userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants'] =\n typeof pluginConfig.userHasAccessToAllTenants === 'function'\n ? pluginConfig.userHasAccessToAllTenants\n : () => false\n const tenantsCollectionSlug = (pluginConfig.tenantsSlug =\n pluginConfig.tenantsSlug || defaults.tenantCollectionSlug)\n const tenantFieldName = pluginConfig?.tenantField?.name || defaults.tenantFieldName\n const tenantsArrayFieldName =\n pluginConfig?.tenantsArrayField?.arrayFieldName || defaults.tenantsArrayFieldName\n const tenantsArrayTenantFieldName =\n pluginConfig?.tenantsArrayField?.arrayTenantFieldName || defaults.tenantsArrayTenantFieldName\n const tenantAccessScopes: TenantAccessConfig[] = []\n\n /**\n * Add defaults for admin properties\n */\n if (!incomingConfig.admin) {\n incomingConfig.admin = {}\n }\n if (!incomingConfig.admin?.components) {\n incomingConfig.admin.components = {\n actions: [],\n beforeNav: [],\n providers: [],\n }\n }\n if (!incomingConfig.admin.components?.providers) {\n incomingConfig.admin.components.providers = []\n }\n if (!incomingConfig.admin.components?.actions) {\n incomingConfig.admin.components.actions = []\n }\n if (!incomingConfig.admin.components?.beforeNav) {\n incomingConfig.admin.components.beforeNav = []\n }\n if (!incomingConfig.collections) {\n incomingConfig.collections = []\n }\n\n /**\n * Add tenants array field to users collection\n */\n const adminUsersCollection = incomingConfig.collections.find(({ slug, auth }) => {\n if (incomingConfig.admin?.user) {\n return slug === incomingConfig.admin.user\n } else if (auth) {\n return true\n }\n })\n\n if (!adminUsersCollection) {\n throw Error('An auth enabled collection was not found')\n }\n\n /**\n * Add tenants array field to users collection\n */\n if (pluginConfig?.tenantsArrayField?.includeDefaultField !== false) {\n adminUsersCollection.fields.push(\n tenantsArrayField({\n ...(pluginConfig?.tenantsArrayField || {}),\n arrayFieldAccess: {\n create: ({ req }) => Boolean(req.user && userHasAccessToAllTenants(req.user)),\n update: ({ req }) => Boolean(req.user && userHasAccessToAllTenants(req.user)),\n ...(pluginConfig?.tenantsArrayField?.arrayFieldAccess || {}),\n },\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n }),\n )\n }\n\n tenantAccessScopes.push({\n accessResultCallback: pluginConfig.usersAccessResultOverride,\n adminUsersSlug: adminUsersCollection.slug,\n collection: adminUsersCollection,\n fieldName: `${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n userHasAccessToAllTenants,\n })\n\n if (pluginConfig.useUsersTenantFilter !== false) {\n if (!adminUsersCollection.admin) {\n adminUsersCollection.admin = {}\n }\n\n const baseFilter =\n adminUsersCollection.admin?.baseFilter ?? adminUsersCollection.admin?.baseListFilter\n adminUsersCollection.admin.baseFilter = combineFilters({\n baseFilter,\n customFilter: (args) =>\n filterDocumentsByTenants({\n filterFieldName: `${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`,\n req: args.req,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n }),\n })\n }\n\n let tenantCollection: CollectionConfig | undefined\n\n const [collectionSlugs, globalCollectionSlugs] = Object.keys(pluginConfig.collections).reduce<\n [string[], string[]]\n >(\n (acc, slug) => {\n if (slug === adminUsersCollection.slug) {\n return acc\n }\n if (pluginConfig?.collections?.[slug]?.isGlobal) {\n acc[1].push(slug)\n } else {\n acc[0].push(slug)\n }\n\n return acc\n },\n [[], []],\n )\n\n // used to track and not duplicate filterOptions on referenced blocks\n const blockReferencesWithFilters: string[] = []\n\n // used to validate enabled collection slugs\n const multiTenantCollectionsFound: string[] = []\n\n /**\n * Modify collections\n */\n incomingConfig.collections.forEach((collection) => {\n /**\n * Modify tenants collection\n */\n if (collection.slug === tenantsCollectionSlug) {\n tenantCollection = collection\n\n if (pluginConfig.useTenantsCollectionAccess !== false) {\n /**\n * Add access control constraint to tenants collection\n * - constrains access a users assigned tenants\n */\n tenantAccessScopes.push({\n adminUsersSlug: adminUsersCollection.slug,\n collection,\n fieldName: 'id',\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n userHasAccessToAllTenants,\n })\n }\n\n if (pluginConfig.useTenantsListFilter !== false) {\n /**\n * Add list filter to tenants collection\n * - filter by selected tenant\n */\n if (!collection.admin) {\n collection.admin = {}\n }\n\n const baseFilter = collection.admin?.baseFilter ?? collection.admin?.baseListFilter\n collection.admin.baseFilter = combineFilters({\n baseFilter,\n customFilter: (args) =>\n filterDocumentsByTenants({\n filterFieldName: 'id',\n req: args.req,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n }),\n })\n }\n\n if (pluginConfig.cleanupAfterTenantDelete !== false) {\n /**\n * Add cleanup logic when tenant is deleted\n * - delete documents related to tenant\n * - remove tenant from users\n */\n addTenantCleanup({\n collection,\n enabledSlugs: [...collectionSlugs, ...globalCollectionSlugs],\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug: adminUsersCollection.slug,\n usersTenantsArrayFieldName: tenantsArrayFieldName,\n usersTenantsArrayTenantFieldName: tenantsArrayTenantFieldName,\n })\n }\n\n /**\n * Add custom tenant field that watches and dispatches updates to the selector\n */\n collection.fields.push({\n name: '_watchTenant',\n type: 'ui',\n admin: {\n components: {\n Field: {\n path: '@payloadcms/plugin-multi-tenant/client#WatchTenantCollection',\n },\n },\n disabled: { bulkEdit: true, column: true },\n },\n })\n\n collection.endpoints = [\n ...(collection.endpoints || []),\n getTenantOptionsEndpoint({\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n useAsTitle: tenantCollection.admin?.useAsTitle || 'id',\n userHasAccessToAllTenants,\n }),\n ]\n } else if (pluginConfig.collections?.[collection.slug]) {\n if (collection.slug === adminUsersCollection.slug) {\n // eslint-disable-next-line no-console\n console.warn(\n `[plugin-multi-tenant] The admin users collection \"${collection.slug}\" should not be listed in pluginConfig.collections — it is already handled by the plugin. Skipping tenant-field processing for this collection to avoid double access control and validation errors.`,\n )\n return\n }\n multiTenantCollectionsFound.push(collection.slug)\n const isGlobal = Boolean(pluginConfig.collections[collection.slug]?.isGlobal)\n\n if (isGlobal) {\n collection.disableDuplicate = true\n }\n\n if (!pluginConfig.debug && !isGlobal) {\n collection.admin ??= {}\n collection.admin.components ??= {}\n collection.admin.components.edit ??= {}\n collection.admin.components.edit.editMenuItems ??= []\n const assignTenantTriggerPath =\n '@payloadcms/plugin-multi-tenant/client#AssignTenantFieldTrigger'\n const hasAssignTenantTrigger = collection.admin.components.edit.editMenuItems.some(\n (component) => {\n if (!component) {\n return false\n }\n\n const { exportName, path } = parsePayloadComponent(component)\n\n return (\n path === '@payloadcms/plugin-multi-tenant/client' &&\n exportName === 'AssignTenantFieldTrigger'\n )\n },\n )\n\n if (!hasAssignTenantTrigger) {\n collection.admin.components.edit.editMenuItems.push({ path: assignTenantTriggerPath })\n }\n }\n\n /**\n * Add filter options to all relationship fields\n */\n collection.fields = addFilterOptionsToFields({\n blockReferencesWithFilters,\n config: incomingConfig,\n fields: collection.fields,\n tenantEnabledCollectionSlugs: collectionSlugs,\n tenantEnabledGlobalSlugs: globalCollectionSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n })\n\n if (pluginConfig.collections[collection.slug]?.customTenantField !== true) {\n /**\n * Add tenant field to enabled collections\n */\n collection.fields.unshift(\n tenantField({\n name: tenantFieldName,\n adminUsersSlug: adminUsersCollection.slug,\n debug: pluginConfig.debug,\n isAutosaveEnabled: hasAutosaveEnabled(collection),\n overrides: pluginConfig.collections[collection.slug]?.tenantFieldOverrides\n ? pluginConfig.collections[collection.slug]?.tenantFieldOverrides\n : pluginConfig.tenantField || {},\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n unique: isGlobal,\n userHasAccessToAllTenants,\n }),\n )\n }\n\n collection.hooks ??= {}\n collection.hooks.beforeValidate ??= []\n collection.hooks.beforeValidate.push(\n enforceTenantMembership({\n collectionSlug: collection.slug,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n userHasAccessToAllTenants,\n }),\n )\n\n const { useBaseFilter, useBaseListFilter } = pluginConfig.collections[collection.slug] || {}\n if (useBaseFilter ?? useBaseListFilter ?? true) {\n /**\n * Add list filter to enabled collections\n * - filters results by selected tenant\n */\n collection.admin = collection.admin || {}\n collection.admin.baseFilter = combineFilters({\n baseFilter: collection.admin?.baseFilter ?? collection.admin?.baseListFilter,\n customFilter: (args) =>\n filterDocumentsByTenants({\n filterFieldName: tenantFieldName,\n req: args.req,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n }),\n })\n }\n\n if (pluginConfig.collections[collection.slug]?.useTenantAccess !== false) {\n /**\n * Add access control constraint to tenant enabled collection\n */\n tenantAccessScopes.push({\n accessResultCallback: pluginConfig.collections[collection.slug]?.accessResultOverride,\n adminUsersSlug: adminUsersCollection.slug,\n collection,\n fieldName: tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n userHasAccessToAllTenants,\n })\n }\n }\n })\n\n if (!tenantCollection) {\n throw new Error(`Tenants collection not found with slug: ${tenantsCollectionSlug}`)\n }\n\n if (\n multiTenantCollectionsFound.length !==\n collectionSlugs.length + globalCollectionSlugs.length\n ) {\n const missingSlugs = [...collectionSlugs, ...globalCollectionSlugs].filter(\n (slug) => !multiTenantCollectionsFound.includes(slug),\n )\n // eslint-disable-next-line no-console\n console.error(\n miniChalk.yellowBold('WARNING (plugin-multi-tenant)'),\n 'missing collections',\n missingSlugs,\n 'try placing the multi-tenant plugin after other plugins.',\n )\n }\n\n addCollectionAccess({ config: incomingConfig, scopes: tenantAccessScopes })\n\n /**\n * Add TenantSelectionProvider to admin providers\n */\n incomingConfig.admin.components.providers.push({\n clientProps: {\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug: tenantCollection.slug,\n useAsTitle: tenantCollection.admin?.useAsTitle || 'id',\n userHasAccessToAllTenants,\n },\n path: '@payloadcms/plugin-multi-tenant/rsc#TenantSelectionProvider',\n })\n\n /**\n * Add global redirect action\n */\n if (globalCollectionSlugs.length) {\n incomingConfig.admin.components.actions.push({\n path: '@payloadcms/plugin-multi-tenant/rsc#GlobalViewRedirect',\n serverProps: {\n globalSlugs: globalCollectionSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n useAsTitle: tenantCollection.admin?.useAsTitle || 'id',\n userHasAccessToAllTenants,\n },\n })\n }\n\n /**\n * Add tenant selector to admin UI\n */\n incomingConfig.admin.components.beforeNav.push({\n clientProps: {\n enabledSlugs: [\n ...collectionSlugs,\n ...globalCollectionSlugs,\n adminUsersCollection.slug,\n tenantCollection.slug,\n ],\n label: pluginConfig.tenantSelectorLabel || undefined,\n },\n path: '@payloadcms/plugin-multi-tenant/rsc#TenantSelector',\n })\n\n /**\n * Merge plugin translations\n */\n if (!incomingConfig.i18n) {\n incomingConfig.i18n = {}\n }\n Object.entries(translations).forEach(([locale, pluginI18nObject]) => {\n const typedLocale = locale as AcceptedLanguages\n if (!incomingConfig.i18n!.translations) {\n incomingConfig.i18n!.translations = {}\n }\n if (!(typedLocale in incomingConfig.i18n!.translations)) {\n incomingConfig.i18n!.translations[typedLocale] = {}\n }\n if (!('plugin-multi-tenant' in incomingConfig.i18n!.translations[typedLocale]!)) {\n ;(incomingConfig.i18n!.translations[typedLocale] as PluginDefaultTranslationsObject)[\n 'plugin-multi-tenant'\n ] = {} as PluginDefaultTranslationsObject['plugin-multi-tenant']\n }\n\n ;(incomingConfig.i18n!.translations[typedLocale] as PluginDefaultTranslationsObject)[\n 'plugin-multi-tenant'\n ] = {\n ...pluginI18nObject.translations['plugin-multi-tenant'],\n ...(pluginConfig.i18n?.translations?.[typedLocale] || {}),\n }\n })\n\n return incomingConfig\n },\n})\n"],"names":["definePlugin","hasAutosaveEnabled","parsePayloadComponent","defaults","getTenantOptionsEndpoint","tenantField","tenantsArrayField","filterDocumentsByTenants","addTenantCleanup","enforceTenantMembership","translations","addCollectionAccess","addFilterOptionsToFields","combineFilters","miniChalk","multiTenantPlugin","slug","plugin","config","incomingConfig","options","pluginConfig","enabled","userHasAccessToAllTenants","tenantsCollectionSlug","tenantsSlug","tenantCollectionSlug","tenantFieldName","name","tenantsArrayFieldName","arrayFieldName","tenantsArrayTenantFieldName","arrayTenantFieldName","tenantAccessScopes","admin","components","actions","beforeNav","providers","collections","adminUsersCollection","find","auth","user","Error","includeDefaultField","fields","push","arrayFieldAccess","create","req","Boolean","update","accessResultCallback","usersAccessResultOverride","adminUsersSlug","collection","fieldName","useUsersTenantFilter","baseFilter","baseListFilter","customFilter","args","filterFieldName","tenantCollection","collectionSlugs","globalCollectionSlugs","Object","keys","reduce","acc","isGlobal","blockReferencesWithFilters","multiTenantCollectionsFound","forEach","useTenantsCollectionAccess","useTenantsListFilter","cleanupAfterTenantDelete","enabledSlugs","usersSlug","usersTenantsArrayFieldName","usersTenantsArrayTenantFieldName","type","Field","path","disabled","bulkEdit","column","endpoints","useAsTitle","console","warn","disableDuplicate","debug","edit","editMenuItems","assignTenantTriggerPath","hasAssignTenantTrigger","some","component","exportName","tenantEnabledCollectionSlugs","tenantEnabledGlobalSlugs","customTenantField","unshift","isAutosaveEnabled","overrides","tenantFieldOverrides","unique","hooks","beforeValidate","collectionSlug","useBaseFilter","useBaseListFilter","useTenantAccess","accessResultOverride","length","missingSlugs","filter","includes","error","yellowBold","scopes","clientProps","serverProps","globalSlugs","label","tenantSelectorLabel","undefined","i18n","entries","locale","pluginI18nObject","typedLocale"],"mappings":"AAGA,SAASA,YAAY,QAAQ,UAAS;AACtC,SAASC,kBAAkB,EAAEC,qBAAqB,QAAQ,iBAAgB;AAK1E,SAASC,QAAQ,QAAQ,gBAAe;AACxC,SAASC,wBAAwB,QAAQ,0CAAyC;AAClF,SAASC,WAAW,QAAQ,gCAA+B;AAC3D,SAASC,iBAAiB,QAAQ,sCAAqC;AACvE,SAASC,wBAAwB,QAAQ,wCAAuC;AAChF,SAASC,gBAAgB,QAAQ,+BAA8B;AAC/D,SAASC,uBAAuB,QAAQ,qCAAoC;AAC5E,SAASC,YAAY,QAAQ,0BAAy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A;;KAEC,GACD,IAAI,CAACjE,eAAe8G,IAAI,EAAE;YACxB9G,eAAe8G,IAAI,GAAG,CAAC;QACzB;QACA9D,OAAO+D,OAAO,CAACxH,cAAcgE,OAAO,CAAC,CAAC,CAACyD,QAAQC,iBAAiB;YAC9D,MAAMC,cAAcF;YACpB,IAAI,CAAChH,eAAe8G,IAAI,CAAEvH,YAAY,EAAE;gBACtCS,eAAe8G,IAAI,CAAEvH,YAAY,GAAG,CAAC;YACvC;YACA,IAAI,CAAE2H,CAAAA,eAAelH,eAAe8G,IAAI,CAAEvH,YAAY,AAAD,GAAI;gBACvDS,eAAe8G,IAAI,CAAEvH,YAAY,CAAC2H,YAAY,GAAG,CAAC;YACpD;YACA,IAAI,CAAE,CAAA,yBAAyBlH,eAAe8G,IAAI,CAAEvH,YAAY,CAAC2H,YAAY,GAAI;;gBAC7ElH,eAAe8G,IAAI,CAAEvH,YAAY,CAAC2H,YAAY,AAAoC,CAClF,sBACD,GAAG,CAAC;YACP;;YAEElH,eAAe8G,IAAI,CAAEvH,YAAY,CAAC2H,YAAY,AAAoC,CAClF,sBACD,GAAG;gBACF,GAAGD,iBAAiB1H,YAAY,CAAC,sBAAsB;gBACvD,GAAIW,aAAa4G,IAAI,EAAEvH,cAAc,CAAC2H,YAAY,IAAI,CAAC,CAAC;YAC1D;QACF;QAEA,OAAOlH;IACT;AACF,GAAE"}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { AcceptedLanguages } from '@payloadcms/translations'\nimport type { Block, CollectionConfig } from 'payload'\n\nimport { definePlugin } from 'payload'\nimport { hasAutosaveEnabled } from 'payload/shared'\n\nimport type { MultiTenantPluginConfig } from './types.js'\n\nimport { defaults } from './defaults.js'\nimport { getTenantOptionsEndpoint } from './endpoints/getTenantOptionsEndpoint.js'\nimport { tenantField } from './fields/tenantField/index.js'\nimport { tenantsArrayField } from './fields/tenantsArrayField/index.js'\nimport { filterDocumentsByTenants } from './filters/filterDocumentsByTenants.js'\nimport { addTenantCleanup } from './hooks/afterTenantDelete.js'\nimport { enforceTenantMembership } from './hooks/enforceTenantMembership.js'\nimport { translations } from './translations/index.js'\nimport { addCollectionAccess, type TenantAccessConfig } from './utilities/addCollectionAccess.js'\nimport { addFilterOptionsToFields } from './utilities/addFilterOptionsToFields.js'\nimport { combineFilters } from './utilities/combineFilters.js'\nimport { miniChalk } from './utilities/miniChalk.js'\n\nexport const multiTenantPlugin = definePlugin<MultiTenantPluginConfig>({\n slug: '@payloadcms/plugin-multi-tenant',\n plugin: ({ config, options: pluginConfig }) => {\n if (pluginConfig.enabled === false) {\n return config\n }\n\n let incomingConfig = config\n\n /**\n * Set defaults\n */\n const userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants'] =\n typeof pluginConfig.userHasAccessToAllTenants === 'function'\n ? pluginConfig.userHasAccessToAllTenants\n : () => false\n const tenantsCollectionSlug = pluginConfig.tenantsSlug || defaults.tenantCollectionSlug\n const tenantFieldName = pluginConfig?.tenantField?.name || defaults.tenantFieldName\n const tenantsArrayFieldName =\n pluginConfig?.tenantsArrayField?.arrayFieldName || defaults.tenantsArrayFieldName\n const tenantsArrayTenantFieldName =\n pluginConfig?.tenantsArrayField?.arrayTenantFieldName || defaults.tenantsArrayTenantFieldName\n const collectionAccessConfigs: TenantAccessConfig[] = []\n\n /**\n * Add tenants array field to users collection\n */\n const originalAdminUsersCollection = incomingConfig.collections?.find(({ slug, auth }) => {\n if (incomingConfig.admin?.user) {\n return slug === incomingConfig.admin.user\n } else if (auth) {\n return true\n }\n })\n\n if (!originalAdminUsersCollection) {\n throw Error('An auth enabled collection was not found')\n }\n\n let adminUsersCollection: CollectionConfig = originalAdminUsersCollection\n\n /**\n * Add tenants array field to users collection\n */\n if (pluginConfig?.tenantsArrayField?.includeDefaultField !== false) {\n adminUsersCollection = {\n ...adminUsersCollection,\n fields: [\n ...adminUsersCollection.fields,\n tenantsArrayField({\n ...(pluginConfig?.tenantsArrayField || {}),\n arrayFieldAccess: {\n create: ({ req }) => Boolean(req.user && userHasAccessToAllTenants(req.user)),\n update: ({ req }) => Boolean(req.user && userHasAccessToAllTenants(req.user)),\n ...(pluginConfig?.tenantsArrayField?.arrayFieldAccess || {}),\n },\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n }),\n ],\n }\n }\n\n collectionAccessConfigs.push({\n accessResultCallback: pluginConfig.usersAccessResultOverride,\n adminUsersSlug: adminUsersCollection.slug,\n collection: adminUsersCollection,\n fieldName: `${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n userHasAccessToAllTenants,\n })\n\n if (pluginConfig.useUsersTenantFilter !== false) {\n const baseFilter =\n adminUsersCollection.admin?.baseFilter ?? adminUsersCollection.admin?.baseListFilter\n adminUsersCollection = {\n ...adminUsersCollection,\n admin: {\n ...adminUsersCollection.admin,\n baseFilter: combineFilters({\n baseFilter,\n customFilter: (args) =>\n filterDocumentsByTenants({\n filterFieldName: `${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`,\n req: args.req,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n }),\n }),\n },\n }\n }\n\n let tenantCollection: CollectionConfig | undefined\n\n const [collectionSlugs, globalCollectionSlugs] = Object.keys(pluginConfig.collections).reduce<\n [string[], string[]]\n >(\n (acc, slug) => {\n if (slug === adminUsersCollection.slug) {\n return acc\n }\n if (pluginConfig?.collections?.[slug]?.isGlobal) {\n acc[1].push(slug)\n } else {\n acc[0].push(slug)\n }\n\n return acc\n },\n [[], []],\n )\n\n // used to track and not duplicate filterOptions on referenced blocks\n const blockReferencesWithFilters: string[] = []\n\n // used to validate enabled collection slugs\n const multiTenantCollectionsFound: string[] = []\n\n /**\n * Modify collections\n */\n const collections = (incomingConfig.collections || []).map((originalCollection) => {\n let collection =\n originalCollection.slug === adminUsersCollection.slug\n ? adminUsersCollection\n : originalCollection\n /**\n * Modify tenants collection\n */\n if (collection.slug === tenantsCollectionSlug) {\n tenantCollection = collection\n\n if (pluginConfig.useTenantsCollectionAccess !== false) {\n /**\n * Add access control constraint to tenants collection\n * - constrains access a users assigned tenants\n */\n collectionAccessConfigs.push({\n adminUsersSlug: adminUsersCollection.slug,\n collection,\n fieldName: 'id',\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n userHasAccessToAllTenants,\n })\n }\n\n if (pluginConfig.useTenantsListFilter !== false) {\n /**\n * Add list filter to tenants collection\n * - filter by selected tenant\n */\n const baseFilter = collection.admin?.baseFilter ?? collection.admin?.baseListFilter\n collection = {\n ...collection,\n admin: {\n ...collection.admin,\n baseFilter: combineFilters({\n baseFilter,\n customFilter: (args) =>\n filterDocumentsByTenants({\n filterFieldName: 'id',\n req: args.req,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n }),\n }),\n },\n }\n }\n\n if (pluginConfig.cleanupAfterTenantDelete !== false) {\n /**\n * Add cleanup logic when tenant is deleted\n * - delete documents related to tenant\n * - remove tenant from users\n */\n collection = addTenantCleanup({\n collection,\n enabledSlugs: [...collectionSlugs, ...globalCollectionSlugs],\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug: adminUsersCollection.slug,\n usersTenantsArrayFieldName: tenantsArrayFieldName,\n usersTenantsArrayTenantFieldName: tenantsArrayTenantFieldName,\n })\n }\n\n /**\n * Add custom tenant field that watches and dispatches updates to the selector\n */\n collection = {\n ...collection,\n endpoints: [\n ...(collection.endpoints || []),\n getTenantOptionsEndpoint({\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n useAsTitle: tenantCollection.admin?.useAsTitle || 'id',\n userHasAccessToAllTenants,\n }),\n ],\n fields: [\n ...collection.fields,\n {\n name: '_watchTenant',\n type: 'ui',\n admin: {\n components: {\n Field: {\n path: '@payloadcms/plugin-multi-tenant/client#WatchTenantCollection',\n },\n },\n disabled: { bulkEdit: true, column: true },\n },\n },\n ],\n }\n tenantCollection = collection\n } else if (pluginConfig.collections?.[collection.slug]) {\n if (collection.slug === adminUsersCollection.slug) {\n // eslint-disable-next-line no-console\n console.warn(\n `[plugin-multi-tenant] The admin users collection \"${collection.slug}\" should not be listed in pluginConfig.collections — it is already handled by the plugin. Skipping tenant-field processing for this collection to avoid double access control and validation errors.`,\n )\n return collection\n }\n multiTenantCollectionsFound.push(collection.slug)\n const isGlobal = Boolean(pluginConfig.collections[collection.slug]?.isGlobal)\n\n if (isGlobal) {\n collection = { ...collection, disableDuplicate: true }\n }\n\n if (!pluginConfig.debug && !isGlobal) {\n collection = {\n ...collection,\n admin: {\n ...collection.admin,\n components: {\n ...collection.admin?.components,\n edit: {\n ...collection.admin?.components?.edit,\n editMenuItems: [\n ...(collection.admin?.components?.edit?.editMenuItems || []),\n { path: '@payloadcms/plugin-multi-tenant/client#AssignTenantFieldTrigger' },\n ],\n },\n },\n },\n }\n }\n\n /**\n * Add filter options to all relationship fields\n */\n collection = {\n ...collection,\n fields: addFilterOptionsToFields({\n blockReferencesWithFilters,\n fields: collection.fields,\n tenantEnabledCollectionSlugs: collectionSlugs,\n tenantEnabledGlobalSlugs: globalCollectionSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n }),\n }\n\n if (pluginConfig.collections[collection.slug]?.customTenantField !== true) {\n /**\n * Add tenant field to enabled collections\n */\n collection = {\n ...collection,\n fields: [\n tenantField({\n name: tenantFieldName,\n adminUsersSlug: adminUsersCollection.slug,\n debug: pluginConfig.debug,\n isAutosaveEnabled: hasAutosaveEnabled(collection),\n overrides: pluginConfig.collections[collection.slug]?.tenantFieldOverrides\n ? pluginConfig.collections[collection.slug]?.tenantFieldOverrides\n : pluginConfig.tenantField || {},\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n unique: isGlobal,\n userHasAccessToAllTenants,\n }),\n ...collection.fields,\n ],\n }\n }\n\n collection = {\n ...collection,\n hooks: {\n ...collection.hooks,\n beforeValidate: [\n ...(collection.hooks?.beforeValidate || []),\n enforceTenantMembership({\n collectionSlug: collection.slug,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n userHasAccessToAllTenants,\n }),\n ],\n },\n }\n\n const { useBaseFilter, useBaseListFilter } = pluginConfig.collections[collection.slug] || {}\n if (useBaseFilter ?? useBaseListFilter ?? true) {\n /**\n * Add list filter to enabled collections\n * - filters results by selected tenant\n */\n collection = {\n ...collection,\n admin: {\n ...collection.admin,\n baseFilter: combineFilters({\n baseFilter: collection.admin?.baseFilter ?? collection.admin?.baseListFilter,\n customFilter: (args) =>\n filterDocumentsByTenants({\n filterFieldName: tenantFieldName,\n req: args.req,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n }),\n }),\n },\n }\n }\n\n if (pluginConfig.collections[collection.slug]?.useTenantAccess !== false) {\n /**\n * Add access control constraint to tenant enabled collection\n */\n collectionAccessConfigs.push({\n accessResultCallback: pluginConfig.collections[collection.slug]?.accessResultOverride,\n adminUsersSlug: adminUsersCollection.slug,\n collection,\n fieldName: tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n userHasAccessToAllTenants,\n })\n }\n }\n return collection\n })\n\n incomingConfig = { ...incomingConfig, collections }\n\n // Referenced blocks are shared config objects. Transform each referenced definition once.\n const updatedBlocks = new Map<string, Block>()\n for (const slug of blockReferencesWithFilters) {\n const block = incomingConfig.blocks?.find((block) => block.slug === slug)\n if (block) {\n updatedBlocks.set(slug, {\n ...block,\n fields: addFilterOptionsToFields({\n blockReferencesWithFilters,\n fields: block.fields,\n tenantEnabledCollectionSlugs: collectionSlugs,\n tenantEnabledGlobalSlugs: globalCollectionSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n }),\n })\n }\n }\n if (updatedBlocks.size) {\n incomingConfig = {\n ...incomingConfig,\n blocks: incomingConfig.blocks?.map((block) => updatedBlocks.get(block.slug) || block),\n }\n }\n\n if (!tenantCollection) {\n throw new Error(`Tenants collection not found with slug: ${tenantsCollectionSlug}`)\n }\n\n if (\n multiTenantCollectionsFound.length !==\n collectionSlugs.length + globalCollectionSlugs.length\n ) {\n const missingSlugs = [...collectionSlugs, ...globalCollectionSlugs].filter(\n (slug) => !multiTenantCollectionsFound.includes(slug),\n )\n // eslint-disable-next-line no-console\n console.error(\n miniChalk.yellowBold('WARNING (plugin-multi-tenant)'),\n 'missing collections',\n missingSlugs,\n 'try placing the multi-tenant plugin after other plugins.',\n )\n }\n\n incomingConfig = addCollectionAccess({\n collectionAccessConfigs,\n config: incomingConfig,\n })\n\n const pluginTranslations = Object.fromEntries(\n Object.entries(translations).map(([locale, pluginI18nObject]) => {\n const typedLocale = locale as AcceptedLanguages\n\n return [\n locale,\n {\n ...incomingConfig.i18n?.translations?.[typedLocale],\n 'plugin-multi-tenant': {\n ...pluginI18nObject.translations['plugin-multi-tenant'],\n ...(pluginConfig.i18n?.translations?.[typedLocale] || {}),\n },\n },\n ]\n }),\n )\n\n return {\n ...incomingConfig,\n admin: {\n ...incomingConfig.admin,\n components: {\n ...incomingConfig.admin?.components,\n actions: [\n ...(incomingConfig.admin?.components?.actions || []),\n ...(globalCollectionSlugs.length\n ? [\n {\n path: '@payloadcms/plugin-multi-tenant/rsc#GlobalViewRedirect',\n serverProps: {\n globalSlugs: globalCollectionSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n useAsTitle: tenantCollection.admin?.useAsTitle || 'id',\n userHasAccessToAllTenants,\n },\n },\n ]\n : []),\n ],\n beforeNav: [\n ...(incomingConfig.admin?.components?.beforeNav || []),\n {\n clientProps: {\n enabledSlugs: [\n ...collectionSlugs,\n ...globalCollectionSlugs,\n adminUsersCollection.slug,\n tenantCollection.slug,\n ],\n label: pluginConfig.tenantSelectorLabel || undefined,\n },\n path: '@payloadcms/plugin-multi-tenant/rsc#TenantSelector',\n },\n ],\n providers: [\n ...(incomingConfig.admin?.components?.providers || []),\n {\n clientProps: {\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug: tenantCollection.slug,\n useAsTitle: tenantCollection.admin?.useAsTitle || 'id',\n userHasAccessToAllTenants,\n },\n path: '@payloadcms/plugin-multi-tenant/rsc#TenantSelectionProvider',\n },\n ],\n },\n },\n i18n: {\n ...incomingConfig.i18n,\n translations: {\n ...incomingConfig.i18n?.translations,\n ...pluginTranslations,\n },\n },\n }\n },\n})\n"],"names":["definePlugin","hasAutosaveEnabled","defaults","getTenantOptionsEndpoint","tenantField","tenantsArrayField","filterDocumentsByTenants","addTenantCleanup","enforceTenantMembership","translations","addCollectionAccess","addFilterOptionsToFields","combineFilters","miniChalk","multiTenantPlugin","slug","plugin","config","options","pluginConfig","enabled","incomingConfig","userHasAccessToAllTenants","tenantsCollectionSlug","tenantsSlug","tenantCollectionSlug","tenantFieldName","name","tenantsArrayFieldName","arrayFieldName","tenantsArrayTenantFieldName","arrayTenantFieldName","collectionAccessConfigs","originalAdminUsersCollection","collections","find","auth","admin","user","Error","adminUsersCollection","includeDefaultField","fields","arrayFieldAccess","create","req","Boolean","update","push","accessResultCallback","usersAccessResultOverride","adminUsersSlug","collection","fieldName","useUsersTenantFilter","baseFilter","baseListFilter","customFilter","args","filterFieldName","tenantCollection","collectionSlugs","globalCollectionSlugs","Object","keys","reduce","acc","isGlobal","blockReferencesWithFilters","multiTenantCollectionsFound","map","originalCollection","useTenantsCollectionAccess","useTenantsListFilter","cleanupAfterTenantDelete","enabledSlugs","usersSlug","usersTenantsArrayFieldName","usersTenantsArrayTenantFieldName","endpoints","useAsTitle","type","components","Field","path","disabled","bulkEdit","column","console","warn","disableDuplicate","debug","edit","editMenuItems","tenantEnabledCollectionSlugs","tenantEnabledGlobalSlugs","customTenantField","isAutosaveEnabled","overrides","tenantFieldOverrides","unique","hooks","beforeValidate","collectionSlug","useBaseFilter","useBaseListFilter","useTenantAccess","accessResultOverride","updatedBlocks","Map","block","blocks","set","size","get","length","missingSlugs","filter","includes","error","yellowBold","pluginTranslations","fromEntries","entries","locale","pluginI18nObject","typedLocale","i18n","actions","serverProps","globalSlugs","beforeNav","clientProps","label","tenantSelectorLabel","undefined","providers"],"mappings":"AAGA,SAASA,YAAY,QAAQ,UAAS;AACtC,SAASC,kBAAkB,QAAQ,iBAAgB;AAInD,SAASC,QAAQ,QAAQ,gBAAe;AACxC,SAASC,wBAAwB,QAAQ,0CAAyC;AAClF,SAASC,WAAW,QAAQ,gCAA+B;AAC3D,SAASC,iBAAiB,QAAQ,sCAAqC;AACvE,SAASC,wBAAwB,QAAQ,wCAAuC;AAChF,SAASC,gBAAgB,QAAQ,+BAA8B;AAC/D,SAASC,uBAAuB,QAAQ,qCAAoC;AAC5E,SAASC,YAAY,QAAQ,0BAAyB;AACtD,SAASC,mBAAmB,QAAiC,qCAAoC;AACjG,SAASC,wBAAwB,QAAQ,0CAAyC;AAClF,SAASC,cAAc,QAAQ,gCAA+B;AAC9D,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,oBAAoBd,aAAsC;IACrEe,MAAM;IACNC,QAAQ,CAAC,EAAEC,MAAM,EAAEC,SAASC,YAAY,EAAE;QACxC,IAAIA,aAAaC,OAAO,KAAK,OAAO;YAClC,OAAOH;QACT;QAEA,IAAII,iBAAiBJ;QAErB;;KAEC,GACD,MAAMK,4BACJ,OAAOH,aAAaG,yBAAyB,KAAK,aAC9CH,aAAaG,yBAAyB,GACtC,IAAM;QACZ,MAAMC,wBAAwBJ,aAAaK,WAAW,IAAItB,SAASuB,oBAAoB;QACvF,MAAMC,kBAAkBP,cAAcf,aAAauB,QAAQzB,SAASwB,eAAe;QACnF,MAAME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,IAAI;gBACtBtH,cAAc;oBACZ,GAAGY,eAAe0G,IAAI,EAAEtH,YAAY;oBACpC,GAAGgH,kBAAkB;gBACvB;YACF;QACF;IACF;AACF,GAAE"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { CollectionConfig, Config } from 'payload';
|
|
2
|
-
import type {
|
|
3
|
-
export declare const collectionAccessKeys: AllAccessKeys;
|
|
2
|
+
import type { MultiTenantPluginConfig } from '../types.js';
|
|
4
3
|
export type TenantAccessConfig = {
|
|
5
4
|
accessResultCallback?: MultiTenantPluginConfig['usersAccessResultOverride'];
|
|
6
5
|
adminUsersSlug: string;
|
|
@@ -10,8 +9,8 @@ export type TenantAccessConfig = {
|
|
|
10
9
|
tenantsArrayTenantFieldName?: string;
|
|
11
10
|
userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants'];
|
|
12
11
|
};
|
|
13
|
-
export declare const addCollectionAccess: ({
|
|
12
|
+
export declare const addCollectionAccess: ({ collectionAccessConfigs, config, }: {
|
|
13
|
+
collectionAccessConfigs: TenantAccessConfig[];
|
|
14
14
|
config: Config;
|
|
15
|
-
|
|
16
|
-
}) => void;
|
|
15
|
+
}) => Config;
|
|
17
16
|
//# sourceMappingURL=addCollectionAccess.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addCollectionAccess.d.ts","sourceRoot":"","sources":["../../src/utilities/addCollectionAccess.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"addCollectionAccess.d.ts","sourceRoot":"","sources":["../../src/utilities/addCollectionAccess.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAEvD,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAK1D,MAAM,MAAM,kBAAkB,GAAG;IAC/B,oBAAoB,CAAC,EAAE,uBAAuB,CAAC,2BAA2B,CAAC,CAAA;IAC3E,cAAc,EAAE,MAAM,CAAA;IACtB,UAAU,EAAE,gBAAgB,CAAA;IAC5B,SAAS,EAAE,MAAM,CAAA;IACjB,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAC9B,2BAA2B,CAAC,EAAE,MAAM,CAAA;IACpC,yBAAyB,EAAE,QAAQ,CAAC,uBAAuB,CAAC,CAAC,2BAA2B,CAAC,CAAA;CAC1F,CAAA;AAED,eAAO,MAAM,mBAAmB,yCAG7B;IACD,uBAAuB,EAAE,kBAAkB,EAAE,CAAA;IAC7C,MAAM,EAAE,MAAM,CAAA;CACf,KAAG,MAiDH,CAAA"}
|
|
@@ -1,110 +1,44 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
'readVersions',
|
|
8
|
-
'unlock',
|
|
9
|
-
'validate'
|
|
10
|
-
];
|
|
11
|
-
const getTenantAccessResult = ({ accessKey, accessResult, accessResultCallback, adminUsersSlug, args, collection, fieldName, tenantsArrayFieldName, tenantsArrayTenantFieldName, userHasAccessToAllTenants })=>{
|
|
12
|
-
const finish = (result)=>accessResultCallback ? accessResultCallback({
|
|
13
|
-
accessKey,
|
|
14
|
-
accessResult: result,
|
|
15
|
-
...args
|
|
16
|
-
}) : result;
|
|
17
|
-
if (!accessResult) {
|
|
18
|
-
return finish(false);
|
|
19
|
-
}
|
|
20
|
-
const user = args.req.user;
|
|
21
|
-
if (!user || user.collection !== adminUsersSlug || userHasAccessToAllTenants(user)) {
|
|
22
|
-
return finish(accessResult);
|
|
23
|
-
}
|
|
24
|
-
const tenantConstraint = getTenantAccess({
|
|
25
|
-
fieldName,
|
|
26
|
-
tenantsArrayFieldName,
|
|
27
|
-
tenantsArrayTenantFieldName,
|
|
28
|
-
user
|
|
29
|
-
});
|
|
30
|
-
if (tenantConstraint[fieldName]?.in.length === 0) {
|
|
31
|
-
return finish(collection.slug === user.collection ? {
|
|
32
|
-
id: {
|
|
33
|
-
equals: user.id
|
|
34
|
-
}
|
|
35
|
-
} : false);
|
|
36
|
-
}
|
|
37
|
-
const tenantResult = collection.slug === user.collection ? {
|
|
38
|
-
or: [
|
|
39
|
-
{
|
|
40
|
-
id: {
|
|
41
|
-
equals: user.id
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
tenantConstraint
|
|
45
|
-
]
|
|
46
|
-
} : tenantConstraint;
|
|
47
|
-
return finish(accessResult === true ? tenantResult : {
|
|
48
|
-
and: [
|
|
49
|
-
accessResult,
|
|
50
|
-
tenantResult
|
|
51
|
-
]
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
const wrapCollectionAccess = (scope)=>{
|
|
55
|
-
scope.collection.access ??= {};
|
|
56
|
-
const updateAccessFallback = scope.collection.access.update;
|
|
57
|
-
for (const accessKey of collectionAccessKeys){
|
|
58
|
-
// A collection without its own `validate` access is governed by `update`, matching core's
|
|
59
|
-
// fallback contract, rather than the generic "any authenticated user" default below.
|
|
60
|
-
const accessFunction = scope.collection.access[accessKey] ?? (accessKey === 'validate' ? updateAccessFallback : undefined) ?? (({ req })=>Boolean(req.user));
|
|
61
|
-
scope.collection.access[accessKey] = async (args)=>getTenantAccessResult({
|
|
62
|
-
...scope,
|
|
63
|
-
accessKey,
|
|
64
|
-
accessResult: await accessFunction(args),
|
|
65
|
-
args,
|
|
66
|
-
fieldName: accessKey === 'readVersions' ? `version.${scope.fieldName}` : scope.fieldName
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
export const addCollectionAccess = ({ config, scopes })=>{
|
|
71
|
-
const baseAccessScopes = new Map();
|
|
72
|
-
for (const scope of scopes){
|
|
73
|
-
if (scope.accessResultCallback) {
|
|
74
|
-
wrapCollectionAccess(scope);
|
|
1
|
+
import { combineBaseAccessWithTenantAccess } from './combineBaseAccessWithTenantAccess.js';
|
|
2
|
+
import { combineCollectionAccessWithTenantAccess } from './combineCollectionAccessWithTenantAccess.js';
|
|
3
|
+
export const addCollectionAccess = ({ collectionAccessConfigs, config })=>{
|
|
4
|
+
const { baseAccessBySlug, collectionAccessBySlug } = collectionAccessConfigs.reduce((acc, accessConfig)=>{
|
|
5
|
+
if (accessConfig.accessResultCallback) {
|
|
6
|
+
acc.collectionAccessBySlug.set(accessConfig.collection.slug, accessConfig);
|
|
75
7
|
} else {
|
|
76
|
-
|
|
8
|
+
acc.baseAccessBySlug.set(accessConfig.collection.slug, accessConfig);
|
|
77
9
|
}
|
|
10
|
+
return acc;
|
|
11
|
+
}, {
|
|
12
|
+
baseAccessBySlug: new Map(),
|
|
13
|
+
collectionAccessBySlug: new Map()
|
|
14
|
+
});
|
|
15
|
+
let updatedConfig = config;
|
|
16
|
+
// Override callbacks need the collection's own access result.
|
|
17
|
+
if (collectionAccessBySlug.size) {
|
|
18
|
+
updatedConfig = {
|
|
19
|
+
...updatedConfig,
|
|
20
|
+
collections: updatedConfig.collections?.map((collection)=>{
|
|
21
|
+
const accessConfig = collectionAccessBySlug.get(collection.slug);
|
|
22
|
+
if (!accessConfig) {
|
|
23
|
+
return collection;
|
|
24
|
+
}
|
|
25
|
+
return combineCollectionAccessWithTenantAccess({
|
|
26
|
+
...accessConfig,
|
|
27
|
+
collection
|
|
28
|
+
});
|
|
29
|
+
})
|
|
30
|
+
};
|
|
78
31
|
}
|
|
79
|
-
if (
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
};
|
|
87
|
-
for (const accessKey of collectionAccessKeys){
|
|
88
|
-
const baseAccessFunction = originalBaseAccess[accessKey] ?? (accessKey === 'validate' ? originalBaseAccess.update : undefined);
|
|
89
|
-
config.baseAccess.collections[accessKey] = async (args)=>{
|
|
90
|
-
const baseResult = baseAccessFunction ? await baseAccessFunction(args) : true;
|
|
91
|
-
const scope = baseAccessScopes.get(args.slug);
|
|
92
|
-
if (!baseResult || !scope) {
|
|
93
|
-
return baseResult;
|
|
94
|
-
}
|
|
95
|
-
if (accessKey === 'create' && typeof baseResult === 'object') {
|
|
96
|
-
return baseResult;
|
|
97
|
-
}
|
|
98
|
-
const tenantResult = await getTenantAccessResult({
|
|
99
|
-
...scope,
|
|
100
|
-
accessKey,
|
|
101
|
-
accessResult: baseResult,
|
|
102
|
-
args,
|
|
103
|
-
fieldName: accessKey === 'readVersions' ? `version.${scope.fieldName}` : scope.fieldName
|
|
104
|
-
});
|
|
105
|
-
return accessKey === 'create' ? Boolean(tenantResult) : tenantResult;
|
|
32
|
+
if (baseAccessBySlug.size) {
|
|
33
|
+
updatedConfig = {
|
|
34
|
+
...updatedConfig,
|
|
35
|
+
baseAccess: combineBaseAccessWithTenantAccess({
|
|
36
|
+
accessConfigBySlug: baseAccessBySlug,
|
|
37
|
+
baseAccess: config.baseAccess
|
|
38
|
+
})
|
|
106
39
|
};
|
|
107
40
|
}
|
|
41
|
+
return updatedConfig;
|
|
108
42
|
};
|
|
109
43
|
|
|
110
44
|
//# sourceMappingURL=addCollectionAccess.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utilities/addCollectionAccess.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/addCollectionAccess.ts"],"sourcesContent":["import type { CollectionConfig, Config } from 'payload'\n\nimport type { MultiTenantPluginConfig } from '../types.js'\n\nimport { combineBaseAccessWithTenantAccess } from './combineBaseAccessWithTenantAccess.js'\nimport { combineCollectionAccessWithTenantAccess } from './combineCollectionAccessWithTenantAccess.js'\n\nexport type TenantAccessConfig = {\n accessResultCallback?: MultiTenantPluginConfig['usersAccessResultOverride']\n adminUsersSlug: string\n collection: CollectionConfig\n fieldName: string\n tenantsArrayFieldName?: string\n tenantsArrayTenantFieldName?: string\n userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants']\n}\n\nexport const addCollectionAccess = ({\n collectionAccessConfigs,\n config,\n}: {\n collectionAccessConfigs: TenantAccessConfig[]\n config: Config\n}): Config => {\n const { baseAccessBySlug, collectionAccessBySlug } = collectionAccessConfigs.reduce<{\n baseAccessBySlug: Map<string, TenantAccessConfig>\n collectionAccessBySlug: Map<string, TenantAccessConfig>\n }>(\n (acc, accessConfig) => {\n if (accessConfig.accessResultCallback) {\n acc.collectionAccessBySlug.set(accessConfig.collection.slug, accessConfig)\n } else {\n acc.baseAccessBySlug.set(accessConfig.collection.slug, accessConfig)\n }\n\n return acc\n },\n {\n baseAccessBySlug: new Map(),\n collectionAccessBySlug: new Map(),\n },\n )\n\n let updatedConfig = config\n\n // Override callbacks need the collection's own access result.\n if (collectionAccessBySlug.size) {\n updatedConfig = {\n ...updatedConfig,\n collections: updatedConfig.collections?.map((collection) => {\n const accessConfig = collectionAccessBySlug.get(collection.slug)\n\n if (!accessConfig) {\n return collection\n }\n\n return combineCollectionAccessWithTenantAccess({ ...accessConfig, collection })\n }),\n }\n }\n\n if (baseAccessBySlug.size) {\n updatedConfig = {\n ...updatedConfig,\n baseAccess: combineBaseAccessWithTenantAccess({\n accessConfigBySlug: baseAccessBySlug,\n baseAccess: config.baseAccess,\n }),\n }\n }\n\n return updatedConfig\n}\n"],"names":["combineBaseAccessWithTenantAccess","combineCollectionAccessWithTenantAccess","addCollectionAccess","collectionAccessConfigs","config","baseAccessBySlug","collectionAccessBySlug","reduce","acc","accessConfig","accessResultCallback","set","collection","slug","Map","updatedConfig","size","collections","map","get","baseAccess","accessConfigBySlug"],"mappings":"AAIA,SAASA,iCAAiC,QAAQ,yCAAwC;AAC1F,SAASC,uCAAuC,QAAQ,+CAA8C;AAYtG,OAAO,MAAMC,sBAAsB,CAAC,EAClCC,uBAAuB,EACvBC,MAAM,EAIP;IACC,MAAM,EAAEC,gBAAgB,EAAEC,sBAAsB,EAAE,GAAGH,wBAAwBI,MAAM,CAIjF,CAACC,KAAKC;QACJ,IAAIA,aAAaC,oBAAoB,EAAE;YACrCF,IAAIF,sBAAsB,CAACK,GAAG,CAACF,aAAaG,UAAU,CAACC,IAAI,EAAEJ;QAC/D,OAAO;YACLD,IAAIH,gBAAgB,CAACM,GAAG,CAACF,aAAaG,UAAU,CAACC,IAAI,EAAEJ;QACzD;QAEA,OAAOD;IACT,GACA;QACEH,kBAAkB,IAAIS;QACtBR,wBAAwB,IAAIQ;IAC9B;IAGF,IAAIC,gBAAgBX;IAEpB,8DAA8D;IAC9D,IAAIE,uBAAuBU,IAAI,EAAE;QAC/BD,gBAAgB;YACd,GAAGA,aAAa;YAChBE,aAAaF,cAAcE,WAAW,EAAEC,IAAI,CAACN;gBAC3C,MAAMH,eAAeH,uBAAuBa,GAAG,CAACP,WAAWC,IAAI;gBAE/D,IAAI,CAACJ,cAAc;oBACjB,OAAOG;gBACT;gBAEA,OAAOX,wCAAwC;oBAAE,GAAGQ,YAAY;oBAAEG;gBAAW;YAC/E;QACF;IACF;IAEA,IAAIP,iBAAiBW,IAAI,EAAE;QACzBD,gBAAgB;YACd,GAAGA,aAAa;YAChBK,YAAYpB,kCAAkC;gBAC5CqB,oBAAoBhB;gBACpBe,YAAYhB,OAAOgB,UAAU;YAC/B;QACF;IACF;IAEA,OAAOL;AACT,EAAC"}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Field } from 'payload';
|
|
2
2
|
import type { MultiTenantPluginConfig } from '../types.js';
|
|
3
3
|
type AddFilterOptionsToFieldsArgs = {
|
|
4
4
|
blockReferencesWithFilters: string[];
|
|
5
|
-
config: Config | SanitizedConfig;
|
|
6
5
|
fields: Field[];
|
|
7
6
|
tenantEnabledCollectionSlugs: string[];
|
|
8
7
|
tenantEnabledGlobalSlugs: string[];
|
|
@@ -12,6 +11,6 @@ type AddFilterOptionsToFieldsArgs = {
|
|
|
12
11
|
tenantsCollectionSlug: string;
|
|
13
12
|
userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants'];
|
|
14
13
|
};
|
|
15
|
-
export declare function addFilterOptionsToFields({ blockReferencesWithFilters,
|
|
14
|
+
export declare function addFilterOptionsToFields({ blockReferencesWithFilters, fields, tenantEnabledCollectionSlugs, tenantEnabledGlobalSlugs, tenantFieldName, tenantsArrayFieldName, tenantsArrayTenantFieldName, tenantsCollectionSlug, userHasAccessToAllTenants, }: AddFilterOptionsToFieldsArgs): Field[];
|
|
16
15
|
export {};
|
|
17
16
|
//# sourceMappingURL=addFilterOptionsToFields.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addFilterOptionsToFields.d.ts","sourceRoot":"","sources":["../../src/utilities/addFilterOptionsToFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAS,
|
|
1
|
+
{"version":3,"file":"addFilterOptionsToFields.d.ts","sourceRoot":"","sources":["../../src/utilities/addFilterOptionsToFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAS,KAAK,EAAqB,MAAM,SAAS,CAAA;AAE9D,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAK1D,KAAK,4BAA4B,GAAG;IAClC,0BAA0B,EAAE,MAAM,EAAE,CAAA;IACpC,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,4BAA4B,EAAE,MAAM,EAAE,CAAA;IACtC,wBAAwB,EAAE,MAAM,EAAE,CAAA;IAClC,eAAe,EAAE,MAAM,CAAA;IACvB,qBAAqB,EAAE,MAAM,CAAA;IAC7B,2BAA2B,EAAE,MAAM,CAAA;IACnC,qBAAqB,EAAE,MAAM,CAAA;IAC7B,yBAAyB,EAAE,QAAQ,CAAC,uBAAuB,CAAC,CAAC,2BAA2B,CAAC,CAAA;CAC1F,CAAA;AAED,wBAAgB,wBAAwB,CAAC,EACvC,0BAA0B,EAC1B,MAAM,EACN,4BAA4B,EAC5B,wBAAwB,EACxB,eAAe,EACf,qBAAsD,EACtD,2BAAkE,EAClE,qBAAqB,EACrB,yBAAyB,GAC1B,EAAE,4BAA4B,GAAG,KAAK,EAAE,CAwHxC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defaults } from '../defaults.js';
|
|
2
2
|
import { filterDocumentsByTenants } from '../filters/filterDocumentsByTenants.js';
|
|
3
|
-
export function addFilterOptionsToFields({ blockReferencesWithFilters,
|
|
3
|
+
export function addFilterOptionsToFields({ blockReferencesWithFilters, fields, tenantEnabledCollectionSlugs, tenantEnabledGlobalSlugs, tenantFieldName, tenantsArrayFieldName = defaults.tenantsArrayFieldName, tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName, tenantsCollectionSlug, userHasAccessToAllTenants }) {
|
|
4
4
|
const newFields = [];
|
|
5
5
|
for (const field of fields){
|
|
6
6
|
let newField = {
|
|
@@ -43,7 +43,6 @@ export function addFilterOptionsToFields({ blockReferencesWithFilters, config, f
|
|
|
43
43
|
if (newField.type === 'row' || newField.type === 'array' || newField.type === 'collapsible' || newField.type === 'group') {
|
|
44
44
|
newField.fields = addFilterOptionsToFields({
|
|
45
45
|
blockReferencesWithFilters,
|
|
46
|
-
config,
|
|
47
46
|
fields: newField.fields,
|
|
48
47
|
tenantEnabledCollectionSlugs,
|
|
49
48
|
tenantEnabledGlobalSlugs,
|
|
@@ -62,11 +61,9 @@ export function addFilterOptionsToFields({ blockReferencesWithFilters, config, f
|
|
|
62
61
|
if (typeof _block === 'string') {
|
|
63
62
|
isReference = true;
|
|
64
63
|
if (!blockReferencesWithFilters.includes(_block)) {
|
|
65
|
-
block = config?.blocks?.find((b)=>b.slug === _block);
|
|
66
64
|
blockReferencesWithFilters.push(_block);
|
|
67
65
|
}
|
|
68
66
|
} else {
|
|
69
|
-
// Create a shallow copy to avoid mutating the original block reference
|
|
70
67
|
block = {
|
|
71
68
|
..._block
|
|
72
69
|
};
|
|
@@ -74,7 +71,6 @@ export function addFilterOptionsToFields({ blockReferencesWithFilters, config, f
|
|
|
74
71
|
if (block?.fields) {
|
|
75
72
|
block.fields = addFilterOptionsToFields({
|
|
76
73
|
blockReferencesWithFilters,
|
|
77
|
-
config,
|
|
78
74
|
fields: block.fields,
|
|
79
75
|
tenantEnabledCollectionSlugs,
|
|
80
76
|
tenantEnabledGlobalSlugs,
|
|
@@ -96,7 +92,6 @@ export function addFilterOptionsToFields({ blockReferencesWithFilters, config, f
|
|
|
96
92
|
};
|
|
97
93
|
newTab.fields = addFilterOptionsToFields({
|
|
98
94
|
blockReferencesWithFilters,
|
|
99
|
-
config,
|
|
100
95
|
fields: tab.fields,
|
|
101
96
|
tenantEnabledCollectionSlugs,
|
|
102
97
|
tenantEnabledGlobalSlugs,
|
|
@@ -116,42 +111,44 @@ export function addFilterOptionsToFields({ blockReferencesWithFilters, config, f
|
|
|
116
111
|
function addRelationshipFilter({ field, tenantEnabledCollectionSlugs, tenantFieldName, tenantsArrayFieldName = defaults.tenantsArrayFieldName, tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName, tenantsCollectionSlug, userHasAccessToAllTenants }) {
|
|
117
112
|
// User specified filter
|
|
118
113
|
const originalFilter = field.filterOptions;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
114
|
+
return {
|
|
115
|
+
...field,
|
|
116
|
+
filterOptions: async (args)=>{
|
|
117
|
+
const originalFilterResult = typeof originalFilter === 'function' ? await originalFilter(args) : originalFilter ?? true;
|
|
118
|
+
// If the relationTo is not a tenant enabled collection, return early
|
|
119
|
+
if (args.relationTo && !tenantEnabledCollectionSlugs.includes(args.relationTo)) {
|
|
120
|
+
return originalFilterResult;
|
|
121
|
+
}
|
|
122
|
+
// If the original filter returns false, return early
|
|
123
|
+
if (originalFilterResult === false) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
// Custom tenant filter
|
|
127
|
+
const tenantFilterResults = filterDocumentsByTenants({
|
|
128
|
+
docTenantID: args.data?.[tenantFieldName],
|
|
129
|
+
filterFieldName: tenantFieldName,
|
|
130
|
+
req: args.req,
|
|
131
|
+
tenantsArrayFieldName,
|
|
132
|
+
tenantsArrayTenantFieldName,
|
|
133
|
+
tenantsCollectionSlug,
|
|
134
|
+
userHasAccessToAllTenants
|
|
135
|
+
});
|
|
136
|
+
// If the tenant filter returns null, meaning no tenant filter, just use the original filter
|
|
137
|
+
if (tenantFilterResults === null) {
|
|
138
|
+
return originalFilterResult;
|
|
139
|
+
}
|
|
140
|
+
// If the original filter returns true, just use the tenant filter
|
|
141
|
+
if (originalFilterResult === true) {
|
|
142
|
+
return tenantFilterResults;
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
and: [
|
|
146
|
+
originalFilterResult,
|
|
147
|
+
tenantFilterResults
|
|
148
|
+
]
|
|
149
|
+
};
|
|
146
150
|
}
|
|
147
|
-
return {
|
|
148
|
-
and: [
|
|
149
|
-
originalFilterResult,
|
|
150
|
-
tenantFilterResults
|
|
151
|
-
]
|
|
152
|
-
};
|
|
153
151
|
};
|
|
154
|
-
return field;
|
|
155
152
|
}
|
|
156
153
|
|
|
157
154
|
//# sourceMappingURL=addFilterOptionsToFields.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utilities/addFilterOptionsToFields.ts"],"sourcesContent":["import type { Block, Config, Field, RelationshipField, SanitizedConfig } from 'payload'\n\nimport type { MultiTenantPluginConfig } from '../types.js'\n\nimport { defaults } from '../defaults.js'\nimport { filterDocumentsByTenants } from '../filters/filterDocumentsByTenants.js'\n\ntype AddFilterOptionsToFieldsArgs = {\n blockReferencesWithFilters: string[]\n config: Config | SanitizedConfig\n fields: Field[]\n tenantEnabledCollectionSlugs: string[]\n tenantEnabledGlobalSlugs: string[]\n tenantFieldName: string\n tenantsArrayFieldName: string\n tenantsArrayTenantFieldName: string\n tenantsCollectionSlug: string\n userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants']\n}\n\nexport function addFilterOptionsToFields({\n blockReferencesWithFilters,\n config,\n fields,\n tenantEnabledCollectionSlugs,\n tenantEnabledGlobalSlugs,\n tenantFieldName,\n tenantsArrayFieldName = defaults.tenantsArrayFieldName,\n tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n}: AddFilterOptionsToFieldsArgs): Field[] {\n const newFields = []\n for (const field of fields) {\n let newField: Field = { ...field }\n if (newField.type === 'relationship') {\n let hasTenantRelationsips = false\n /**\n * Adjusts relationship fields to filter by tenant\n * and ensures relationTo cannot be a tenant global collection\n */\n if (typeof newField.relationTo === 'string') {\n if (tenantEnabledGlobalSlugs.includes(newField.relationTo)) {\n throw new Error(\n `The collection ${newField.relationTo} is a global collection and cannot be related to a tenant enabled collection.`,\n )\n }\n if (tenantEnabledCollectionSlugs.includes(newField.relationTo)) {\n hasTenantRelationsips = true\n }\n } else {\n for (const relationTo of newField.relationTo) {\n if (tenantEnabledGlobalSlugs.includes(relationTo)) {\n throw new Error(\n `The collection ${relationTo} is a global collection and cannot be related to a tenant enabled collection.`,\n )\n }\n if (tenantEnabledCollectionSlugs.includes(relationTo)) {\n hasTenantRelationsips = true\n }\n }\n }\n\n if (hasTenantRelationsips) {\n newField = addRelationshipFilter({\n field: newField,\n tenantEnabledCollectionSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n })\n }\n }\n\n if (\n newField.type === 'row' ||\n newField.type === 'array' ||\n newField.type === 'collapsible' ||\n newField.type === 'group'\n ) {\n newField.fields = addFilterOptionsToFields({\n blockReferencesWithFilters,\n config,\n fields: newField.fields,\n tenantEnabledCollectionSlugs,\n tenantEnabledGlobalSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n })\n }\n\n if (newField.type === 'blocks') {\n const newBlocks: (Block | string)[] = []\n newField.blocks.forEach((_block) => {\n let block: Block | undefined\n let isReference = false\n\n if (typeof _block === 'string') {\n isReference = true\n if (!blockReferencesWithFilters.includes(_block)) {\n block = config?.blocks?.find((b) => b.slug === _block)\n blockReferencesWithFilters.push(_block)\n }\n } else {\n // Create a shallow copy to avoid mutating the original block reference\n block = { ..._block }\n }\n\n if (block?.fields) {\n block.fields = addFilterOptionsToFields({\n blockReferencesWithFilters,\n config,\n fields: block.fields,\n tenantEnabledCollectionSlugs,\n tenantEnabledGlobalSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n })\n }\n\n newBlocks.push(isReference ? _block : block!)\n })\n newField.blocks = newBlocks\n }\n\n if (newField.type === 'tabs') {\n newField.tabs = newField.tabs.map((tab) => {\n const newTab = { ...tab }\n newTab.fields = addFilterOptionsToFields({\n blockReferencesWithFilters,\n config,\n fields: tab.fields,\n tenantEnabledCollectionSlugs,\n tenantEnabledGlobalSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n })\n return newTab\n })\n }\n\n newFields.push(newField)\n }\n\n return newFields\n}\n\ntype AddFilterArgs = {\n field: RelationshipField\n tenantEnabledCollectionSlugs: string[]\n tenantFieldName: string\n tenantsArrayFieldName: string\n tenantsArrayTenantFieldName: string\n tenantsCollectionSlug: string\n userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants']\n}\nfunction addRelationshipFilter({\n field,\n tenantEnabledCollectionSlugs,\n tenantFieldName,\n tenantsArrayFieldName = defaults.tenantsArrayFieldName,\n tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n}: AddFilterArgs): Field {\n // User specified filter\n const originalFilter = field.filterOptions\n field.filterOptions = async (args) => {\n const originalFilterResult =\n typeof originalFilter === 'function' ? await originalFilter(args) : (originalFilter ?? true)\n\n // If the relationTo is not a tenant enabled collection, return early\n if (args.relationTo && !tenantEnabledCollectionSlugs.includes(args.relationTo)) {\n return originalFilterResult\n }\n\n // If the original filtr returns false, return early\n if (originalFilterResult === false) {\n return false\n }\n\n // Custom tenant filter\n const tenantFilterResults = filterDocumentsByTenants({\n docTenantID: args.data?.[tenantFieldName],\n filterFieldName: tenantFieldName,\n req: args.req,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n })\n\n // If the tenant filter returns null, meaning no tenant filter, just use the original filter\n if (tenantFilterResults === null) {\n return originalFilterResult\n }\n\n // If the original filter returns true, just use the tenant filter\n if (originalFilterResult === true) {\n return tenantFilterResults\n }\n\n return {\n and: [originalFilterResult, tenantFilterResults],\n }\n }\n\n return field\n}\n"],"names":["defaults","filterDocumentsByTenants","addFilterOptionsToFields","blockReferencesWithFilters","config","fields","tenantEnabledCollectionSlugs","tenantEnabledGlobalSlugs","tenantFieldName","tenantsArrayFieldName","tenantsArrayTenantFieldName","tenantsCollectionSlug","userHasAccessToAllTenants","newFields","field","newField","type","hasTenantRelationsips","relationTo","includes","Error","addRelationshipFilter","newBlocks","blocks","forEach","_block","block","isReference","find","b","slug","push","tabs","map","tab","newTab","originalFilter","filterOptions","args","originalFilterResult","tenantFilterResults","docTenantID","data","filterFieldName","req","and"],"mappings":"AAIA,SAASA,QAAQ,QAAQ,iBAAgB;AACzC,SAASC,wBAAwB,QAAQ,yCAAwC;AAejF,OAAO,SAASC,yBAAyB,EACvCC,0BAA0B,EAC1BC,MAAM,EACNC,MAAM,EACNC,4BAA4B,EAC5BC,wBAAwB,EACxBC,eA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C1CvB,MAAMuB,aAAa,GAAG,OAAOC;QAC3B,MAAMC,uBACJ,OAAOH,mBAAmB,aAAa,MAAMA,eAAeE,QAASF,kBAAkB;QAEzF,qEAAqE;QACrE,IAAIE,KAAKpB,UAAU,IAAI,CAACZ,6BAA6Ba,QAAQ,CAACmB,KAAKpB,UAAU,GAAG;YAC9E,OAAOqB;QACT;QAEA,oDAAoD;QACpD,IAAIA,yBAAyB,OAAO;YAClC,OAAO;QACT;QAEA,uBAAuB;QACvB,MAAMC,sBAAsBvC,yBAAyB;YACnDwC,aAAaH,KAAKI,IAAI,EAAE,CAAClC,gBAAgB;YACzCmC,iBAAiBnC;YACjBoC,KAAKN,KAAKM,GAAG;YACbnC;YACAC;YACAC;YACAC;QACF;QAEA,4FAA4F;QAC5F,IAAI4B,wBAAwB,MAAM;YAChC,OAAOD;QACT;QAEA,kEAAkE;QAClE,IAAIA,yBAAyB,MAAM;YACjC,OAAOC;QACT;QAEA,OAAO;YACLK,KAAK;gBAACN;gBAAsBC;aAAoB;QAClD;IACF;IAEA,OAAO1B;AACT"}
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/addFilterOptionsToFields.ts"],"sourcesContent":["import type { Block, Field, RelationshipField } from 'payload'\n\nimport type { MultiTenantPluginConfig } from '../types.js'\n\nimport { defaults } from '../defaults.js'\nimport { filterDocumentsByTenants } from '../filters/filterDocumentsByTenants.js'\n\ntype AddFilterOptionsToFieldsArgs = {\n blockReferencesWithFilters: string[]\n fields: Field[]\n tenantEnabledCollectionSlugs: string[]\n tenantEnabledGlobalSlugs: string[]\n tenantFieldName: string\n tenantsArrayFieldName: string\n tenantsArrayTenantFieldName: string\n tenantsCollectionSlug: string\n userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants']\n}\n\nexport function addFilterOptionsToFields({\n blockReferencesWithFilters,\n fields,\n tenantEnabledCollectionSlugs,\n tenantEnabledGlobalSlugs,\n tenantFieldName,\n tenantsArrayFieldName = defaults.tenantsArrayFieldName,\n tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n}: AddFilterOptionsToFieldsArgs): Field[] {\n const newFields = []\n for (const field of fields) {\n let newField: Field = { ...field }\n if (newField.type === 'relationship') {\n let hasTenantRelationsips = false\n /**\n * Adjusts relationship fields to filter by tenant\n * and ensures relationTo cannot be a tenant global collection\n */\n if (typeof newField.relationTo === 'string') {\n if (tenantEnabledGlobalSlugs.includes(newField.relationTo)) {\n throw new Error(\n `The collection ${newField.relationTo} is a global collection and cannot be related to a tenant enabled collection.`,\n )\n }\n if (tenantEnabledCollectionSlugs.includes(newField.relationTo)) {\n hasTenantRelationsips = true\n }\n } else {\n for (const relationTo of newField.relationTo) {\n if (tenantEnabledGlobalSlugs.includes(relationTo)) {\n throw new Error(\n `The collection ${relationTo} is a global collection and cannot be related to a tenant enabled collection.`,\n )\n }\n if (tenantEnabledCollectionSlugs.includes(relationTo)) {\n hasTenantRelationsips = true\n }\n }\n }\n\n if (hasTenantRelationsips) {\n newField = addRelationshipFilter({\n field: newField,\n tenantEnabledCollectionSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n })\n }\n }\n\n if (\n newField.type === 'row' ||\n newField.type === 'array' ||\n newField.type === 'collapsible' ||\n newField.type === 'group'\n ) {\n newField.fields = addFilterOptionsToFields({\n blockReferencesWithFilters,\n fields: newField.fields,\n tenantEnabledCollectionSlugs,\n tenantEnabledGlobalSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n })\n }\n\n if (newField.type === 'blocks') {\n const newBlocks: (Block | string)[] = []\n newField.blocks.forEach((_block) => {\n let block: Block | undefined\n let isReference = false\n\n if (typeof _block === 'string') {\n isReference = true\n if (!blockReferencesWithFilters.includes(_block)) {\n blockReferencesWithFilters.push(_block)\n }\n } else {\n block = { ..._block }\n }\n\n if (block?.fields) {\n block.fields = addFilterOptionsToFields({\n blockReferencesWithFilters,\n fields: block.fields,\n tenantEnabledCollectionSlugs,\n tenantEnabledGlobalSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n })\n }\n\n newBlocks.push(isReference ? _block : block!)\n })\n newField.blocks = newBlocks\n }\n\n if (newField.type === 'tabs') {\n newField.tabs = newField.tabs.map((tab) => {\n const newTab = { ...tab }\n newTab.fields = addFilterOptionsToFields({\n blockReferencesWithFilters,\n fields: tab.fields,\n tenantEnabledCollectionSlugs,\n tenantEnabledGlobalSlugs,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n })\n return newTab\n })\n }\n\n newFields.push(newField)\n }\n\n return newFields\n}\n\ntype AddFilterArgs = {\n field: RelationshipField\n tenantEnabledCollectionSlugs: string[]\n tenantFieldName: string\n tenantsArrayFieldName: string\n tenantsArrayTenantFieldName: string\n tenantsCollectionSlug: string\n userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants']\n}\nfunction addRelationshipFilter({\n field,\n tenantEnabledCollectionSlugs,\n tenantFieldName,\n tenantsArrayFieldName = defaults.tenantsArrayFieldName,\n tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n}: AddFilterArgs): RelationshipField {\n // User specified filter\n const originalFilter = field.filterOptions\n\n return {\n ...field,\n filterOptions: async (args) => {\n const originalFilterResult =\n typeof originalFilter === 'function' ? await originalFilter(args) : (originalFilter ?? true)\n\n // If the relationTo is not a tenant enabled collection, return early\n if (args.relationTo && !tenantEnabledCollectionSlugs.includes(args.relationTo)) {\n return originalFilterResult\n }\n\n // If the original filter returns false, return early\n if (originalFilterResult === false) {\n return false\n }\n\n // Custom tenant filter\n const tenantFilterResults = filterDocumentsByTenants({\n docTenantID: args.data?.[tenantFieldName],\n filterFieldName: tenantFieldName,\n req: args.req,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n })\n\n // If the tenant filter returns null, meaning no tenant filter, just use the original filter\n if (tenantFilterResults === null) {\n return originalFilterResult\n }\n\n // If the original filter returns true, just use the tenant filter\n if (originalFilterResult === true) {\n return tenantFilterResults\n }\n\n return {\n and: [originalFilterResult, tenantFilterResults],\n }\n },\n }\n}\n"],"names":["defaults","filterDocumentsByTenants","addFilterOptionsToFields","blockReferencesWithFilters","fields","tenantEnabledCollectionSlugs","tenantEnabledGlobalSlugs","tenantFieldName","tenantsArrayFieldName","tenantsArrayTenantFieldName","tenantsCollectionSlug","userHasAccessToAllTenants","newFields","field","newField","type","hasTenantRelationsips","relationTo","includes","Error","addRelationshipFilter","newBlocks","blocks","forEach","_block","block","isReference","push","tabs","map","tab","newTab","originalFilter","filterOptions","args","originalFilterResult","tenantFilterResults","docTenantID","data","filterFieldName","req","and"],"mappings":"AAIA,SAASA,QAAQ,QAAQ,iBAAgB;AACzC,SAASC,wBAAwB,QAAQ,yCAAwC;AAcjF,OAAO,SAASC,yBAAyB,EACvCC,0BAA0B,EAC1BC,MAAM,EACNC,4BAA4B,EAC5BC,wBAAwB,EACxBC,eAAe,EACfC,wBAAwBR,SAASQ,qBAAqB,EACtDC,8BAA8BT,SAASS,2BAA2B,EAClEC,qBAAqB,EACrBC,yBAAyB,EACI;IAC7B,MAAMC,YAAY,EAAE;IACpB,KAAK,MAAMC,SAAST,OAAQ;QAC1B,IAAIU,WAAkB;YAAE,GAAGD,KAAK;QAAC;QACjC,IAAIC,SAASC,IAAI,KAAK,gBAAgB;YACpC,IAAIC,wBAAwB;YAC5B;;;OAGC,GACD,IAAI,OAAOF,SAASG,UAAU,KAAK,UAAU;gBAC3C,IAAIX,yBAAyBY,QAAQ,CAACJ,SAASG,UAAU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A,4FAA4F;YAC5F,IAAIyB,wBAAwB,MAAM;gBAChC,OAAOD;YACT;YAEA,kEAAkE;YAClE,IAAIA,yBAAyB,MAAM;gBACjC,OAAOC;YACT;YAEA,OAAO;gBACLK,KAAK;oBAACN;oBAAsBC;iBAAoB;YAClD;QACF;IACF;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collectionAccessKeys.d.ts","sourceRoot":"","sources":["../../src/utilities/collectionAccessKeys.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAEhD,eAAO,MAAM,oBAAoB,EAAE,aAQzB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/collectionAccessKeys.ts"],"sourcesContent":["import type { AllAccessKeys } from '../types.js'\n\nexport const collectionAccessKeys: AllAccessKeys = [\n 'create',\n 'read',\n 'update',\n 'delete',\n 'readVersions',\n 'unlock',\n 'validate',\n] as const\n"],"names":["collectionAccessKeys"],"mappings":"AAEA,OAAO,MAAMA,uBAAsC;IACjD;IACA;IACA;IACA;IACA;IACA;IACA;CACD,CAAS"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Config } from 'payload';
|
|
2
|
+
import type { TenantAccessConfig } from './addCollectionAccess.js';
|
|
3
|
+
export declare const combineBaseAccessWithTenantAccess: ({ accessConfigBySlug, baseAccess, }: {
|
|
4
|
+
accessConfigBySlug: Map<string, TenantAccessConfig>;
|
|
5
|
+
baseAccess: Config['baseAccess'];
|
|
6
|
+
}) => Config['baseAccess'];
|
|
7
|
+
//# sourceMappingURL=combineBaseAccessWithTenantAccess.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"combineBaseAccessWithTenantAccess.d.ts","sourceRoot":"","sources":["../../src/utilities/combineBaseAccessWithTenantAccess.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAErC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAKlE,eAAO,MAAM,iCAAiC,wCAG3C;IACD,kBAAkB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAA;IACnD,UAAU,EAAE,MAAM,CAAC,YAAY,CAAC,CAAA;CACjC,KAAG,MAAM,CAAC,YAAY,CA0CtB,CAAA"}
|