2022-11-28 18:54:04 +01:00
|
|
|
import { doWithDB, queryPlatformView, getGlobalDBName } from "../db"
|
2022-06-13 11:51:29 +02:00
|
|
|
import {
|
2022-03-24 14:04:49 +01:00
|
|
|
DEFAULT_TENANT_ID,
|
2022-11-10 18:38:26 +01:00
|
|
|
getTenantId,
|
2022-03-24 14:04:49 +01:00
|
|
|
getTenantIDFromAppID,
|
2022-11-10 18:38:26 +01:00
|
|
|
isMultiTenant,
|
2022-06-13 11:51:29 +02:00
|
|
|
} from "../context"
|
|
|
|
import env from "../environment"
|
2022-11-09 17:35:16 +01:00
|
|
|
import {
|
|
|
|
BBContext,
|
|
|
|
PlatformUser,
|
|
|
|
TenantResolutionStrategy,
|
|
|
|
GetTenantIdOptions,
|
|
|
|
} from "@budibase/types"
|
2022-11-28 18:54:04 +01:00
|
|
|
import { Header, StaticDatabases, ViewName } from "../constants"
|
2021-08-05 10:59:08 +02:00
|
|
|
|
|
|
|
const TENANT_DOC = StaticDatabases.PLATFORM_INFO.docs.tenants
|
|
|
|
const PLATFORM_INFO_DB = StaticDatabases.PLATFORM_INFO.name
|
|
|
|
|
2022-11-10 18:38:26 +01:00
|
|
|
export function addTenantToUrl(url: string) {
|
2021-08-05 10:59:08 +02:00
|
|
|
const tenantId = getTenantId()
|
|
|
|
|
|
|
|
if (isMultiTenant()) {
|
|
|
|
const char = url.indexOf("?") === -1 ? "?" : "&"
|
|
|
|
url += `${char}tenantId=${tenantId}`
|
|
|
|
}
|
|
|
|
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2022-11-10 18:38:26 +01:00
|
|
|
export async function doesTenantExist(tenantId: string) {
|
2022-06-13 11:51:29 +02:00
|
|
|
return doWithDB(PLATFORM_INFO_DB, async (db: any) => {
|
2022-04-19 20:42:52 +02:00
|
|
|
let tenants
|
|
|
|
try {
|
|
|
|
tenants = await db.get(TENANT_DOC)
|
|
|
|
} catch (err) {
|
|
|
|
// if theres an error the doc doesn't exist, no tenants exist
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
tenants &&
|
|
|
|
Array.isArray(tenants.tenantIds) &&
|
|
|
|
tenants.tenantIds.indexOf(tenantId) !== -1
|
|
|
|
)
|
|
|
|
})
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 18:38:26 +01:00
|
|
|
export async function tryAddTenant(
|
2022-06-13 11:51:29 +02:00
|
|
|
tenantId: string,
|
|
|
|
userId: string,
|
|
|
|
email: string,
|
|
|
|
afterCreateTenant: () => Promise<void>
|
2022-11-10 18:38:26 +01:00
|
|
|
) {
|
2022-06-13 11:51:29 +02:00
|
|
|
return doWithDB(PLATFORM_INFO_DB, async (db: any) => {
|
|
|
|
const getDoc = async (id: string) => {
|
2022-04-19 20:42:52 +02:00
|
|
|
if (!id) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return await db.get(id)
|
|
|
|
} catch (err) {
|
|
|
|
return { _id: id }
|
|
|
|
}
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
2022-04-19 20:42:52 +02:00
|
|
|
let [tenants, userIdDoc, emailDoc] = await Promise.all([
|
|
|
|
getDoc(TENANT_DOC),
|
|
|
|
getDoc(userId),
|
|
|
|
getDoc(email),
|
|
|
|
])
|
|
|
|
if (!Array.isArray(tenants.tenantIds)) {
|
|
|
|
tenants = {
|
|
|
|
_id: TENANT_DOC,
|
|
|
|
tenantIds: [],
|
|
|
|
}
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
2022-04-19 20:42:52 +02:00
|
|
|
let promises = []
|
|
|
|
if (userIdDoc) {
|
|
|
|
userIdDoc.tenantId = tenantId
|
|
|
|
promises.push(db.put(userIdDoc))
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
2022-04-19 20:42:52 +02:00
|
|
|
if (emailDoc) {
|
|
|
|
emailDoc.tenantId = tenantId
|
|
|
|
emailDoc.userId = userId
|
|
|
|
promises.push(db.put(emailDoc))
|
|
|
|
}
|
|
|
|
if (tenants.tenantIds.indexOf(tenantId) === -1) {
|
|
|
|
tenants.tenantIds.push(tenantId)
|
|
|
|
promises.push(db.put(tenants))
|
2022-06-13 11:51:29 +02:00
|
|
|
await afterCreateTenant()
|
2022-04-19 20:42:52 +02:00
|
|
|
}
|
|
|
|
await Promise.all(promises)
|
|
|
|
})
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 18:38:26 +01:00
|
|
|
export function doWithGlobalDB(tenantId: string, cb: any) {
|
2022-06-13 11:51:29 +02:00
|
|
|
return doWithDB(getGlobalDBName(tenantId), cb)
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 18:38:26 +01:00
|
|
|
export async function lookupTenantId(userId: string) {
|
2022-06-13 11:51:29 +02:00
|
|
|
return doWithDB(StaticDatabases.PLATFORM_INFO.name, async (db: any) => {
|
2022-04-19 20:42:52 +02:00
|
|
|
let tenantId = env.MULTI_TENANCY ? DEFAULT_TENANT_ID : null
|
|
|
|
try {
|
|
|
|
const doc = await db.get(userId)
|
|
|
|
if (doc && doc.tenantId) {
|
|
|
|
tenantId = doc.tenantId
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// just return the default
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
2022-04-19 20:42:52 +02:00
|
|
|
return tenantId
|
|
|
|
})
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
2021-10-06 16:15:46 +02:00
|
|
|
|
|
|
|
// lookup, could be email or userId, either will return a doc
|
2022-11-10 18:38:26 +01:00
|
|
|
export async function getTenantUser(
|
2022-09-14 15:22:53 +02:00
|
|
|
identifier: string
|
2022-11-10 18:38:26 +01:00
|
|
|
): Promise<PlatformUser | undefined> {
|
2022-09-02 10:12:03 +02:00
|
|
|
// use the view here and allow to find anyone regardless of casing
|
2022-11-10 18:38:26 +01:00
|
|
|
// Use lowercase to ensure email login is case-insensitive
|
|
|
|
const users = await queryPlatformView<PlatformUser>(
|
|
|
|
ViewName.PLATFORM_USERS_LOWERCASE,
|
|
|
|
{
|
|
|
|
keys: [identifier.toLowerCase()],
|
|
|
|
include_docs: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if (Array.isArray(users)) {
|
|
|
|
return users[0]
|
|
|
|
} else {
|
|
|
|
return users
|
|
|
|
}
|
2021-10-06 16:15:46 +02:00
|
|
|
}
|
2021-10-07 16:49:26 +02:00
|
|
|
|
2022-11-10 18:38:26 +01:00
|
|
|
export function isUserInAppTenant(appId: string, user?: any) {
|
2021-10-07 16:49:26 +02:00
|
|
|
let userTenantId
|
|
|
|
if (user) {
|
|
|
|
userTenantId = user.tenantId || DEFAULT_TENANT_ID
|
|
|
|
} else {
|
|
|
|
userTenantId = getTenantId()
|
|
|
|
}
|
2022-03-24 14:04:49 +01:00
|
|
|
const tenantId = getTenantIDFromAppID(appId) || DEFAULT_TENANT_ID
|
2021-10-07 16:49:26 +02:00
|
|
|
return tenantId === userTenantId
|
|
|
|
}
|
2022-01-24 11:48:59 +01:00
|
|
|
|
2022-11-10 18:38:26 +01:00
|
|
|
export async function getTenantIds() {
|
2022-06-13 11:51:29 +02:00
|
|
|
return doWithDB(PLATFORM_INFO_DB, async (db: any) => {
|
2022-04-19 20:42:52 +02:00
|
|
|
let tenants
|
|
|
|
try {
|
|
|
|
tenants = await db.get(TENANT_DOC)
|
|
|
|
} catch (err) {
|
|
|
|
// if theres an error the doc doesn't exist, no tenants exist
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
return (tenants && tenants.tenantIds) || []
|
|
|
|
})
|
2022-01-24 11:48:59 +01:00
|
|
|
}
|
2022-11-09 17:35:16 +01:00
|
|
|
|
|
|
|
const ALL_STRATEGIES = Object.values(TenantResolutionStrategy)
|
|
|
|
|
|
|
|
export const getTenantIDFromCtx = (
|
|
|
|
ctx: BBContext,
|
|
|
|
opts: GetTenantIdOptions
|
|
|
|
): string | null => {
|
|
|
|
// exit early if not multi-tenant
|
|
|
|
if (!isMultiTenant()) {
|
|
|
|
return DEFAULT_TENANT_ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// opt defaults
|
|
|
|
if (opts.allowNoTenant === undefined) {
|
|
|
|
opts.allowNoTenant = false
|
|
|
|
}
|
|
|
|
if (!opts.includeStrategies) {
|
|
|
|
opts.includeStrategies = ALL_STRATEGIES
|
|
|
|
}
|
|
|
|
if (!opts.excludeStrategies) {
|
|
|
|
opts.excludeStrategies = []
|
|
|
|
}
|
|
|
|
|
|
|
|
const isAllowed = (strategy: TenantResolutionStrategy) => {
|
|
|
|
// excluded takes precedence
|
|
|
|
if (opts.excludeStrategies?.includes(strategy)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (opts.includeStrategies?.includes(strategy)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// always use user first
|
|
|
|
if (isAllowed(TenantResolutionStrategy.USER)) {
|
|
|
|
const userTenantId = ctx.user?.tenantId
|
|
|
|
if (userTenantId) {
|
|
|
|
return userTenantId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// header
|
|
|
|
if (isAllowed(TenantResolutionStrategy.HEADER)) {
|
2022-11-16 19:28:45 +01:00
|
|
|
const headerTenantId = ctx.request.headers[Header.TENANT_ID]
|
2022-11-09 17:35:16 +01:00
|
|
|
if (headerTenantId) {
|
|
|
|
return headerTenantId as string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// query param
|
|
|
|
if (isAllowed(TenantResolutionStrategy.QUERY)) {
|
|
|
|
const queryTenantId = ctx.request.query.tenantId
|
|
|
|
if (queryTenantId) {
|
|
|
|
return queryTenantId as string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// subdomain
|
|
|
|
if (isAllowed(TenantResolutionStrategy.SUBDOMAIN)) {
|
|
|
|
// e.g. budibase.app or local.com:10000
|
|
|
|
const platformHost = new URL(env.PLATFORM_URL).host.split(":")[0]
|
|
|
|
// e.g. tenant.budibase.app or tenant.local.com
|
|
|
|
const requestHost = ctx.host
|
|
|
|
// parse the tenant id from the difference
|
2022-11-11 12:10:07 +01:00
|
|
|
if (requestHost.includes(platformHost)) {
|
|
|
|
const tenantId = requestHost.substring(
|
|
|
|
0,
|
|
|
|
requestHost.indexOf(`.${platformHost}`)
|
|
|
|
)
|
|
|
|
if (tenantId) {
|
|
|
|
return tenantId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 16:43:41 +01:00
|
|
|
// path
|
2022-11-11 12:10:07 +01:00
|
|
|
if (isAllowed(TenantResolutionStrategy.PATH)) {
|
|
|
|
// params - have to parse manually due to koa-router not run yet
|
|
|
|
const match = ctx.matched.find(
|
|
|
|
(m: any) => !!m.paramNames.find((p: any) => p.name === "tenantId")
|
2022-11-09 17:35:16 +01:00
|
|
|
)
|
2022-11-16 12:34:16 +01:00
|
|
|
|
|
|
|
// get the raw path url - without any query params
|
|
|
|
const ctxUrl = ctx.originalUrl
|
|
|
|
let url
|
|
|
|
if (ctxUrl.includes("?")) {
|
|
|
|
url = ctxUrl.split("?")[0]
|
|
|
|
} else {
|
|
|
|
url = ctxUrl
|
|
|
|
}
|
|
|
|
|
2022-11-11 12:10:07 +01:00
|
|
|
if (match) {
|
2022-11-16 12:34:16 +01:00
|
|
|
const params = match.params(url, match.captures(url), {})
|
2022-11-11 12:10:07 +01:00
|
|
|
if (params.tenantId) {
|
|
|
|
return params.tenantId
|
|
|
|
}
|
2022-11-09 17:35:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.allowNoTenant) {
|
|
|
|
ctx.throw(403, "Tenant id not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|