@payloadcms/plugin-multi-tenant
Version:
Multi Tenant plugin for Payload
37 lines (36 loc) • 1.4 kB
JavaScript
import { defaults } from '../defaults.js';
import { getCollectionIDType } from '../utilities/getCollectionIDType.js';
import { getTenantFromCookie } from '../utilities/getTenantFromCookie.js';
import { getUserTenantIDs } from '../utilities/getUserTenantIDs.js';
export const filterDocumentsByTenants = ({ filterFieldName, req, tenantsArrayFieldName = defaults.tenantsArrayFieldName, tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName, tenantsCollectionSlug })=>{
const idType = getCollectionIDType({
collectionSlug: tenantsCollectionSlug,
payload: req.payload
});
// scope results to selected tenant
const selectedTenant = getTenantFromCookie(req.headers, idType);
if (selectedTenant) {
return {
[filterFieldName]: {
in: [
selectedTenant
]
}
};
}
// scope to user assigned tenants
const userAssignedTenants = getUserTenantIDs(req.user, {
tenantsArrayFieldName,
tenantsArrayTenantFieldName
});
if (userAssignedTenants.length > 0) {
return {
[filterFieldName]: {
in: userAssignedTenants
}
};
}
// no tenant selected and no user tenants, return null to allow access control to handle it
return null;
};
//# sourceMappingURL=filterDocumentsByTenants.js.map