@payloadcms/plugin-multi-tenant 4.0.0-internal.5b1e7cd → 4.0.0-internal.5d2e3d1

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.
Files changed (114) hide show
  1. package/LICENSE.md +1 -1
  2. package/dist/components/AssignTenantFieldModal/index.client.d.ts +2 -3
  3. package/dist/components/AssignTenantFieldModal/index.client.d.ts.map +1 -1
  4. package/dist/components/AssignTenantFieldModal/index.client.js +26 -81
  5. package/dist/components/AssignTenantFieldModal/index.client.js.map +1 -1
  6. package/dist/components/GlobalViewRedirect/index.d.ts +3 -3
  7. package/dist/components/GlobalViewRedirect/index.d.ts.map +1 -1
  8. package/dist/components/GlobalViewRedirect/index.js +8 -6
  9. package/dist/components/GlobalViewRedirect/index.js.map +1 -1
  10. package/dist/components/TenantField/index.client.d.ts +1 -1
  11. package/dist/components/TenantField/index.client.d.ts.map +1 -1
  12. package/dist/components/TenantField/index.client.js +86 -76
  13. package/dist/components/TenantField/index.client.js.map +1 -1
  14. package/dist/components/TenantField/index.css +44 -0
  15. package/dist/components/TenantSelector/index.client.d.ts +2 -2
  16. package/dist/components/TenantSelector/index.client.d.ts.map +1 -1
  17. package/dist/components/TenantSelector/index.client.js +4 -2
  18. package/dist/components/TenantSelector/index.client.js.map +1 -1
  19. package/dist/components/TenantSelector/index.css +13 -0
  20. package/dist/defaults.d.ts.map +1 -1
  21. package/dist/endpoints/getTenantOptionsEndpoint.d.ts +2 -2
  22. package/dist/endpoints/getTenantOptionsEndpoint.d.ts.map +1 -1
  23. package/dist/endpoints/getTenantOptionsEndpoint.js.map +1 -1
  24. package/dist/fields/tenantField/index.d.ts +4 -2
  25. package/dist/fields/tenantField/index.d.ts.map +1 -1
  26. package/dist/fields/tenantField/index.js +41 -12
  27. package/dist/fields/tenantField/index.js.map +1 -1
  28. package/dist/filters/filterDocumentsByTenants.d.ts +3 -3
  29. package/dist/filters/filterDocumentsByTenants.d.ts.map +1 -1
  30. package/dist/filters/filterDocumentsByTenants.js.map +1 -1
  31. package/dist/hooks/afterTenantDelete.d.ts +2 -2
  32. package/dist/hooks/afterTenantDelete.d.ts.map +1 -1
  33. package/dist/hooks/afterTenantDelete.js +24 -17
  34. package/dist/hooks/afterTenantDelete.js.map +1 -1
  35. package/dist/hooks/enforceTenantMembership.d.ts +15 -0
  36. package/dist/hooks/enforceTenantMembership.d.ts.map +1 -0
  37. package/dist/hooks/enforceTenantMembership.js +39 -0
  38. package/dist/hooks/enforceTenantMembership.js.map +1 -0
  39. package/dist/index.d.ts +1 -2
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +294 -266
  42. package/dist/index.js.map +1 -1
  43. package/dist/providers/TenantSelectionProvider/index.client.d.ts.map +1 -1
  44. package/dist/providers/TenantSelectionProvider/index.client.js +44 -17
  45. package/dist/providers/TenantSelectionProvider/index.client.js.map +1 -1
  46. package/dist/providers/TenantSelectionProvider/index.d.ts +6 -5
  47. package/dist/providers/TenantSelectionProvider/index.d.ts.map +1 -1
  48. package/dist/providers/TenantSelectionProvider/index.js +5 -3
  49. package/dist/providers/TenantSelectionProvider/index.js.map +1 -1
  50. package/dist/translations/languages/en.d.ts.map +1 -1
  51. package/dist/types.d.ts +9 -9
  52. package/dist/types.d.ts.map +1 -1
  53. package/dist/types.js.map +1 -1
  54. package/dist/utilities/addCollectionAccess.d.ts +9 -12
  55. package/dist/utilities/addCollectionAccess.d.ts.map +1 -1
  56. package/dist/utilities/addCollectionAccess.js +39 -27
  57. package/dist/utilities/addCollectionAccess.js.map +1 -1
  58. package/dist/utilities/addFilterOptionsToFields.d.ts +4 -5
  59. package/dist/utilities/addFilterOptionsToFields.d.ts.map +1 -1
  60. package/dist/utilities/addFilterOptionsToFields.js +42 -48
  61. package/dist/utilities/addFilterOptionsToFields.js.map +1 -1
  62. package/dist/utilities/collectionAccessKeys.d.ts +3 -0
  63. package/dist/utilities/collectionAccessKeys.d.ts.map +1 -0
  64. package/dist/utilities/collectionAccessKeys.js +11 -0
  65. package/dist/utilities/collectionAccessKeys.js.map +1 -0
  66. package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts +7 -0
  67. package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts.map +1 -0
  68. package/dist/utilities/combineBaseAccessWithTenantAccess.js +35 -0
  69. package/dist/utilities/combineBaseAccessWithTenantAccess.js.map +1 -0
  70. package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts +4 -0
  71. package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts.map +1 -0
  72. package/dist/utilities/combineCollectionAccessWithTenantAccess.js +26 -0
  73. package/dist/utilities/combineCollectionAccessWithTenantAccess.js.map +1 -0
  74. package/dist/utilities/generateCookie.d.ts.map +1 -1
  75. package/dist/utilities/getCollectionIDType.d.ts +1 -1
  76. package/dist/utilities/getGlobalViewRedirect.d.ts +5 -4
  77. package/dist/utilities/getGlobalViewRedirect.d.ts.map +1 -1
  78. package/dist/utilities/getGlobalViewRedirect.js +26 -21
  79. package/dist/utilities/getGlobalViewRedirect.js.map +1 -1
  80. package/dist/utilities/getTenantAccessResult.d.ts +9 -0
  81. package/dist/utilities/getTenantAccessResult.d.ts.map +1 -0
  82. package/dist/utilities/getTenantAccessResult.js +46 -0
  83. package/dist/utilities/getTenantAccessResult.js.map +1 -0
  84. package/dist/utilities/getTenantIDsFromValue.d.ts +2 -0
  85. package/dist/utilities/getTenantIDsFromValue.d.ts.map +1 -0
  86. package/dist/utilities/getTenantIDsFromValue.js +14 -0
  87. package/dist/utilities/getTenantIDsFromValue.js.map +1 -0
  88. package/dist/utilities/getTenantOptions.d.ts +3 -3
  89. package/dist/utilities/getTenantOptions.d.ts.map +1 -1
  90. package/dist/utilities/getTenantOptions.js.map +1 -1
  91. package/dist/utilities/getUserTenantIDs.d.ts.map +1 -1
  92. package/dist/utilities/getUserTenantIDs.js +5 -3
  93. package/dist/utilities/getUserTenantIDs.js.map +1 -1
  94. package/dist/utilities/isValidTenantAssignment.d.ts +13 -0
  95. package/dist/utilities/isValidTenantAssignment.d.ts.map +1 -0
  96. package/dist/utilities/isValidTenantAssignment.js +28 -0
  97. package/dist/utilities/isValidTenantAssignment.js.map +1 -0
  98. package/dist/utilities/miniChalk.d.ts.map +1 -1
  99. package/dist/utilities/shouldRefreshTenantSelection.d.ts +5 -0
  100. package/dist/utilities/shouldRefreshTenantSelection.d.ts.map +1 -0
  101. package/dist/utilities/shouldRefreshTenantSelection.js +3 -0
  102. package/dist/utilities/shouldRefreshTenantSelection.js.map +1 -0
  103. package/package.json +24 -28
  104. package/dist/components/AssignTenantFieldModal/index.scss +0 -78
  105. package/dist/components/TenantField/index.scss +0 -52
  106. package/dist/components/TenantSelector/index.scss +0 -4
  107. package/dist/utilities/combineWhereConstraints.d.ts +0 -3
  108. package/dist/utilities/combineWhereConstraints.d.ts.map +0 -1
  109. package/dist/utilities/combineWhereConstraints.js +0 -19
  110. package/dist/utilities/combineWhereConstraints.js.map +0 -1
  111. package/dist/utilities/withTenantAccess.d.ts +0 -16
  112. package/dist/utilities/withTenantAccess.d.ts.map +0 -1
  113. package/dist/utilities/withTenantAccess.js +0 -76
  114. package/dist/utilities/withTenantAccess.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { definePlugin } from 'payload';
