Skip to content

Commit b0674fa

Browse files
fix(plugin-multi-tenant): auto assign tenant when autosave is enabled (#14745)
When autosave is enabled, mutli-tenant would not auto-assign the tenant since we implemented the modal overlay allowing you to select the tenant for the new document. When autosave is enabled the doc saves and redirects to the edit view, but the user is unable to set the tenant in the modal because it does not show. So then the user would not have access to the document because there was no assigned tenant. This fix auto-assigns a tenant when autosave is enabled on a collection.
1 parent becceb7 commit b0674fa

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

packages/plugin-multi-tenant/src/fields/tenantField/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const fieldValidation =
3232

3333
type Args = {
3434
debug?: boolean
35+
isDraftsEnabled?: boolean
3536
name: string
3637
overrides?: RootTenantFieldConfigOverrides
3738
tenantsArrayFieldName: string
@@ -42,6 +43,7 @@ type Args = {
4243
export const tenantField = ({
4344
name = defaults.tenantFieldName,
4445
debug,
46+
isDraftsEnabled,
4547
overrides: _overrides = {},
4648
tenantsArrayFieldName = defaults.tenantsArrayFieldName,
4749
tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName,
@@ -102,6 +104,15 @@ export const tenantField = ({
102104
})
103105
return isValidTenant ? tenantFromCookie : null
104106
}
107+
if (req.user && isDraftsEnabled) {
108+
const userTenants = getUserTenantIDs(req.user, {
109+
tenantsArrayFieldName,
110+
tenantsArrayTenantFieldName,
111+
})
112+
if (userTenants.length > 0) {
113+
return userTenants[0]
114+
}
115+
}
105116
return null
106117
}),
107118
filterOptions:

packages/plugin-multi-tenant/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ export const multiTenantPlugin =
190190
tenantField({
191191
name: tenantFieldName,
192192
debug: pluginConfig.debug,
193+
isDraftsEnabled: Boolean(
194+
collection.versions &&
195+
typeof collection.versions === 'object' &&
196+
collection.versions.drafts,
197+
),
193198
overrides: pluginConfig.collections[collection.slug]?.tenantFieldOverrides
194199
? pluginConfig.collections[collection.slug]?.tenantFieldOverrides
195200
: pluginConfig.tenantField || {},
@@ -376,6 +381,11 @@ export const multiTenantPlugin =
376381
tenantField({
377382
name: tenantFieldName,
378383
debug: pluginConfig.debug,
384+
isDraftsEnabled: Boolean(
385+
collection.versions &&
386+
typeof collection.versions === 'object' &&
387+
collection.versions.drafts,
388+
),
379389
overrides: pluginConfig.collections[collection.slug]?.tenantFieldOverrides
380390
? pluginConfig.collections[collection.slug]?.tenantFieldOverrides
381391
: pluginConfig.tenantField || {},

0 commit comments

Comments
 (0)