@payloadcms/plugin-multi-tenant 4.0.0-internal.b8e7c89 → 4.0.0-internal.bd1d413

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