@payloadcms/plugin-multi-tenant 4.0.0-internal.688c4d0 → 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 (111) hide show
  1. package/LICENSE.md +1 -1
  2. package/dist/components/AssignTenantFieldModal/index.client.d.ts +2 -2
  3. package/dist/components/AssignTenantFieldModal/index.client.d.ts.map +1 -1
  4. package/dist/components/AssignTenantFieldModal/index.client.js.map +1 -1
  5. package/dist/components/GlobalViewRedirect/index.d.ts +1 -1
  6. package/dist/components/GlobalViewRedirect/index.d.ts.map +1 -1
  7. package/dist/components/GlobalViewRedirect/index.js.map +1 -1
  8. package/dist/components/TenantField/index.client.d.ts.map +1 -1
  9. package/dist/components/TenantField/index.client.js +84 -74
  10. package/dist/components/TenantField/index.client.js.map +1 -1
  11. package/dist/components/TenantSelector/index.client.d.ts +1 -1
  12. package/dist/components/TenantSelector/index.client.d.ts.map +1 -1
  13. package/dist/components/TenantSelector/index.css +4 -4
  14. package/dist/components/TenantSelector/index.d.ts.map +1 -1
  15. package/dist/defaults.d.ts.map +1 -1
  16. package/dist/endpoints/getTenantOptionsEndpoint.d.ts +2 -2
  17. package/dist/endpoints/getTenantOptionsEndpoint.d.ts.map +1 -1
  18. package/dist/endpoints/getTenantOptionsEndpoint.js.map +1 -1
  19. package/dist/fields/tenantField/index.d.ts +4 -2
  20. package/dist/fields/tenantField/index.d.ts.map +1 -1
  21. package/dist/fields/tenantField/index.js +36 -9
  22. package/dist/fields/tenantField/index.js.map +1 -1
  23. package/dist/fields/tenantsArrayField/index.d.ts.map +1 -1
  24. package/dist/filters/filterDocumentsByTenants.d.ts +3 -3
  25. package/dist/filters/filterDocumentsByTenants.d.ts.map +1 -1
  26. package/dist/filters/filterDocumentsByTenants.js.map +1 -1
  27. package/dist/hooks/afterTenantDelete.d.ts +2 -2
  28. package/dist/hooks/afterTenantDelete.d.ts.map +1 -1
  29. package/dist/hooks/afterTenantDelete.js +24 -17
  30. package/dist/hooks/afterTenantDelete.js.map +1 -1
  31. package/dist/hooks/enforceTenantMembership.d.ts +15 -0
  32. package/dist/hooks/enforceTenantMembership.d.ts.map +1 -0
  33. package/dist/hooks/enforceTenantMembership.js +39 -0
  34. package/dist/hooks/enforceTenantMembership.js.map +1 -0
  35. package/dist/index.d.ts +1 -2
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +293 -195
  38. package/dist/index.js.map +1 -1
  39. package/dist/providers/TenantSelectionProvider/index.client.d.ts.map +1 -1
  40. package/dist/providers/TenantSelectionProvider/index.client.js +44 -24
  41. package/dist/providers/TenantSelectionProvider/index.client.js.map +1 -1
  42. package/dist/providers/TenantSelectionProvider/index.d.ts +5 -5
  43. package/dist/providers/TenantSelectionProvider/index.d.ts.map +1 -1
  44. package/dist/providers/TenantSelectionProvider/index.js.map +1 -1
  45. package/dist/translations/languages/en.d.ts.map +1 -1
  46. package/dist/types.d.ts +9 -9
  47. package/dist/types.d.ts.map +1 -1
  48. package/dist/types.js.map +1 -1
  49. package/dist/utilities/addCollectionAccess.d.ts +9 -12
  50. package/dist/utilities/addCollectionAccess.d.ts.map +1 -1
  51. package/dist/utilities/addCollectionAccess.js +39 -27
  52. package/dist/utilities/addCollectionAccess.js.map +1 -1
  53. package/dist/utilities/addFilterOptionsToFields.d.ts +4 -5
  54. package/dist/utilities/addFilterOptionsToFields.d.ts.map +1 -1
  55. package/dist/utilities/addFilterOptionsToFields.js +37 -40
  56. package/dist/utilities/addFilterOptionsToFields.js.map +1 -1
  57. package/dist/utilities/collectionAccessKeys.d.ts +3 -0
  58. package/dist/utilities/collectionAccessKeys.d.ts.map +1 -0
  59. package/dist/utilities/collectionAccessKeys.js +11 -0
  60. package/dist/utilities/collectionAccessKeys.js.map +1 -0
  61. package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts +7 -0
  62. package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts.map +1 -0
  63. package/dist/utilities/combineBaseAccessWithTenantAccess.js +35 -0
  64. package/dist/utilities/combineBaseAccessWithTenantAccess.js.map +1 -0
  65. package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts +4 -0
  66. package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts.map +1 -0
  67. package/dist/utilities/combineCollectionAccessWithTenantAccess.js +26 -0
  68. package/dist/utilities/combineCollectionAccessWithTenantAccess.js.map +1 -0
  69. package/dist/utilities/combineFilters.d.ts.map +1 -1
  70. package/dist/utilities/extractID.d.ts.map +1 -1
  71. package/dist/utilities/generateCookie.d.ts.map +1 -1
  72. package/dist/utilities/getCollectionIDType.d.ts +1 -1
  73. package/dist/utilities/getCollectionIDType.d.ts.map +1 -1
  74. package/dist/utilities/getGlobalViewRedirect.d.ts +3 -3
  75. package/dist/utilities/getGlobalViewRedirect.d.ts.map +1 -1
  76. package/dist/utilities/getGlobalViewRedirect.js +24 -18
  77. package/dist/utilities/getGlobalViewRedirect.js.map +1 -1
  78. package/dist/utilities/getTenantAccessResult.d.ts +9 -0
  79. package/dist/utilities/getTenantAccessResult.d.ts.map +1 -0
  80. package/dist/utilities/getTenantAccessResult.js +46 -0
  81. package/dist/utilities/getTenantAccessResult.js.map +1 -0
  82. package/dist/utilities/getTenantIDsFromValue.d.ts +2 -0
  83. package/dist/utilities/getTenantIDsFromValue.d.ts.map +1 -0
  84. package/dist/utilities/getTenantIDsFromValue.js +14 -0
  85. package/dist/utilities/getTenantIDsFromValue.js.map +1 -0
  86. package/dist/utilities/getTenantOptions.d.ts +3 -3
  87. package/dist/utilities/getTenantOptions.d.ts.map +1 -1
  88. package/dist/utilities/getTenantOptions.js.map +1 -1
  89. package/dist/utilities/getUserTenantIDs.d.ts.map +1 -1
  90. package/dist/utilities/getUserTenantIDs.js +5 -3
  91. package/dist/utilities/getUserTenantIDs.js.map +1 -1
  92. package/dist/utilities/isValidTenantAssignment.d.ts +13 -0
  93. package/dist/utilities/isValidTenantAssignment.d.ts.map +1 -0
  94. package/dist/utilities/isValidTenantAssignment.js +28 -0
  95. package/dist/utilities/isValidTenantAssignment.js.map +1 -0
  96. package/dist/utilities/miniChalk.d.ts.map +1 -1
  97. package/dist/utilities/shouldRefreshTenantSelection.d.ts +5 -0
  98. package/dist/utilities/shouldRefreshTenantSelection.d.ts.map +1 -0
  99. package/dist/utilities/shouldRefreshTenantSelection.js +3 -0
  100. package/dist/utilities/shouldRefreshTenantSelection.js.map +1 -0
  101. package/package.json +24 -28
  102. package/dist/@types/assets.d.js +0 -2
  103. package/dist/@types/assets.d.js.map +0 -1
  104. package/dist/utilities/combineWhereConstraints.d.ts +0 -3
  105. package/dist/utilities/combineWhereConstraints.d.ts.map +0 -1
  106. package/dist/utilities/combineWhereConstraints.js +0 -19
  107. package/dist/utilities/combineWhereConstraints.js.map +0 -1
  108. package/dist/utilities/withTenantAccess.d.ts +0 -16
  109. package/dist/utilities/withTenantAccess.d.ts.map +0 -1
  110. package/dist/utilities/withTenantAccess.js +0 -76
  111. package/dist/utilities/withTenantAccess.js.map +0 -1