1
2
  import { hasAutosaveEnabled } from 'payload/shared';
2
3
  import { defaults } from './defaults.js';
3
4
  import { getTenantOptionsEndpoint } from './endpoints/getTenantOptionsEndpoint.js';
@@ -5,69 +6,62 @@ import { tenantField } from './fields/tenantField/index.js';
5
6
  import { tenantsArrayField } from './fields/tenantsArrayField/index.js';
6
7
  import { filterDocumentsByTenants } from './filters/filterDocumentsByTenants.js';
7
8
  import { addTenantCleanup } from './hooks/afterTenantDelete.js';
9
+ import { enforceTenantMembership } from './hooks/enforceTenantMembership.js';
8
10
  import { translations } from './translations/index.js';
9
11
  import { addCollectionAccess } from './utilities/addCollectionAccess.js';
10
12
  import { addFilterOptionsToFields } from './utilities/addFilterOptionsToFields.js';
11
13
  import { combineFilters } from './utilities/combineFilters.js';
12
14
  import { miniChalk } from './utilities/miniChalk.js';
13
- export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
15
+ export const multiTenantPlugin = definePlugin({
16
+ slug: '@payloadcms/plugin-multi-tenant',
17
+ plugin: ({ config, options: pluginConfig })=>{
14
18
  if (pluginConfig.enabled === false) {
15
- return incomingConfig;
19
+ return config;
16
20
  }
21
+ let incomingConfig = config;
17
22
  /**
18
23
  * Set defaults
19
24
  */ const userHasAccessToAllTenants = typeof pluginConfig.userHasAccessToAllTenants === 'function' ? pluginConfig.userHasAccessToAllTenants : ()=>false;
20
- const tenantsCollectionSlug = pluginConfig.tenantsSlug = pluginConfig.tenantsSlug || defaults.tenantCollectionSlug;
25
+ const tenantsCollectionSlug = pluginConfig.tenantsSlug || defaults.tenantCollectionSlug;
21
26
  const tenantFieldName = pluginConfig?.tenantField?.name || defaults.tenantFieldName;
22
27
  const tenantsArrayFieldName = pluginConfig?.tenantsArrayField?.arrayFieldName || defaults.tenantsArrayFieldName;
23
28
  const tenantsArrayTenantFieldName = pluginConfig?.tenantsArrayField?.arrayTenantFieldName || defaults.tenantsArrayTenantFieldName;
24
- /**
25
- * Add defaults for admin properties
26
- */ if (!incomingConfig.admin) {
27
- incomingConfig.admin = {};
28
- }
29
- if (!incomingConfig.admin?.components) {
30
- incomingConfig.admin.components = {
31
- actions: [],
32
- beforeNav: [],
33
- providers: []
34
- };
35
- }
36
- if (!incomingConfig.admin.components?.providers) {
37
- incomingConfig.admin.components.providers = [];
38
- }
39
- if (!incomingConfig.admin.components?.actions) {
40
- incomingConfig.admin.components.actions = [];
41
- }
42
- if (!incomingConfig.admin.components?.beforeNav) {
43
- incomingConfig.admin.components.beforeNav = [];
44
- }
45
- if (!incomingConfig.collections) {
46
- incomingConfig.collections = [];
47
- }
29
+ const collectionAccessConfigs = [];
48
30
  /**
49
31
  * Add tenants array field to users collection
50
- */ const adminUsersCollection = incomingConfig.collections.find(({ slug, auth })=>{
32
+ */ const originalAdminUsersCollection = incomingConfig.collections?.find(({ slug, auth })=>{
51
33
  if (incomingConfig.admin?.user) {
52
34
  return slug === incomingConfig.admin.user;
53
35
  } else if (auth) {
54
36
  return true;
55
37
  }
56
38
  });
57
- if (!adminUsersCollection) {
39
+ if (!originalAdminUsersCollection) {
58
40
  throw Error('An auth enabled collection was not found');
59
41
  }
42
+ let adminUsersCollection = originalAdminUsersCollection;
60
43
  /**
61
44
  * Add tenants array field to users collection
62
45
  */ if (pluginConfig?.tenantsArrayField?.includeDefaultField !== false) {
63
- adminUsersCollection.fields.push(tenantsArrayField({
64
- ...pluginConfig?.tenantsArrayField || {},
65
- tenantsArrayFieldName,
66
- tenantsArrayTenantFieldName,
67
- tenantsCollectionSlug
68
- }));
46
+ adminUsersCollection = {
47
+ ...adminUsersCollection,
48
+ fields: [
49
+ ...adminUsersCollection.fields,
50
+ tenantsArrayField({
51
+ ...pluginConfig?.tenantsArrayField || {},
52
+ arrayFieldAccess: {
53
+ create: ({ req })=>Boolean(req.user && userHasAccessToAllTenants(req.user)),
54
+ update: ({ req })=>Boolean(req.user && userHasAccessToAllTenants(req.user)),
55
+ ...pluginConfig?.tenantsArrayField?.arrayFieldAccess || {}
56
+ },
57
+ tenantsArrayFieldName,
58
+ tenantsArrayTenantFieldName,
59
+ tenantsCollectionSlug
60
+ })
61
+ ]
62
+ };
69
63
  }
70
- addCollectionAccess({
64
+ collectionAccessConfigs.push({
71
65
  accessResultCallback: pluginConfig.usersAccessResultOverride,
72
66
  adminUsersSlug: adminUsersCollection.slug,
73
67
  collection: adminUsersCollection,
@@ -77,21 +71,24 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
77
71
  userHasAccessToAllTenants
78
72
  });
79
73
  if (pluginConfig.useUsersTenantFilter !== false) {
80
- if (!adminUsersCollection.admin) {
81
- adminUsersCollection.admin = {};
82
- }
83
74
  const baseFilter = adminUsersCollection.admin?.baseFilter ?? adminUsersCollection.admin?.baseListFilter;
84
- adminUsersCollection.admin.baseFilter = combineFilters({
85
- baseFilter,
86
- customFilter: (args)=>filterDocumentsByTenants({
87
- filterFieldName: `${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`,
88
- req: args.req,
89
- tenantsArrayFieldName,
90
- tenantsArrayTenantFieldName,
91
- tenantsCollectionSlug,
92
- userHasAccessToAllTenants
75
+ adminUsersCollection = {
76
+ ...adminUsersCollection,
77
+ admin: {
78
+ ...adminUsersCollection.admin,
79
+ baseFilter: combineFilters({
80
+ baseFilter,
81
+ customFilter: (args)=>filterDocumentsByTenants({
82
+ filterFieldName: `${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`,
83
+ req: args.req,
84
+ tenantsArrayFieldName,
85
+ tenantsArrayTenantFieldName,
86
+ tenantsCollectionSlug,
87
+ userHasAccessToAllTenants
88
+ })
93
89
  })
94
- });
90
+ }
91
+ };
95
92
  }
96
93
  let tenantCollection;
97
94
  const [collectionSlugs, globalCollectionSlugs] = Object.keys(pluginConfig.collections).reduce((acc, slug)=>{
@@ -113,80 +110,9 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
113
110
  // used to validate enabled collection slugs
114
111
  const multiTenantCollectionsFound = [];
115
112
  /**
116
- * The folders collection is added AFTER the plugin is initialized
117
- * so if they added the folder slug to the plugin collections,
118
- * we can assume that they have folders enabled
119
- */ const foldersSlug = incomingConfig.folders ? incomingConfig.folders.slug || 'payload-folders' : 'payload-folders';
120
- if (collectionSlugs.includes(foldersSlug)) {
121
- multiTenantCollectionsFound.push(foldersSlug);
122
- incomingConfig.folders = incomingConfig.folders || {};
123
- incomingConfig.folders.collectionOverrides = incomingConfig.folders.collectionOverrides || [];
124
- incomingConfig.folders.collectionOverrides.push(({ collection })=>{
125
- /**
126
- * Add filter options to all relationship fields
127
- */ collection.fields = addFilterOptionsToFields({
128
- blockReferencesWithFilters,
129
- config: incomingConfig,
130
- fields: collection.fields,
131
- tenantEnabledCollectionSlugs: collectionSlugs,
132
- tenantEnabledGlobalSlugs: globalCollectionSlugs,
133
- tenantFieldName,
134
- tenantsArrayFieldName,
135
- tenantsArrayTenantFieldName,
136
- tenantsCollectionSlug,
137
- userHasAccessToAllTenants
138
- });
139
- if (pluginConfig.collections[foldersSlug]?.customTenantField !== true) {
140
- /**
141
- * Add tenant field to enabled collections
142
- */ collection.fields.unshift(tenantField({
143
- name: tenantFieldName,
144
- debug: pluginConfig.debug,
145
- isAutosaveEnabled: hasAutosaveEnabled(collection),
146
- overrides: pluginConfig.collections[collection.slug]?.tenantFieldOverrides ? pluginConfig.collections[collection.slug]?.tenantFieldOverrides : pluginConfig.tenantField || {},
147
- tenantsArrayFieldName,
148
- tenantsArrayTenantFieldName,
149
- tenantsCollectionSlug,
150
- unique: false
151
- }));
152
- }
153
- const { useBaseFilter, useBaseListFilter } = pluginConfig.collections[collection.slug] || {};
154
- if (useBaseFilter ?? useBaseListFilter ?? true) {
155
- /**
156
- * Add list filter to enabled collections
157
- * - filters results by selected tenant
158
- */ collection.admin = collection.admin || {};
159
- collection.admin.baseFilter = combineFilters({
160
- baseFilter: collection.admin?.baseFilter ?? collection.admin?.baseListFilter,
161
- customFilter: (args)=>filterDocumentsByTenants({
162
- filterFieldName: tenantFieldName,
163
- req: args.req,
164
- tenantsArrayFieldName,
165
- tenantsArrayTenantFieldName,
166
- tenantsCollectionSlug,
167
- userHasAccessToAllTenants
168
- })
169
- });
170
- }
171
- if (pluginConfig.collections[foldersSlug]?.useTenantAccess !== false) {
172
- /**
173
- * Add access control constraint to tenant enabled folders collection
174
- */ addCollectionAccess({
175
- accessResultCallback: pluginConfig.collections[foldersSlug]?.accessResultOverride,
176
- adminUsersSlug: adminUsersCollection.slug,
177
- collection,
178
- fieldName: tenantFieldName,
179
- tenantsArrayFieldName,
180
- tenantsArrayTenantFieldName,
181
- userHasAccessToAllTenants
182
- });
183
- }
184
- return collection;
185
- });
186
- }
187
- /**
188
113
  * Modify collections
189
- */ incomingConfig.collections.forEach((collection)=>{
114
+ */ const collections = (incomingConfig.collections || []).map((originalCollection)=>{
115
+ let collection = originalCollection.slug === adminUsersCollection.slug ? adminUsersCollection : originalCollection;
190
116
  /**
191
117
  * Modify tenants collection
192
118
  */ if (collection.slug === tenantsCollectionSlug) {
@@ -195,7 +121,7 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
195
121
  /**
196
122
  * Add access control constraint to tenants collection
197
123
  * - constrains access a users assigned tenants
198
- */ addCollectionAccess({
124
+ */ collectionAccessConfigs.push({
199
125
  adminUsersSlug: adminUsersCollection.slug,
200
126
  collection,
201
127
  fieldName: 'id',
@@ -208,28 +134,31 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
208
134
  /**
209
135
  * Add list filter to tenants collection
210
136
  * - filter by selected tenant
211
- */ if (!collection.admin) {
212
- collection.admin = {};
213
- }
214
- const baseFilter = collection.admin?.baseFilter ?? collection.admin?.baseListFilter;
215
- collection.admin.baseFilter = combineFilters({
216
- baseFilter,
217
- customFilter: (args)=>filterDocumentsByTenants({
218
- filterFieldName: 'id',
219
- req: args.req,
220
- tenantsArrayFieldName,
221
- tenantsArrayTenantFieldName,
222
- tenantsCollectionSlug,
223
- userHasAccessToAllTenants
137
+ */ const baseFilter = collection.admin?.baseFilter ?? collection.admin?.baseListFilter;
138
+ collection = {
139
+ ...collection,
140
+ admin: {
141
+ ...collection.admin,
142
+ baseFilter: combineFilters({
143
+ baseFilter,
144
+ customFilter: (args)=>filterDocumentsByTenants({
145
+ filterFieldName: 'id',
146
+ req: args.req,
147
+ tenantsArrayFieldName,
148
+ tenantsArrayTenantFieldName,
149
+ tenantsCollectionSlug,
150
+ userHasAccessToAllTenants
151
+ })
224
152
  })
225
- });
153
+ }
154
+ };
226
155
  }
227
156
  if (pluginConfig.cleanupAfterTenantDelete !== false) {
228
157
  /**
229
158
  * Add cleanup logic when tenant is deleted
230
159
  * - delete documents related to tenant
231
160
  * - remove tenant from users
232
- */ addTenantCleanup({
161
+ */ collection = addTenantCleanup({
233
162
  collection,
234
163
  enabledSlugs: [
235
164
  ...collectionSlugs,
@@ -244,99 +173,153 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
244
173
  }
245
174
  /**
246
175
  * Add custom tenant field that watches and dispatches updates to the selector
247
- */ collection.fields.push({
248
- name: '_watchTenant',
249
- type: 'ui',
250
- admin: {
251
- components: {
252
- Field: {
253
- path: '@payloadcms/plugin-multi-tenant/client#WatchTenantCollection'
176
+ */ collection = {
177
+ ...collection,
178
+ endpoints: [
179
+ ...collection.endpoints || [],
180
+ getTenantOptionsEndpoint({
181
+ tenantsArrayFieldName,
182
+ tenantsArrayTenantFieldName,
183
+ tenantsCollectionSlug,
184
+ useAsTitle: tenantCollection.admin?.useAsTitle || 'id',
185
+ userHasAccessToAllTenants
186
+ })
187
+ ],
188
+ fields: [
189
+ ...collection.fields,
190
+ {
191
+ name: '_watchTenant',
192
+ type: 'ui',
193
+ admin: {
194
+ components: {
195
+ Field: {
196
+ path: '@payloadcms/plugin-multi-tenant/client#WatchTenantCollection'
197
+ }
198
+ },
199
+ disabled: {
200
+ bulkEdit: true,
201
+ column: true
202
+ }
254
203
  }
255
- },
256
- disableBulkEdit: true,
257
- disableListColumn: true
258
- }
259
- });
260
- collection.endpoints = [
261
- ...collection.endpoints || [],
262
- getTenantOptionsEndpoint({
263
- tenantsArrayFieldName,
264
- tenantsArrayTenantFieldName,
265
- tenantsCollectionSlug,
266
- useAsTitle: tenantCollection.admin?.useAsTitle || 'id',
267
- userHasAccessToAllTenants
268
- })
269
- ];
204
+ }
205
+ ]
206
+ };
207
+ tenantCollection = collection;
270
208
  } else if (pluginConfig.collections?.[collection.slug]) {
271
209
  if (collection.slug === adminUsersCollection.slug) {
272
210
  // eslint-disable-next-line no-console
273
211
  console.warn(`[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.`);
274
- return;
212
+ return collection;
275
213
  }
276
214
  multiTenantCollectionsFound.push(collection.slug);
277
215
  const isGlobal = Boolean(pluginConfig.collections[collection.slug]?.isGlobal);
278
216
  if (isGlobal) {
279
- collection.disableDuplicate = true;
217
+ collection = {
218
+ ...collection,
219
+ disableDuplicate: true
220
+ };
280
221
  }
281
222
  if (!pluginConfig.debug && !isGlobal) {
282
- collection.admin ??= {};
283
- collection.admin.components ??= {};
284
- collection.admin.components.edit ??= {};
285
- collection.admin.components.edit.editMenuItems ??= [];
286
- collection.admin.components.edit.editMenuItems.push({
287
- path: '@payloadcms/plugin-multi-tenant/client#AssignTenantFieldTrigger'
288
- });
223
+ collection = {
224
+ ...collection,
225
+ admin: {
226
+ ...collection.admin,
227
+ components: {
228
+ ...collection.admin?.components,
229
+ edit: {
230
+ ...collection.admin?.components?.edit,
231
+ editMenuItems: [
232
+ ...collection.admin?.components?.edit?.editMenuItems || [],
233
+ {
234
+ path: '@payloadcms/plugin-multi-tenant/client#AssignTenantFieldTrigger'
235
+ }
236
+ ]
237
+ }
238
+ }
239
+ }
240
+ };
289
241
  }
290
242
  /**
291
243
  * Add filter options to all relationship fields
292
- */ collection.fields = addFilterOptionsToFields({
293
- blockReferencesWithFilters,
294
- config: incomingConfig,
295
- fields: collection.fields,
296
- tenantEnabledCollectionSlugs: collectionSlugs,
297
- tenantEnabledGlobalSlugs: globalCollectionSlugs,
298
- tenantFieldName,
299
- tenantsArrayFieldName,
300
- tenantsArrayTenantFieldName,
301
- tenantsCollectionSlug,
302
- userHasAccessToAllTenants
303
- });
304
- if (pluginConfig.collections[collection.slug]?.customTenantField !== true) {
305
- /**
306
- * Add tenant field to enabled collections
307
- */ collection.fields.unshift(tenantField({
308
- name: tenantFieldName,
309
- debug: pluginConfig.debug,
310
- isAutosaveEnabled: hasAutosaveEnabled(collection),
311
- overrides: pluginConfig.collections[collection.slug]?.tenantFieldOverrides ? pluginConfig.collections[collection.slug]?.tenantFieldOverrides : pluginConfig.tenantField || {},
244
+ */ collection = {
245
+ ...collection,
246
+ fields: addFilterOptionsToFields({
247
+ blockReferencesWithFilters,
248
+ fields: collection.fields,
249
+ tenantEnabledCollectionSlugs: collectionSlugs,
250
+ tenantEnabledGlobalSlugs: globalCollectionSlugs,
251
+ tenantFieldName,
312
252
  tenantsArrayFieldName,
313
253
  tenantsArrayTenantFieldName,
314
254
  tenantsCollectionSlug,
315
- unique: isGlobal
316
- }));
255
+ userHasAccessToAllTenants
256
+ })
257
+ };
258
+ if (pluginConfig.collections[collection.slug]?.customTenantField !== true) {
259
+ /**
260
+ * Add tenant field to enabled collections
261
+ */ collection = {
262
+ ...collection,
263
+ fields: [
264
+ tenantField({
265
+ name: tenantFieldName,
266
+ adminUsersSlug: adminUsersCollection.slug,
267
+ debug: pluginConfig.debug,
268
+ isAutosaveEnabled: hasAutosaveEnabled(collection),
269
+ overrides: pluginConfig.collections[collection.slug]?.tenantFieldOverrides ? pluginConfig.collections[collection.slug]?.tenantFieldOverrides : pluginConfig.tenantField || {},
270
+ tenantsArrayFieldName,
271
+ tenantsArrayTenantFieldName,
272
+ tenantsCollectionSlug,
273
+ unique: isGlobal,
274
+ userHasAccessToAllTenants
275
+ }),
276
+ ...collection.fields
277
+ ]
278
+ };
317
279
  }
280
+ collection = {
281
+ ...collection,
282
+ hooks: {
283
+ ...collection.hooks,
284
+ beforeValidate: [
285
+ ...collection.hooks?.beforeValidate || [],
286
+ enforceTenantMembership({
287
+ collectionSlug: collection.slug,
288
+ tenantFieldName,
289
+ tenantsArrayFieldName,
290
+ tenantsArrayTenantFieldName,
291
+ userHasAccessToAllTenants
292
+ })
293
+ ]
294
+ }
295
+ };
318
296
  const { useBaseFilter, useBaseListFilter } = pluginConfig.collections[collection.slug] || {};
319
297
  if (useBaseFilter ?? useBaseListFilter ?? true) {
320
298
  /**
321
299
  * Add list filter to enabled collections
322
300
  * - filters results by selected tenant
323
- */ collection.admin = collection.admin || {};
324
- collection.admin.baseFilter = combineFilters({
325
- baseFilter: collection.admin?.baseFilter ?? collection.admin?.baseListFilter,
326
- customFilter: (args)=>filterDocumentsByTenants({
327
- filterFieldName: tenantFieldName,
328
- req: args.req,
329
- tenantsArrayFieldName,
330
- tenantsArrayTenantFieldName,
331
- tenantsCollectionSlug,
332
- userHasAccessToAllTenants
301
+ */ collection = {
302
+ ...collection,
303
+ admin: {
304
+ ...collection.admin,
305
+ baseFilter: combineFilters({
306
+ baseFilter: collection.admin?.baseFilter ?? collection.admin?.baseListFilter,
307
+ customFilter: (args)=>filterDocumentsByTenants({
308
+ filterFieldName: tenantFieldName,
309
+ req: args.req,
310
+ tenantsArrayFieldName,
311
+ tenantsArrayTenantFieldName,
312
+ tenantsCollectionSlug,
313
+ userHasAccessToAllTenants
314
+ })
333
315
  })
334
- });
316
+ }
317
+ };
335
318
  }
336
319
  if (pluginConfig.collections[collection.slug]?.useTenantAccess !== false) {
337
320
  /**
338
321
  * Add access control constraint to tenant enabled collection
339
- */ addCollectionAccess({
322
+ */ collectionAccessConfigs.push({
340
323
  accessResultCallback: pluginConfig.collections[collection.slug]?.accessResultOverride,
341
324
  adminUsersSlug: adminUsersCollection.slug,
342
325
  collection,
@@ -347,7 +330,39 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
347
330
  });
348
331
  }
349
332
  }
333
+ return collection;
350
334
  });
335
+ incomingConfig = {
336
+ ...incomingConfig,
337
+ collections
338
+ };
339
+ // Referenced blocks are shared config objects. Transform each referenced definition once.
340
+ const updatedBlocks = new Map();
341
+ for (const slug of blockReferencesWithFilters){
342
+ const block = incomingConfig.blocks?.find((block)=>block.slug === slug);
343
+ if (block) {
344
+ updatedBlocks.set(slug, {
345
+ ...block,
346
+ fields: addFilterOptionsToFields({
347
+ blockReferencesWithFilters,
348
+ fields: block.fields,
349
+ tenantEnabledCollectionSlugs: collectionSlugs,
350
+ tenantEnabledGlobalSlugs: globalCollectionSlugs,
351
+ tenantFieldName,
352
+ tenantsArrayFieldName,
353
+ tenantsArrayTenantFieldName,
354
+ tenantsCollectionSlug,
355
+ userHasAccessToAllTenants
356
+ })
357
+ });
358
+ }
359
+ }
360
+ if (updatedBlocks.size) {
361
+ incomingConfig = {
362
+ ...incomingConfig,
363
+ blocks: incomingConfig.blocks?.map((block)=>updatedBlocks.get(block.slug) || block)
364
+ };
365
+ }
351
366
  if (!tenantCollection) {
352
367
  throw new Error(`Tenants collection not found with slug: ${tenantsCollectionSlug}`);
353
368
  }
@@ -359,72 +374,85 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
359
374
  // eslint-disable-next-line no-console
360
375
  console.error(miniChalk.yellowBold('WARNING (plugin-multi-tenant)'), 'missing collections', missingSlugs, 'try placing the multi-tenant plugin after other plugins.');
361
376
  }
362
- /**
363
- * Add TenantSelectionProvider to admin providers
364
- */ incomingConfig.admin.components.providers.push({
365
- clientProps: {
366
- tenantsArrayFieldName,
367
- tenantsArrayTenantFieldName,
368
- tenantsCollectionSlug: tenantCollection.slug,
369
- useAsTitle: tenantCollection.admin?.useAsTitle || 'id',
370
- userHasAccessToAllTenants
371
- },
372
- path: '@payloadcms/plugin-multi-tenant/rsc#TenantSelectionProvider'
377
+ incomingConfig = addCollectionAccess({
378
+ collectionAccessConfigs,
379
+ config: incomingConfig
373
380
  });
374
- /**
375
- * Add global redirect action
376
- */ if (globalCollectionSlugs.length) {
377
- incomingConfig.admin.components.actions.push({
378
- path: '@payloadcms/plugin-multi-tenant/rsc#GlobalViewRedirect',
379
- serverProps: {
380
- globalSlugs: globalCollectionSlugs,
381
- tenantFieldName,
382
- tenantsArrayFieldName,
383
- tenantsArrayTenantFieldName,
384
- tenantsCollectionSlug,
385
- useAsTitle: tenantCollection.admin?.useAsTitle || 'id',
386
- userHasAccessToAllTenants
381
+ const pluginTranslations = Object.fromEntries(Object.entries(translations).map(([locale, pluginI18nObject])=>{
382
+ const typedLocale = locale;
383
+ return [
384
+ locale,
385
+ {
386
+ ...incomingConfig.i18n?.translations?.[typedLocale],
387
+ 'plugin-multi-tenant': {
388
+ ...pluginI18nObject.translations['plugin-multi-tenant'],
389
+ ...pluginConfig.i18n?.translations?.[typedLocale] || {}
390
+ }
391
+ }
392
+ ];
393
+ }));
394
+ return {
395
+ ...incomingConfig,
396
+ admin: {
397
+ ...incomingConfig.admin,
398
+ components: {
399
+ ...incomingConfig.admin?.components,
400
+ actions: [
401
+ ...incomingConfig.admin?.components?.actions || [],
402
+ ...globalCollectionSlugs.length ? [
403
+ {
404
+ path: '@payloadcms/plugin-multi-tenant/rsc#GlobalViewRedirect',
405
+ serverProps: {
406
+ globalSlugs: globalCollectionSlugs,
407
+ tenantFieldName,
408
+ tenantsArrayFieldName,
409
+ tenantsArrayTenantFieldName,
410
+ tenantsCollectionSlug,
411
+ useAsTitle: tenantCollection.admin?.useAsTitle || 'id',
412
+ userHasAccessToAllTenants
413
+ }
414
+ }
415
+ ] : []
416
+ ],
417
+ beforeNav: [
418
+ ...incomingConfig.admin?.components?.beforeNav || [],
419
+ {
420
+ clientProps: {
421
+ enabledSlugs: [
422
+ ...collectionSlugs,
423
+ ...globalCollectionSlugs,
424
+ adminUsersCollection.slug,
425
+ tenantCollection.slug
426
+ ],
427
+ label: pluginConfig.tenantSelectorLabel || undefined
428
+ },
429
+ path: '@payloadcms/plugin-multi-tenant/rsc#TenantSelector'
430
+ }
431
+ ],
432
+ providers: [
433
+ ...incomingConfig.admin?.components?.providers || [],
434
+ {
435
+ clientProps: {
436
+ tenantsArrayFieldName,
437
+ tenantsArrayTenantFieldName,
438
+ tenantsCollectionSlug: tenantCollection.slug,
439
+ useAsTitle: tenantCollection.admin?.useAsTitle || 'id',
440
+ userHasAccessToAllTenants
441
+ },
442
+ path: '@payloadcms/plugin-multi-tenant/rsc#TenantSelectionProvider'
443
+ }
444
+ ]
387
445
  }
388
- });
389
- }
390
- /**
391
- * Add tenant selector to admin UI
392
- */ incomingConfig.admin.components.beforeNav.push({
393
- clientProps: {
394
- enabledSlugs: [
395
- ...collectionSlugs,
396
- ...globalCollectionSlugs,
397
- adminUsersCollection.slug,
398
- tenantCollection.slug
399
- ],
400
- label: pluginConfig.tenantSelectorLabel || undefined
401
446
  },
402
- path: '@payloadcms/plugin-multi-tenant/rsc#TenantSelector'
403
- });
404
- /**
405
- * Merge plugin translations
406
- */ if (!incomingConfig.i18n) {
407
- incomingConfig.i18n = {};
408
- }
409
- Object.entries(translations).forEach(([locale, pluginI18nObject])=>{
410
- const typedLocale = locale;
411
- if (!incomingConfig.i18n.translations) {
412
- incomingConfig.i18n.translations = {};
413
- }
414
- if (!(typedLocale in incomingConfig.i18n.translations)) {
415
- incomingConfig.i18n.translations[typedLocale] = {};
416
- }
417
- if (!('plugin-multi-tenant' in incomingConfig.i18n.translations[typedLocale])) {
418
- ;
419
- incomingConfig.i18n.translations[typedLocale]['plugin-multi-tenant'] = {};
447
+ i18n: {
448
+ ...incomingConfig.i18n,
449
+ translations: {
450
+ ...incomingConfig.i18n?.translations,
451
+ ...pluginTranslations
452
+ }
420
453
  }
421
- ;
422
- incomingConfig.i18n.translations[typedLocale]['plugin-multi-tenant'] = {
423
- ...pluginI18nObject.translations['plugin-multi-tenant'],
424
- ...pluginConfig.i18n?.translations?.[typedLocale] || {}
425
- };
426
- });
427
- return incomingConfig;
428
- };
454
+ };
455
+ }
456
+ });
429
457
 
430
458
  //# sourceMappingURL=index.js.map