Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-multi-tenant): fixed hardcoded user tenants field #10782

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ export const tenantsArrayField = (args: {
arrayFieldAccess?: ArrayField['access']
rowFields?: ArrayField['fields']
tenantFieldAccess?: RelationshipField['access']
tenantsArrayFieldName: ArrayField['name']
tenantsArrayTenantFieldName: RelationshipField['name']
tenantsCollectionSlug: string
}): ArrayField => ({
name: 'tenants',
name: args.tenantsArrayFieldName,
type: 'array',
access: args?.arrayFieldAccess,
fields: [
{
name: 'tenant',
name: args.tenantsArrayTenantFieldName,
type: 'relationship',
access: args.tenantFieldAccess,
index: true,
Expand Down
10 changes: 9 additions & 1 deletion packages/plugin-multi-tenant/src/hooks/afterTenantDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Args = {
tenantFieldName: string
tenantsCollectionSlug: string
usersSlug: string
usersTenantsArrayFieldName: string
usersTenantsArrayTenantFieldName: string
}
/**
* Add cleanup logic when tenant is deleted
Expand All @@ -30,6 +32,8 @@ export const addTenantCleanup = ({
tenantFieldName,
tenantsCollectionSlug,
usersSlug,
usersTenantsArrayFieldName,
usersTenantsArrayTenantFieldName,
}: Args) => {
if (!collection.hooks) {
collection.hooks = {}
Expand All @@ -43,6 +47,8 @@ export const addTenantCleanup = ({
tenantFieldName,
tenantsCollectionSlug,
usersSlug,
usersTenantsArrayFieldName,
usersTenantsArrayTenantFieldName,
}),
)
}
Expand All @@ -53,6 +59,8 @@ export const afterTenantDelete =
tenantFieldName,
tenantsCollectionSlug,
usersSlug,
usersTenantsArrayFieldName,
usersTenantsArrayTenantFieldName,
}: Omit<Args, 'collection'>): CollectionAfterDeleteHook =>
async ({ id, req }) => {
const idType = getCollectionIDType({
Expand Down Expand Up @@ -95,7 +103,7 @@ export const afterTenantDelete =
depth: 0,
limit: 0,
where: {
'tenants.tenant': {
[`${usersTenantsArrayFieldName}.${usersTenantsArrayTenantFieldName}`]: {
equals: id,
},
},
Expand Down
13 changes: 11 additions & 2 deletions packages/plugin-multi-tenant/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { withTenantListFilter } from './utilities/withTenantListFilter.js'
const defaults = {
tenantCollectionSlug: 'tenants',
tenantFieldName: 'tenant',
userTenantsArrayFieldName: 'tenants',
tenantsArrayFieldName: 'tenants',
tenantsArrayTenantFieldName: 'tenant',
}

export const multiTenantPlugin =
Expand All @@ -34,6 +35,10 @@ export const multiTenantPlugin =
const tenantsCollectionSlug = (pluginConfig.tenantsSlug =
pluginConfig.tenantsSlug || defaults.tenantCollectionSlug)
const tenantFieldName = pluginConfig?.tenantField?.name || defaults.tenantFieldName
const tenantsArrayFieldName =
pluginConfig?.tenantsArrayField?.arrayFieldName || defaults.tenantsArrayFieldName
const tenantsArrayTenantFieldName =
pluginConfig?.tenantsArrayField?.arrayTenantFieldName || defaults.tenantsArrayTenantFieldName

/**
* Add defaults for admin properties
Expand Down Expand Up @@ -83,14 +88,16 @@ export const multiTenantPlugin =
adminUsersCollection.fields.push(
tenantsArrayField({
...(pluginConfig?.tenantsArrayField || {}),
tenantsArrayFieldName,
tenantsArrayTenantFieldName,
tenantsCollectionSlug,
}),
)
}

addCollectionAccess({
collection: adminUsersCollection,
fieldName: 'tenants.tenant',
fieldName: `${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`,
userHasAccessToAllTenants,
})

Expand Down Expand Up @@ -143,6 +150,8 @@ export const multiTenantPlugin =
tenantFieldName,
tenantsCollectionSlug,
usersSlug: adminUsersCollection.slug,
usersTenantsArrayFieldName: tenantsArrayFieldName,
usersTenantsArrayTenantFieldName: tenantsArrayTenantFieldName,
})
}
} else if (pluginConfig.collections?.[collection.slug]) {
Expand Down
14 changes: 14 additions & 0 deletions packages/plugin-multi-tenant/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ export type MultiTenantPluginConfig<ConfigTypes = unknown> = {
* Access configuration for the array field
*/
arrayFieldAccess?: ArrayField['access']
/**
* Name of the array field
*
* @default 'tenants'
*/
arrayFieldName?: string
/**
* Name of the tenant field
*
* @default 'tenant'
*/
arrayTenantFieldName?: string
/**
* When `includeDefaultField` is `true`, the field will be added to the users collection automatically
*/
Expand All @@ -86,6 +98,8 @@ export type MultiTenantPluginConfig<ConfigTypes = unknown> = {
}
| {
arrayFieldAccess?: never
arrayFieldName?: string
arrayTenantFieldName?: string
/**
* When `includeDefaultField` is `false`, you must include the field on your users collection manually
*/
Expand Down
42 changes: 42 additions & 0 deletions test/hooks/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export interface FieldPath {
id?: string | null;
}[]
| null;
fieldWithinRowWithinArray?: string | null;
id?: string | null;
}[]
| null;
Expand Down Expand Up @@ -371,6 +372,42 @@ export interface FieldPath {
| number
| boolean
| null;
fieldWithinRowWithinArray_beforeValidate_FieldPaths?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
fieldWithinRowWithinArray_beforeChange_FieldPaths?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
fieldWithinRowWithinArray_afterRead_FieldPaths?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
fieldWithinRowWithinArray_beforeDuplicate_FieldPaths?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
fieldWithinRow_beforeValidate_FieldPaths?:
| {
[k: string]: unknown;
Expand Down Expand Up @@ -772,6 +809,7 @@ export interface FieldPathsSelect<T extends boolean = true> {
fieldWithinNestedArray?: T;
id?: T;
};
fieldWithinRowWithinArray?: T;
id?: T;
};
fieldWithinRow?: T;
Expand All @@ -794,6 +832,10 @@ export interface FieldPathsSelect<T extends boolean = true> {
fieldWithinNestedArray_beforeChange_FieldPaths?: T;
fieldWithinNestedArray_afterRead_FieldPaths?: T;
fieldWithinNestedArray_beforeDuplicate_FieldPaths?: T;
fieldWithinRowWithinArray_beforeValidate_FieldPaths?: T;
fieldWithinRowWithinArray_beforeChange_FieldPaths?: T;
fieldWithinRowWithinArray_afterRead_FieldPaths?: T;
fieldWithinRowWithinArray_beforeDuplicate_FieldPaths?: T;
fieldWithinRow_beforeValidate_FieldPaths?: T;
fieldWithinRow_beforeChange_FieldPaths?: T;
fieldWithinRow_afterRead_FieldPaths?: T;
Expand Down