@payloadcms/plugin-multi-tenant 0.0.1 → 3.18.0

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 (104) hide show
  1. package/README.md +130 -95
  2. package/dist/components/GlobalViewRedirect/index.d.ts +3 -2
  3. package/dist/components/GlobalViewRedirect/index.d.ts.map +1 -1
  4. package/dist/components/GlobalViewRedirect/index.js +1 -0
  5. package/dist/components/GlobalViewRedirect/index.js.map +1 -1
  6. package/dist/components/TenantField/index.client.d.ts +3 -3
  7. package/dist/components/TenantField/index.client.d.ts.map +1 -1
  8. package/dist/components/TenantField/index.client.js +55 -17
  9. package/dist/components/TenantField/index.client.js.map +1 -1
  10. package/dist/components/TenantField/index.scss +14 -0
  11. package/dist/components/TenantSelector/index.d.ts +5 -8
  12. package/dist/components/TenantSelector/index.d.ts.map +1 -1
  13. package/dist/components/TenantSelector/index.js +35 -24
  14. package/dist/components/TenantSelector/index.js.map +1 -1
  15. package/dist/constants.d.ts +2 -0
  16. package/dist/constants.d.ts.map +1 -0
  17. package/dist/constants.js +4 -0
  18. package/dist/constants.js.map +1 -0
  19. package/dist/exports/client.d.ts +3 -0
  20. package/dist/exports/client.d.ts.map +1 -0
  21. package/dist/exports/client.js +4 -0
  22. package/dist/exports/client.js.map +1 -0
  23. package/dist/exports/fields.d.ts +3 -0
  24. package/dist/exports/fields.d.ts.map +1 -0
  25. package/dist/exports/fields.js +4 -0
  26. package/dist/exports/fields.js.map +1 -0
  27. package/dist/exports/rsc.d.ts +1 -2
  28. package/dist/exports/rsc.d.ts.map +1 -1
  29. package/dist/exports/rsc.js +1 -2
  30. package/dist/exports/rsc.js.map +1 -1
  31. package/dist/fields/tenantField/index.d.ts +2 -3
  32. package/dist/fields/tenantField/index.d.ts.map +1 -1
  33. package/dist/fields/tenantField/index.js +8 -4
  34. package/dist/fields/tenantField/index.js.map +1 -1
  35. package/dist/fields/tenantsArrayField/index.d.ts +7 -0
  36. package/dist/fields/tenantsArrayField/index.d.ts.map +1 -0
  37. package/dist/fields/{userTenantsArrayField → tenantsArrayField}/index.js +3 -3
  38. package/dist/fields/tenantsArrayField/index.js.map +1 -0
  39. package/dist/hooks/afterTenantDelete.d.ts +16 -0
  40. package/dist/hooks/afterTenantDelete.d.ts.map +1 -0
  41. package/dist/hooks/afterTenantDelete.js +71 -0
  42. package/dist/hooks/afterTenantDelete.js.map +1 -0
  43. package/dist/index.d.ts +1 -1
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +112 -49
  46. package/dist/index.js.map +1 -1
  47. package/dist/providers/TenantSelectionProvider/index.client.d.ts +19 -0
  48. package/dist/providers/TenantSelectionProvider/index.client.d.ts.map +1 -0
  49. package/dist/providers/TenantSelectionProvider/index.client.js +69 -0
  50. package/dist/providers/TenantSelectionProvider/index.client.js.map +1 -0
  51. package/dist/providers/TenantSelectionProvider/index.d.ts +11 -0
  52. package/dist/providers/TenantSelectionProvider/index.d.ts.map +1 -0
  53. package/dist/providers/TenantSelectionProvider/index.js +31 -0
  54. package/dist/providers/TenantSelectionProvider/index.js.map +1 -0
  55. package/dist/types.d.ts +54 -17
  56. package/dist/types.d.ts.map +1 -1
  57. package/dist/types.js.map +1 -1
  58. package/dist/utilities/addCollectionAccess.d.ts +13 -0
  59. package/dist/utilities/addCollectionAccess.d.ts.map +1 -0
  60. package/dist/utilities/addCollectionAccess.js +29 -0
  61. package/dist/utilities/addCollectionAccess.js.map +1 -0
  62. package/dist/utilities/addFilterOptionsToFields.d.ts +9 -0
  63. package/dist/utilities/addFilterOptionsToFields.d.ts.map +1 -0
  64. package/dist/utilities/addFilterOptionsToFields.js +107 -0
  65. package/dist/utilities/addFilterOptionsToFields.js.map +1 -0
  66. package/dist/utilities/combineWhereConstraints.js +2 -2
  67. package/dist/utilities/combineWhereConstraints.js.map +1 -1
  68. package/dist/utilities/getGlobalViewRedirect.d.ts +4 -3
  69. package/dist/utilities/getGlobalViewRedirect.d.ts.map +1 -1
  70. package/dist/utilities/getGlobalViewRedirect.js +6 -5
  71. package/dist/utilities/getGlobalViewRedirect.js.map +1 -1
  72. package/dist/utilities/getTenantAccess.d.ts +7 -3
  73. package/dist/utilities/getTenantAccess.d.ts.map +1 -1
  74. package/dist/utilities/getTenantAccess.js +6 -9
  75. package/dist/utilities/getTenantAccess.js.map +1 -1
  76. package/dist/utilities/getTenantFromCookie.d.ts +8 -1
  77. package/dist/utilities/getTenantFromCookie.d.ts.map +1 -1
  78. package/dist/utilities/getTenantFromCookie.js +7 -1
  79. package/dist/utilities/getTenantFromCookie.js.map +1 -1
  80. package/dist/utilities/getTenantListFilter.d.ts.map +1 -1
  81. package/dist/utilities/getTenantListFilter.js +8 -8
  82. package/dist/utilities/getTenantListFilter.js.map +1 -1
  83. package/dist/utilities/getUserTenantIDs.d.ts +1 -2
  84. package/dist/utilities/getUserTenantIDs.d.ts.map +1 -1
  85. package/dist/utilities/getUserTenantIDs.js +2 -6
  86. package/dist/utilities/getUserTenantIDs.js.map +1 -1
  87. package/dist/utilities/withTenantAccess.d.ts +4 -3
  88. package/dist/utilities/withTenantAccess.d.ts.map +1 -1
  89. package/dist/utilities/withTenantAccess.js +12 -16
  90. package/dist/utilities/withTenantAccess.js.map +1 -1
  91. package/dist/utilities/withTenantListFilter.js +1 -1
  92. package/dist/utilities/withTenantListFilter.js.map +1 -1
  93. package/package.json +20 -10
  94. package/dist/components/TenantField/index.d.ts +0 -3
  95. package/dist/components/TenantField/index.d.ts.map +0 -1
  96. package/dist/components/TenantField/index.js +0 -33
  97. package/dist/components/TenantField/index.js.map +0 -1
  98. package/dist/components/TenantSelector/index.client.d.ts +0 -11
  99. package/dist/components/TenantSelector/index.client.d.ts.map +0 -1
  100. package/dist/components/TenantSelector/index.client.js +0 -61
  101. package/dist/components/TenantSelector/index.client.js.map +0 -1
  102. package/dist/fields/userTenantsArrayField/index.d.ts +0 -4
  103. package/dist/fields/userTenantsArrayField/index.d.ts.map +0 -1
  104. package/dist/fields/userTenantsArrayField/index.js.map +0 -1
