@payloadcms/plugin-multi-tenant 4.0.0-internal.697830c → 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.
Files changed (99) hide show
  1. package/dist/components/AssignTenantFieldModal/index.client.d.ts +2 -2
  2. package/dist/components/AssignTenantFieldModal/index.client.d.ts.map +1 -1
  3. package/dist/components/AssignTenantFieldModal/index.client.js.map +1 -1
  4. package/dist/components/GlobalViewRedirect/index.d.ts.map +1 -1
  5. package/dist/components/TenantField/index.client.d.ts.map +1 -1
  6. package/dist/components/TenantField/index.client.js +84 -74
  7. package/dist/components/TenantField/index.client.js.map +1 -1
  8. package/dist/components/TenantSelector/index.client.d.ts +1 -1
  9. package/dist/components/TenantSelector/index.client.d.ts.map +1 -1
  10. package/dist/components/TenantSelector/index.css +4 -4
  11. package/dist/components/TenantSelector/index.d.ts.map +1 -1
  12. package/dist/defaults.d.ts.map +1 -1
  13. package/dist/endpoints/getTenantOptionsEndpoint.d.ts +1 -1
  14. package/dist/endpoints/getTenantOptionsEndpoint.d.ts.map +1 -1
  15. package/dist/fields/tenantField/index.d.ts +4 -2
  16. package/dist/fields/tenantField/index.d.ts.map +1 -1
  17. package/dist/fields/tenantField/index.js +36 -9
  18. package/dist/fields/tenantField/index.js.map +1 -1
  19. package/dist/fields/tenantsArrayField/index.d.ts.map +1 -1
  20. package/dist/filters/filterDocumentsByTenants.d.ts.map +1 -1
  21. package/dist/filters/filterDocumentsByTenants.js.map +1 -1
  22. package/dist/hooks/afterTenantDelete.d.ts +2 -2
  23. package/dist/hooks/afterTenantDelete.d.ts.map +1 -1
  24. package/dist/hooks/afterTenantDelete.js +20 -16
  25. package/dist/hooks/afterTenantDelete.js.map +1 -1
  26. package/dist/hooks/enforceTenantMembership.d.ts +15 -0
  27. package/dist/hooks/enforceTenantMembership.d.ts.map +1 -0
  28. package/dist/hooks/enforceTenantMembership.js +39 -0
  29. package/dist/hooks/enforceTenantMembership.js.map +1 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +288 -194
  32. package/dist/index.js.map +1 -1
  33. package/dist/providers/TenantSelectionProvider/index.client.d.ts.map +1 -1
  34. package/dist/providers/TenantSelectionProvider/index.client.js +22 -24
  35. package/dist/providers/TenantSelectionProvider/index.client.js.map +1 -1
  36. package/dist/providers/TenantSelectionProvider/index.d.ts.map +1 -1
  37. package/dist/translations/languages/en.d.ts.map +1 -1
  38. package/dist/types.d.ts +5 -3
  39. package/dist/types.d.ts.map +1 -1
  40. package/dist/types.js.map +1 -1
  41. package/dist/utilities/addCollectionAccess.d.ts +7 -10
  42. package/dist/utilities/addCollectionAccess.d.ts.map +1 -1
  43. package/dist/utilities/addCollectionAccess.js +39 -27
  44. package/dist/utilities/addCollectionAccess.js.map +1 -1
  45. package/dist/utilities/addFilterOptionsToFields.d.ts +2 -3
  46. package/dist/utilities/addFilterOptionsToFields.d.ts.map +1 -1
  47. package/dist/utilities/addFilterOptionsToFields.js +37 -40
  48. package/dist/utilities/addFilterOptionsToFields.js.map +1 -1
  49. package/dist/utilities/collectionAccessKeys.d.ts +3 -0
  50. package/dist/utilities/collectionAccessKeys.d.ts.map +1 -0
  51. package/dist/utilities/collectionAccessKeys.js +11 -0
  52. package/dist/utilities/collectionAccessKeys.js.map +1 -0
  53. package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts +7 -0
  54. package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts.map +1 -0
  55. package/dist/utilities/combineBaseAccessWithTenantAccess.js +35 -0
  56. package/dist/utilities/combineBaseAccessWithTenantAccess.js.map +1 -0
  57. package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts +4 -0
  58. package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts.map +1 -0
  59. package/dist/utilities/combineCollectionAccessWithTenantAccess.js +26 -0
  60. package/dist/utilities/combineCollectionAccessWithTenantAccess.js.map +1 -0
  61. package/dist/utilities/combineFilters.d.ts.map +1 -1
  62. package/dist/utilities/extractID.d.ts.map +1 -1
  63. package/dist/utilities/generateCookie.d.ts.map +1 -1
  64. package/dist/utilities/getCollectionIDType.d.ts +1 -1
  65. package/dist/utilities/getCollectionIDType.d.ts.map +1 -1
  66. package/dist/utilities/getGlobalViewRedirect.d.ts.map +1 -1
  67. package/dist/utilities/getGlobalViewRedirect.js +24 -18
  68. package/dist/utilities/getGlobalViewRedirect.js.map +1 -1
  69. package/dist/utilities/getTenantAccessResult.d.ts +9 -0
  70. package/dist/utilities/getTenantAccessResult.d.ts.map +1 -0
  71. package/dist/utilities/getTenantAccessResult.js +46 -0
  72. package/dist/utilities/getTenantAccessResult.js.map +1 -0
  73. package/dist/utilities/getTenantIDsFromValue.d.ts +2 -0
  74. package/dist/utilities/getTenantIDsFromValue.d.ts.map +1 -0
  75. package/dist/utilities/getTenantIDsFromValue.js +14 -0
  76. package/dist/utilities/getTenantIDsFromValue.js.map +1 -0
  77. package/dist/utilities/getTenantOptions.d.ts +1 -1
  78. package/dist/utilities/getTenantOptions.d.ts.map +1 -1
  79. package/dist/utilities/getUserTenantIDs.d.ts.map +1 -1
  80. package/dist/utilities/getUserTenantIDs.js +3 -2
  81. package/dist/utilities/getUserTenantIDs.js.map +1 -1
  82. package/dist/utilities/isValidTenantAssignment.d.ts +13 -0
  83. package/dist/utilities/isValidTenantAssignment.d.ts.map +1 -0
  84. package/dist/utilities/isValidTenantAssignment.js +28 -0
  85. package/dist/utilities/isValidTenantAssignment.js.map +1 -0
  86. package/dist/utilities/miniChalk.d.ts.map +1 -1
  87. package/dist/utilities/shouldRefreshTenantSelection.d.ts +5 -0
  88. package/dist/utilities/shouldRefreshTenantSelection.d.ts.map +1 -0
  89. package/dist/utilities/shouldRefreshTenantSelection.js +3 -0
  90. package/dist/utilities/shouldRefreshTenantSelection.js.map +1 -0
  91. package/package.json +24 -28
  92. package/dist/utilities/combineWhereConstraints.d.ts +0 -3
  93. package/dist/utilities/combineWhereConstraints.d.ts.map +0 -1
  94. package/dist/utilities/combineWhereConstraints.js +0 -19
  95. package/dist/utilities/combineWhereConstraints.js.map +0 -1
  96. package/dist/utilities/withTenantAccess.d.ts +0 -16
  97. package/dist/utilities/withTenantAccess.d.ts.map +0 -1
  98. package/dist/utilities/withTenantAccess.js +0 -76
  99. 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
