@payloadcms/plugin-multi-tenant 4.0.0-internal.f851106 → 4.0.0-internal.f980c7c

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 (80) hide show
  1. package/dist/components/AssignTenantFieldModal/index.client.d.ts +2 -2
  2. package/dist/components/AssignTenantFieldModal/index.client.d.ts.map +1 -1
  3. package/dist/components/AssignTenantFieldModal/index.client.js.map +1 -1
  4. package/dist/components/TenantField/index.client.d.ts.map +1 -1
  5. package/dist/components/TenantField/index.client.js +84 -74
  6. package/dist/components/TenantField/index.client.js.map +1 -1
  7. package/dist/components/TenantSelector/index.css +4 -4
  8. package/dist/fields/tenantField/index.d.ts +4 -2
  9. package/dist/fields/tenantField/index.d.ts.map +1 -1
  10. package/dist/fields/tenantField/index.js +36 -9
  11. package/dist/fields/tenantField/index.js.map +1 -1
  12. package/dist/filters/filterDocumentsByTenants.js.map +1 -1
  13. package/dist/hooks/afterTenantDelete.d.ts +1 -1
  14. package/dist/hooks/afterTenantDelete.d.ts.map +1 -1
  15. package/dist/hooks/afterTenantDelete.js +20 -16
  16. package/dist/hooks/afterTenantDelete.js.map +1 -1
  17. package/dist/hooks/enforceTenantMembership.d.ts +15 -0
  18. package/dist/hooks/enforceTenantMembership.d.ts.map +1 -0
  19. package/dist/hooks/enforceTenantMembership.js +39 -0
  20. package/dist/hooks/enforceTenantMembership.js.map +1 -0
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +288 -194
  23. package/dist/index.js.map +1 -1
  24. package/dist/providers/TenantSelectionProvider/index.client.d.ts.map +1 -1
  25. package/dist/providers/TenantSelectionProvider/index.client.js +22 -24
  26. package/dist/providers/TenantSelectionProvider/index.client.js.map +1 -1
  27. package/dist/types.d.ts +5 -3
  28. package/dist/types.d.ts.map +1 -1
  29. package/dist/types.js.map +1 -1
  30. package/dist/utilities/addCollectionAccess.d.ts +7 -10
  31. package/dist/utilities/addCollectionAccess.d.ts.map +1 -1
  32. package/dist/utilities/addCollectionAccess.js +39 -27
  33. package/dist/utilities/addCollectionAccess.js.map +1 -1
  34. package/dist/utilities/addFilterOptionsToFields.d.ts +2 -3
  35. package/dist/utilities/addFilterOptionsToFields.d.ts.map +1 -1
  36. package/dist/utilities/addFilterOptionsToFields.js +37 -40
  37. package/dist/utilities/addFilterOptionsToFields.js.map +1 -1
  38. package/dist/utilities/collectionAccessKeys.d.ts +3 -0
  39. package/dist/utilities/collectionAccessKeys.d.ts.map +1 -0
  40. package/dist/utilities/collectionAccessKeys.js +11 -0
  41. package/dist/utilities/collectionAccessKeys.js.map +1 -0
  42. package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts +7 -0
  43. package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts.map +1 -0
  44. package/dist/utilities/combineBaseAccessWithTenantAccess.js +35 -0
  45. package/dist/utilities/combineBaseAccessWithTenantAccess.js.map +1 -0
  46. package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts +4 -0
  47. package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts.map +1 -0
  48. package/dist/utilities/combineCollectionAccessWithTenantAccess.js +26 -0
  49. package/dist/utilities/combineCollectionAccessWithTenantAccess.js.map +1 -0
  50. package/dist/utilities/getGlobalViewRedirect.d.ts.map +1 -1
  51. package/dist/utilities/getGlobalViewRedirect.js +24 -18
  52. package/dist/utilities/getGlobalViewRedirect.js.map +1 -1
  53. package/dist/utilities/getTenantAccessResult.d.ts +9 -0
  54. package/dist/utilities/getTenantAccessResult.d.ts.map +1 -0
  55. package/dist/utilities/getTenantAccessResult.js +46 -0
  56. package/dist/utilities/getTenantAccessResult.js.map +1 -0
  57. package/dist/utilities/getTenantIDsFromValue.d.ts +2 -0
  58. package/dist/utilities/getTenantIDsFromValue.d.ts.map +1 -0
  59. package/dist/utilities/getTenantIDsFromValue.js +14 -0
  60. package/dist/utilities/getTenantIDsFromValue.js.map +1 -0
  61. package/dist/utilities/getUserTenantIDs.d.ts.map +1 -1
  62. package/dist/utilities/getUserTenantIDs.js +3 -2
  63. package/dist/utilities/getUserTenantIDs.js.map +1 -1
  64. package/dist/utilities/isValidTenantAssignment.d.ts +13 -0
  65. package/dist/utilities/isValidTenantAssignment.d.ts.map +1 -0
  66. package/dist/utilities/isValidTenantAssignment.js +28 -0
  67. package/dist/utilities/isValidTenantAssignment.js.map +1 -0
  68. package/dist/utilities/shouldRefreshTenantSelection.d.ts +5 -0
  69. package/dist/utilities/shouldRefreshTenantSelection.d.ts.map +1 -0
  70. package/dist/utilities/shouldRefreshTenantSelection.js +3 -0
  71. package/dist/utilities/shouldRefreshTenantSelection.js.map +1 -0
  72. package/package.json +24 -28
  73. package/dist/utilities/combineWhereConstraints.d.ts +0 -3
  74. package/dist/utilities/combineWhereConstraints.d.ts.map +0 -1
  75. package/dist/utilities/combineWhereConstraints.js +0 -19
  76. package/dist/utilities/combineWhereConstraints.js.map +0 -1
  77. package/dist/utilities/withTenantAccess.d.ts +0 -16
  78. package/dist/utilities/withTenantAccess.d.ts.map +0 -1
  79. package/dist/utilities/withTenantAccess.js +0 -76
  80. package/dist/utilities/withTenantAccess.js.map +0 -1
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ import { tenantField } from './fields/tenantField/index.js';
6
6
  import { tenantsArrayField } from './fields/tenantsArrayField/index.js';
