@@ -6,9 +6,12 @@ import { defaults } from './defaults.js'
6
6
import { tenantField } from './fields/tenantField/index.js'
7
7
import { tenantsArrayField } from './fields/tenantsArrayField/index.js'
8
8
import { addTenantCleanup } from './hooks/afterTenantDelete.js'
9
+ import { filterDocumentsBySelectedTenant } from './list-filters/filterDocumentsBySelectedTenant.js'
10
+ import { filterTenantsBySelectedTenant } from './list-filters/filterTenantsBySelectedTenant.js'
11
+ import { filterUsersBySelectedTenant } from './list-filters/filterUsersBySelectedTenant.js'
9
12
import { addCollectionAccess } from './utilities/addCollectionAccess.js'
10
13
import { addFilterOptionsToFields } from './utilities/addFilterOptionsToFields.js'
11
- import { withTenantListFilter } from './utilities/withTenantListFilter .js'
14
+ import { combineListFilters } from './utilities/combineListFilters .js'
12
15
13
16
export const multiTenantPlugin =
14
17
< ConfigType > ( pluginConfig : MultiTenantPluginConfig < ConfigType > ) =>
@@ -97,6 +100,23 @@ export const multiTenantPlugin =
97
100
userHasAccessToAllTenants,
98
101
} )
99
102
103
+ if ( pluginConfig . useUsersTenantFilter !== false ) {
104
+ if ( ! adminUsersCollection . admin ) {
105
+ adminUsersCollection . admin = { }
106
+ }
107
+
108
+ adminUsersCollection . admin . baseListFilter = combineListFilters ( {
109
+ baseListFilter : adminUsersCollection . admin ?. baseListFilter ,
110
+ customFilter : ( args ) =>
111
+ filterUsersBySelectedTenant ( {
112
+ req : args . req ,
113
+ tenantsArrayFieldName,
114
+ tenantsArrayTenantFieldName,
115
+ tenantsCollectionSlug,
116
+ } ) ,
117
+ } )
118
+ }
119
+
100
120
let tenantCollection : CollectionConfig | undefined
101
121
102
122
const [ collectionSlugs , globalCollectionSlugs ] = Object . keys ( pluginConfig . collections ) . reduce <
@@ -138,6 +158,25 @@ export const multiTenantPlugin =
138
158
} )
139
159
}
140
160
161
+ if ( pluginConfig . useTenantsListFilter !== false ) {
162
+ /**
163
+ * Add list filter to tenants collection
164
+ * - filter by selected tenant
165
+ */
166
+ if ( ! collection . admin ) {
167
+ collection . admin = { }
168
+ }
169
+
170
+ collection . admin . baseListFilter = combineListFilters ( {
171
+ baseListFilter : collection . admin ?. baseListFilter ,
172
+ customFilter : ( args ) =>
173
+ filterTenantsBySelectedTenant ( {
174
+ req : args . req ,
175
+ tenantsCollectionSlug,
176
+ } ) ,
177
+ } )
178
+ }
179
+
141
180
if ( pluginConfig . cleanupAfterTenantDelete !== false ) {
142
181
/**
143
182
* Add cleanup logic when tenant is deleted
@@ -195,10 +234,15 @@ export const multiTenantPlugin =
195
234
if ( ! collection . admin ) {
196
235
collection . admin = { }
197
236
}
198
- collection . admin . baseListFilter = withTenantListFilter ( {
237
+
238
+ collection . admin . baseListFilter = combineListFilters ( {
199
239
baseListFilter : collection . admin ?. baseListFilter ,
200
- tenantFieldName,
201
- tenantsCollectionSlug,
240
+ customFilter : ( args ) =>
241
+ filterDocumentsBySelectedTenant ( {
242
+ req : args . req ,
243
+ tenantFieldName,
244
+ tenantsCollectionSlug,
245
+ } ) ,
202
246
} )
203
247
}
204
248
0 commit comments