@@ -0,0 +1,31 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { cookies as getCookies } from 'next/headers.js';
3
+ import { TenantSelectionProviderClient } from './index.client.js';
4
+ export const TenantSelectionProvider = async ({ children, payload, tenantsCollectionSlug, useAsTitle, user })=>{
5
+ const { docs: userTenants } = await payload.find({
6
+ collection: tenantsCollectionSlug,
7
+ depth: 0,
8
+ limit: 1000,
9
+ overrideAccess: false,
10
+ sort: useAsTitle,
11
+ user
12
+ });
13
+ const tenantOptions = userTenants.map((doc)=>({
14
+ label: String(doc[useAsTitle]),
15
+ value: String(doc.id)
16
+ }));
17
+ const cookies = await getCookies();
18
+ const tenantCookie = cookies.get('payload-tenant')?.value;
19
+ const selectedTenant = tenantOptions.find((option)=>option.value === tenantCookie)?.label || tenantCookie;
20
+ return /*#__PURE__*/ _jsx("span", {
21
+ "data-selected-tenant-id": tenantCookie,
22
+ "data-selected-tenant-title": selectedTenant,
23
+ children: /*#__PURE__*/ _jsx(TenantSelectionProviderClient, {
24
+ initialValue: tenantCookie,
25
+ tenantOptions: tenantOptions,
26
+ children: children
27
+ })
28
+ });
29
+ };
30
+
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/providers/TenantSelectionProvider/index.tsx"],"sourcesContent":["import type { OptionObject, Payload, User } from 'payload'\n\nimport { cookies as getCookies } from 'next/headers.js'\n\nimport { TenantSelectionProviderClient } from './index.client.js'\n\ntype Args = {\n children: React.ReactNode\n payload: Payload\n tenantsCollectionSlug: string\n useAsTitle: string\n user: User\n}\n\nexport const TenantSelectionProvider = async ({\n children,\n payload,\n tenantsCollectionSlug,\n useAsTitle,\n user,\n}: Args) => {\n const { docs: userTenants } = await payload.find({\n collection: tenantsCollectionSlug,\n depth: 0,\n limit: 1000,\n overrideAccess: false,\n sort: useAsTitle,\n user,\n })\n\n const tenantOptions: OptionObject[] = userTenants.map((doc) => ({\n label: String(doc[useAsTitle]),\n value: String(doc.id),\n }))\n const cookies = await getCookies()\n const tenantCookie = cookies.get('payload-tenant')?.value\n const selectedTenant =\n tenantOptions.find((option) => option.value === tenantCookie)?.label || tenantCookie\n\n return (\n <span data-selected-tenant-id={tenantCookie} data-selected-tenant-title={selectedTenant}>\n <TenantSelectionProviderClient initialValue={tenantCookie} tenantOptions={tenantOptions}>\n {children}\n </TenantSelectionProviderClient>\n </span>\n )\n}\n"],"names":["cookies","getCookies","TenantSelectionProviderClient","TenantSelectionProvider","children","payload","tenantsCollectionSlug","useAsTitle","user","docs","userTenants","find","collection","depth","limit","overrideAccess","sort","tenantOptions","map","doc","label","String","value","id","tenantCookie","get","selectedTenant","option","span","data-selected-tenant-id","data-selected-tenant-title","initialValue"],"mappings":";AAEA,SAASA,WAAWC,UAAU,QAAQ,kBAAiB;AAEvD,SAASC,6BAA6B,QAAQ,oBAAmB;AAUjE,OAAO,MAAMC,0BAA0B,OAAO,EAC5CC,QAAQ,EACRC,OAAO,EACPC,qBAAqB,EACrBC,UAAU,EACVC,IAAI,EACC;IACL,MAAM,EAAEC,MAAMC,WAAW,EAAE,GAAG,MAAML,QAAQM,IAAI,CAAC;QAC/CC,YAAYN;QACZO,OAAO;QACPC,OAAO;QACPC,gBAAgB;QAChBC,MAAMT;QACNC;IACF;IAEA,MAAMS,gBAAgCP,YAAYQ,GAAG,CAAC,CAACC,MAAS,CAAA;YAC9DC,OAAOC,OAAOF,GAAG,CAACZ,WAAW;YAC7Be,OAAOD,OAAOF,IAAII,EAAE;QACtB,CAAA;IACA,MAAMvB,UAAU,MAAMC;IACtB,MAAMuB,eAAexB,QAAQyB,GAAG,CAAC,mBAAmBH;IACpD,MAAMI,iBACJT,cAAcN,IAAI,CAAC,CAACgB,SAAWA,OAAOL,KAAK,KAAKE,eAAeJ,SAASI;IAE1E,qBACE,KAACI;QAAKC,2BAAyBL;QAAcM,8BAA4BJ;kBACvE,cAAA,KAACxB;YAA8B6B,cAAcP;YAAcP,eAAeA;sBACvEb;;;AAIT,EAAC"}
package/dist/types.d.ts CHANGED
@@ -1,7 +1,18 @@
1
- import type { ArrayField, Field, RelationshipField, User } from 'payload';
2
- export type MultiTenantPluginConfig = {
1
+ import type { ArrayField, CollectionSlug, Field, RelationshipField, User } from 'payload';
2
+ export type MultiTenantPluginConfig<ConfigTypes = unknown> = {
3
+ /**
4
+ * After a tenant is deleted, the plugin will attempt to clean up related documents
5
+ * - removing documents with the tenant ID
6
+ * - removing the tenant from users
7
+ *
8
+ * @default true
9
+ */
10
+ cleanupAfterTenantDelete?: boolean;
11
+ /**
12
+ * Automatically
13
+ */
3
14
  collections: {
4
- [collectionSlug: string]: {
15
+ [key in CollectionSlug]?: {
5
16
  /**
6
17
  * Set to `true` if you want the collection to behave as a global
7
18
  *
@@ -29,11 +40,17 @@ export type MultiTenantPluginConfig = {
29
40
  * @default false
30
41
  */
31
42
  debug?: boolean;
43
+ /**
44
+ * Enables the multi-tenant plugin
45
+ *
46
+ * @default true
47
+ */
48
+ enabled?: boolean;
32
49
  /**
33
50
  * Field configuration for the field added to all tenant enabled collections
34
51
  */
35
- documentTenantField: {
36
- access: RelationshipField['access'];
52
+ tenantField?: {
53
+ access?: RelationshipField['access'];
37
54
  /**
38
55
  * The name of the field added to all tenant enabled collections
39
56
  *
@@ -42,11 +59,37 @@ export type MultiTenantPluginConfig = {
42
59
  name?: string;
43
60
  };
44
61
  /**
45
- * Enables the multi-tenant plugin
62
+ * Field configuration for the field added to the users collection
46
63
  *
47
- * @default true
64
+ * If `includeDefaultField` is `false`, you must include the field on your users collection manually
65
+ * This is useful if you want to customize the field or place the field in a specific location
48
66
  */
49
- enabled?: boolean;
67
+ tenantsArrayField?: {
68
+ /**
69
+ * Access configuration for the array field
70
+ */
71
+ arrayFieldAccess?: ArrayField['access'];
72
+ /**
73
+ * When `includeDefaultField` is `true`, the field will be added to the users collection automatically
74
+ */
75
+ includeDefaultField?: true;
76
+ /**
77
+ * Additional fields to include on the tenants array field
78
+ */
79
+ rowFields?: Field[];
80
+ /**
81
+ * Access configuration for the tenant field
82
+ */
83
+ tenantFieldAccess?: RelationshipField['access'];
84
+ } | {
85
+ arrayFieldAccess?: never;
86
+ /**
87
+ * When `includeDefaultField` is `false`, you must include the field on your users collection manually
88
+ */
89
+ includeDefaultField?: false;
90
+ rowFields?: never;
91
+ tenantFieldAccess?: never;
92
+ };
50
93
  /**
51
94
  * The slug for the tenant collection
52
95
  *
@@ -58,14 +101,9 @@ export type MultiTenantPluginConfig = {
58
101
  *
59
102
  * Useful for super-admin type users
60
103
  */
61
- userHasAccessToAllTenants?: (user: User) => boolean;
62
- /**
63
- * Field configuration for the field added to the users collection
64
- */
65
- userTenantsField?: {
66
- access?: ArrayField['access'];
67
- rowFields?: Field[];
68
- };
104
+ userHasAccessToAllTenants?: (user: ConfigTypes extends {
105
+ user: User;
106
+ } ? ConfigTypes['user'] : User) => boolean;
69
107
  };
70
108
  export type Tenant<IDType = number | string> = {
71
109
  id: IDType;
@@ -73,7 +111,6 @@ export type Tenant<IDType = number | string> = {
73
111
  };
74
112
  export type UserWithTenantsField = {
75
113
  tenants: {
76
- roles: string[];
77
114
  tenant: number | string | Tenant;
78
115
  }[];
79
116
  } & User;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,iBAAiB,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAEzE,MAAM,MAAM,uBAAuB,GAAG;IACpC,WAAW,EAAE;QACX,CAAC,cAAc,EAAE,MAAM,GAAG;YACxB;;;;eAIG;YACH,QAAQ,CAAC,EAAE,OAAO,CAAA;YAClB;;;;eAIG;YACH,iBAAiB,CAAC,EAAE,OAAO,CAAA;YAC3B;;;;eAIG;YACH,eAAe,CAAC,EAAE,OAAO,CAAA;SAC1B,CAAA;KACF,CAAA;IACD;;;;;OAKG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;IACf;;OAEG;IACH,mBAAmB,EAAE;QACnB,MAAM,EAAE,iBAAiB,CAAC,QAAQ,CAAC,CAAA;QACnC;;;;WAIG;QACH,IAAI,CAAC,EAAE,MAAM,CAAA;KACd,CAAA;IACD;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;;OAIG;IACH,yBAAyB,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAA;IACnD;;OAEG;IACH,gBAAgB,CAAC,EAAE;QACjB,MAAM,CAAC,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAA;QAC7B,SAAS,CAAC,EAAE,KAAK,EAAE,CAAA;KACpB,CAAA;CACF,CAAA;AAED,MAAM,MAAM,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,IAAI;IAC7C,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;CACb,CAAA;AAED,MAAM,MAAM,oBAAoB,GAAG;IACjC,OAAO,EAAE;QACP,KAAK,EAAE,MAAM,EAAE,CAAA;QACf,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;KACjC,EAAE,CAAA;CACJ,GAAG,IAAI,CAAA"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAE,KAAK,EAAE,iBAAiB,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAEzF,MAAM,MAAM,uBAAuB,CAAC,WAAW,GAAG,OAAO,IAAI;IAC3D;;;;;;OAMG;IACH,wBAAwB,CAAC,EAAE,OAAO,CAAA;IAClC;;OAEG;IACH,WAAW,EAAE;SACV,GAAG,IAAI,cAAc,CAAC,CAAC,EAAE;YACxB;;;;eAIG;YACH,QAAQ,CAAC,EAAE,OAAO,CAAA;YAClB;;;;eAIG;YACH,iBAAiB,CAAC,EAAE,OAAO,CAAA;YAC3B;;;;eAIG;YACH,eAAe,CAAC,EAAE,OAAO,CAAA;SAC1B;KACF,CAAA;IACD;;;;;OAKG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;IACf;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;OAEG;IACH,WAAW,CAAC,EAAE;QACZ,MAAM,CAAC,EAAE,iBAAiB,CAAC,QAAQ,CAAC,CAAA;QACpC;;;;WAIG;QACH,IAAI,CAAC,EAAE,MAAM,CAAA;KACd,CAAA;IACD;;;;;OAKG;IACH,iBAAiB,CAAC,EACd;QACE;;WAEG;QACH,gBAAgB,CAAC,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAA;QACvC;;WAEG;QACH,mBAAmB,CAAC,EAAE,IAAI,CAAA;QAC1B;;WAEG;QACH,SAAS,CAAC,EAAE,KAAK,EAAE,CAAA;QACnB;;WAEG;QACH,iBAAiB,CAAC,EAAE,iBAAiB,CAAC,QAAQ,CAAC,CAAA;KAChD,GACD;QACE,gBAAgB,CAAC,EAAE,KAAK,CAAA;QACxB;;WAEG;QACH,mBAAmB,CAAC,EAAE,KAAK,CAAA;QAC3B,SAAS,CAAC,EAAE,KAAK,CAAA;QACjB,iBAAiB,CAAC,EAAE,KAAK,CAAA;KAC1B,CAAA;IACL;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;;OAIG;IACH,yBAAyB,CAAC,EAAE,CAC1B,IAAI,EAAE,WAAW,SAAS;QAAE,IAAI,EAAE,IAAI,CAAA;KAAE,GAAG,WAAW,CAAC,MAAM,CAAC,GAAG,IAAI,KAClE,OAAO,CAAA;CACb,CAAA;AAED,MAAM,MAAM,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,IAAI;IAC7C,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;CACb,CAAA;AAED,MAAM,MAAM,oBAAoB,GAAG;IACjC,OAAO,EAAE;QACP,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;KACjC,EAAE,CAAA;CACJ,GAAG,IAAI,CAAA"}
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.ts"],"sourcesContent":["import type { ArrayField, Field, RelationshipField, User } from 'payload'\n\nexport type MultiTenantPluginConfig = {\n collections: {\n [collectionSlug: string]: {\n /**\n * Set to `true` if you want the collection to behave as a global\n *\n * @default false\n */\n isGlobal?: boolean\n /**\n * Set to `false` if you want to manually apply the baseListFilter\n *\n * @default true\n */\n useBaseListFilter?: boolean\n /**\n * Set to `false` if you want to handle collection access manually without the multi-tenant constraints applied\n *\n * @default true\n */\n useTenantAccess?: boolean\n }\n }\n /**\n * Enables debug mode\n * - Makes the tenant field visible in the admin UI within applicable collections\n *\n * @default false\n */\n debug?: boolean\n /**\n * Field configuration for the field added to all tenant enabled collections\n */\n documentTenantField: {\n access: RelationshipField['access']\n /**\n * The name of the field added to all tenant enabled collections\n *\n * @default 'tenant'\n */\n name?: string\n }\n /**\n * Enables the multi-tenant plugin\n *\n * @default true\n */\n enabled?: boolean\n /**\n * The slug for the tenant collection\n *\n * @default 'tenants'\n */\n tenantsSlug?: string\n /**\n * Function that determines if a user has access to _all_ tenants\n *\n * Useful for super-admin type users\n */\n userHasAccessToAllTenants?: (user: User) => boolean\n /**\n * Field configuration for the field added to the users collection\n */\n userTenantsField?: {\n access?: ArrayField['access']\n rowFields?: Field[]\n }\n}\n\nexport type Tenant<IDType = number | string> = {\n id: IDType\n name: string\n}\n\nexport type UserWithTenantsField = {\n tenants: {\n roles: string[]\n tenant: number | string | Tenant\n }[]\n} & User\n"],"names":[],"mappings":"AA4EA,WAKQ"}
1
+ {"version":3,"sources":["../src/types.ts"],"sourcesContent":["import type { ArrayField, CollectionSlug, Field, RelationshipField, User } from 'payload'\n\nexport type MultiTenantPluginConfig<ConfigTypes = unknown> = {\n /**\n * After a tenant is deleted, the plugin will attempt to clean up related documents\n * - removing documents with the tenant ID\n * - removing the tenant from users\n *\n * @default true\n */\n cleanupAfterTenantDelete?: boolean\n /**\n * Automatically\n */\n collections: {\n [key in CollectionSlug]?: {\n /**\n * Set to `true` if you want the collection to behave as a global\n *\n * @default false\n */\n isGlobal?: boolean\n /**\n * Set to `false` if you want to manually apply the baseListFilter\n *\n * @default true\n */\n useBaseListFilter?: boolean\n /**\n * Set to `false` if you want to handle collection access manually without the multi-tenant constraints applied\n *\n * @default true\n */\n useTenantAccess?: boolean\n }\n }\n /**\n * Enables debug mode\n * - Makes the tenant field visible in the admin UI within applicable collections\n *\n * @default false\n */\n debug?: boolean\n /**\n * Enables the multi-tenant plugin\n *\n * @default true\n */\n enabled?: boolean\n /**\n * Field configuration for the field added to all tenant enabled collections\n */\n tenantField?: {\n access?: RelationshipField['access']\n /**\n * The name of the field added to all tenant enabled collections\n *\n * @default 'tenant'\n */\n name?: string\n }\n /**\n * Field configuration for the field added to the users collection\n *\n * If `includeDefaultField` is `false`, you must include the field on your users collection manually\n * This is useful if you want to customize the field or place the field in a specific location\n */\n tenantsArrayField?:\n | {\n /**\n * Access configuration for the array field\n */\n arrayFieldAccess?: ArrayField['access']\n /**\n * When `includeDefaultField` is `true`, the field will be added to the users collection automatically\n */\n includeDefaultField?: true\n /**\n * Additional fields to include on the tenants array field\n */\n rowFields?: Field[]\n /**\n * Access configuration for the tenant field\n */\n tenantFieldAccess?: RelationshipField['access']\n }\n | {\n arrayFieldAccess?: never\n /**\n * When `includeDefaultField` is `false`, you must include the field on your users collection manually\n */\n includeDefaultField?: false\n rowFields?: never\n tenantFieldAccess?: never\n }\n /**\n * The slug for the tenant collection\n *\n * @default 'tenants'\n */\n tenantsSlug?: string\n /**\n * Function that determines if a user has access to _all_ tenants\n *\n * Useful for super-admin type users\n */\n userHasAccessToAllTenants?: (\n user: ConfigTypes extends { user: User } ? ConfigTypes['user'] : User,\n ) => boolean\n}\n\nexport type Tenant<IDType = number | string> = {\n id: IDType\n name: string\n}\n\nexport type UserWithTenantsField = {\n tenants: {\n tenant: number | string | Tenant\n }[]\n} & User\n"],"names":[],"mappings":"AAoHA,WAIQ"}
@@ -0,0 +1,13 @@
1
+ import type { CollectionConfig } from 'payload';
2
+ import type { MultiTenantPluginConfig } from '../types.js';
3
+ type Args<ConfigType> = {
4
+ collection: CollectionConfig;
5
+ fieldName: string;
6
+ userHasAccessToAllTenants: Required<MultiTenantPluginConfig<ConfigType>>['userHasAccessToAllTenants'];
7
+ };
8
+ /**
9
+ * Adds tenant access constraint to collection
10
+ */
11
+ export declare const addCollectionAccess: <ConfigType>({ collection, fieldName, userHasAccessToAllTenants, }: Args<ConfigType>) => void;
12
+ export {};
13
+ //# sourceMappingURL=addCollectionAccess.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addCollectionAccess.d.ts","sourceRoot":"","sources":["../../src/utilities/addCollectionAccess.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAU,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAEvD,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAiB1D,KAAK,IAAI,CAAC,UAAU,IAAI;IACtB,UAAU,EAAE,gBAAgB,CAAA;IAC5B,SAAS,EAAE,MAAM,CAAA;IACjB,yBAAyB,EAAE,QAAQ,CACjC,uBAAuB,CAAC,UAAU,CAAC,CACpC,CAAC,2BAA2B,CAAC,CAAA;CAC/B,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,UAAU,yDAI3C,IAAI,CAAC,UAAU,CAAC,KAAG,IAkBrB,CAAA"}
@@ -0,0 +1,29 @@
1
+ import { withTenantAccess } from './withTenantAccess.js';
2
+ const collectionAccessKeys = [
3
+ 'create',
4
+ 'read',
5
+ 'update',
6
+ 'delete',
7
+ 'readVersions',
8
+ 'unlock'
9
+ ];
10
+ /**
11
+ * Adds tenant access constraint to collection
12
+ */ export const addCollectionAccess = ({ collection, fieldName, userHasAccessToAllTenants })=>{
13
+ if (!collection?.access) {
14
+ collection.access = {};
15
+ }
16
+ collectionAccessKeys.reduce((acc, key)=>{
17
+ if (!collection.access) {
18
+ return acc;
19
+ }
20
+ collection.access[key] = withTenantAccess({
21
+ accessFunction: collection.access?.[key],
22
+ fieldName,
23
+ userHasAccessToAllTenants
24
+ });
25
+ return acc;
26
+ }, {});
27
+ };
28
+
29
+ //# sourceMappingURL=addCollectionAccess.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utilities/addCollectionAccess.ts"],"sourcesContent":["import type { Access, CollectionConfig } from 'payload'\n\nimport type { MultiTenantPluginConfig } from '../types.js'\n\nimport { withTenantAccess } from './withTenantAccess.js'\n\ntype AllAccessKeys<T extends readonly string[]> = T[number] extends keyof Omit<\n Required<CollectionConfig>['access'],\n 'admin'\n>\n ? keyof Omit<Required<CollectionConfig>['access'], 'admin'> extends T[number]\n ? T\n : never\n : never\n\nconst collectionAccessKeys: AllAccessKeys<\n ['create', 'read', 'update', 'delete', 'readVersions', 'unlock']\n> = ['create', 'read', 'update', 'delete', 'readVersions', 'unlock'] as const\n\ntype Args<ConfigType> = {\n collection: CollectionConfig\n fieldName: string\n userHasAccessToAllTenants: Required<\n MultiTenantPluginConfig<ConfigType>\n >['userHasAccessToAllTenants']\n}\n\n/**\n * Adds tenant access constraint to collection\n */\nexport const addCollectionAccess = <ConfigType>({\n collection,\n fieldName,\n userHasAccessToAllTenants,\n}: Args<ConfigType>): void => {\n if (!collection?.access) {\n collection.access = {}\n }\n collectionAccessKeys.reduce<{\n [key in (typeof collectionAccessKeys)[number]]?: Access\n }>((acc, key) => {\n if (!collection.access) {\n return acc\n }\n collection.access[key] = withTenantAccess<ConfigType>({\n accessFunction: collection.access?.[key],\n fieldName,\n userHasAccessToAllTenants,\n })\n\n return acc\n }, {})\n}\n"],"names":["withTenantAccess","collectionAccessKeys","addCollectionAccess","collection","fieldName","userHasAccessToAllTenants","access","reduce","acc","key","accessFunction"],"mappings":"AAIA,SAASA,gBAAgB,QAAQ,wBAAuB;AAWxD,MAAMC,uBAEF;IAAC;IAAU;IAAQ;IAAU;IAAU;IAAgB;CAAS;AAUpE;;CAEC,GACD,OAAO,MAAMC,sBAAsB,CAAa,EAC9CC,UAAU,EACVC,SAAS,EACTC,yBAAyB,EACR;IACjB,IAAI,CAACF,YAAYG,QAAQ;QACvBH,WAAWG,MAAM,GAAG,CAAC;IACvB;IACAL,qBAAqBM,MAAM,CAExB,CAACC,KAAKC;QACP,IAAI,CAACN,WAAWG,MAAM,EAAE;YACtB,OAAOE;QACT;QACAL,WAAWG,MAAM,CAACG,IAAI,GAAGT,iBAA6B;YACpDU,gBAAgBP,WAAWG,MAAM,EAAE,CAACG,IAAI;YACxCL;YACAC;QACF;QAEA,OAAOG;IACT,GAAG,CAAC;AACN,EAAC"}
@@ -0,0 +1,9 @@
1
+ import type { Field } from 'payload';
2
+ type AddFilterOptionsToFieldsArgs = {
3
+ fields: Field[];
4
+ tenantEnabledCollectionSlugs: string[];
5
+ tenantEnabledGlobalSlugs: string[];
6
+ };
7
+ export declare function addFilterOptionsToFields({ fields, tenantEnabledCollectionSlugs, tenantEnabledGlobalSlugs, }: AddFilterOptionsToFieldsArgs): void;
8
+ export {};
9
+ //# sourceMappingURL=addFilterOptionsToFields.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addFilterOptionsToFields.d.ts","sourceRoot":"","sources":["../../src/utilities/addFilterOptionsToFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAgD,MAAM,SAAS,CAAA;AAIlF,KAAK,4BAA4B,GAAG;IAClC,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,4BAA4B,EAAE,MAAM,EAAE,CAAA;IACtC,wBAAwB,EAAE,MAAM,EAAE,CAAA;CACnC,CAAA;AACD,wBAAgB,wBAAwB,CAAC,EACvC,MAAM,EACN,4BAA4B,EAC5B,wBAAwB,GACzB,EAAE,4BAA4B,QA+D9B"}
@@ -0,0 +1,107 @@
1
+ import { getTenantFromCookie } from './getTenantFromCookie.js';
2
+ export function addFilterOptionsToFields({ fields, tenantEnabledCollectionSlugs, tenantEnabledGlobalSlugs }) {
3
+ fields.forEach((field)=>{
4
+ if (field.type === 'relationship') {
5
+ /**
6
+ * Adjusts relationship fields to filter by tenant
7
+ * and ensures relationTo cannot be a tenant global collection
8
+ */ if (typeof field.relationTo === 'string') {
9
+ if (tenantEnabledGlobalSlugs.includes(field.relationTo)) {
10
+ throw new Error(`The collection ${field.relationTo} is a global collection and cannot be related to a tenant enabled collection.`);
11
+ }
12
+ if (tenantEnabledCollectionSlugs.includes(field.relationTo)) {
13
+ addFilter(field, tenantEnabledCollectionSlugs);
14
+ }
15
+ } else {
16
+ field.relationTo.map((relationTo)=>{
17
+ if (tenantEnabledGlobalSlugs.includes(relationTo)) {
18
+ throw new Error(`The collection ${relationTo} is a global collection and cannot be related to a tenant enabled collection.`);
19
+ }
20
+ if (tenantEnabledCollectionSlugs.includes(relationTo)) {
21
+ addFilter(field, tenantEnabledCollectionSlugs);
22
+ }
23
+ });
24
+ }
25
+ }
26
+ if (field.type === 'row' || field.type === 'array' || field.type === 'collapsible' || field.type === 'group') {
27
+ addFilterOptionsToFields({
28
+ fields: field.fields,
29
+ tenantEnabledCollectionSlugs,
30
+ tenantEnabledGlobalSlugs
31
+ });
32
+ }
33
+ if (field.type === 'blocks') {
34
+ field.blocks.forEach((block)=>{
35
+ addFilterOptionsToFields({
36
+ fields: block.fields,
37
+ tenantEnabledCollectionSlugs,
38
+ tenantEnabledGlobalSlugs
39
+ });
40
+ });
41
+ }
42
+ if (field.type === 'tabs') {
43
+ field.tabs.forEach((tab)=>{
44
+ addFilterOptionsToFields({
45
+ fields: tab.fields,
46
+ tenantEnabledCollectionSlugs,
47
+ tenantEnabledGlobalSlugs
48
+ });
49
+ });
50
+ }
51
+ });
52
+ }
53
+ function addFilter(field, tenantEnabledCollectionSlugs) {
54
+ // User specified filter
55
+ const originalFilter = field.filterOptions;
56
+ field.filterOptions = async (args)=>{
57
+ const originalFilterResult = typeof originalFilter === 'function' ? await originalFilter(args) : originalFilter ?? true;
58
+ // If the relationTo is not a tenant enabled collection, return early
59
+ if (args.relationTo && !tenantEnabledCollectionSlugs.includes(args.relationTo)) {
60
+ return originalFilterResult;
61
+ }
62
+ // If the original filtr returns false, return early
63
+ if (originalFilterResult === false) {
64
+ return false;
65
+ }
66
+ // Custom tenant filter
67
+ const tenantFilterResults = filterOptionsByTenant(args);
68
+ // If the tenant filter returns true, just use the original filter
69
+ if (tenantFilterResults === true) {
70
+ return originalFilterResult;
71
+ }
72
+ // If the original filter returns true, just use the tenant filter
73
+ if (originalFilterResult === true) {
74
+ return tenantFilterResults;
75
+ }
76
+ return {
77
+ and: [
78
+ originalFilterResult,
79
+ tenantFilterResults
80
+ ]
81
+ };
82
+ };
83
+ }
84
+ const filterOptionsByTenant = ({ req, tenantFieldName = 'tenant' })=>{
85
+ const selectedTenant = getTenantFromCookie(req.headers, req.payload.db.defaultIDType);
86
+ if (!selectedTenant) {
87
+ return true;
88
+ }
89
+ return {
90
+ or: [
91
+ // ie a related collection that doesn't have a tenant field
92
+ {
93
+ [tenantFieldName]: {
94
+ exists: false
95
+ }
96
+ },
97
+ // related collections that have a tenant field
98
+ {
99
+ [tenantFieldName]: {
100
+ equals: selectedTenant
101
+ }
102
+ }
103
+ ]
104
+ };
105
+ };
106
+
107
+ //# sourceMappingURL=addFilterOptionsToFields.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utilities/addFilterOptionsToFields.ts"],"sourcesContent":["import type { Field, FilterOptionsProps, RelationshipField, Where } from 'payload'\n\nimport { getTenantFromCookie } from './getTenantFromCookie.js'\n\ntype AddFilterOptionsToFieldsArgs = {\n fields: Field[]\n tenantEnabledCollectionSlugs: string[]\n tenantEnabledGlobalSlugs: string[]\n}\nexport function addFilterOptionsToFields({\n fields,\n tenantEnabledCollectionSlugs,\n tenantEnabledGlobalSlugs,\n}: AddFilterOptionsToFieldsArgs) {\n fields.forEach((field) => {\n if (field.type === 'relationship') {\n /**\n * Adjusts relationship fields to filter by tenant\n * and ensures relationTo cannot be a tenant global collection\n */\n if (typeof field.relationTo === 'string') {\n if (tenantEnabledGlobalSlugs.includes(field.relationTo)) {\n throw new Error(\n `The collection ${field.relationTo} is a global collection and cannot be related to a tenant enabled collection.`,\n )\n }\n if (tenantEnabledCollectionSlugs.includes(field.relationTo)) {\n addFilter(field, tenantEnabledCollectionSlugs)\n }\n } else {\n field.relationTo.map((relationTo) => {\n if (tenantEnabledGlobalSlugs.includes(relationTo)) {\n throw new Error(\n `The collection ${relationTo} is a global collection and cannot be related to a tenant enabled collection.`,\n )\n }\n if (tenantEnabledCollectionSlugs.includes(relationTo)) {\n addFilter(field, tenantEnabledCollectionSlugs)\n }\n })\n }\n }\n\n if (\n field.type === 'row' ||\n field.type === 'array' ||\n field.type === 'collapsible' ||\n field.type === 'group'\n ) {\n addFilterOptionsToFields({\n fields: field.fields,\n tenantEnabledCollectionSlugs,\n tenantEnabledGlobalSlugs,\n })\n }\n\n if (field.type === 'blocks') {\n field.blocks.forEach((block) => {\n addFilterOptionsToFields({\n fields: block.fields,\n tenantEnabledCollectionSlugs,\n tenantEnabledGlobalSlugs,\n })\n })\n }\n\n if (field.type === 'tabs') {\n field.tabs.forEach((tab) => {\n addFilterOptionsToFields({\n fields: tab.fields,\n tenantEnabledCollectionSlugs,\n tenantEnabledGlobalSlugs,\n })\n })\n }\n })\n}\n\nfunction addFilter(field: RelationshipField, tenantEnabledCollectionSlugs: string[]) {\n // User specified filter\n const originalFilter = field.filterOptions\n field.filterOptions = async (args) => {\n const originalFilterResult =\n typeof originalFilter === 'function' ? await originalFilter(args) : (originalFilter ?? true)\n\n // If the relationTo is not a tenant enabled collection, return early\n if (args.relationTo && !tenantEnabledCollectionSlugs.includes(args.relationTo)) {\n return originalFilterResult\n }\n\n // If the original filtr returns false, return early\n if (originalFilterResult === false) {\n return false\n }\n\n // Custom tenant filter\n const tenantFilterResults = filterOptionsByTenant(args)\n\n // If the tenant filter returns true, just use the original filter\n if (tenantFilterResults === true) {\n return originalFilterResult\n }\n\n // If the original filter returns true, just use the tenant filter\n if (originalFilterResult === true) {\n return tenantFilterResults\n }\n\n return {\n and: [originalFilterResult, tenantFilterResults],\n }\n }\n}\n\ntype Args = {\n tenantFieldName?: string\n} & FilterOptionsProps\nconst filterOptionsByTenant = ({ req, tenantFieldName = 'tenant' }: Args) => {\n const selectedTenant = getTenantFromCookie(req.headers, req.payload.db.defaultIDType)\n if (!selectedTenant) {\n return true\n }\n\n return {\n or: [\n // ie a related collection that doesn't have a tenant field\n {\n [tenantFieldName]: {\n exists: false,\n },\n },\n // related collections that have a tenant field\n {\n [tenantFieldName]: {\n equals: selectedTenant,\n },\n },\n ],\n }\n}\n"],"names":["getTenantFromCookie","addFilterOptionsToFields","fields","tenantEnabledCollectionSlugs","tenantEnabledGlobalSlugs","forEach","field","type","relationTo","includes","Error","addFilter","map","blocks","block","tabs","tab","originalFilter","filterOptions","args","originalFilterResult","tenantFilterResults","filterOptionsByTenant","and","req","tenantFieldName","selectedTenant","headers","payload","db","defaultIDType","or","exists","equals"],"mappings":"AAEA,SAASA,mBAAmB,QAAQ,2BAA0B;AAO9D,OAAO,SAASC,yBAAyB,EACvCC,MAAM,EACNC,4BAA4B,EAC5BC,wBAAwB,EACK;IAC7BF,OAAOG,OAAO,CAAC,CAACC;QACd,IAAIA,MAAMC,IAAI,KAAK,gBAAgB;YACjC;;;OAGC,GACD,IAAI,OAAOD,MAAME,UAAU,KAAK,UAAU;gBACxC,IAAIJ,yBAAyBK,QAAQ,CAACH,MAAME,UAAU,GAAG;oBACvD,MAAM,IAAIE,MACR,CAAC,eAAe,EAAEJ,MAAME,UAAU,CAAC,6EAA6E,CAAC;gBAErH;gBACA,IAAIL,6BAA6BM,QAAQ,CAACH,MAAME,UAAU,GAAG;oBAC3DG,UAAUL,OAAOH;gBACnB;YACF,OAAO;gBACLG,MAAME,UAAU,CAACI,GAAG,CAAC,CAACJ;oBACpB,IAAIJ,yBAAyBK,QAAQ,CAACD,aAAa;wBACjD,MAAM,IAAIE,MACR,CAAC,eAAe,EAAEF,WAAW,6EAA6E,CAAC;oBAE/G;oBACA,IAAIL,6BAA6BM,QAAQ,CAACD,aAAa;wBACrDG,UAAUL,OAAOH;oBACnB;gBACF;YACF;QACF;QAEA,IACEG,MAAMC,IAAI,KAAK,SACfD,MAAMC,IAAI,KAAK,WACfD,MAAMC,IAAI,KAAK,iBACfD,MAAMC,IAAI,KAAK,SACf;YACAN,yBAAyB;gBACvBC,QAAQI,MAAMJ,MAAM;gBACpBC;gBACAC;YACF;QACF;QAEA,IAAIE,MAAMC,IAAI,KAAK,UAAU;YAC3BD,MAAMO,MAAM,CAACR,OAAO,CAAC,CAACS;gBACpBb,yBAAyB;oBACvBC,QAAQY,MAAMZ,MAAM;oBACpBC;oBACAC;gBACF;YACF;QACF;QAEA,IAAIE,MAAMC,IAAI,KAAK,QAAQ;YACzBD,MAAMS,IAAI,CAACV,OAAO,CAAC,CAACW;gBAClBf,yBAAyB;oBACvBC,QAAQc,IAAId,MAAM;oBAClBC;oBACAC;gBACF;YACF;QACF;IACF;AACF;AAEA,SAASO,UAAUL,KAAwB,EAAEH,4BAAsC;IACjF,wBAAwB;IACxB,MAAMc,iBAAiBX,MAAMY,aAAa;IAC1CZ,MAAMY,aAAa,GAAG,OAAOC;QAC3B,MAAMC,uBACJ,OAAOH,mBAAmB,aAAa,MAAMA,eAAeE,QAASF,kBAAkB;QAEzF,qEAAqE;QACrE,IAAIE,KAAKX,UAAU,IAAI,CAACL,6BAA6BM,QAAQ,CAACU,KAAKX,UAAU,GAAG;YAC9E,OAAOY;QACT;QAEA,oDAAoD;QACpD,IAAIA,yBAAyB,OAAO;YAClC,OAAO;QACT;QAEA,uBAAuB;QACvB,MAAMC,sBAAsBC,sBAAsBH;QAElD,kEAAkE;QAClE,IAAIE,wBAAwB,MAAM;YAChC,OAAOD;QACT;QAEA,kEAAkE;QAClE,IAAIA,yBAAyB,MAAM;YACjC,OAAOC;QACT;QAEA,OAAO;YACLE,KAAK;gBAACH;gBAAsBC;aAAoB;QAClD;IACF;AACF;AAKA,MAAMC,wBAAwB,CAAC,EAAEE,GAAG,EAAEC,kBAAkB,QAAQ,EAAQ;IACtE,MAAMC,iBAAiB1B,oBAAoBwB,IAAIG,OAAO,EAAEH,IAAII,OAAO,CAACC,EAAE,CAACC,aAAa;IACpF,IAAI,CAACJ,gBAAgB;QACnB,OAAO;IACT;IAEA,OAAO;QACLK,IAAI;YACF,2DAA2D;YAC3D;gBACE,CAACN,gBAAgB,EAAE;oBACjBO,QAAQ;gBACV;YACF;YACA,+CAA+C;YAC/C;gBACE,CAACP,gBAAgB,EAAE;oBACjBQ,QAAQP;gBACV;YACF;SACD;IACH;AACF"}
@@ -2,14 +2,14 @@ export function combineWhereConstraints(constraints) {
2
2
  if (constraints.length === 0) {
3
3
  return {};
4
4
  }
5
- if (constraints.length === 1) {
5
+ if (constraints.length === 1 && constraints[0]) {
6
6
  return constraints[0];
7
7
  }
8
8
  const andConstraint = {
9
9
  and: []
10
10
  };
11
11
  constraints.forEach((constraint)=>{
12
- if (typeof constraint === 'object') {
12
+ if (andConstraint.and && constraint && typeof constraint === 'object') {
13
13
  andConstraint.and.push(constraint);
14
14
  }
15
15
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utilities/combineWhereConstraints.ts"],"sourcesContent":["import type { Where } from 'payload'\n\nexport function combineWhereConstraints(constraints: Array<Where>): Where {\n if (constraints.length === 0) {\n return {}\n }\n if (constraints.length === 1) {\n return constraints[0]\n }\n const andConstraint: Where = {\n and: [],\n }\n constraints.forEach((constraint) => {\n if (typeof constraint === 'object') {\n andConstraint.and.push(constraint)\n }\n })\n return andConstraint\n}\n"],"names":["combineWhereConstraints","constraints","length","andConstraint","and","forEach","constraint","push"],"mappings":"AAEA,OAAO,SAASA,wBAAwBC,WAAyB;IAC/D,IAAIA,YAAYC,MAAM,KAAK,GAAG;QAC5B,OAAO,CAAC;IACV;IACA,IAAID,YAAYC,MAAM,KAAK,GAAG;QAC5B,OAAOD,WAAW,CAAC,EAAE;IACvB;IACA,MAAME,gBAAuB;QAC3BC,KAAK,EAAE;IACT;IACAH,YAAYI,OAAO,CAAC,CAACC;QACnB,IAAI,OAAOA,eAAe,UAAU;YAClCH,cAAcC,GAAG,CAACG,IAAI,CAACD;QACzB;IACF;IACA,OAAOH;AACT"}
1
+ {"version":3,"sources":["../../src/utilities/combineWhereConstraints.ts"],"sourcesContent":["import type { Where } from 'payload'\n\nexport function combineWhereConstraints(constraints: Array<Where>): Where {\n if (constraints.length === 0) {\n return {}\n }\n if (constraints.length === 1 && constraints[0]) {\n return constraints[0]\n }\n const andConstraint: Where = {\n and: [],\n }\n constraints.forEach((constraint) => {\n if (andConstraint.and && constraint && typeof constraint === 'object') {\n andConstraint.and.push(constraint)\n }\n })\n return andConstraint\n}\n"],"names":["combineWhereConstraints","constraints","length","andConstraint","and","forEach","constraint","push"],"mappings":"AAEA,OAAO,SAASA,wBAAwBC,WAAyB;IAC/D,IAAIA,YAAYC,MAAM,KAAK,GAAG;QAC5B,OAAO,CAAC;IACV;IACA,IAAID,YAAYC,MAAM,KAAK,KAAKD,WAAW,CAAC,EAAE,EAAE;QAC9C,OAAOA,WAAW,CAAC,EAAE;IACvB;IACA,MAAME,gBAAuB;QAC3BC,KAAK,EAAE;IACT;IACAH,YAAYI,OAAO,CAAC,CAACC;QACnB,IAAIH,cAAcC,GAAG,IAAIE,cAAc,OAAOA,eAAe,UAAU;YACrEH,cAAcC,GAAG,CAACG,IAAI,CAACD;QACzB;IACF;IACA,OAAOH;AACT"}
@@ -1,10 +1,11 @@
1
- import type { Payload } from 'payload';
1
+ import type { Payload, ViewTypes } from 'payload';
2
2
  type Args = {
3
3
  docID?: number | string;
4
4
  payload: Payload;
5
5
  slug: string;
6
- view: 'edit' | 'list';
6
+ tenantFieldName: string;
7
+ view: ViewTypes;
7
8
  };
8
- export declare function getGlobalViewRedirect({ slug, docID, payload, view, }: Args): Promise<string | void>;
9
+ export declare function getGlobalViewRedirect({ slug, docID, payload, tenantFieldName, view, }: Args): Promise<string | void>;
9
10
  export {};
10
11
  //# sourceMappingURL=getGlobalViewRedirect.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getGlobalViewRedirect.d.ts","sourceRoot":"","sources":["../../src/utilities/getGlobalViewRedirect.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAMtC,KAAK,IAAI,GAAG;IACV,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IACvB,OAAO,EAAE,OAAO,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,GAAG,MAAM,CAAA;CACtB,CAAA;AACD,wBAAsB,qBAAqB,CAAC,EAC1C,IAAI,EACJ,KAAK,EACL,OAAO,EACP,IAAI,GACL,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CA0C/B"}
1
+ {"version":3,"file":"getGlobalViewRedirect.d.ts","sourceRoot":"","sources":["../../src/utilities/getGlobalViewRedirect.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAOjD,KAAK,IAAI,GAAG;IACV,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IACvB,OAAO,EAAE,OAAO,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,eAAe,EAAE,MAAM,CAAA;IACvB,IAAI,EAAE,SAAS,CAAA;CAChB,CAAA;AACD,wBAAsB,qBAAqB,CAAC,EAC1C,IAAI,EACJ,KAAK,EACL,OAAO,EACP,eAAe,EACf,IAAI,GACL,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAgD/B"}
@@ -1,6 +1,7 @@
1
1
  import { headers as getHeaders } from 'next/headers.js';
2
+ import { SELECT_ALL } from '../constants.js';
2
3
  import { getTenantFromCookie } from './getTenantFromCookie.js';
3
- export async function getGlobalViewRedirect({ slug, docID, payload, view }) {
4
+ export async function getGlobalViewRedirect({ slug, docID, payload, tenantFieldName, view }) {
4
5
  const headers = await getHeaders();
5
6
  const tenant = getTenantFromCookie(headers, payload.db.defaultIDType);
6
7
  let redirectRoute;
@@ -10,14 +11,14 @@ export async function getGlobalViewRedirect({ slug, docID, payload, view }) {
10
11
  collection: slug,
11
12
  depth: 0,
12
13
  limit: 1,
13
- where: {
14
- tenant: {
14
+ where: tenant === SELECT_ALL ? {} : {
15
+ [tenantFieldName]: {
15
16
  equals: tenant
16
17
  }
17
18
  }
18
19
  });
19
20
  const tenantDocID = docs?.[0]?.id;
20
- if (view === 'edit') {
21
+ if (view === 'document') {
21
22
  if (docID && !tenantDocID) {
22
23
  // viewing a document with an id but does not match the selected tenant, redirect to create route
23
24
  redirectRoute = `${payload.config.routes.admin}/collections/${slug}/create`;
@@ -35,7 +36,7 @@ export async function getGlobalViewRedirect({ slug, docID, payload, view }) {
35
36
  }
36
37
  }
37
38
  } catch (e) {
38
- payload.logger.error(e, `${e?.message} - Multi Tenant Redirect`);
39
+ payload.logger.error(e, `${typeof e === 'object' && e && 'message' in e ? `e?.message - ` : ''}Multi Tenant Redirect Error`);
39
40
  }
40
41
  }
41
42
  return redirectRoute;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utilities/getGlobalViewRedirect.ts"],"sourcesContent":["import type { Payload } from 'payload'\n\nimport { headers as getHeaders } from 'next/headers.js'\n\nimport { getTenantFromCookie } from './getTenantFromCookie.js'\n\ntype Args = {\n docID?: number | string\n payload: Payload\n slug: string\n view: 'edit' | 'list'\n}\nexport async function getGlobalViewRedirect({\n slug,\n docID,\n payload,\n view,\n}: Args): Promise<string | void> {\n const headers = await getHeaders()\n const tenant = getTenantFromCookie(headers, payload.db.defaultIDType)\n let redirectRoute\n\n if (tenant) {\n try {\n const { docs } = await payload.find({\n collection: slug,\n depth: 0,\n limit: 1,\n where: {\n tenant: {\n equals: tenant,\n },\n },\n })\n\n const tenantDocID = docs?.[0]?.id\n\n if (view === 'edit') {\n if (docID && !tenantDocID) {\n // viewing a document with an id but does not match the selected tenant, redirect to create route\n redirectRoute = `${payload.config.routes.admin}/collections/${slug}/create`\n } else if (tenantDocID && docID !== tenantDocID) {\n // tenant document already exists but does not match current route doc ID, redirect to matching tenant doc\n redirectRoute = `${payload.config.routes.admin}/collections/${slug}/${tenantDocID}`\n }\n } else if (view === 'list') {\n if (tenantDocID) {\n // tenant document exists, redirect to edit view\n redirectRoute = `${payload.config.routes.admin}/collections/${slug}/${tenantDocID}`\n } else {\n // tenant document does not exist, redirect to create route\n redirectRoute = `${payload.config.routes.admin}/collections/${slug}/create`\n }\n }\n } catch (e) {\n payload.logger.error(e, `${e?.message} - Multi Tenant Redirect`)\n }\n }\n return redirectRoute\n}\n"],"names":["headers","getHeaders","getTenantFromCookie","getGlobalViewRedirect","slug","docID","payload","view","tenant","db","defaultIDType","redirectRoute","docs","find","collection","depth","limit","where","equals","tenantDocID","id","config","routes","admin","e","logger","error","message"],"mappings":"AAEA,SAASA,WAAWC,UAAU,QAAQ,kBAAiB;AAEvD,SAASC,mBAAmB,QAAQ,2BAA0B;AAQ9D,OAAO,eAAeC,sBAAsB,EAC1CC,IAAI,EACJC,KAAK,EACLC,OAAO,EACPC,IAAI,EACC;IACL,MAAMP,UAAU,MAAMC;IACtB,MAAMO,SAASN,oBAAoBF,SAASM,QAAQG,EAAE,CAACC,aAAa;IACpE,IAAIC;IAEJ,IAAIH,QAAQ;QACV,IAAI;YACF,MAAM,EAAEI,IAAI,EAAE,GAAG,MAAMN,QAAQO,IAAI,CAAC;gBAClCC,YAAYV;gBACZW,OAAO;gBACPC,OAAO;gBACPC,OAAO;oBACLT,QAAQ;wBACNU,QAAQV;oBACV;gBACF;YACF;YAEA,MAAMW,cAAcP,MAAM,CAAC,EAAE,EAAEQ;YAE/B,IAAIb,SAAS,QAAQ;gBACnB,IAAIF,SAAS,CAACc,aAAa;oBACzB,iGAAiG;oBACjGR,gBAAgB,GAAGL,QAAQe,MAAM,CAACC,MAAM,CAACC,KAAK,CAAC,aAAa,EAAEnB,KAAK,OAAO,CAAC;gBAC7E,OAAO,IAAIe,eAAed,UAAUc,aAAa;oBAC/C,0GAA0G;oBAC1GR,gBAAgB,GAAGL,QAAQe,MAAM,CAACC,MAAM,CAACC,KAAK,CAAC,aAAa,EAAEnB,KAAK,CAAC,EAAEe,aAAa;gBACrF;YACF,OAAO,IAAIZ,SAAS,QAAQ;gBAC1B,IAAIY,aAAa;oBACf,gDAAgD;oBAChDR,gBAAgB,GAAGL,QAAQe,MAAM,CAACC,MAAM,CAACC,KAAK,CAAC,aAAa,EAAEnB,KAAK,CAAC,EAAEe,aAAa;gBACrF,OAAO;oBACL,2DAA2D;oBAC3DR,gBAAgB,GAAGL,QAAQe,MAAM,CAACC,MAAM,CAACC,KAAK,CAAC,aAAa,EAAEnB,KAAK,OAAO,CAAC;gBAC7E;YACF;QACF,EAAE,OAAOoB,GAAG;YACVlB,QAAQmB,MAAM,CAACC,KAAK,CAACF,GAAG,GAAGA,GAAGG,QAAQ,wBAAwB,CAAC;QACjE;IACF;IACA,OAAOhB;AACT"}
1
+ {"version":3,"sources":["../../src/utilities/getGlobalViewRedirect.ts"],"sourcesContent":["import type { Payload, ViewTypes } from 'payload'\n\nimport { headers as getHeaders } from 'next/headers.js'\n\nimport { SELECT_ALL } from '../constants.js'\nimport { getTenantFromCookie } from './getTenantFromCookie.js'\n\ntype Args = {\n docID?: number | string\n payload: Payload\n slug: string\n tenantFieldName: string\n view: ViewTypes\n}\nexport async function getGlobalViewRedirect({\n slug,\n docID,\n payload,\n tenantFieldName,\n view,\n}: Args): Promise<string | void> {\n const headers = await getHeaders()\n const tenant = getTenantFromCookie(headers, payload.db.defaultIDType)\n let redirectRoute\n\n if (tenant) {\n try {\n const { docs } = await payload.find({\n collection: slug,\n depth: 0,\n limit: 1,\n where:\n tenant === SELECT_ALL\n ? {}\n : {\n [tenantFieldName]: {\n equals: tenant,\n },\n },\n })\n\n const tenantDocID = docs?.[0]?.id\n\n if (view === 'document') {\n if (docID && !tenantDocID) {\n // viewing a document with an id but does not match the selected tenant, redirect to create route\n redirectRoute = `${payload.config.routes.admin}/collections/${slug}/create`\n } else if (tenantDocID && docID !== tenantDocID) {\n // tenant document already exists but does not match current route doc ID, redirect to matching tenant doc\n redirectRoute = `${payload.config.routes.admin}/collections/${slug}/${tenantDocID}`\n }\n } else if (view === 'list') {\n if (tenantDocID) {\n // tenant document exists, redirect to edit view\n redirectRoute = `${payload.config.routes.admin}/collections/${slug}/${tenantDocID}`\n } else {\n // tenant document does not exist, redirect to create route\n redirectRoute = `${payload.config.routes.admin}/collections/${slug}/create`\n }\n }\n } catch (e: unknown) {\n payload.logger.error(\n e,\n `${typeof e === 'object' && e && 'message' in e ? `e?.message - ` : ''}Multi Tenant Redirect Error`,\n )\n }\n }\n return redirectRoute\n}\n"],"names":["headers","getHeaders","SELECT_ALL","getTenantFromCookie","getGlobalViewRedirect","slug","docID","payload","tenantFieldName","view","tenant","db","defaultIDType","redirectRoute","docs","find","collection","depth","limit","where","equals","tenantDocID","id","config","routes","admin","e","logger","error"],"mappings":"AAEA,SAASA,WAAWC,UAAU,QAAQ,kBAAiB;AAEvD,SAASC,UAAU,QAAQ,kBAAiB;AAC5C,SAASC,mBAAmB,QAAQ,2BAA0B;AAS9D,OAAO,eAAeC,sBAAsB,EAC1CC,IAAI,EACJC,KAAK,EACLC,OAAO,EACPC,eAAe,EACfC,IAAI,EACC;IACL,MAAMT,UAAU,MAAMC;IACtB,MAAMS,SAASP,oBAAoBH,SAASO,QAAQI,EAAE,CAACC,aAAa;IACpE,IAAIC;IAEJ,IAAIH,QAAQ;QACV,IAAI;YACF,MAAM,EAAEI,IAAI,EAAE,GAAG,MAAMP,QAAQQ,IAAI,CAAC;gBAClCC,YAAYX;gBACZY,OAAO;gBACPC,OAAO;gBACPC,OACET,WAAWR,aACP,CAAC,IACD;oBACE,CAACM,gBAAgB,EAAE;wBACjBY,QAAQV;oBACV;gBACF;YACR;YAEA,MAAMW,cAAcP,MAAM,CAAC,EAAE,EAAEQ;YAE/B,IAAIb,SAAS,YAAY;gBACvB,IAAIH,SAAS,CAACe,aAAa;oBACzB,iGAAiG;oBACjGR,gBAAgB,GAAGN,QAAQgB,MAAM,CAACC,MAAM,CAACC,KAAK,CAAC,aAAa,EAAEpB,KAAK,OAAO,CAAC;gBAC7E,OAAO,IAAIgB,eAAef,UAAUe,aAAa;oBAC/C,0GAA0G;oBAC1GR,gBAAgB,GAAGN,QAAQgB,MAAM,CAACC,MAAM,CAACC,KAAK,CAAC,aAAa,EAAEpB,KAAK,CAAC,EAAEgB,aAAa;gBACrF;YACF,OAAO,IAAIZ,SAAS,QAAQ;gBAC1B,IAAIY,aAAa;oBACf,gDAAgD;oBAChDR,gBAAgB,GAAGN,QAAQgB,MAAM,CAACC,MAAM,CAACC,KAAK,CAAC,aAAa,EAAEpB,KAAK,CAAC,EAAEgB,aAAa;gBACrF,OAAO;oBACL,2DAA2D;oBAC3DR,gBAAgB,GAAGN,QAAQgB,MAAM,CAACC,MAAM,CAACC,KAAK,CAAC,aAAa,EAAEpB,KAAK,OAAO,CAAC;gBAC7E;YACF;QACF,EAAE,OAAOqB,GAAY;YACnBnB,QAAQoB,MAAM,CAACC,KAAK,CAClBF,GACA,GAAG,OAAOA,MAAM,YAAYA,KAAK,aAAaA,IAAI,CAAC,aAAa,CAAC,GAAG,GAAG,2BAA2B,CAAC;QAEvG;IACF;IACA,OAAOb;AACT"}
@@ -1,5 +1,9 @@
1
1
  import type { Where } from 'payload';
2
- export declare function getTenantAccess({ user }: {
3
- user: any;
4
- }): null | Where;
2
+ import type { UserWithTenantsField } from '../types.js';
3
+ type Args = {
4
+ fieldName: string;
5
+ user: UserWithTenantsField;
6
+ };
7
+ export declare function getTenantAccess({ fieldName, user }: Args): Where;
8
+ export {};
5
9
  //# sourceMappingURL=getTenantAccess.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getTenantAccess.d.ts","sourceRoot":"","sources":["../../src/utilities/getTenantAccess.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAMpC,wBAAgB,eAAe,CAAC,EAAE,IAAI,EAAE;;CAAA,GAAG,IAAI,GAAG,KAAK,CAYtD"}
1
+ {"version":3,"file":"getTenantAccess.d.ts","sourceRoot":"","sources":["../../src/utilities/getTenantAccess.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAEpC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAIvD,KAAK,IAAI,GAAG;IACV,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,oBAAoB,CAAA;CAC3B,CAAA;AACD,wBAAgB,eAAe,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,IAAI,GAAG,KAAK,CAQhE"}
@@ -1,14 +1,11 @@
1
1
  import { getUserTenantIDs } from './getUserTenantIDs.js';
2
- export function getTenantAccess({ user }) {
2
+ export function getTenantAccess({ fieldName, user }) {
3
3
  const userAssignedTenantIDs = getUserTenantIDs(user);
4
- if (userAssignedTenantIDs.length) {
5
- return {
6
- tenant: {
7
- in: userAssignedTenantIDs
8
- }
9
- };
10
- }
11
- return null;
4
+ return {
5
+ [fieldName]: {
6
+ in: userAssignedTenantIDs || []
7
+ }
8
+ };
12
9
  }
13
10
 
14
11
  //# sourceMappingURL=getTenantAccess.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utilities/getTenantAccess.ts"],"sourcesContent":["import type { Where } from 'payload'\n\nimport type { UserWithTenantsField } from '../types.js'\n\nimport { getUserTenantIDs } from './getUserTenantIDs.js'\n\nexport function getTenantAccess({ user }): null | Where {\n const userAssignedTenantIDs = getUserTenantIDs(user as UserWithTenantsField)\n\n if (userAssignedTenantIDs.length) {\n return {\n tenant: {\n in: userAssignedTenantIDs,\n },\n }\n }\n\n return null\n}\n"],"names":["getUserTenantIDs","getTenantAccess","user","userAssignedTenantIDs","length","tenant","in"],"mappings":"AAIA,SAASA,gBAAgB,QAAQ,wBAAuB;AAExD,OAAO,SAASC,gBAAgB,EAAEC,IAAI,EAAE;IACtC,MAAMC,wBAAwBH,iBAAiBE;IAE/C,IAAIC,sBAAsBC,MAAM,EAAE;QAChC,OAAO;YACLC,QAAQ;gBACNC,IAAIH;YACN;QACF;IACF;IAEA,OAAO;AACT"}
1
+ {"version":3,"sources":["../../src/utilities/getTenantAccess.ts"],"sourcesContent":["import type { Where } from 'payload'\n\nimport type { UserWithTenantsField } from '../types.js'\n\nimport { getUserTenantIDs } from './getUserTenantIDs.js'\n\ntype Args = {\n fieldName: string\n user: UserWithTenantsField\n}\nexport function getTenantAccess({ fieldName, user }: Args): Where {\n const userAssignedTenantIDs = getUserTenantIDs(user)\n\n return {\n [fieldName]: {\n in: userAssignedTenantIDs || [],\n },\n }\n}\n"],"names":["getUserTenantIDs","getTenantAccess","fieldName","user","userAssignedTenantIDs","in"],"mappings":"AAIA,SAASA,gBAAgB,QAAQ,wBAAuB;AAMxD,OAAO,SAASC,gBAAgB,EAAEC,SAAS,EAAEC,IAAI,EAAQ;IACvD,MAAMC,wBAAwBJ,iBAAiBG;IAE/C,OAAO;QACL,CAACD,UAAU,EAAE;YACXG,IAAID,yBAAyB,EAAE;QACjC;IACF;AACF"}
@@ -1,2 +1,9 @@
1
- export declare function getTenantFromCookie(headers: Headers, idType: 'number' | 'text'): string | number;
1
+ /**
2
+ * A function that takes request headers and an idType and returns the current tenant ID from the cookie
3
+ *
4
+ * @param headers Headers, usually derived from req.headers or next/headers
5
+ * @param idType can be 'number' | 'text', usually derived from payload.db.defaultIDType
6
+ * @returns string | number | null
7
+ */
8
+ export declare function getTenantFromCookie(headers: Headers, idType: 'number' | 'text'): null | number | string;
2
9
  //# sourceMappingURL=getTenantFromCookie.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getTenantFromCookie.d.ts","sourceRoot":"","sources":["../../src/utilities/getTenantFromCookie.ts"],"names":[],"mappings":"AAEA,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,GAAG,MAAM,mBAI9E"}
1
+ {"version":3,"file":"getTenantFromCookie.d.ts","sourceRoot":"","sources":["../../src/utilities/getTenantFromCookie.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,QAAQ,GAAG,MAAM,GACxB,IAAI,GAAG,MAAM,GAAG,MAAM,CAIxB"}
@@ -1,5 +1,11 @@
1
1
  import { parseCookies } from 'payload';
2
- export function getTenantFromCookie(headers, idType) {
2
+ /**
3
+ * A function that takes request headers and an idType and returns the current tenant ID from the cookie
4
+ *
5
+ * @param headers Headers, usually derived from req.headers or next/headers
6
+ * @param idType can be 'number' | 'text', usually derived from payload.db.defaultIDType
7
+ * @returns string | number | null
8
+ */ export function getTenantFromCookie(headers, idType) {
3
9
  const cookies = parseCookies(headers);
4
10
  const selectedTenant = cookies.get('payload-tenant') || null;
5
11
  return selectedTenant ? idType === 'number' ? parseInt(selectedTenant) : selectedTenant : null;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utilities/getTenantFromCookie.ts"],"sourcesContent":["import { parseCookies } from 'payload'\n\nexport function getTenantFromCookie(headers: Headers, idType: 'number' | 'text') {\n const cookies = parseCookies(headers)\n const selectedTenant = cookies.get('payload-tenant') || null\n return selectedTenant ? (idType === 'number' ? parseInt(selectedTenant) : selectedTenant) : null\n}\n"],"names":["parseCookies","getTenantFromCookie","headers","idType","cookies","selectedTenant","get","parseInt"],"mappings":"AAAA,SAASA,YAAY,QAAQ,UAAS;AAEtC,OAAO,SAASC,oBAAoBC,OAAgB,EAAEC,MAAyB;IAC7E,MAAMC,UAAUJ,aAAaE;IAC7B,MAAMG,iBAAiBD,QAAQE,GAAG,CAAC,qBAAqB;IACxD,OAAOD,iBAAkBF,WAAW,WAAWI,SAASF,kBAAkBA,iBAAkB;AAC9F"}
1
+ {"version":3,"sources":["../../src/utilities/getTenantFromCookie.ts"],"sourcesContent":["import { parseCookies } from 'payload'\n\n/**\n * A function that takes request headers and an idType and returns the current tenant ID from the cookie\n *\n * @param headers Headers, usually derived from req.headers or next/headers\n * @param idType can be 'number' | 'text', usually derived from payload.db.defaultIDType\n * @returns string | number | null\n */\nexport function getTenantFromCookie(\n headers: Headers,\n idType: 'number' | 'text',\n): null | number | string {\n const cookies = parseCookies(headers)\n const selectedTenant = cookies.get('payload-tenant') || null\n return selectedTenant ? (idType === 'number' ? parseInt(selectedTenant) : selectedTenant) : null\n}\n"],"names":["parseCookies","getTenantFromCookie","headers","idType","cookies","selectedTenant","get","parseInt"],"mappings":"AAAA,SAASA,YAAY,QAAQ,UAAS;AAEtC;;;;;;CAMC,GACD,OAAO,SAASC,oBACdC,OAAgB,EAChBC,MAAyB;IAEzB,MAAMC,UAAUJ,aAAaE;IAC7B,MAAMG,iBAAiBD,QAAQE,GAAG,CAAC,qBAAqB;IACxD,OAAOD,iBAAkBF,WAAW,WAAWI,SAASF,kBAAkBA,iBAAkB;AAC9F"}
@@ -1 +1 @@
1
- {"version":3,"file":"getTenantListFilter.d.ts","sourceRoot":"","sources":["../../src/utilities/getTenantListFilter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAIpD,KAAK,IAAI,GAAG;IACV,GAAG,EAAE,cAAc,CAAA;IACnB,eAAe,EAAE,MAAM,CAAA;CACxB,CAAA;AACD,eAAO,MAAM,mBAAmB,6BAA8B,IAAI,KAAG,IAAI,GAAG,KAa3E,CAAA"}
1
+ {"version":3,"file":"getTenantListFilter.d.ts","sourceRoot":"","sources":["../../src/utilities/getTenantListFilter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAKpD,KAAK,IAAI,GAAG;IACV,GAAG,EAAE,cAAc,CAAA;IACnB,eAAe,EAAE,MAAM,CAAA;CACxB,CAAA;AACD,eAAO,MAAM,mBAAmB,6BAA8B,IAAI,KAAG,IAAI,GAAG,KAY3E,CAAA"}
@@ -1,15 +1,15 @@
1
+ import { SELECT_ALL } from '../constants.js';
1
2
  import { getTenantFromCookie } from './getTenantFromCookie.js';
2
3
  export const getTenantListFilter = ({ req, tenantFieldName })=>{
3
4
  const selectedTenant = getTenantFromCookie(req.headers, req.payload.db.defaultIDType);
4
- if (selectedTenant) {
5
- return {
6
- [tenantFieldName]: {
7
- equals: selectedTenant
8
- }
9
- };
5
+ if (selectedTenant === SELECT_ALL) {
6
+ return {};
10
7
  }
11
- // Access control will take it from here
12
- return null;
8
+ return {
9
+ [tenantFieldName]: {
10
+ equals: selectedTenant
11
+ }
12
+ };
13
13
  };
14
14
 
15
15
  //# sourceMappingURL=getTenantListFilter.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utilities/getTenantListFilter.ts"],"sourcesContent":["import type { PayloadRequest, Where } from 'payload'\n\nimport { getTenantFromCookie } from './getTenantFromCookie.js'\n\ntype Args = {\n req: PayloadRequest\n tenantFieldName: string\n}\nexport const getTenantListFilter = ({ req, tenantFieldName }: Args): null | Where => {\n const selectedTenant = getTenantFromCookie(req.headers, req.payload.db.defaultIDType)\n\n if (selectedTenant) {\n return {\n [tenantFieldName]: {\n equals: selectedTenant,\n },\n }\n }\n\n // Access control will take it from here\n return null\n}\n"],"names":["getTenantFromCookie","getTenantListFilter","req","tenantFieldName","selectedTenant","headers","payload","db","defaultIDType","equals"],"mappings":"AAEA,SAASA,mBAAmB,QAAQ,2BAA0B;AAM9D,OAAO,MAAMC,sBAAsB,CAAC,EAAEC,GAAG,EAAEC,eAAe,EAAQ;IAChE,MAAMC,iBAAiBJ,oBAAoBE,IAAIG,OAAO,EAAEH,IAAII,OAAO,CAACC,EAAE,CAACC,aAAa;IAEpF,IAAIJ,gBAAgB;QAClB,OAAO;YACL,CAACD,gBAAgB,EAAE;gBACjBM,QAAQL;YACV;QACF;IACF;IAEA,wCAAwC;IACxC,OAAO;AACT,EAAC"}
1
+ {"version":3,"sources":["../../src/utilities/getTenantListFilter.ts"],"sourcesContent":["import type { PayloadRequest, Where } from 'payload'\n\nimport { SELECT_ALL } from '../constants.js'\nimport { getTenantFromCookie } from './getTenantFromCookie.js'\n\ntype Args = {\n req: PayloadRequest\n tenantFieldName: string\n}\nexport const getTenantListFilter = ({ req, tenantFieldName }: Args): null | Where => {\n const selectedTenant = getTenantFromCookie(req.headers, req.payload.db.defaultIDType)\n\n if (selectedTenant === SELECT_ALL) {\n return {}\n }\n\n return {\n [tenantFieldName]: {\n equals: selectedTenant,\n },\n }\n}\n"],"names":["SELECT_ALL","getTenantFromCookie","getTenantListFilter","req","tenantFieldName","selectedTenant","headers","payload","db","defaultIDType","equals"],"mappings":"AAEA,SAASA,UAAU,QAAQ,kBAAiB;AAC5C,SAASC,mBAAmB,QAAQ,2BAA0B;AAM9D,OAAO,MAAMC,sBAAsB,CAAC,EAAEC,GAAG,EAAEC,eAAe,EAAQ;IAChE,MAAMC,iBAAiBJ,oBAAoBE,IAAIG,OAAO,EAAEH,IAAII,OAAO,CAACC,EAAE,CAACC,aAAa;IAEpF,IAAIJ,mBAAmBL,YAAY;QACjC,OAAO,CAAC;IACV;IAEA,OAAO;QACL,CAACI,gBAAgB,EAAE;YACjBM,QAAQL;QACV;IACF;AACF,EAAC"}
@@ -3,7 +3,6 @@ import type { UserWithTenantsField } from '../types.js';
3
3
  * Returns array of all tenant IDs assigned to a user
4
4
  *
5
5
  * @param user - User object with tenants field
6
- * @param role - Optional role to filter by
7
6
  */
8
- export declare const getUserTenantIDs: <IDType extends number | string>(user: null | UserWithTenantsField, role?: string) => IDType[];
7
+ export declare const getUserTenantIDs: <IDType extends number | string>(user: null | UserWithTenantsField) => IDType[];
9
8
  //# sourceMappingURL=getUserTenantIDs.d.ts.map