7
7
  import { filterDocumentsByTenants } from './filters/filterDocumentsByTenants.js';
8
8
  import { addTenantCleanup } from './hooks/afterTenantDelete.js';
9
+ import { enforceTenantMembership } from './hooks/enforceTenantMembership.js';
9
10
  import { translations } from './translations/index.js';
10
11
  import { addCollectionAccess } from './utilities/addCollectionAccess.js';
11
12
  import { addFilterOptionsToFields } from './utilities/addFilterOptionsToFields.js';
@@ -13,64 +14,54 @@ import { combineFilters } from './utilities/combineFilters.js';
13
14
  import { miniChalk } from './utilities/miniChalk.js';
14
15
  export const multiTenantPlugin = definePlugin({
15
16
  slug: '@payloadcms/plugin-multi-tenant',
16
- plugin: ({ config: incomingConfig, options: pluginConfig })=>{
17
+ plugin: ({ config, options: pluginConfig })=>{
17
18
  if (pluginConfig.enabled === false) {
18
- return incomingConfig;
19
+ return config;
19
20
  }
21
+ let incomingConfig = config;
20
22
  /**
21
23
  * Set defaults
22
24
  */ const userHasAccessToAllTenants = typeof pluginConfig.userHasAccessToAllTenants === 'function' ? pluginConfig.userHasAccessToAllTenants : ()=>false;
23
- const tenantsCollectionSlug = pluginConfig.tenantsSlug = pluginConfig.tenantsSlug || defaults.tenantCollectionSlug;
25
+ const tenantsCollectionSlug = pluginConfig.tenantsSlug || defaults.tenantCollectionSlug;
24
26
  const tenantFieldName = pluginConfig?.tenantField?.name || defaults.tenantFieldName;
25
27
  const tenantsArrayFieldName = pluginConfig?.tenantsArrayField?.arrayFieldName || defaults.tenantsArrayFieldName;
26
28
  const tenantsArrayTenantFieldName = pluginConfig?.tenantsArrayField?.arrayTenantFieldName || defaults.tenantsArrayTenantFieldName;
27
- /**
28
- * Add defaults for admin properties
29
- */ if (!incomingConfig.admin) {
30
- incomingConfig.admin = {};
31
- }
32
- if (!incomingConfig.admin?.components) {
33
- incomingConfig.admin.components = {
34
- actions: [],
35
- beforeNav: [],
36
- providers: []
37
- };
38
- }
39
- if (!incomingConfig.admin.components?.providers) {
40
- incomingConfig.admin.components.providers = [];
41
- }
42
- if (!incomingConfig.admin.components?.actions) {
43
- incomingConfig.admin.components.actions = [];
44
- }
45
- if (!incomingConfig.admin.components?.beforeNav) {
46
- incomingConfig.admin.components.beforeNav = [];
47
- }
48
- if (!incomingConfig.collections) {
49
- incomingConfig.collections = [];
50
- }
29
+ const collectionAccessConfigs = [];
51
30
  /**
52
31
  * Add tenants array field to users collection
53
- */ const adminUsersCollection = incomingConfig.collections.find(({ slug, auth })=>{
32
+ */ const originalAdminUsersCollection = incomingConfig.collections?.find(({ slug, auth })=>{
54
33
  if (incomingConfig.admin?.user) {
55
34
  return slug === incomingConfig.admin.user;
56
35
  } else if (auth) {
57
36
  return true;
58
37
  }
59
38
  });
60
- if (!adminUsersCollection) {
39
+ if (!originalAdminUsersCollection) {
61
40
  throw Error('An auth enabled collection was not found');
62
41
  }
42
+ let adminUsersCollection = originalAdminUsersCollection;
63
43
  /**
64
44
  * Add tenants array field to users collection
65
45
  */ if (pluginConfig?.tenantsArrayField?.includeDefaultField !== false) {
66
- adminUsersCollection.fields.push(tenantsArrayField({
67
- ...pluginConfig?.tenantsArrayField || {},
68
- tenantsArrayFieldName,
69
- tenantsArrayTenantFieldName,
70
- tenantsCollectionSlug
71
- }));
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
+ };
72
63
  }
73
- addCollectionAccess({
64
+ collectionAccessConfigs.push({
74
65
  accessResultCallback: pluginConfig.usersAccessResultOverride,
75
66
  adminUsersSlug: adminUsersCollection.slug,
76
67
  collection: adminUsersCollection,
@@ -80,21 +71,24 @@ export const multiTenantPlugin = definePlugin({
80
71
  userHasAccessToAllTenants
81
72
  });
82
73
  if (pluginConfig.useUsersTenantFilter !== false) {
83
- if (!adminUsersCollection.admin) {
84
- adminUsersCollection.admin = {};
85
- }
86
74
  const baseFilter = adminUsersCollection.admin?.baseFilter ?? adminUsersCollection.admin?.baseListFilter;
87
- adminUsersCollection.admin.baseFilter = combineFilters({
88
- baseFilter,
89
- customFilter: (args)=>filterDocumentsByTenants({
90
- filterFieldName: `${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`,
91
- req: args.req,
92
- tenantsArrayFieldName,
93
- tenantsArrayTenantFieldName,
94
- tenantsCollectionSlug,
95
- 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
+ })
96
89
  })
97
- });
90
+ }
91
+ };
98
92
  }
99
93
  let tenantCollection;
100
94
  const [collectionSlugs, globalCollectionSlugs] = Object.keys(pluginConfig.collections).reduce((acc, slug)=>{
@@ -117,7 +111,8 @@ export const multiTenantPlugin = definePlugin({
117
111
  const multiTenantCollectionsFound = [];
118
112
  /**
119
113
  * Modify collections
120
- */ incomingConfig.collections.forEach((collection)=>{
114
+ */ const collections = (incomingConfig.collections || []).map((originalCollection)=>{
115
+ let collection = originalCollection.slug === adminUsersCollection.slug ? adminUsersCollection : originalCollection;
121
116
  /**
122
117
  * Modify tenants collection
123
118
  */ if (collection.slug === tenantsCollectionSlug) {
@@ -126,7 +121,7 @@ export const multiTenantPlugin = definePlugin({
126
121
  /**
127
122
  * Add access control constraint to tenants collection
128
123
  * - constrains access a users assigned tenants
129
- */ addCollectionAccess({
124
+ */ collectionAccessConfigs.push({
130
125
  adminUsersSlug: adminUsersCollection.slug,
131
126
  collection,
132
127
  fieldName: 'id',
@@ -139,28 +134,31 @@ export const multiTenantPlugin = definePlugin({
139
134
  /**
140
135
  * Add list filter to tenants collection
141
136
  * - filter by selected tenant
142
- */ if (!collection.admin) {
143
- collection.admin = {};
144
- }
145
- const baseFilter = collection.admin?.baseFilter ?? collection.admin?.baseListFilter;
146
- collection.admin.baseFilter = combineFilters({
147
- baseFilter,
148
- customFilter: (args)=>filterDocumentsByTenants({
149
- filterFieldName: 'id',
150
- req: args.req,
151
- tenantsArrayFieldName,
152
- tenantsArrayTenantFieldName,
153
- tenantsCollectionSlug,
154
- 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
+ })
155
152
  })
156
- });
153
+ }
154
+ };
157
155
  }
158
156
  if (pluginConfig.cleanupAfterTenantDelete !== false) {
159
157
  /**
160
158
  * Add cleanup logic when tenant is deleted
161
159
  * - delete documents related to tenant
162
160
  * - remove tenant from users
163
- */ addTenantCleanup({
161
+ */ collection = addTenantCleanup({
164
162
  collection,
165
163
  enabledSlugs: [
166
164
  ...collectionSlugs,
@@ -175,101 +173,153 @@ export const multiTenantPlugin = definePlugin({
175
173
  }
176
174
  /**
177
175
  * Add custom tenant field that watches and dispatches updates to the selector
178
- */ collection.fields.push({
179
- name: '_watchTenant',
180
- type: 'ui',
181
- admin: {
182
- components: {
183
- Field: {
184
- 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
+ }
185
203
  }
186
- },
187
- disabled: {
188
- bulkEdit: true,
189
- column: true
190
204
  }
191
- }
192
- });
193
- collection.endpoints = [
194
- ...collection.endpoints || [],
195
- getTenantOptionsEndpoint({
196
- tenantsArrayFieldName,
197
- tenantsArrayTenantFieldName,
198
- tenantsCollectionSlug,
199
- useAsTitle: tenantCollection.admin?.useAsTitle || 'id',
200
- userHasAccessToAllTenants
201
- })
202
- ];
205
+ ]
206
+ };
207
+ tenantCollection = collection;
203
208
  } else if (pluginConfig.collections?.[collection.slug]) {
204
209
  if (collection.slug === adminUsersCollection.slug) {
205
210
  // eslint-disable-next-line no-console
206
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.`);
207
- return;
212
+ return collection;
208
213
  }
209
214
  multiTenantCollectionsFound.push(collection.slug);
210
215
  const isGlobal = Boolean(pluginConfig.collections[collection.slug]?.isGlobal);
211
216
  if (isGlobal) {
212
- collection.disableDuplicate = true;
217
+ collection = {
218
+ ...collection,
219
+ disableDuplicate: true
220
+ };
213
221
  }
214
222
  if (!pluginConfig.debug && !isGlobal) {
215
- collection.admin ??= {};
216
- collection.admin.components ??= {};
217
- collection.admin.components.edit ??= {};
218
- collection.admin.components.edit.editMenuItems ??= [];
219
- collection.admin.components.edit.editMenuItems.push({
220
- path: '@payloadcms/plugin-multi-tenant/client#AssignTenantFieldTrigger'
221
- });
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
+ };
222
241
  }
223
242
  /**
224
243
  * Add filter options to all relationship fields
225
- */ collection.fields = addFilterOptionsToFields({
226
- blockReferencesWithFilters,
227
- config: incomingConfig,
228
- fields: collection.fields,
229
- tenantEnabledCollectionSlugs: collectionSlugs,
230
- tenantEnabledGlobalSlugs: globalCollectionSlugs,
231
- tenantFieldName,
232
- tenantsArrayFieldName,
233
- tenantsArrayTenantFieldName,
234
- tenantsCollectionSlug,
235
- userHasAccessToAllTenants
236
- });
237
- if (pluginConfig.collections[collection.slug]?.customTenantField !== true) {
238
- /**
239
- * Add tenant field to enabled collections
240
- */ collection.fields.unshift(tenantField({
241
- name: tenantFieldName,
242
- debug: pluginConfig.debug,
243
- isAutosaveEnabled: hasAutosaveEnabled(collection),
244
- 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,
245
252
  tenantsArrayFieldName,
246
253
  tenantsArrayTenantFieldName,
247
254
  tenantsCollectionSlug,
248
- unique: isGlobal
249
- }));
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
+ };
250
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
+ };
251
296
  const { useBaseFilter, useBaseListFilter } = pluginConfig.collections[collection.slug] || {};
252
297
  if (useBaseFilter ?? useBaseListFilter ?? true) {
253
298
  /**
254
299
  * Add list filter to enabled collections
255
300
  * - filters results by selected tenant
256
- */ collection.admin = collection.admin || {};
257
- collection.admin.baseFilter = combineFilters({
258
- baseFilter: collection.admin?.baseFilter ?? collection.admin?.baseListFilter,
259
- customFilter: (args)=>filterDocumentsByTenants({
260
- filterFieldName: tenantFieldName,
261
- req: args.req,
262
- tenantsArrayFieldName,
263
- tenantsArrayTenantFieldName,
264
- tenantsCollectionSlug,
265
- 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
+ })
266
315
  })
267
- });
316
+ }
317
+ };
268
318
  }
269
319
  if (pluginConfig.collections[collection.slug]?.useTenantAccess !== false) {
270
320
  /**
271
321
  * Add access control constraint to tenant enabled collection
272
- */ addCollectionAccess({
322
+ */ collectionAccessConfigs.push({
273
323
  accessResultCallback: pluginConfig.collections[collection.slug]?.accessResultOverride,
274
324
  adminUsersSlug: adminUsersCollection.slug,
275
325
  collection,
@@ -280,7 +330,39 @@ export const multiTenantPlugin = definePlugin({
280
330
  });
281
331
  }
282
332
  }
333
+ return collection;
283
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
+ }
284
366
  if (!tenantCollection) {
285
367
  throw new Error(`Tenants collection not found with slug: ${tenantsCollectionSlug}`);
286
368
  }
@@ -292,72 +374,84 @@ export const multiTenantPlugin = definePlugin({
292
374
  // eslint-disable-next-line no-console
293
375
  console.error(miniChalk.yellowBold('WARNING (plugin-multi-tenant)'), 'missing collections', missingSlugs, 'try placing the multi-tenant plugin after other plugins.');
294
376
  }
295
- /**
296
- * Add TenantSelectionProvider to admin providers
297
- */ incomingConfig.admin.components.providers.push({
298
- clientProps: {
299
- tenantsArrayFieldName,
300
- tenantsArrayTenantFieldName,
301
- tenantsCollectionSlug: tenantCollection.slug,
302
- useAsTitle: tenantCollection.admin?.useAsTitle || 'id',
303
- userHasAccessToAllTenants
304
- },
305
- path: '@payloadcms/plugin-multi-tenant/rsc#TenantSelectionProvider'
377
+ incomingConfig = addCollectionAccess({
378
+ collectionAccessConfigs,
379
+ config: incomingConfig
306
380
  });
307
- /**
308
- * Add global redirect action
309
- */ if (globalCollectionSlugs.length) {
310
- incomingConfig.admin.components.actions.push({
311
- path: '@payloadcms/plugin-multi-tenant/rsc#GlobalViewRedirect',
312
- serverProps: {
313
- globalSlugs: globalCollectionSlugs,
314
- tenantFieldName,
315
- tenantsArrayFieldName,
316
- tenantsArrayTenantFieldName,
317
- tenantsCollectionSlug,
318
- useAsTitle: tenantCollection.admin?.useAsTitle || 'id',
319
- 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
+ ]
320
445
  }
321
- });
322
- }
323
- /**
324
- * Add tenant selector to admin UI
325
- */ incomingConfig.admin.components.beforeNav.push({
326
- clientProps: {
327
- enabledSlugs: [
328
- ...collectionSlugs,
329
- ...globalCollectionSlugs,
330
- adminUsersCollection.slug,
331
- tenantCollection.slug
332
- ],
333
- label: pluginConfig.tenantSelectorLabel || undefined
334
446
  },
335
- path: '@payloadcms/plugin-multi-tenant/rsc#TenantSelector'
336
- });
337
- /**
338
- * Merge plugin translations
339
- */ if (!incomingConfig.i18n) {
340
- incomingConfig.i18n = {};
341
- }
342
- Object.entries(translations).forEach(([locale, pluginI18nObject])=>{
343
- const typedLocale = locale;
344
- if (!incomingConfig.i18n.translations) {
345
- incomingConfig.i18n.translations = {};
346
- }
347
- if (!(typedLocale in incomingConfig.i18n.translations)) {
348
- incomingConfig.i18n.translations[typedLocale] = {};
349
- }
350
- if (!('plugin-multi-tenant' in incomingConfig.i18n.translations[typedLocale])) {
351
- ;
352
- incomingConfig.i18n.translations[typedLocale]['plugin-multi-tenant'] = {};
447
+ i18n: {
448
+ ...incomingConfig.i18n,
449
+ translations: {
450
+ ...incomingConfig.i18n?.translations,
451
+ ...pluginTranslations
452
+ }
353
453
  }
354
- ;
355
- incomingConfig.i18n.translations[typedLocale]['plugin-multi-tenant'] = {
356
- ...pluginI18nObject.translations['plugin-multi-tenant'],
357
- ...pluginConfig.i18n?.translations?.[typedLocale] || {}
358
- };
359
- });
360
- return incomingConfig;
454
+ };
361
455
  }
362
456
  });
363
457