@payloadcms/plugin-multi-tenant 4.0.0-internal.688c4d0 → 4.0.0-internal.6e89a16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -1
- package/dist/components/AssignTenantFieldModal/index.client.d.ts +2 -2
- package/dist/components/AssignTenantFieldModal/index.client.d.ts.map +1 -1
- package/dist/components/AssignTenantFieldModal/index.client.js.map +1 -1
- package/dist/components/GlobalViewRedirect/index.d.ts +1 -1
- package/dist/components/GlobalViewRedirect/index.d.ts.map +1 -1
- package/dist/components/GlobalViewRedirect/index.js.map +1 -1
- package/dist/components/TenantField/index.client.d.ts.map +1 -1
- package/dist/components/TenantField/index.client.js +84 -74
- package/dist/components/TenantField/index.client.js.map +1 -1
- package/dist/components/TenantSelector/index.client.d.ts +1 -1
- package/dist/components/TenantSelector/index.client.d.ts.map +1 -1
- package/dist/components/TenantSelector/index.css +4 -4
- package/dist/components/TenantSelector/index.d.ts.map +1 -1
- package/dist/defaults.d.ts.map +1 -1
- package/dist/endpoints/getTenantOptionsEndpoint.d.ts +2 -2
- package/dist/endpoints/getTenantOptionsEndpoint.d.ts.map +1 -1
- package/dist/endpoints/getTenantOptionsEndpoint.js.map +1 -1
- package/dist/fields/tenantField/index.d.ts +4 -2
- package/dist/fields/tenantField/index.d.ts.map +1 -1
- package/dist/fields/tenantField/index.js +36 -9
- package/dist/fields/tenantField/index.js.map +1 -1
- package/dist/fields/tenantsArrayField/index.d.ts.map +1 -1
- package/dist/filters/filterDocumentsByTenants.d.ts +3 -3
- package/dist/filters/filterDocumentsByTenants.d.ts.map +1 -1
- package/dist/filters/filterDocumentsByTenants.js.map +1 -1
- package/dist/hooks/afterTenantDelete.d.ts +2 -2
- package/dist/hooks/afterTenantDelete.d.ts.map +1 -1
- package/dist/hooks/afterTenantDelete.js +24 -17
- package/dist/hooks/afterTenantDelete.js.map +1 -1
- package/dist/hooks/enforceTenantMembership.d.ts +15 -0
- package/dist/hooks/enforceTenantMembership.d.ts.map +1 -0
- package/dist/hooks/enforceTenantMembership.js +39 -0
- package/dist/hooks/enforceTenantMembership.js.map +1 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +293 -195
- package/dist/index.js.map +1 -1
- package/dist/providers/TenantSelectionProvider/index.client.d.ts.map +1 -1
- package/dist/providers/TenantSelectionProvider/index.client.js +44 -24
- package/dist/providers/TenantSelectionProvider/index.client.js.map +1 -1
- package/dist/providers/TenantSelectionProvider/index.d.ts +5 -5
- package/dist/providers/TenantSelectionProvider/index.d.ts.map +1 -1
- package/dist/providers/TenantSelectionProvider/index.js.map +1 -1
- package/dist/translations/languages/en.d.ts.map +1 -1
- package/dist/types.d.ts +9 -9
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utilities/addCollectionAccess.d.ts +9 -12
- package/dist/utilities/addCollectionAccess.d.ts.map +1 -1
- package/dist/utilities/addCollectionAccess.js +39 -27
- package/dist/utilities/addCollectionAccess.js.map +1 -1
- package/dist/utilities/addFilterOptionsToFields.d.ts +4 -5
- package/dist/utilities/addFilterOptionsToFields.d.ts.map +1 -1
- package/dist/utilities/addFilterOptionsToFields.js +37 -40
- package/dist/utilities/addFilterOptionsToFields.js.map +1 -1
- package/dist/utilities/collectionAccessKeys.d.ts +3 -0
- package/dist/utilities/collectionAccessKeys.d.ts.map +1 -0
- package/dist/utilities/collectionAccessKeys.js +11 -0
- package/dist/utilities/collectionAccessKeys.js.map +1 -0
- package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts +7 -0
- package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts.map +1 -0
- package/dist/utilities/combineBaseAccessWithTenantAccess.js +35 -0
- package/dist/utilities/combineBaseAccessWithTenantAccess.js.map +1 -0
- package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts +4 -0
- package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts.map +1 -0
- package/dist/utilities/combineCollectionAccessWithTenantAccess.js +26 -0
- package/dist/utilities/combineCollectionAccessWithTenantAccess.js.map +1 -0
- package/dist/utilities/combineFilters.d.ts.map +1 -1
- package/dist/utilities/extractID.d.ts.map +1 -1
- package/dist/utilities/generateCookie.d.ts.map +1 -1
- package/dist/utilities/getCollectionIDType.d.ts +1 -1
- package/dist/utilities/getCollectionIDType.d.ts.map +1 -1
- package/dist/utilities/getGlobalViewRedirect.d.ts +3 -3
- package/dist/utilities/getGlobalViewRedirect.d.ts.map +1 -1
- package/dist/utilities/getGlobalViewRedirect.js +24 -18
- package/dist/utilities/getGlobalViewRedirect.js.map +1 -1
- package/dist/utilities/getTenantAccessResult.d.ts +9 -0
- package/dist/utilities/getTenantAccessResult.d.ts.map +1 -0
- package/dist/utilities/getTenantAccessResult.js +46 -0
- package/dist/utilities/getTenantAccessResult.js.map +1 -0
- package/dist/utilities/getTenantIDsFromValue.d.ts +2 -0
- package/dist/utilities/getTenantIDsFromValue.d.ts.map +1 -0
- package/dist/utilities/getTenantIDsFromValue.js +14 -0
- package/dist/utilities/getTenantIDsFromValue.js.map +1 -0
- package/dist/utilities/getTenantOptions.d.ts +3 -3
- package/dist/utilities/getTenantOptions.d.ts.map +1 -1
- package/dist/utilities/getTenantOptions.js.map +1 -1
- package/dist/utilities/getUserTenantIDs.d.ts.map +1 -1
- package/dist/utilities/getUserTenantIDs.js +5 -3
- package/dist/utilities/getUserTenantIDs.js.map +1 -1
- package/dist/utilities/isValidTenantAssignment.d.ts +13 -0
- package/dist/utilities/isValidTenantAssignment.d.ts.map +1 -0
- package/dist/utilities/isValidTenantAssignment.js +28 -0
- package/dist/utilities/isValidTenantAssignment.js.map +1 -0
- package/dist/utilities/miniChalk.d.ts.map +1 -1
- package/dist/utilities/shouldRefreshTenantSelection.d.ts +5 -0
- package/dist/utilities/shouldRefreshTenantSelection.d.ts.map +1 -0
- package/dist/utilities/shouldRefreshTenantSelection.js +3 -0
- package/dist/utilities/shouldRefreshTenantSelection.js.map +1 -0
- package/package.json +24 -28
- package/dist/@types/assets.d.js +0 -2
- package/dist/@types/assets.d.js.map +0 -1
- package/dist/utilities/combineWhereConstraints.d.ts +0 -3
- package/dist/utilities/combineWhereConstraints.d.ts.map +0 -1
- package/dist/utilities/combineWhereConstraints.js +0 -19
- package/dist/utilities/combineWhereConstraints.js.map +0 -1
- package/dist/utilities/withTenantAccess.d.ts +0 -16
- package/dist/utilities/withTenantAccess.d.ts.map +0 -1
- package/dist/utilities/withTenantAccess.js +0 -76
- 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 = (
|
|
15
|
+
export const multiTenantPlugin = definePlugin({
|
|
16
|
+
slug: '@payloadcms/plugin-multi-tenant',
|
|
17
|
+
plugin: ({ config, options: pluginConfig })=>{
|
|
14
18
|
if (pluginConfig.enabled === false) {
|
|
15
|
-
return
|
|
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
|
|
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
|
|
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 (!
|
|
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
|
|
64
|
-
...
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
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
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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)=>{
|
|
@@ -114,7 +111,8 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
|
|
|
114
111
|
const multiTenantCollectionsFound = [];
|
|
115
112
|
/**
|
|
116
113
|
* Modify collections
|
|
117
|
-
*/ incomingConfig.collections.
|
|
114
|
+
*/ const collections = (incomingConfig.collections || []).map((originalCollection)=>{
|
|
115
|
+
let collection = originalCollection.slug === adminUsersCollection.slug ? adminUsersCollection : originalCollection;
|
|
118
116
|
/**
|
|
119
117
|
* Modify tenants collection
|
|
120
118
|
*/ if (collection.slug === tenantsCollectionSlug) {
|
|
@@ -123,7 +121,7 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
|
|
|
123
121
|
/**
|
|
124
122
|
* Add access control constraint to tenants collection
|
|
125
123
|
* - constrains access a users assigned tenants
|
|
126
|
-
*/
|
|
124
|
+
*/ collectionAccessConfigs.push({
|
|
127
125
|
adminUsersSlug: adminUsersCollection.slug,
|
|
128
126
|
collection,
|
|
129
127
|
fieldName: 'id',
|
|
@@ -136,28 +134,31 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
|
|
|
136
134
|
/**
|
|
137
135
|
* Add list filter to tenants collection
|
|
138
136
|
* - filter by selected tenant
|
|
139
|
-
*/
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
+
})
|
|
152
152
|
})
|
|
153
|
-
|
|
153
|
+
}
|
|
154
|
+
};
|
|
154
155
|
}
|
|
155
156
|
if (pluginConfig.cleanupAfterTenantDelete !== false) {
|
|
156
157
|
/**
|
|
157
158
|
* Add cleanup logic when tenant is deleted
|
|
158
159
|
* - delete documents related to tenant
|
|
159
160
|
* - remove tenant from users
|
|
160
|
-
*/ addTenantCleanup({
|
|
161
|
+
*/ collection = addTenantCleanup({
|
|
161
162
|
collection,
|
|
162
163
|
enabledSlugs: [
|
|
163
164
|
...collectionSlugs,
|
|
@@ -172,101 +173,153 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
|
|
|
172
173
|
}
|
|
173
174
|
/**
|
|
174
175
|
* Add custom tenant field that watches and dispatches updates to the selector
|
|
175
|
-
*/ collection
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
+
}
|
|
182
203
|
}
|
|
183
|
-
},
|
|
184
|
-
disabled: {
|
|
185
|
-
bulkEdit: true,
|
|
186
|
-
column: true
|
|
187
204
|
}
|
|
188
|
-
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
...collection.endpoints || [],
|
|
192
|
-
getTenantOptionsEndpoint({
|
|
193
|
-
tenantsArrayFieldName,
|
|
194
|
-
tenantsArrayTenantFieldName,
|
|
195
|
-
tenantsCollectionSlug,
|
|
196
|
-
useAsTitle: tenantCollection.admin?.useAsTitle || 'id',
|
|
197
|
-
userHasAccessToAllTenants
|
|
198
|
-
})
|
|
199
|
-
];
|
|
205
|
+
]
|
|
206
|
+
};
|
|
207
|
+
tenantCollection = collection;
|
|
200
208
|
} else if (pluginConfig.collections?.[collection.slug]) {
|
|
201
209
|
if (collection.slug === adminUsersCollection.slug) {
|
|
202
210
|
// eslint-disable-next-line no-console
|
|
203
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.`);
|
|
204
|
-
return;
|
|
212
|
+
return collection;
|
|
205
213
|
}
|
|
206
214
|
multiTenantCollectionsFound.push(collection.slug);
|
|
207
215
|
const isGlobal = Boolean(pluginConfig.collections[collection.slug]?.isGlobal);
|
|
208
216
|
if (isGlobal) {
|
|
209
|
-
collection
|
|
217
|
+
collection = {
|
|
218
|
+
...collection,
|
|
219
|
+
disableDuplicate: true
|
|
220
|
+
};
|
|
210
221
|
}
|
|
211
222
|
if (!pluginConfig.debug && !isGlobal) {
|
|
212
|
-
collection
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
+
};
|
|
219
241
|
}
|
|
220
242
|
/**
|
|
221
243
|
* Add filter options to all relationship fields
|
|
222
|
-
*/ collection
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
tenantsArrayTenantFieldName,
|
|
231
|
-
tenantsCollectionSlug,
|
|
232
|
-
userHasAccessToAllTenants
|
|
233
|
-
});
|
|
234
|
-
if (pluginConfig.collections[collection.slug]?.customTenantField !== true) {
|
|
235
|
-
/**
|
|
236
|
-
* Add tenant field to enabled collections
|
|
237
|
-
*/ collection.fields.unshift(tenantField({
|
|
238
|
-
name: tenantFieldName,
|
|
239
|
-
debug: pluginConfig.debug,
|
|
240
|
-
isAutosaveEnabled: hasAutosaveEnabled(collection),
|
|
241
|
-
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,
|
|
242
252
|
tenantsArrayFieldName,
|
|
243
253
|
tenantsArrayTenantFieldName,
|
|
244
254
|
tenantsCollectionSlug,
|
|
245
|
-
|
|
246
|
-
})
|
|
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
|
+
};
|
|
247
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
|
+
};
|
|
248
296
|
const { useBaseFilter, useBaseListFilter } = pluginConfig.collections[collection.slug] || {};
|
|
249
297
|
if (useBaseFilter ?? useBaseListFilter ?? true) {
|
|
250
298
|
/**
|
|
251
299
|
* Add list filter to enabled collections
|
|
252
300
|
* - filters results by selected tenant
|
|
253
|
-
*/ collection
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
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
|
+
})
|
|
263
315
|
})
|
|
264
|
-
|
|
316
|
+
}
|
|
317
|
+
};
|
|
265
318
|
}
|
|
266
319
|
if (pluginConfig.collections[collection.slug]?.useTenantAccess !== false) {
|
|
267
320
|
/**
|
|
268
321
|
* Add access control constraint to tenant enabled collection
|
|
269
|
-
*/
|
|
322
|
+
*/ collectionAccessConfigs.push({
|
|
270
323
|
accessResultCallback: pluginConfig.collections[collection.slug]?.accessResultOverride,
|
|
271
324
|
adminUsersSlug: adminUsersCollection.slug,
|
|
272
325
|
collection,
|
|
@@ -277,7 +330,39 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
|
|
|
277
330
|
});
|
|
278
331
|
}
|
|
279
332
|
}
|
|
333
|
+
return collection;
|
|
280
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
|
+
}
|
|
281
366
|
if (!tenantCollection) {
|
|
282
367
|
throw new Error(`Tenants collection not found with slug: ${tenantsCollectionSlug}`);
|
|
283
368
|
}
|
|
@@ -289,72 +374,85 @@ export const multiTenantPlugin = (pluginConfig)=>(incomingConfig)=>{
|
|
|
289
374
|
// eslint-disable-next-line no-console
|
|
290
375
|
console.error(miniChalk.yellowBold('WARNING (plugin-multi-tenant)'), 'missing collections', missingSlugs, 'try placing the multi-tenant plugin after other plugins.');
|
|
291
376
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
clientProps: {
|
|
296
|
-
tenantsArrayFieldName,
|
|
297
|
-
tenantsArrayTenantFieldName,
|
|
298
|
-
tenantsCollectionSlug: tenantCollection.slug,
|
|
299
|
-
useAsTitle: tenantCollection.admin?.useAsTitle || 'id',
|
|
300
|
-
userHasAccessToAllTenants
|
|
301
|
-
},
|
|
302
|
-
path: '@payloadcms/plugin-multi-tenant/rsc#TenantSelectionProvider'
|
|
377
|
+
incomingConfig = addCollectionAccess({
|
|
378
|
+
collectionAccessConfigs,
|
|
379
|
+
config: incomingConfig
|
|
303
380
|
});
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
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
|
+
]
|
|
317
445
|
}
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* Add tenant selector to admin UI
|
|
322
|
-
*/ incomingConfig.admin.components.beforeNav.push({
|
|
323
|
-
clientProps: {
|
|
324
|
-
enabledSlugs: [
|
|
325
|
-
...collectionSlugs,
|
|
326
|
-
...globalCollectionSlugs,
|
|
327
|
-
adminUsersCollection.slug,
|
|
328
|
-
tenantCollection.slug
|
|
329
|
-
],
|
|
330
|
-
label: pluginConfig.tenantSelectorLabel || undefined
|
|
331
446
|
},
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}
|
|
339
|
-
Object.entries(translations).forEach(([locale, pluginI18nObject])=>{
|
|
340
|
-
const typedLocale = locale;
|
|
341
|
-
if (!incomingConfig.i18n.translations) {
|
|
342
|
-
incomingConfig.i18n.translations = {};
|
|
343
|
-
}
|
|
344
|
-
if (!(typedLocale in incomingConfig.i18n.translations)) {
|
|
345
|
-
incomingConfig.i18n.translations[typedLocale] = {};
|
|
346
|
-
}
|
|
347
|
-
if (!('plugin-multi-tenant' in incomingConfig.i18n.translations[typedLocale])) {
|
|
348
|
-
;
|
|
349
|
-
incomingConfig.i18n.translations[typedLocale]['plugin-multi-tenant'] = {};
|
|
447
|
+
i18n: {
|
|
448
|
+
...incomingConfig.i18n,
|
|
449
|
+
translations: {
|
|
450
|
+
...incomingConfig.i18n?.translations,
|
|
451
|
+
...pluginTranslations
|
|
452
|
+
}
|
|
350
453
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
...pluginConfig.i18n?.translations?.[typedLocale] || {}
|
|
355
|
-
};
|
|
356
|
-
});
|
|
357
|
-
return incomingConfig;
|
|
358
|
-
};
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
});
|
|
359
457
|
|
|
360
458
|
//# sourceMappingURL=index.js.map
|