@payloadcms/plugin-multi-tenant 4.0.0-internal.cd47d20 → 4.0.0-internal.ce66418

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