1
0
Fork 0
mirror of synced 2024-09-25 05:41:31 +12:00
budibase/packages/backend-core/src/middleware/tenancy.ts

36 lines
1.1 KiB
TypeScript
Raw Normal View History

import { doInTenant, getTenantIDFromCtx } from "../tenancy"
import { buildMatcherRegex, matches } from "./matchers"
2022-11-17 07:28:45 +13:00
import { Header } from "../constants"
import {
BBContext,
EndpointMatcher,
GetTenantIdOptions,
TenantResolutionStrategy,
} from "@budibase/types"
export default function (
allowQueryStringPatterns: EndpointMatcher[],
noTenancyPatterns: EndpointMatcher[],
opts: { noTenancyRequired?: boolean } = { noTenancyRequired: false }
) {
const allowQsOptions = buildMatcherRegex(allowQueryStringPatterns)
const noTenancyOptions = buildMatcherRegex(noTenancyPatterns)
return async function (ctx: BBContext | any, next: any) {
const allowNoTenant =
opts.noTenancyRequired || !!matches(ctx, noTenancyOptions)
const tenantOpts: GetTenantIdOptions = {
allowNoTenant,
}
const allowQs = !!matches(ctx, allowQsOptions)
if (!allowQs) {
tenantOpts.excludeStrategies = [TenantResolutionStrategy.QUERY]
}
const tenantId = getTenantIDFromCtx(ctx, tenantOpts)
2022-11-17 07:28:45 +13:00
ctx.set(Header.TENANT_ID, tenantId as string)
return doInTenant(tenantId, next)
}
}