@@ -1,6 +1,6 @@
1
1
  import type { PayloadRequest, Where } from 'payload';
2
2
  import type { MultiTenantPluginConfig } from '../types.js';
3
- type Args<ConfigType = unknown> = {
3
+ type Args = {
4
4
  /**
5
5
  * If the document this filter is run belongs to a tenant, the tenant ID should be passed here.
6
6
  * If set, this will be used instead of the tenant cookie
@@ -12,8 +12,8 @@ type Args<ConfigType = unknown> = {
12
12
  tenantsArrayFieldName?: string;
13
13
  tenantsArrayTenantFieldName?: string;
14
14
  tenantsCollectionSlug: string;
15
- userHasAccessToAllTenants: Required<MultiTenantPluginConfig<ConfigType>>['userHasAccessToAllTenants'];
15
+ userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants'];
16
16
  };
17
- export declare const filterDocumentsByTenants: <ConfigType = unknown>({ docTenantID, filterFieldName, req, tenantsArrayFieldName, tenantsArrayTenantFieldName, tenantsCollectionSlug, userHasAccessToAllTenants, }: Args<ConfigType>) => null | Where;
17
+ export declare const filterDocumentsByTenants: ({ docTenantID, filterFieldName, req, tenantsArrayFieldName, tenantsArrayTenantFieldName, tenantsCollectionSlug, userHasAccessToAllTenants, }: Args) => null | Where;
18
18
  export {};
19
19
  //# sourceMappingURL=filterDocumentsByTenants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"filterDocumentsByTenants.d.ts","sourceRoot":"","sources":["../../src/filters/filterDocumentsByTenants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAa,KAAK,EAAE,MAAM,SAAS,CAAA;AAE/D,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAO1D,KAAK,IAAI,CAAC,UAAU,GAAG,OAAO,IAAI;IAChC;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,GAAG,MAAM,EAAE,CAAA;IACnD,eAAe,EAAE,MAAM,CAAA;IACvB,GAAG,EAAE,cAAc,CAAA;IACnB,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAC9B,2BAA2B,CAAC,EAAE,MAAM,CAAA;IACpC,qBAAqB,EAAE,MAAM,CAAA;IAC7B,yBAAyB,EAAE,QAAQ,CACjC,uBAAuB,CAAC,UAAU,CAAC,CACpC,CAAC,2BAA2B,CAAC,CAAA;CAC/B,CAAA;AACD,eAAO,MAAM,wBAAwB,GAAI,UAAU,GAAG,OAAO,EAAE,8IAQ5D,IAAI,CAAC,UAAU,CAAC,KAAG,IAAI,GAAG,KAwC5B,CAAA"}
1
+ {"version":3,"file":"filterDocumentsByTenants.d.ts","sourceRoot":"","sources":["../../src/filters/filterDocumentsByTenants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAQ,KAAK,EAAE,MAAM,SAAS,CAAA;AAE1D,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAO1D,KAAK,IAAI,GAAG;IACV;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,GAAG,MAAM,EAAE,CAAA;IACnD,eAAe,EAAE,MAAM,CAAA;IACvB,GAAG,EAAE,cAAc,CAAA;IACnB,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAC9B,2BAA2B,CAAC,EAAE,MAAM,CAAA;IACpC,qBAAqB,EAAE,MAAM,CAAA;IAC7B,yBAAyB,EAAE,QAAQ,CAAC,uBAAuB,CAAC,CAAC,2BAA2B,CAAC,CAAA;CAC1F,CAAA;AACD,eAAO,MAAM,wBAAwB,iJAQlC,IAAI,KAAG,IAAI,GAAG,KAmChB,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/filters/filterDocumentsByTenants.ts"],"sourcesContent":["import type { PayloadRequest, TypedUser, Where } from 'payload'\n\nimport type { MultiTenantPluginConfig } from '../types.js'\n\nimport { defaults } from '../defaults.js'\nimport { getCollectionIDType } from '../utilities/getCollectionIDType.js'\nimport { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'\nimport { getUserTenantIDs } from '../utilities/getUserTenantIDs.js'\n\ntype Args<ConfigType = unknown> = {\n /**\n * If the document this filter is run belongs to a tenant, the tenant ID should be passed here.\n * If set, this will be used instead of the tenant cookie\n * Can be an array when hasMany is true\n */\n docTenantID?: number | number[] | string | string[]\n filterFieldName: string\n req: PayloadRequest\n tenantsArrayFieldName?: string\n tenantsArrayTenantFieldName?: string\n tenantsCollectionSlug: string\n userHasAccessToAllTenants: Required<\n MultiTenantPluginConfig<ConfigType>\n >['userHasAccessToAllTenants']\n}\nexport const filterDocumentsByTenants = <ConfigType = unknown>({\n docTenantID,\n filterFieldName,\n req,\n tenantsArrayFieldName = defaults.tenantsArrayFieldName,\n tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n}: Args<ConfigType>): null | Where => {\n const idType = getCollectionIDType({\n collectionSlug: tenantsCollectionSlug,\n payload: req.payload,\n })\n\n // scope results to selected tenant\n const selectedTenant = docTenantID ?? getTenantFromCookie(req.headers, idType)\n if (selectedTenant) {\n return {\n [filterFieldName]: {\n in: Array.isArray(selectedTenant) ? selectedTenant : [selectedTenant],\n },\n }\n }\n\n if (\n req.user &&\n userHasAccessToAllTenants(\n req?.user as ConfigType extends { user: unknown } ? ConfigType['user'] : TypedUser,\n )\n ) {\n return null\n }\n\n // scope to user assigned tenants\n const userAssignedTenants = getUserTenantIDs(req.user, {\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n })\n if (userAssignedTenants.length > 0) {\n return {\n [filterFieldName]: {\n in: userAssignedTenants,\n },\n }\n }\n\n // no tenant selected and no user tenants, return null to allow access control to handle it\n return null\n}\n"],"names":["defaults","getCollectionIDType","getTenantFromCookie","getUserTenantIDs","filterDocumentsByTenants","docTenantID","filterFieldName","req","tenantsArrayFieldName","tenantsArrayTenantFieldName","tenantsCollectionSlug","userHasAccessToAllTenants","idType","collectionSlug","payload","selectedTenant","headers","in","Array","isArray","user","userAssignedTenants","length"],"mappings":"AAIA,SAASA,QAAQ,QAAQ,iBAAgB;AACzC,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,gBAAgB,QAAQ,mCAAkC;AAkBnE,OAAO,MAAMC,2BAA2B,CAAuB,EAC7DC,WAAW,EACXC,eAAe,EACfC,GAAG,EACHC,wBAAwBR,SAASQ,qBAAqB,EACtDC,8BAA8BT,SAASS,2BAA2B,EAClEC,qBAAqB,EACrBC,yBAAyB,EACR;IACjB,MAAMC,SAASX,oBAAoB;QACjCY,gBAAgBH;QAChBI,SAASP,IAAIO,OAAO;IACtB;IAEA,mCAAmC;IACnC,MAAMC,iBAAiBV,eAAeH,oBAAoBK,IAAIS,OAAO,EAAEJ;IACvE,IAAIG,gBAAgB;QAClB,OAAO;YACL,CAACT,gBAAgB,EAAE;gBACjBW,IAAIC,MAAMC,OAAO,CAACJ,kBAAkBA,iBAAiB;oBAACA;iBAAe;YACvE;QACF;IACF;IAEA,IACER,IAAIa,IAAI,IACRT,0BACEJ,KAAKa,OAEP;QACA,OAAO;IACT;IAEA,iCAAiC;IACjC,MAAMC,sBAAsBlB,iBAAiBI,IAAIa,IAAI,EAAE;QACrDZ;QACAC;IACF;IACA,IAAIY,oBAAoBC,MAAM,GAAG,GAAG;QAClC,OAAO;YACL,CAAChB,gBAAgB,EAAE;gBACjBW,IAAII;YACN;QACF;IACF;IAEA,2FAA2F;IAC3F,OAAO;AACT,EAAC"}
1
+ {"version":3,"sources":["../../src/filters/filterDocumentsByTenants.ts"],"sourcesContent":["import type { PayloadRequest, User, Where } from 'payload'\n\nimport type { MultiTenantPluginConfig } from '../types.js'\n\nimport { defaults } from '../defaults.js'\nimport { getCollectionIDType } from '../utilities/getCollectionIDType.js'\nimport { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'\nimport { getUserTenantIDs } from '../utilities/getUserTenantIDs.js'\n\ntype Args = {\n /**\n * If the document this filter is run belongs to a tenant, the tenant ID should be passed here.\n * If set, this will be used instead of the tenant cookie\n * Can be an array when hasMany is true\n */\n docTenantID?: number | number[] | string | string[]\n filterFieldName: string\n req: PayloadRequest\n tenantsArrayFieldName?: string\n tenantsArrayTenantFieldName?: string\n tenantsCollectionSlug: string\n userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants']\n}\nexport const filterDocumentsByTenants = ({\n docTenantID,\n filterFieldName,\n req,\n tenantsArrayFieldName = defaults.tenantsArrayFieldName,\n tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n}: Args): null | Where => {\n const idType = getCollectionIDType({\n collectionSlug: tenantsCollectionSlug,\n payload: req.payload,\n })\n\n // scope results to selected tenant\n const selectedTenant = docTenantID ?? getTenantFromCookie(req.headers, idType)\n if (selectedTenant) {\n return {\n [filterFieldName]: {\n in: Array.isArray(selectedTenant) ? selectedTenant : [selectedTenant],\n },\n }\n }\n\n if (req.user && userHasAccessToAllTenants(req?.user)) {\n return null\n }\n\n // scope to user assigned tenants\n const userAssignedTenants = getUserTenantIDs(req.user, {\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n })\n if (userAssignedTenants.length > 0) {\n return {\n [filterFieldName]: {\n in: userAssignedTenants,\n },\n }\n }\n\n // no tenant selected and no user tenants, return null to allow access control to handle it\n return null\n}\n"],"names":["defaults","getCollectionIDType","getTenantFromCookie","getUserTenantIDs","filterDocumentsByTenants","docTenantID","filterFieldName","req","tenantsArrayFieldName","tenantsArrayTenantFieldName","tenantsCollectionSlug","userHasAccessToAllTenants","idType","collectionSlug","payload","selectedTenant","headers","in","Array","isArray","user","userAssignedTenants","length"],"mappings":"AAIA,SAASA,QAAQ,QAAQ,iBAAgB;AACzC,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,gBAAgB,QAAQ,mCAAkC;AAgBnE,OAAO,MAAMC,2BAA2B,CAAC,EACvCC,WAAW,EACXC,eAAe,EACfC,GAAG,EACHC,wBAAwBR,SAASQ,qBAAqB,EACtDC,8BAA8BT,SAASS,2BAA2B,EAClEC,qBAAqB,EACrBC,yBAAyB,EACpB;IACL,MAAMC,SAASX,oBAAoB;QACjCY,gBAAgBH;QAChBI,SAASP,IAAIO,OAAO;IACtB;IAEA,mCAAmC;IACnC,MAAMC,iBAAiBV,eAAeH,oBAAoBK,IAAIS,OAAO,EAAEJ;IACvE,IAAIG,gBAAgB;QAClB,OAAO;YACL,CAACT,gBAAgB,EAAE;gBACjBW,IAAIC,MAAMC,OAAO,CAACJ,kBAAkBA,iBAAiB;oBAACA;iBAAe;YACvE;QACF;IACF;IAEA,IAAIR,IAAIa,IAAI,IAAIT,0BAA0BJ,KAAKa,OAAO;QACpD,OAAO;IACT;IAEA,iCAAiC;IACjC,MAAMC,sBAAsBlB,iBAAiBI,IAAIa,IAAI,EAAE;QACrDZ;QACAC;IACF;IACA,IAAIY,oBAAoBC,MAAM,GAAG,GAAG;QAClC,OAAO;YACL,CAAChB,gBAAgB,EAAE;gBACjBW,IAAII;YACN;QACF;IACF;IAEA,2FAA2F;IAC3F,OAAO;AACT,EAAC"}
@@ -13,7 +13,7 @@ type Args = {
13
13
  * - delete documents related to tenant
14
14
  * - remove tenant from users
15
15
  */
16
- export declare const addTenantCleanup: ({ collection, enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName, }: Args) => void;
17
- export declare const afterTenantDelete: ({ enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName, }: Omit<Args, "collection">) => CollectionAfterDeleteHook;
16
+ export declare const addTenantCleanup: ({ collection, enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName, }: Args) => CollectionConfig;
17
+ export declare const afterTenantDelete: ({ enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName, }: Omit<Args, 'collection'>) => CollectionAfterDeleteHook;
18
18
  export {};
19
19
  //# sourceMappingURL=afterTenantDelete.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"afterTenantDelete.d.ts","sourceRoot":"","sources":["../../src/hooks/afterTenantDelete.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yBAAyB,EACzB,gBAAgB,EAGjB,MAAM,SAAS,CAAA;AAShB,KAAK,IAAI,GAAG;IACV,UAAU,EAAE,gBAAgB,CAAA;IAC5B,YAAY,EAAE,MAAM,EAAE,CAAA;IACtB,eAAe,EAAE,MAAM,CAAA;IACvB,qBAAqB,EAAE,MAAM,CAAA;IAC7B,SAAS,EAAE,MAAM,CAAA;IACjB,0BAA0B,EAAE,MAAM,CAAA;IAClC,gCAAgC,EAAE,MAAM,CAAA;CACzC,CAAA;AACD;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,GAAI,gJAQ9B,IAAI,SAiBN,CAAA;AAED,eAAO,MAAM,iBAAiB,GAC3B,oIAOE,IAAI,CAAC,IAAI,EAAE,YAAY,CAAC,KAAG,yBAsE7B,CAAA"}
1
+ {"version":3,"file":"afterTenantDelete.d.ts","sourceRoot":"","sources":["../../src/hooks/afterTenantDelete.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yBAAyB,EACzB,gBAAgB,EAGjB,MAAM,SAAS,CAAA;AAShB,KAAK,IAAI,GAAG;IACV,UAAU,EAAE,gBAAgB,CAAA;IAC5B,YAAY,EAAE,MAAM,EAAE,CAAA;IACtB,eAAe,EAAE,MAAM,CAAA;IACvB,qBAAqB,EAAE,MAAM,CAAA;IAC7B,SAAS,EAAE,MAAM,CAAA;IACjB,0BAA0B,EAAE,MAAM,CAAA;IAClC,gCAAgC,EAAE,MAAM,CAAA;CACzC,CAAA;AACD;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,mJAQ1B,IAAI,KAAG,gBAgBR,CAAA;AAEF,eAAO,MAAM,iBAAiB,uIAQzB,IAAI,CAAC,IAAI,EAAE,YAAY,CAAC,KAAG,yBA+E7B,CAAA"}
@@ -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,8 @@ 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,
48
+ req,
46
49
  where: {
47
50
  [tenantFieldName]: {
48
51
  in: [
@@ -57,6 +60,8 @@ export const afterTenantDelete = ({ enabledSlugs, tenantFieldName, tenantsCollec
57
60
  collection: usersSlug,
58
61
  depth: 0,
59
62
  limit: 0,
63
+ overrideAccess: true,
64
+ req,
60
65
  where: {
61
66
  [`${usersTenantsArrayFieldName}.${usersTenantsArrayTenantFieldName}`]: {
62
67
  in: [
@@ -75,7 +80,9 @@ export const afterTenantDelete = ({ enabledSlugs, tenantFieldName, tenantsCollec
75
80
  return row[usersTenantsArrayTenantFieldName] !== id;
76
81
  }
77
82
  })
78
- }
83
+ },
84
+ overrideAccess: true,
85
+ req
79
86
  }));
80
87
  });
81
88
  } catch (e) {
@@ -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 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 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]: (user[usersTenantsArrayFieldName] || []).filter(\n (row: Record<string, string>) => {\n if (row[usersTenantsArrayTenantFieldName]) {\n return row[usersTenantsArrayTenantFieldName] !== id\n }\n },\n ),\n },\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;YAChC,MAAMO,aAAa,IAAIC,QAAQ;gBAC7B,cAAcxB,eAAuB;oBACnCyB,MAAM;oBACNC,SAAS,IAAIC,KAAKA,KAAKC,GAAG,KAAK;oBAC/BC,MAAM;oBACNC,sBAAsB;oBACtBC,OAAO;gBACT;YACF;YAEAd,IAAIe,eAAe,GAAGf,IAAIe,eAAe,GACrC/B,aAAagB,IAAIe,eAAe,EAAET,cAClCA;QACN;QACA,MAAMU,kBAAyC,EAAE;QACjD3B,aAAa4B,OAAO,CAAC,CAACC;YACpBF,gBAAgBnB,IAAI,CAClBG,IAAIG,OAAO,CAACgB,MAAM,CAAC;gBACjB/B,YAAY8B;gBACZE,OAAO;oBACL,CAAC9B,gBAAgB,EAAE;wBACjB+B,IAAI;4BAACtB;yBAAG;oBACV;gBACF;YACF;QAEJ;QAEA,IAAI;YACF,MAAMuB,kBAAmB,MAAMtB,IAAIG,OAAO,CAACoB,IAAI,CAAC;gBAC9CnC,YAAYI;gBACZgC,OAAO;gBACPC,OAAO;gBACPL,OAAO;oBACL,CAAC,GAAG3B,2BAA2B,CAAC,EAAEC,kCAAkC,CAAC,EAAE;wBACrE2B,IAAI;4BAACtB;yBAAG;oBACV;gBACF;YACF;YAEAuB,iBAAiBI,MAAMT,QAAQ,CAACU;gBAC9BX,gBAAgBnB,IAAI,CAClBG,IAAIG,OAAO,CAACyB,MAAM,CAAC;oBACjB7B,IAAI4B,KAAK5B,EAAE;oBACXX,YAAYI;oBACZqC,MAAM;wBACJ,CAACpC,2BAA2B,EAAE,AAACkC,CAAAA,IAAI,CAAClC,2BAA2B,IAAI,EAAE,AAAD,EAAGqC,MAAM,CAC3E,CAACC;4BACC,IAAIA,GAAG,CAACrC,iCAAiC,EAAE;gCACzC,OAAOqC,GAAG,CAACrC,iCAAiC,KAAKK;4BACnD;wBACF;oBAEJ;gBACF;YAEJ;QACF,EAAE,OAAOiC,GAAG;YACVC,QAAQC,KAAK,CAAC,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,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 CHANGED
@@ -1,4 +1,3 @@
1
- import type { Config } from 'payload';
2
1
  import type { MultiTenantPluginConfig } from './types.js';
3
- export declare const multiTenantPlugin: <ConfigType>(pluginConfig: MultiTenantPluginConfig<ConfigType>) => (incomingConfig: Config) => Config;
2
+ export declare const multiTenantPlugin: (options: MultiTenantPluginConfig) => import("payload").Plugin;
4
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAoB,MAAM,EAAE,MAAM,SAAS,CAAA;AAMvD,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAczD,eAAO,MAAM,iBAAiB,GAC3B,UAAU,EAAE,cAAc,uBAAuB,CAAC,UAAU,CAAC,MAC7D,gBAAgB,MAAM,KAAG,MAsazB,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"}