2022-05-23 23:14:44 +02:00
|
|
|
import * as context from "../context"
|
|
|
|
import env from "../environment"
|
|
|
|
import {
|
|
|
|
Hosting,
|
|
|
|
User,
|
|
|
|
SessionUser,
|
|
|
|
Identity,
|
|
|
|
IdentityType,
|
|
|
|
Account,
|
|
|
|
BudibaseIdentity,
|
2022-05-24 10:54:36 +02:00
|
|
|
isCloudAccount,
|
|
|
|
isSSOAccount,
|
2022-05-24 21:01:13 +02:00
|
|
|
TenantIdentity,
|
|
|
|
SettingsConfig,
|
2022-05-26 11:13:26 +02:00
|
|
|
CloudAccount,
|
|
|
|
UserIdentity,
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-25 22:32:08 +02:00
|
|
|
export const identifyTenant = async (
|
|
|
|
tenantId: string,
|
2022-05-26 11:13:26 +02:00
|
|
|
account: CloudAccount | undefined,
|
2022-05-25 22:32:08 +02:00
|
|
|
timestamp?: string | number
|
|
|
|
) => {
|
2022-05-24 21:01:13 +02:00
|
|
|
const global = await getGlobalIdentifiers(tenantId)
|
2022-05-26 11:13:26 +02:00
|
|
|
const id = global.id
|
|
|
|
const hosting = getHostingFromEnv()
|
|
|
|
const type = IdentityType.TENANT
|
|
|
|
const profession = account?.profession
|
|
|
|
const companySize = account?.size
|
2022-05-24 21:01:13 +02:00
|
|
|
|
|
|
|
const identity: TenantIdentity = {
|
2022-05-26 11:13:26 +02:00
|
|
|
id,
|
2022-05-24 21:01:13 +02:00
|
|
|
tenantId: global.tenantId,
|
2022-05-26 11:13:26 +02:00
|
|
|
hosting,
|
|
|
|
type,
|
|
|
|
profession,
|
|
|
|
companySize,
|
2022-05-24 21:01:13 +02:00
|
|
|
}
|
2022-05-25 22:32:08 +02:00
|
|
|
await identify(identity, timestamp)
|
2022-05-24 21:01:13 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 11:13:26 +02:00
|
|
|
export const identifyUser = async (
|
|
|
|
user: User,
|
|
|
|
account: CloudAccount | undefined,
|
|
|
|
timestamp?: string | number
|
|
|
|
) => {
|
2022-05-23 23:14:44 +02:00
|
|
|
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-26 11:13:26 +02:00
|
|
|
const accountHolder = account?.budibaseUserId === user._id
|
|
|
|
const verified = account ? account.verified : false
|
|
|
|
const profession = account?.profession
|
|
|
|
const companySize = account?.size
|
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-26 11:13:26 +02:00
|
|
|
accountHolder,
|
|
|
|
verified,
|
|
|
|
profession,
|
|
|
|
companySize,
|
2022-05-23 23:14:44 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 22:32:08 +02:00
|
|
|
await identify(identity, timestamp)
|
2022-05-23 23:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-26 11:13:26 +02:00
|
|
|
const verified = account.verified
|
|
|
|
const profession = account.profession
|
|
|
|
const companySize = account.size
|
|
|
|
const accountHolder = true
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 11:13:26 +02:00
|
|
|
const identity: UserIdentity = {
|
2022-05-23 23:14:44 +02:00
|
|
|
id,
|
|
|
|
tenantId,
|
|
|
|
hosting,
|
|
|
|
type,
|
2022-05-24 21:01:13 +02:00
|
|
|
providerType,
|
2022-05-26 11:13:26 +02:00
|
|
|
verified,
|
|
|
|
profession,
|
|
|
|
companySize,
|
|
|
|
accountHolder,
|
2022-05-23 23:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await identify(identity)
|
|
|
|
}
|
|
|
|
|
2022-05-25 22:32:08 +02:00
|
|
|
const identify = async (identity: Identity, timestamp?: string | number) => {
|
|
|
|
await analyticsProcessor.identify(identity, timestamp)
|
2022-05-23 23:14:44 +02:00
|
|
|
}
|