2022-07-13 14:22:21 +02:00
|
|
|
import env from "../environment"
|
|
|
|
import { SEPARATOR, DocumentTypes } from "../db/constants"
|
|
|
|
import cls from "./FunctionContext"
|
|
|
|
import { dangerousGetDB, closeDB } from "../db"
|
|
|
|
import { baseGlobalDBName } from "../tenancy/utils"
|
|
|
|
import { IdentityContext } from "@budibase/types"
|
|
|
|
import { DEFAULT_TENANT_ID as _DEFAULT_TENANT_ID } from "../constants"
|
2022-07-14 17:02:05 +02:00
|
|
|
import { ContextKeys } from "./constants"
|
|
|
|
import {
|
|
|
|
updateUsing,
|
|
|
|
closeWithUsing,
|
|
|
|
setAppTenantId,
|
|
|
|
setIdentity,
|
|
|
|
closeAppDBs,
|
|
|
|
getContextDB,
|
|
|
|
} from "./utils"
|
2022-07-13 14:22:21 +02:00
|
|
|
|
|
|
|
export const DEFAULT_TENANT_ID = _DEFAULT_TENANT_ID
|
2022-01-27 19:18:31 +01:00
|
|
|
|
|
|
|
// some test cases call functions directly, need to
|
|
|
|
// store an app ID to pretend there is a context
|
2022-07-13 14:22:21 +02:00
|
|
|
let TEST_APP_ID: string | null = null
|
2022-01-27 19:18:31 +01:00
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
export const closeTenancy = async () => {
|
2022-07-14 17:02:05 +02:00
|
|
|
let db
|
|
|
|
try {
|
|
|
|
if (env.USE_COUCH) {
|
|
|
|
db = getGlobalDB()
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// no DB found - skip closing
|
|
|
|
return
|
2022-05-10 17:37:24 +02:00
|
|
|
}
|
2022-07-14 17:02:05 +02:00
|
|
|
await closeDB(db)
|
2022-05-10 17:37:24 +02:00
|
|
|
// clear from context now that database is closed/task is finished
|
|
|
|
cls.setOnContext(ContextKeys.TENANT_ID, null)
|
|
|
|
cls.setOnContext(ContextKeys.GLOBAL_DB, null)
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
// export const isDefaultTenant = () => {
|
|
|
|
// return getTenantId() === DEFAULT_TENANT_ID
|
|
|
|
// }
|
2021-08-05 10:59:08 +02:00
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
export const isMultiTenant = () => {
|
2021-08-05 10:59:08 +02:00
|
|
|
return env.MULTI_TENANCY
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
/**
|
|
|
|
* Given an app ID this will attempt to retrieve the tenant ID from it.
|
|
|
|
* @return {null|string} The tenant ID found within the app ID.
|
|
|
|
*/
|
|
|
|
export const getTenantIDFromAppID = (appId: string) => {
|
|
|
|
if (!appId) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
const split = appId.split(SEPARATOR)
|
|
|
|
const hasDev = split[1] === DocumentTypes.DEV
|
|
|
|
if ((hasDev && split.length === 3) || (!hasDev && split.length === 2)) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
if (hasDev) {
|
|
|
|
return split[2]
|
|
|
|
} else {
|
|
|
|
return split[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 10:59:08 +02:00
|
|
|
// used for automations, API endpoints should always be in context already
|
2022-07-13 14:22:21 +02:00
|
|
|
export const doInTenant = (tenantId: string | null, task: any) => {
|
2022-07-18 19:18:01 +02:00
|
|
|
// make sure default always selected in single tenancy
|
|
|
|
if (!env.MULTI_TENANCY) {
|
|
|
|
tenantId = tenantId || DEFAULT_TENANT_ID
|
|
|
|
}
|
2022-04-19 20:42:52 +02:00
|
|
|
// the internal function is so that we can re-use an existing
|
|
|
|
// context - don't want to close DB on a parent context
|
2022-05-24 21:01:13 +02:00
|
|
|
async function internal(opts = { existing: false }) {
|
2022-07-13 14:22:21 +02:00
|
|
|
// set the tenant id + global db if this is a new context
|
2022-04-19 20:42:52 +02:00
|
|
|
if (!opts.existing) {
|
2022-07-13 14:22:21 +02:00
|
|
|
updateTenantId(tenantId)
|
2022-04-19 20:42:52 +02:00
|
|
|
}
|
2021-08-05 10:59:08 +02:00
|
|
|
|
2022-04-20 18:33:42 +02:00
|
|
|
try {
|
|
|
|
// invoke the task
|
|
|
|
return await task()
|
|
|
|
} finally {
|
2022-07-14 17:02:05 +02:00
|
|
|
await closeWithUsing(ContextKeys.TENANCY_IN_USE, () => {
|
|
|
|
return closeTenancy()
|
|
|
|
})
|
2022-04-19 20:42:52 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-24 10:54:36 +02:00
|
|
|
|
2022-07-14 17:02:05 +02:00
|
|
|
const existing = cls.getFromContext(ContextKeys.TENANT_ID) === tenantId
|
|
|
|
return updateUsing(ContextKeys.TENANCY_IN_USE, existing, internal)
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
export const doInAppContext = (appId: string, task: any) => {
|
2022-03-24 14:04:49 +01:00
|
|
|
if (!appId) {
|
|
|
|
throw new Error("appId is required")
|
|
|
|
}
|
2022-04-21 00:10:39 +02:00
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
const identity = getIdentity()
|
2022-05-24 21:01:13 +02:00
|
|
|
|
2022-04-19 20:42:52 +02:00
|
|
|
// the internal function is so that we can re-use an existing
|
|
|
|
// context - don't want to close DB on a parent context
|
2022-05-28 22:38:22 +02:00
|
|
|
async function internal(opts = { existing: false }) {
|
2022-03-24 14:04:49 +01:00
|
|
|
// set the app tenant id
|
2022-04-19 20:42:52 +02:00
|
|
|
if (!opts.existing) {
|
|
|
|
setAppTenantId(appId)
|
|
|
|
}
|
2022-01-28 01:05:39 +01:00
|
|
|
// set the app ID
|
|
|
|
cls.setOnContext(ContextKeys.APP_ID, appId)
|
2022-07-13 14:22:21 +02:00
|
|
|
|
2022-05-28 22:38:22 +02:00
|
|
|
// preserve the identity
|
2022-07-13 14:22:21 +02:00
|
|
|
if (identity) {
|
|
|
|
setIdentity(identity)
|
|
|
|
}
|
2022-04-20 18:33:42 +02:00
|
|
|
try {
|
|
|
|
// invoke the task
|
|
|
|
return await task()
|
|
|
|
} finally {
|
2022-07-14 17:02:05 +02:00
|
|
|
await closeWithUsing(ContextKeys.APP_IN_USE, async () => {
|
2022-04-20 18:33:42 +02:00
|
|
|
await closeAppDBs()
|
2022-07-13 14:22:21 +02:00
|
|
|
await closeTenancy()
|
2022-07-14 17:02:05 +02:00
|
|
|
})
|
2022-04-19 20:42:52 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-14 17:02:05 +02:00
|
|
|
const existing = cls.getFromContext(ContextKeys.APP_ID) === appId
|
|
|
|
return updateUsing(ContextKeys.APP_IN_USE, existing, internal)
|
2022-01-28 01:05:39 +01:00
|
|
|
}
|
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
export const doInIdentityContext = (identity: IdentityContext, task: any) => {
|
2022-05-28 22:38:22 +02:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error("identity is required")
|
2022-05-24 21:01:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function internal(opts = { existing: false }) {
|
|
|
|
if (!opts.existing) {
|
2022-05-28 22:38:22 +02:00
|
|
|
cls.setOnContext(ContextKeys.IDENTITY, identity)
|
|
|
|
// set the tenant so that doInTenant will preserve identity
|
|
|
|
if (identity.tenantId) {
|
2022-07-13 14:22:21 +02:00
|
|
|
updateTenantId(identity.tenantId)
|
2022-05-24 21:01:13 +02:00
|
|
|
}
|
2022-05-24 10:54:36 +02:00
|
|
|
}
|
2022-05-24 21:01:13 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
// invoke the task
|
|
|
|
return await task()
|
|
|
|
} finally {
|
2022-07-14 17:02:05 +02:00
|
|
|
await closeWithUsing(ContextKeys.IDENTITY_IN_USE, async () => {
|
2022-07-13 14:22:21 +02:00
|
|
|
setIdentity(null)
|
|
|
|
await closeTenancy()
|
2022-07-14 17:02:05 +02:00
|
|
|
})
|
2022-05-24 21:01:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 22:38:22 +02:00
|
|
|
const existing = cls.getFromContext(ContextKeys.IDENTITY)
|
2022-07-14 17:02:05 +02:00
|
|
|
return updateUsing(ContextKeys.IDENTITY_IN_USE, existing, internal)
|
2022-05-24 10:54:36 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
export const getIdentity = (): IdentityContext | undefined => {
|
2022-05-23 23:14:44 +02:00
|
|
|
try {
|
2022-05-28 22:38:22 +02:00
|
|
|
return cls.getFromContext(ContextKeys.IDENTITY)
|
2022-05-23 23:14:44 +02:00
|
|
|
} catch (e) {
|
2022-05-28 22:38:22 +02:00
|
|
|
// do nothing - identity is not in context
|
2022-05-23 23:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
export const updateTenantId = (tenantId: string | null) => {
|
2022-01-27 19:18:31 +01:00
|
|
|
cls.setOnContext(ContextKeys.TENANT_ID, tenantId)
|
2022-05-24 21:01:13 +02:00
|
|
|
if (env.USE_COUCH) {
|
2022-07-13 14:22:21 +02:00
|
|
|
setGlobalDB(tenantId)
|
2022-05-24 21:01:13 +02:00
|
|
|
}
|
2022-01-27 19:18:31 +01:00
|
|
|
}
|
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
export const updateAppId = async (appId: string) => {
|
2022-01-27 19:18:31 +01:00
|
|
|
try {
|
2022-04-20 18:33:42 +02:00
|
|
|
// have to close first, before removing the databases from context
|
2022-04-21 15:56:14 +02:00
|
|
|
await closeAppDBs()
|
2022-01-27 19:18:31 +01:00
|
|
|
cls.setOnContext(ContextKeys.APP_ID, appId)
|
|
|
|
} catch (err) {
|
|
|
|
if (env.isTest()) {
|
|
|
|
TEST_APP_ID = appId
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
export const setGlobalDB = (tenantId: string | null) => {
|
2022-04-19 20:42:52 +02:00
|
|
|
const dbName = baseGlobalDBName(tenantId)
|
|
|
|
const db = dangerousGetDB(dbName)
|
|
|
|
cls.setOnContext(ContextKeys.GLOBAL_DB, db)
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
export const getGlobalDB = () => {
|
2022-04-19 20:42:52 +02:00
|
|
|
const db = cls.getFromContext(ContextKeys.GLOBAL_DB)
|
|
|
|
if (!db) {
|
|
|
|
throw new Error("Global DB not found")
|
|
|
|
}
|
|
|
|
return db
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
export const isTenantIdSet = () => {
|
2022-01-27 19:18:31 +01:00
|
|
|
const tenantId = cls.getFromContext(ContextKeys.TENANT_ID)
|
2021-08-05 10:59:08 +02:00
|
|
|
return !!tenantId
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
export const getTenantId = () => {
|
|
|
|
if (!isMultiTenant()) {
|
|
|
|
return DEFAULT_TENANT_ID
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
2022-01-27 19:18:31 +01:00
|
|
|
const tenantId = cls.getFromContext(ContextKeys.TENANT_ID)
|
2021-08-05 10:59:08 +02:00
|
|
|
if (!tenantId) {
|
2022-02-18 12:18:59 +01:00
|
|
|
throw new Error("Tenant id not found")
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
|
|
|
return tenantId
|
|
|
|
}
|
2022-01-27 19:18:31 +01:00
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
export const getAppId = () => {
|
2022-01-27 19:18:31 +01:00
|
|
|
const foundId = cls.getFromContext(ContextKeys.APP_ID)
|
|
|
|
if (!foundId && env.isTest() && TEST_APP_ID) {
|
|
|
|
return TEST_APP_ID
|
|
|
|
} else {
|
|
|
|
return foundId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens the app database based on whatever the request
|
|
|
|
* contained, dev or prod.
|
|
|
|
*/
|
2022-07-13 14:22:21 +02:00
|
|
|
export const getAppDB = (opts?: any) => {
|
2022-03-29 17:03:44 +02:00
|
|
|
return getContextDB(ContextKeys.CURRENT_DB, opts)
|
2022-01-27 19:18:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This specifically gets the prod app ID, if the request
|
|
|
|
* contained a development app ID, this will open the prod one.
|
|
|
|
*/
|
2022-07-13 14:22:21 +02:00
|
|
|
export const getProdAppDB = (opts?: any) => {
|
2022-03-29 17:03:44 +02:00
|
|
|
return getContextDB(ContextKeys.PROD_DB, opts)
|
2022-01-27 19:18:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This specifically gets the dev app ID, if the request
|
|
|
|
* contained a prod app ID, this will open the dev one.
|
|
|
|
*/
|
2022-07-13 14:22:21 +02:00
|
|
|
export const getDevAppDB = (opts?: any) => {
|
2022-03-29 17:03:44 +02:00
|
|
|
return getContextDB(ContextKeys.DEV_DB, opts)
|
2022-01-27 19:18:31 +01:00
|
|
|
}
|