2022-05-23 23:14:44 +02:00
|
|
|
import * as context from "../context"
|
|
|
|
import env from "../environment"
|
|
|
|
import {
|
|
|
|
Hosting,
|
|
|
|
User,
|
|
|
|
SessionUser,
|
|
|
|
Identity,
|
|
|
|
IdentityType,
|
|
|
|
Account,
|
|
|
|
AccountIdentity,
|
|
|
|
BudibaseIdentity,
|
2022-05-24 10:54:36 +02:00
|
|
|
isCloudAccount,
|
|
|
|
isSSOAccount,
|
2022-05-24 21:01:13 +02:00
|
|
|
TenantIdentity,
|
|
|
|
SettingsConfig,
|
2022-05-23 23:14:44 +02:00
|
|
|
} from "@budibase/types"
|
|
|
|
import { analyticsProcessor } from "./processors"
|
2022-05-24 21:01:13 +02:00
|
|
|
import * as dbUtils from "../db/utils"
|
|
|
|
import { Configs } from "../constants"
|
|
|
|
import * as hashing from "../hashing"
|
2022-05-23 23:14:44 +02:00
|
|
|
|
2022-05-24 21:01:13 +02:00
|
|
|
export const getCurrentIdentity = async (): Promise<Identity> => {
|
2022-05-23 23:14:44 +02:00
|
|
|
const user: SessionUser | undefined = context.getUser()
|
2022-05-24 21:01:13 +02:00
|
|
|
let tenantId = context.getTenantId()
|
2022-05-23 23:14:44 +02:00
|
|
|
let id: string
|
2022-05-25 01:15:52 +02:00
|
|
|
let type: IdentityType
|
2022-05-23 23:14:44 +02:00
|
|
|
|
|
|
|
if (user) {
|
|
|
|
id = user._id
|
2022-05-25 01:15:52 +02:00
|
|
|
type = IdentityType.USER
|
2022-05-23 23:14:44 +02:00
|
|
|
} else {
|
2022-05-24 21:01:13 +02:00
|
|
|
const global = await getGlobalIdentifiers(tenantId)
|
|
|
|
id = global.id
|
|
|
|
tenantId = global.tenantId
|
2022-05-25 01:15:52 +02:00
|
|
|
type = IdentityType.TENANT
|
2022-05-23 23:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
tenantId,
|
2022-05-25 01:15:52 +02:00
|
|
|
type,
|
2022-05-23 23:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 21:01:13 +02:00
|
|
|
const getGlobalId = async (): Promise<string> => {
|
|
|
|
const db = context.getGlobalDB()
|
|
|
|
const config: SettingsConfig = await dbUtils.getScopedFullConfig(db, {
|
|
|
|
type: Configs.SETTINGS,
|
|
|
|
})
|
|
|
|
if (config.config.globalId) {
|
|
|
|
return config.config.globalId
|
|
|
|
} else {
|
|
|
|
const globalId = `global_${hashing.newid()}`
|
|
|
|
config.config.globalId = globalId
|
|
|
|
await db.put(config)
|
|
|
|
return globalId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getGlobalIdentifiers = async (
|
|
|
|
tenantId: string
|
|
|
|
): Promise<{ id: string; tenantId: string }> => {
|
|
|
|
if (env.SELF_HOSTED) {
|
|
|
|
const globalId = await getGlobalId()
|
|
|
|
return {
|
|
|
|
id: globalId,
|
|
|
|
tenantId: `${globalId}-${tenantId}`,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// tenant id's in the cloud are already unique
|
|
|
|
return {
|
|
|
|
id: tenantId,
|
|
|
|
tenantId: tenantId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getHostingFromEnv = () => {
|
|
|
|
return env.SELF_HOSTED ? Hosting.SELF : Hosting.CLOUD
|
|
|
|
}
|
|
|
|
|
|
|
|
export const identifyTenant = async (tenantId: string) => {
|
|
|
|
const global = await getGlobalIdentifiers(tenantId)
|
|
|
|
|
|
|
|
const identity: TenantIdentity = {
|
|
|
|
id: global.id,
|
|
|
|
tenantId: global.tenantId,
|
|
|
|
hosting: getHostingFromEnv(),
|
|
|
|
type: IdentityType.TENANT,
|
|
|
|
}
|
|
|
|
await identify(identity)
|
|
|
|
}
|
|
|
|
|
2022-05-23 23:14:44 +02:00
|
|
|
export const identifyUser = async (user: User) => {
|
|
|
|
const id = user._id as string
|
|
|
|
const tenantId = user.tenantId
|
|
|
|
const hosting = env.SELF_HOSTED ? Hosting.SELF : Hosting.CLOUD
|
|
|
|
const type = IdentityType.USER
|
|
|
|
let builder = user.builder?.global
|
|
|
|
let admin = user.admin?.global
|
2022-05-24 21:01:13 +02:00
|
|
|
let providerType = user.providerType
|
2022-05-23 23:14:44 +02:00
|
|
|
|
|
|
|
const identity: BudibaseIdentity = {
|
|
|
|
id,
|
|
|
|
tenantId,
|
|
|
|
hosting,
|
|
|
|
type,
|
|
|
|
builder,
|
|
|
|
admin,
|
2022-05-24 21:01:13 +02:00
|
|
|
providerType,
|
2022-05-23 23:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await identify(identity)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const identifyAccount = async (account: Account) => {
|
|
|
|
let id = account.accountId
|
|
|
|
const tenantId = account.tenantId
|
|
|
|
const hosting = account.hosting
|
2022-05-25 01:15:52 +02:00
|
|
|
let type = IdentityType.USER
|
2022-05-24 21:01:13 +02:00
|
|
|
let providerType = isSSOAccount(account) ? account.providerType : undefined
|
2022-05-23 23:14:44 +02:00
|
|
|
|
|
|
|
if (isCloudAccount(account)) {
|
|
|
|
if (account.budibaseUserId) {
|
|
|
|
// use the budibase user as the id if set
|
|
|
|
id = account.budibaseUserId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const identity: AccountIdentity = {
|
|
|
|
id,
|
|
|
|
tenantId,
|
|
|
|
hosting,
|
|
|
|
type,
|
2022-05-24 21:01:13 +02:00
|
|
|
providerType,
|
2022-05-23 23:14:44 +02:00
|
|
|
verified: account.verified,
|
|
|
|
profession: account.profession,
|
|
|
|
companySize: account.size,
|
|
|
|
}
|
|
|
|
|
|
|
|
await identify(identity)
|
|
|
|
}
|
|
|
|
|
|
|
|
const identify = async (identity: Identity) => {
|
|
|
|
await analyticsProcessor.identify(identity)
|
|
|
|
}
|