- if (!collection.hooks) {
10
- collection.hooks = {};
11
- }
12
- if (!collection.hooks?.afterDelete) {
13
- collection.hooks.afterDelete = [];
14
- }
15
- collection.hooks.afterDelete.push(afterTenantDelete({
16
- enabledSlugs,
17
- tenantFieldName,
18
- tenantsCollectionSlug,
19
- usersSlug,
20
- usersTenantsArrayFieldName,
21
- usersTenantsArrayTenantFieldName
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 if (!collection.hooks) {\n collection.hooks = {}\n }\n if (!collection.hooks?.afterDelete) {\n collection.hooks.afterDelete = []\n }\n collection.hooks.afterDelete.push(\n afterTenantDelete({\n enabledSlugs,\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug,\n usersTenantsArrayFieldName,\n usersTenantsArrayTenantFieldName,\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 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 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 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","push","afterTenantDelete","id","req","idType","collectionSlug","payload","currentTenantCookieID","headers","newHeaders","Headers","name","expires","Date","now","path","returnCookieAsObject","value","responseHeaders","cleanupPromises","forEach","slug","delete","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;IACL,IAAI,CAACN,WAAWO,KAAK,EAAE;QACrBP,WAAWO,KAAK,GAAG,CAAC;IACtB;IACA,IAAI,CAACP,WAAWO,KAAK,EAAEC,aAAa;QAClCR,WAAWO,KAAK,CAACC,WAAW,GAAG,EAAE;IACnC;IACAR,WAAWO,KAAK,CAACC,WAAW,CAACC,IAAI,CAC/BC,kBAAkB;QAChBT;QACAC;QACAC;QACAC;QACAC;QACAC;IACF;AAEJ,EAAC;AAED,OAAO,MAAMI,oBACX,CAAC,EACCT,YAAY,EACZC,eAAe,EACfC,qBAAqB,EACrBC,SAAS,EACTC,0BAA0B,EAC1BC,gCAAgC,EACP,GAC3B,OAAO,EAAEK,EAAE,EAAEC,GAAG,EAAE;QAChB,MAAMC,SAAShB,oBAAoB;YACjCiB,gBAAgBX;YAChBY,SAASH,IAAIG,OAAO;QACtB;QACA,MAAMC,wBAAwBlB,oBAAoBc,IAAIK,OAAO,EAAEJ;QAC/D,IAAIG,0BAA0BL,IAAI;Y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sCAAsCF;QACtD;QAEA,MAAMG,QAAQC,GAAG,CAACpB;IACpB,EAAC"}
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,SA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;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"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAczD,eAAO,MAAM,iBAAiB,gEAua5B,CAAA"}
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"}