@payloadcms/plugin-multi-tenant 4.0.0-internal.8709764 → 4.0.0-internal.8a7a5a7
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/dist/components/AssignTenantFieldModal/index.client.js +2 -2
- package/dist/components/AssignTenantFieldModal/index.client.js.map +1 -1
- package/dist/components/GlobalViewRedirect/index.d.ts +2 -2
- package/dist/components/GlobalViewRedirect/index.d.ts.map +1 -1
- package/dist/components/GlobalViewRedirect/index.js +2 -2
- 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 +8 -4
- package/dist/components/TenantField/index.client.js.map +1 -1
- package/dist/components/TenantSelector/index.client.d.ts.map +1 -1
- package/dist/components/TenantSelector/index.client.js +2 -2
- package/dist/components/TenantSelector/index.client.js.map +1 -1
- package/dist/components/TenantSelector/index.css +4 -4
- package/dist/endpoints/getTenantOptionsEndpoint.d.ts +5 -1
- package/dist/endpoints/getTenantOptionsEndpoint.d.ts.map +1 -1
- package/dist/endpoints/getTenantOptionsEndpoint.js +7 -4
- 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 +57 -9
- package/dist/fields/tenantField/index.js.map +1 -1
- package/dist/filters/filterDocumentsByTenants.js.map +1 -1
- package/dist/hooks/afterTenantDelete.d.ts +1 -1
- package/dist/hooks/afterTenantDelete.d.ts.map +1 -1
- package/dist/hooks/afterTenantDelete.js +20 -16
- 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.map +1 -1
- package/dist/index.js +288 -194
- package/dist/index.js.map +1 -1
- package/dist/providers/TenantSelectionProvider/index.client.d.ts +3 -1
- package/dist/providers/TenantSelectionProvider/index.client.d.ts.map +1 -1
- package/dist/providers/TenantSelectionProvider/index.client.js +49 -35
- package/dist/providers/TenantSelectionProvider/index.client.js.map +1 -1
- package/dist/providers/TenantSelectionProvider/index.d.ts.map +1 -1
- package/dist/providers/TenantSelectionProvider/index.js +6 -4
- package/dist/providers/TenantSelectionProvider/index.js.map +1 -1
- package/dist/types.d.ts +4 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utilities/addCollectionAccess.d.ts +7 -10
- 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 +2 -3
- 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/getGlobalViewRedirect.d.ts.map +1 -1
- package/dist/utilities/getGlobalViewRedirect.js +25 -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 +2 -3
- package/dist/utilities/getTenantOptions.d.ts.map +1 -1
- package/dist/utilities/getTenantOptions.js +2 -2
- package/dist/utilities/getTenantOptions.js.map +1 -1
- package/dist/utilities/getUserTenantIDs.d.ts.map +1 -1
- package/dist/utilities/getUserTenantIDs.js +3 -2
- 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/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 -27
- 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
|
@@ -5,22 +5,23 @@ import { getTenantFromCookie } from '../utilities/getTenantFromCookie.js';
|
|
|
5
5
|
* Add cleanup logic when tenant is deleted
|
|
6
6
|
* - delete documents related to tenant
|
|
7
7
|
* - remove tenant from users
|
|
8
|
-
*/ export const addTenantCleanup = ({ collection, enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName })=>{
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
8
|
+
*/ export const addTenantCleanup = ({ collection, enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName })=>({
|
|
9
|
+
...collection,
|
|
10
|
+
hooks: {
|
|
11
|
+
...collection.hooks,
|
|
12
|
+
afterDelete: [
|
|
13
|
+
...collection.hooks?.afterDelete || [],
|
|
14
|
+
afterTenantDelete({
|
|
15
|
+
enabledSlugs,
|
|
16
|
+
tenantFieldName,
|
|
17
|
+
tenantsCollectionSlug,
|
|
18
|
+
usersSlug,
|
|
19
|
+
usersTenantsArrayFieldName,
|
|
20
|
+
usersTenantsArrayTenantFieldName
|
|
21
|
+
})
|
|
22
|
+
]
|
|
23
|
+
}
|
|
24
|
+
});
|
|
24
25
|
export const afterTenantDelete = ({ enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName })=>async ({ id, req })=>{
|
|
25
26
|
const idType = getCollectionIDType({
|
|
26
27
|
collectionSlug: tenantsCollectionSlug,
|
|
@@ -43,6 +44,7 @@ export const afterTenantDelete = ({ enabledSlugs, tenantFieldName, tenantsCollec
|
|
|
43
44
|
enabledSlugs.forEach((slug)=>{
|
|
44
45
|
cleanupPromises.push(req.payload.delete({
|
|
45
46
|
collection: slug,
|
|
47
|
+
overrideAccess: true,
|
|
46
48
|
req,
|
|
47
49
|
where: {
|
|
48
50
|
[tenantFieldName]: {
|
|
@@ -58,6 +60,7 @@ export const afterTenantDelete = ({ enabledSlugs, tenantFieldName, tenantsCollec
|
|
|
58
60
|
collection: usersSlug,
|
|
59
61
|
depth: 0,
|
|
60
62
|
limit: 0,
|
|
63
|
+
overrideAccess: true,
|
|
61
64
|
req,
|
|
62
65
|
where: {
|
|
63
66
|
[`${usersTenantsArrayFieldName}.${usersTenantsArrayTenantFieldName}`]: {
|
|
@@ -78,6 +81,7 @@ export const afterTenantDelete = ({ enabledSlugs, tenantFieldName, tenantsCollec
|
|
|
78
81
|
}
|
|
79
82
|
})
|
|
80
83
|
},
|
|
84
|
+
overrideAccess: true,
|
|
81
85
|
req
|
|
82
86
|
}));
|
|
83
87
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/afterTenantDelete.ts"],"sourcesContent":["import type {\n CollectionAfterDeleteHook,\n CollectionConfig,\n JsonObject,\n PaginatedDocs,\n} from 'payload'\n\nimport { generateCookie, mergeHeaders } from 'payload'\n\nimport type { UserWithTenantsField } from '../types.js'\n\nimport { getCollectionIDType } from '../utilities/getCollectionIDType.js'\nimport { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'\n\ntype Args = {\n collection: CollectionConfig\n enabledSlugs: string[]\n tenantFieldName: string\n tenantsCollectionSlug: string\n usersSlug: string\n usersTenantsArrayFieldName: string\n usersTenantsArrayTenantFieldName: string\n}\n/**\n * Add cleanup logic when tenant is deleted\n * - delete documents related to tenant\n * - remove tenant from users\n */\nexport const addTenantCleanup = ({\n collection,\n enabledSlugs,\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug,\n usersTenantsArrayFieldName,\n usersTenantsArrayTenantFieldName,\n}: Args) => {\n
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/afterTenantDelete.ts"],"sourcesContent":["import type {\n CollectionAfterDeleteHook,\n CollectionConfig,\n JsonObject,\n PaginatedDocs,\n} from 'payload'\n\nimport { generateCookie, mergeHeaders } from 'payload'\n\nimport type { UserWithTenantsField } from '../types.js'\n\nimport { getCollectionIDType } from '../utilities/getCollectionIDType.js'\nimport { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'\n\ntype Args = {\n collection: CollectionConfig\n enabledSlugs: string[]\n tenantFieldName: string\n tenantsCollectionSlug: string\n usersSlug: string\n usersTenantsArrayFieldName: string\n usersTenantsArrayTenantFieldName: string\n}\n/**\n * Add cleanup logic when tenant is deleted\n * - delete documents related to tenant\n * - remove tenant from users\n */\nexport const addTenantCleanup = ({\n collection,\n enabledSlugs,\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug,\n usersTenantsArrayFieldName,\n usersTenantsArrayTenantFieldName,\n}: Args): CollectionConfig => ({\n ...collection,\n hooks: {\n ...collection.hooks,\n afterDelete: [\n ...(collection.hooks?.afterDelete || []),\n afterTenantDelete({\n enabledSlugs,\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug,\n usersTenantsArrayFieldName,\n usersTenantsArrayTenantFieldName,\n }),\n ],\n },\n})\n\nexport const afterTenantDelete =\n ({\n enabledSlugs,\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug,\n usersTenantsArrayFieldName,\n usersTenantsArrayTenantFieldName,\n }: Omit<Args, 'collection'>): CollectionAfterDeleteHook =>\n async ({ id, req }) => {\n const idType = getCollectionIDType({\n collectionSlug: tenantsCollectionSlug,\n payload: req.payload,\n })\n const currentTenantCookieID = getTenantFromCookie(req.headers, idType)\n if (currentTenantCookieID === id) {\n const newHeaders = new Headers({\n 'Set-Cookie': generateCookie<string>({\n name: 'payload-tenant',\n expires: new Date(Date.now() - 1000),\n path: '/',\n returnCookieAsObject: false,\n value: '',\n }),\n })\n\n req.responseHeaders = req.responseHeaders\n ? mergeHeaders(req.responseHeaders, newHeaders)\n : newHeaders\n }\n const cleanupPromises: Promise<JsonObject>[] = []\n enabledSlugs.forEach((slug) => {\n cleanupPromises.push(\n req.payload.delete({\n collection: slug,\n overrideAccess: true,\n req,\n where: {\n [tenantFieldName]: {\n in: [id],\n },\n },\n }),\n )\n })\n\n try {\n const usersWithTenant = (await req.payload.find({\n collection: usersSlug,\n depth: 0,\n limit: 0,\n overrideAccess: true,\n req,\n where: {\n [`${usersTenantsArrayFieldName}.${usersTenantsArrayTenantFieldName}`]: {\n in: [id],\n },\n },\n })) as PaginatedDocs<UserWithTenantsField>\n\n usersWithTenant?.docs?.forEach((user) => {\n cleanupPromises.push(\n req.payload.update({\n id: user.id,\n collection: usersSlug,\n data: {\n [usersTenantsArrayFieldName]: (\n ((user as Record<string, unknown>)[usersTenantsArrayFieldName] as Record<\n string,\n string\n >[]) || []\n ).filter((row: Record<string, string>) => {\n if (row[usersTenantsArrayTenantFieldName]) {\n return row[usersTenantsArrayTenantFieldName] !== id\n }\n }),\n },\n overrideAccess: true,\n req,\n }),\n )\n })\n } catch (e) {\n console.error('Error deleting tenants from users:', e)\n }\n\n await Promise.all(cleanupPromises)\n }\n"],"names":["generateCookie","mergeHeaders","getCollectionIDType","getTenantFromCookie","addTenantCleanup","collection","enabledSlugs","tenantFieldName","tenantsCollectionSlug","usersSlug","usersTenantsArrayFieldName","usersTenantsArrayTenantFieldName","hooks","afterDelete","afterTenantDelete","id","req","idType","collectionSlug","payload","currentTenantCookieID","headers","newHeaders","Headers","name","expires","Date","now","path","returnCookieAsObject","value","responseHeaders","cleanupPromises","forEach","slug","push","delete","overrideAccess","where","in","usersWithTenant","find","depth","limit","docs","user","update","data","filter","row","e","console","error","Promise","all"],"mappings":"AAOA,SAASA,cAAc,EAAEC,YAAY,QAAQ,UAAS;AAItD,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,mBAAmB,QAAQ,sCAAqC;AAWzE;;;;CAIC,GACD,OAAO,MAAMC,mBAAmB,CAAC,EAC/BC,UAAU,EACVC,YAAY,EACZC,eAAe,EACfC,qBAAqB,EACrBC,SAAS,EACTC,0BAA0B,EAC1BC,gCAAgC,EAC3B,GAAwB,CAAA;QAC7B,GAAGN,UAAU;QACbO,OAAO;YACL,GAAGP,WAAWO,KAAK;YACnBC,aAAa;mBACPR,WAAWO,KAAK,EAAEC,eAAe,EAAE;gBACvCC,kBAAkB;oBAChBR;oBACAC;oBACAC;oBACAC;oBACAC;oBACAC;gBACF;aACD;QACH;IACF,CAAA,EAAE;AAEF,OAAO,MAAMG,oBACX,CAAC,EACCR,YAAY,EACZC,eAAe,EACfC,qBAAqB,EACrBC,SAAS,EACTC,0BAA0B,EAC1BC,gCAAgC,EACP,GAC3B,OAAO,EAAEI,EAAE,EAAEC,GAAG,EAAE;QAChB,MAAMC,SAASf,oBAAoB;YACjCgB,gBAAgBV;YAChBW,SAASH,IAAIG,OAAO;QACtB;QACA,MAAMC,wBAAwBjB,oBAAoBa,IAAIK,OAAO,EAAEJ;QAC/D,IAAIG,0BAA0BL,IAAI;YAChC,MAAMO,aAAa,IAAIC,QAAQ;gBAC7B,cAAcvB,eAAuB;oBACnCwB,MAAM;oBACNC,SAAS,IAAIC,KAAKA,KAAKC,GAAG,KAAK;oBAC/BC,MAAM;oBACNC,sBAAsB;oBACtBC,OAAO;gBACT;YACF;YAEAd,IAAIe,eAAe,GAAGf,IAAIe,eAAe,GACrC9B,aAAae,IAAIe,eAAe,EAAET,cAClCA;QACN;QACA,MAAMU,kBAAyC,EAAE;QACjD1B,aAAa2B,OAAO,CAAC,CAACC;YACpBF,gBAAgBG,IAAI,CAClBnB,IAAIG,OAAO,CAACiB,MAAM,CAAC;gBACjB/B,YAAY6B;gBACZG,gBAAgB;gBAChBrB;gBACAsB,OAAO;oBACL,CAAC/B,gBAAgB,EAAE;wBACjBgC,IAAI;4BAACxB;yBAAG;oBACV;gBACF;YACF;QAEJ;QAEA,IAAI;YACF,MAAMyB,kBAAmB,MAAMxB,IAAIG,OAAO,CAACsB,IAAI,CAAC;gBAC9CpC,YAAYI;gBACZiC,OAAO;gBACPC,OAAO;gBACPN,gBAAgB;gBAChBrB;gBACAsB,OAAO;oBACL,CAAC,GAAG5B,2BAA2B,CAAC,EAAEC,kCAAkC,CAAC,EAAE;wBACrE4B,IAAI;4BAACxB;yBAAG;oBACV;gBACF;YACF;YAEAyB,iBAAiBI,MAAMX,QAAQ,CAACY;gBAC9Bb,gBAAgBG,IAAI,CAClBnB,IAAIG,OAAO,CAAC2B,MAAM,CAAC;oBACjB/B,IAAI8B,KAAK9B,EAAE;oBACXV,YAAYI;oBACZsC,MAAM;wBACJ,CAACrC,2BAA2B,EAAE,AAC5B,CAAA,AAAC,AAACmC,IAAgC,CAACnC,2BAA2B,IAGtD,EAAE,AAAD,EACTsC,MAAM,CAAC,CAACC;4BACR,IAAIA,GAAG,CAACtC,iCAAiC,EAAE;gCACzC,OAAOsC,GAAG,CAACtC,iCAAiC,KAAKI;4BACnD;wBACF;oBACF;oBACAsB,gBAAgB;oBAChBrB;gBACF;YAEJ;QACF,EAAE,OAAOkC,GAAG;YACVC,QAAQC,KAAK,CAAC,sCAAsCF;QACtD;QAEA,MAAMG,QAAQC,GAAG,CAACtB;IACpB,EAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload';
|
|
2
|
+
import type { MultiTenantPluginConfig } from '../types.js';
|
|
3
|
+
type Args = {
|
|
4
|
+
collection: CollectionConfig;
|
|
5
|
+
tenantFieldName: string;
|
|
6
|
+
tenantsArrayFieldName: string;
|
|
7
|
+
tenantsArrayTenantFieldName: string;
|
|
8
|
+
userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants'];
|
|
9
|
+
};
|
|
10
|
+
export declare const addCollectionBeforeValidateHook: ({ collection, tenantFieldName, tenantsArrayFieldName, tenantsArrayTenantFieldName, userHasAccessToAllTenants, }: Args) => void;
|
|
11
|
+
export declare const enforceTenantMembership: ({ collectionSlug, tenantFieldName, tenantsArrayFieldName, tenantsArrayTenantFieldName, userHasAccessToAllTenants, }: {
|
|
12
|
+
collectionSlug: string;
|
|
13
|
+
} & Omit<Args, 'collection'>) => CollectionBeforeValidateHook;
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=enforceTenantMembership.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enforceTenantMembership.d.ts","sourceRoot":"","sources":["../../src/hooks/enforceTenantMembership.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAI7E,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAI1D,KAAK,IAAI,GAAG;IACV,UAAU,EAAE,gBAAgB,CAAA;IAC5B,eAAe,EAAE,MAAM,CAAA;IACvB,qBAAqB,EAAE,MAAM,CAAA;IAC7B,2BAA2B,EAAE,MAAM,CAAA;IACnC,yBAAyB,EAAE,QAAQ,CAAC,uBAAuB,CAAC,CAAC,2BAA2B,CAAC,CAAA;CAC1F,CAAA;AAED,eAAO,MAAM,+BAA+B,oHAMzC,IAAI,SAYN,CAAA;AAED,eAAO,MAAM,uBAAuB,wHAO/B;IAAE,cAAc,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,IAAI,EAAE,YAAY,CAAC,KAAG,4BA0B1D,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ValidationError } from 'payload';
|
|
2
|
+
import { isValidTenantAssignment } from '../utilities/isValidTenantAssignment.js';
|
|
3
|
+
export const addCollectionBeforeValidateHook = ({ collection, tenantFieldName, tenantsArrayFieldName, tenantsArrayTenantFieldName, userHasAccessToAllTenants })=>{
|
|
4
|
+
collection.hooks ??= {};
|
|
5
|
+
collection.hooks.beforeValidate ??= [];
|
|
6
|
+
collection.hooks.beforeValidate.push(enforceTenantMembership({
|
|
7
|
+
collectionSlug: collection.slug,
|
|
8
|
+
tenantFieldName,
|
|
9
|
+
tenantsArrayFieldName,
|
|
10
|
+
tenantsArrayTenantFieldName,
|
|
11
|
+
userHasAccessToAllTenants
|
|
12
|
+
}));
|
|
13
|
+
};
|
|
14
|
+
export const enforceTenantMembership = ({ collectionSlug, tenantFieldName, tenantsArrayFieldName, tenantsArrayTenantFieldName, userHasAccessToAllTenants })=>({ data, originalDoc, req })=>{
|
|
15
|
+
if (!data || !(tenantFieldName in data)) {
|
|
16
|
+
return data;
|
|
17
|
+
}
|
|
18
|
+
if (!isValidTenantAssignment({
|
|
19
|
+
previousValue: originalDoc?.[tenantFieldName],
|
|
20
|
+
req,
|
|
21
|
+
tenantsArrayFieldName,
|
|
22
|
+
tenantsArrayTenantFieldName,
|
|
23
|
+
userHasAccessToAllTenants,
|
|
24
|
+
value: data[tenantFieldName]
|
|
25
|
+
})) {
|
|
26
|
+
throw new ValidationError({
|
|
27
|
+
collection: collectionSlug,
|
|
28
|
+
errors: [
|
|
29
|
+
{
|
|
30
|
+
message: req.t('validation:invalidSelection'),
|
|
31
|
+
path: tenantFieldName
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}, req.t);
|
|
35
|
+
}
|
|
36
|
+
return data;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=enforceTenantMembership.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/enforceTenantMembership.ts"],"sourcesContent":["import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload'\n\nimport { ValidationError } from 'payload'\n\nimport type { MultiTenantPluginConfig } from '../types.js'\n\nimport { isValidTenantAssignment } from '../utilities/isValidTenantAssignment.js'\n\ntype Args = {\n collection: CollectionConfig\n tenantFieldName: string\n tenantsArrayFieldName: string\n tenantsArrayTenantFieldName: string\n userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants']\n}\n\nexport const addCollectionBeforeValidateHook = ({\n collection,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n userHasAccessToAllTenants,\n}: Args) => {\n collection.hooks ??= {}\n collection.hooks.beforeValidate ??= []\n collection.hooks.beforeValidate.push(\n enforceTenantMembership({\n collectionSlug: collection.slug,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n userHasAccessToAllTenants,\n }),\n )\n}\n\nexport const enforceTenantMembership =\n ({\n collectionSlug,\n tenantFieldName,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n userHasAccessToAllTenants,\n }: { collectionSlug: string } & Omit<Args, 'collection'>): CollectionBeforeValidateHook =>\n ({ data, originalDoc, req }) => {\n if (!data || !(tenantFieldName in data)) {\n return data\n }\n\n if (\n !isValidTenantAssignment({\n previousValue: originalDoc?.[tenantFieldName],\n req,\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n userHasAccessToAllTenants,\n value: (data as Record<string, unknown>)[tenantFieldName],\n })\n ) {\n throw new ValidationError(\n {\n collection: collectionSlug,\n errors: [{ message: req.t('validation:invalidSelection'), path: tenantFieldName }],\n },\n req.t,\n )\n }\n\n return data\n }\n"],"names":["ValidationError","isValidTenantAssignment","addCollectionBeforeValidateHook","collection","tenantFieldName","tenantsArrayFieldName","tenantsArrayTenantFieldName","userHasAccessToAllTenants","hooks","beforeValidate","push","enforceTenantMembership","collectionSlug","slug","data","originalDoc","req","previousValue","value","errors","message","t","path"],"mappings":"AAEA,SAASA,eAAe,QAAQ,UAAS;AAIzC,SAASC,uBAAuB,QAAQ,0CAAyC;AAUjF,OAAO,MAAMC,kCAAkC,CAAC,EAC9CC,UAAU,EACVC,eAAe,EACfC,qBAAqB,EACrBC,2BAA2B,EAC3BC,yBAAyB,EACpB;IACLJ,WAAWK,KAAK,KAAK,CAAC;IACtBL,WAAWK,KAAK,CAACC,cAAc,KAAK,EAAE;IACtCN,WAAWK,KAAK,CAACC,cAAc,CAACC,IAAI,CAClCC,wBAAwB;QACtBC,gBAAgBT,WAAWU,IAAI;QAC/BT;QACAC;QACAC;QACAC;IACF;AAEJ,EAAC;AAED,OAAO,MAAMI,0BACX,CAAC,EACCC,cAAc,EACdR,eAAe,EACfC,qBAAqB,EACrBC,2BAA2B,EAC3BC,yBAAyB,EAC6B,GACxD,CAAC,EAAEO,IAAI,EAAEC,WAAW,EAAEC,GAAG,EAAE;QACzB,IAAI,CAACF,QAAQ,CAAEV,CAAAA,mBAAmBU,IAAG,GAAI;YACvC,OAAOA;QACT;QAEA,IACE,CAACb,wBAAwB;YACvBgB,eAAeF,aAAa,CAACX,gBAAgB;YAC7CY;YACAX;YACAC;YACAC;YACAW,OAAO,AAACJ,IAAgC,CAACV,gBAAgB;QAC3D,IACA;YACA,MAAM,IAAIJ,gBACR;gBACEG,YAAYS;gBACZO,QAAQ;oBAAC;wBAAEC,SAASJ,IAAIK,CAAC,CAAC;wBAAgCC,MAAMlB;oBAAgB;iBAAE;YACpF,GACAY,IAAIK,CAAC;QAET;QAEA,OAAOP;IACT,EAAC"}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAezD,eAAO,MAAM,iBAAiB,gEAsf5B,CAAA"}
|