budibase/packages/backend-core/src/events/identification.ts

202 lines
4.6 KiB
TypeScript
Raw Normal View History

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,
CloudAccount,
UserIdentity,
InstallationIdentity,
Installation,
isInstallation,
2022-05-23 23:14:44 +02:00
} from "@budibase/types"
import { processors } 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
const pkg = require("../../package.json")
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()
const tenantId = await getGlobalTenantId(context.getTenantId())
2022-05-23 23:14:44 +02:00
let id: string
let type: IdentityType
2022-05-23 23:14:44 +02:00
if (user) {
id = user._id
type = IdentityType.USER
2022-05-23 23:14:44 +02:00
} else {
id = tenantId
type = IdentityType.TENANT
2022-05-23 23:14:44 +02:00
}
if (user && isInstallation(user)) {
type = IdentityType.INSTALLATION
}
2022-05-23 23:14:44 +02:00
return {
id,
tenantId,
type,
2022-05-23 23:14:44 +02:00
}
}
const getGlobalId = async (tenantId: string): Promise<string> => {
2022-05-24 21:01:13 +02:00
const db = context.getGlobalDB()
const config: SettingsConfig = await dbUtils.getScopedFullConfig(db, {
type: Configs.SETTINGS,
})
let globalId: string
2022-05-24 21:01:13 +02:00
if (config.config.globalId) {
return config.config.globalId
} else {
globalId = `${hashing.newid()}_${tenantId}`
2022-05-24 21:01:13 +02:00
config.config.globalId = globalId
await db.put(config)
return globalId
}
}
const getGlobalTenantId = async (tenantId: string): Promise<string> => {
2022-05-24 21:01:13 +02:00
if (env.SELF_HOSTED) {
return getGlobalId(tenantId)
2022-05-24 21:01:13 +02:00
} else {
// tenant id's in the cloud are already unique
return tenantId
2022-05-24 21:01:13 +02:00
}
}
const getHostingFromEnv = () => {
return env.SELF_HOSTED ? Hosting.SELF : Hosting.CLOUD
}
export const identifyInstallation = async (
install: Installation,
timestamp: string | number
) => {
const id = install.installId
// the default tenant id, so we can match installations to other events
const tenantId = await getGlobalTenantId(context.getTenantId())
const version: string = pkg.version as string
const type = IdentityType.INSTALLATION
const hosting = getHostingFromEnv()
const identity: InstallationIdentity = {
id,
tenantId,
type,
version,
hosting,
}
await identify(identity, timestamp)
}
2022-05-25 22:32:08 +02:00
export const identifyTenant = async (
tenantId: string,
account: CloudAccount | undefined,
2022-05-25 22:32:08 +02:00
timestamp?: string | number
) => {
const globalTenantId = await getGlobalTenantId(tenantId)
const id = globalTenantId
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 = {
id,
tenantId: globalTenantId,
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
}
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
const accountHolder = account?.budibaseUserId === user._id
const verified =
account && account?.budibaseUserId === user._id ? 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,
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
let type = IdentityType.USER
2022-05-24 21:01:13 +02:00
let providerType = isSSOAccount(account) ? account.providerType : undefined
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
}
}
const identity: UserIdentity = {
2022-05-23 23:14:44 +02:00
id,
tenantId,
hosting,
type,
2022-05-24 21:01:13 +02:00
providerType,
verified,
profession,
companySize,
accountHolder,
2022-05-23 23:14:44 +02:00
}
await identify(identity)
}
export const identify = async (
identity: Identity,
timestamp?: string | number
) => {
await processors.identify(identity, timestamp)
2022-05-23 23:14:44 +02:00
}