Skip to content

Commit 9638dbe

Browse files
authored
fix(plugin-multi-tenant): fixed hardcoded user tenants field (#10782)
### What? When using custom slugs and field names the tenancy field added to the users would still attempt to use `tenants` and fail. ### Why? The tenant/tenancy are hardcoded in `tenantsArrayField()` ### How? Added the same args that are used in `tenantsField()` for the field names and relation.
1 parent 2f66bdc commit 9638dbe

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ export const tenantsArrayField = (args: {
44
arrayFieldAccess?: ArrayField['access']
55
rowFields?: ArrayField['fields']
66
tenantFieldAccess?: RelationshipField['access']
7+
tenantsArrayFieldName: ArrayField['name']
8+
tenantsArrayTenantFieldName: RelationshipField['name']
79
tenantsCollectionSlug: string
810
}): ArrayField => ({
9-
name: 'tenants',
11+
name: args.tenantsArrayFieldName,
1012
type: 'array',
1113
access: args?.arrayFieldAccess,
1214
fields: [
1315
{
14-
name: 'tenant',
16+
name: args.tenantsArrayTenantFieldName,
1517
type: 'relationship',
1618
access: args.tenantFieldAccess,
1719
index: true,

packages/plugin-multi-tenant/src/hooks/afterTenantDelete.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type Args = {
1818
tenantFieldName: string
1919
tenantsCollectionSlug: string
2020
usersSlug: string
21+
usersTenantsArrayFieldName: string
22+
usersTenantsArrayTenantFieldName: string
2123
}
2224
/**
2325
* Add cleanup logic when tenant is deleted
@@ -30,6 +32,8 @@ export const addTenantCleanup = ({
3032
tenantFieldName,
3133
tenantsCollectionSlug,
3234
usersSlug,
35+
usersTenantsArrayFieldName,
36+
usersTenantsArrayTenantFieldName,
3337
}: Args) => {
3438
if (!collection.hooks) {
3539
collection.hooks = {}
@@ -43,6 +47,8 @@ export const addTenantCleanup = ({
4347
tenantFieldName,
4448
tenantsCollectionSlug,
4549
usersSlug,
50+
usersTenantsArrayFieldName,
51+
usersTenantsArrayTenantFieldName,
4652
}),
4753
)
4854
}
@@ -53,6 +59,8 @@ export const afterTenantDelete =
5359
tenantFieldName,
5460
tenantsCollectionSlug,
5561
usersSlug,
62+
usersTenantsArrayFieldName,
63+
usersTenantsArrayTenantFieldName,
5664
}: Omit<Args, 'collection'>): CollectionAfterDeleteHook =>
5765
async ({ id, req }) => {
5866
const idType = getCollectionIDType({
@@ -95,7 +103,7 @@ export const afterTenantDelete =
95103
depth: 0,
96104
limit: 0,
97105
where: {
98-
'tenants.tenant': {
106+
[`${usersTenantsArrayFieldName}.${usersTenantsArrayTenantFieldName}`]: {
99107
equals: id,
100108
},
101109
},

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { withTenantListFilter } from './utilities/withTenantListFilter.js'
1212
const defaults = {
1313
tenantCollectionSlug: 'tenants',
1414
tenantFieldName: 'tenant',
15-
userTenantsArrayFieldName: 'tenants',
15+
tenantsArrayFieldName: 'tenants',
16+
tenantsArrayTenantFieldName: 'tenant',
1617
}
1718

1819
export const multiTenantPlugin =
@@ -34,6 +35,10 @@ export const multiTenantPlugin =
3435
const tenantsCollectionSlug = (pluginConfig.tenantsSlug =
3536
pluginConfig.tenantsSlug || defaults.tenantCollectionSlug)
3637
const tenantFieldName = pluginConfig?.tenantField?.name || defaults.tenantFieldName
38+
const tenantsArrayFieldName =
39+
pluginConfig?.tenantsArrayField?.arrayFieldName || defaults.tenantsArrayFieldName
40+
const tenantsArrayTenantFieldName =
41+
pluginConfig?.tenantsArrayField?.arrayTenantFieldName || defaults.tenantsArrayTenantFieldName
3742

3843
/**
3944
* Add defaults for admin properties
@@ -83,14 +88,16 @@ export const multiTenantPlugin =
8388
adminUsersCollection.fields.push(
8489
tenantsArrayField({
8590
...(pluginConfig?.tenantsArrayField || {}),
91+
tenantsArrayFieldName,
92+
tenantsArrayTenantFieldName,
8693
tenantsCollectionSlug,
8794
}),
8895
)
8996
}
9097

9198
addCollectionAccess({
9299
collection: adminUsersCollection,
93-
fieldName: 'tenants.tenant',
100+
fieldName: `${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`,
94101
userHasAccessToAllTenants,
95102
})
96103

@@ -143,6 +150,8 @@ export const multiTenantPlugin =
143150
tenantFieldName,
144151
tenantsCollectionSlug,
145152
usersSlug: adminUsersCollection.slug,
153+
usersTenantsArrayFieldName: tenantsArrayFieldName,
154+
usersTenantsArrayTenantFieldName: tenantsArrayTenantFieldName,
146155
})
147156
}
148157
} else if (pluginConfig.collections?.[collection.slug]) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ export type MultiTenantPluginConfig<ConfigTypes = unknown> = {
7171
* Access configuration for the array field
7272
*/
7373
arrayFieldAccess?: ArrayField['access']
74+
/**
75+
* Name of the array field
76+
*
77+
* @default 'tenants'
78+
*/
79+
arrayFieldName?: string
80+
/**
81+
* Name of the tenant field
82+
*
83+
* @default 'tenant'
84+
*/
85+
arrayTenantFieldName?: string
7486
/**
7587
* When `includeDefaultField` is `true`, the field will be added to the users collection automatically
7688
*/
@@ -86,6 +98,8 @@ export type MultiTenantPluginConfig<ConfigTypes = unknown> = {
8698
}
8799
| {
88100
arrayFieldAccess?: never
101+
arrayFieldName?: string
102+
arrayTenantFieldName?: string
89103
/**
90104
* When `includeDefaultField` is `false`, you must include the field on your users collection manually
91105
*/

test/hooks/payload-types.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export interface FieldPath {
254254
id?: string | null;
255255
}[]
256256
| null;
257+
fieldWithinRowWithinArray?: string | null;
257258
id?: string | null;
258259
}[]
259260
| null;
@@ -371,6 +372,42 @@ export interface FieldPath {
371372
| number
372373
| boolean
373374
| null;
375+
fieldWithinRowWithinArray_beforeValidate_FieldPaths?:
376+
| {
377+
[k: string]: unknown;
378+
}
379+
| unknown[]
380+
| string
381+
| number
382+
| boolean
383+
| null;
384+
fieldWithinRowWithinArray_beforeChange_FieldPaths?:
385+
| {
386+
[k: string]: unknown;
387+
}
388+
| unknown[]
389+
| string
390+
| number
391+
| boolean
392+
| null;
393+
fieldWithinRowWithinArray_afterRead_FieldPaths?:
394+
| {
395+
[k: string]: unknown;
396+
}
397+
| unknown[]
398+
| string
399+
| number
400+
| boolean
401+
| null;
402+
fieldWithinRowWithinArray_beforeDuplicate_FieldPaths?:
403+
| {
404+
[k: string]: unknown;
405+
}
406+
| unknown[]
407+
| string
408+
| number
409+
| boolean
410+
| null;
374411
fieldWithinRow_beforeValidate_FieldPaths?:
375412
| {
376413
[k: string]: unknown;
@@ -772,6 +809,7 @@ export interface FieldPathsSelect<T extends boolean = true> {
772809
fieldWithinNestedArray?: T;
773810
id?: T;
774811
};
812+
fieldWithinRowWithinArray?: T;
775813
id?: T;
776814
};
777815
fieldWithinRow?: T;
@@ -794,6 +832,10 @@ export interface FieldPathsSelect<T extends boolean = true> {
794832
fieldWithinNestedArray_beforeChange_FieldPaths?: T;
795833
fieldWithinNestedArray_afterRead_FieldPaths?: T;
796834
fieldWithinNestedArray_beforeDuplicate_FieldPaths?: T;
835+
fieldWithinRowWithinArray_beforeValidate_FieldPaths?: T;
836+
fieldWithinRowWithinArray_beforeChange_FieldPaths?: T;
837+
fieldWithinRowWithinArray_afterRead_FieldPaths?: T;
838+
fieldWithinRowWithinArray_beforeDuplicate_FieldPaths?: T;
797839
fieldWithinRow_beforeValidate_FieldPaths?: T;
798840
fieldWithinRow_beforeChange_FieldPaths?: T;
799841
fieldWithinRow_afterRead_FieldPaths?: T;

0 commit comments

Comments
 (0)