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-27 00:57:14 +02:00
|
|
|
InstallationIdentity,
|
|
|
|
Installation,
|
|
|
|
isInstallation,
|
2022-05-23 23:14:44 +02:00
|
|
|
} from "@budibase/types"
|
2022-05-27 00:57:14 +02:00
|
|
|
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
|
|
|
|
2022-05-27 00:57:14 +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()
|
2022-05-27 00:57:14 +02:00
|
|
|
|
|
|
|
const tenantId = await getGlobalTenantId(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-27 00:57:14 +02:00
|
|
|
id = tenantId
|
2022-05-25 01:15:52 +02:00
|
|
|
type = IdentityType.TENANT
|
2022-05-23 23:14:44 +02:00
|
|
|
}
|
|
|
|
|
2022-05-27 00:57:14 +02:00
|
|
|
if (user && isInstallation(user)) {
|
|
|
|
type = IdentityType.INSTALLATION
|
|
|
|
}
|
|
|
|
|
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-27 00:57:14 +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,
|
|
|
|
})
|
2022-05-27 00:57:14 +02:00
|
|
|
|
|
|
|
let globalId: string
|
2022-05-24 21:01:13 +02:00
|
|
|
if (config.config.globalId) {
|
|
|
|
return config.config.globalId
|
|
|
|
} else {
|
2022-05-27 00:57:14 +02:00
|
|
|
globalId = `${hashing.newid()}_${tenantId}`
|
2022-05-24 21:01:13 +02:00
|
|
|
config.config.globalId = globalId
|
|
|
|
await db.put(config)
|
|
|
|
return globalId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 00:57:14 +02:00
|
|
|
const getGlobalTenantId = async (tenantId: string): Promise<string> => {
|
2022-05-24 21:01:13 +02:00
|
|
|
if (env.SELF_HOSTED) {
|
2022-05-27 00:57:14 +02:00
|
|
|
return getGlobalId(tenantId)
|
2022-05-24 21:01:13 +02:00
|
|
|
} else {
|
|
|
|
// tenant id's in the cloud are already unique
|
2022-05-27 00:57:14 +02:00
|
|
|
return tenantId
|
2022-05-24 21:01:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getHostingFromEnv = () => {
|
|
|
|
return env.SELF_HOSTED ? Hosting.SELF : Hosting.CLOUD
|
|
|
|
}
|
|
|
|
|
2022-05-27 00:57:14 +02:00
|
|
|
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,
|
2022-05-26 11:13:26 +02:00
|
|
|
account: CloudAccount | undefined,
|
2022-05-25 22:32:08 +02:00
|
|
|
timestamp?: string | number
|
|
|
|
) => {
|
2022-05-27 00:57:14 +02:00
|
|
|
const globalTenantId = await getGlobalTenantId(tenantId)
|
|
|
|
const id = globalTenantId
|
2022-05-26 11:13:26 +02:00
|
|
|
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-27 00:57:14 +02:00
|
|
|
tenantId: globalTenantId,
|
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
|
2022-05-27 00:57:14 +02:00
|
|
|
const verified =
|
|
|
|
account && account?.budibaseUserId === user._id ? account.verified : false
|
2022-05-26 11:13:26 +02:00
|
|
|
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-27 00:57:14 +02:00
|
|
|
export const identify = async (
|
|
|
|
identity: Identity,
|
|
|
|
timestamp?: string | number
|
|
|
|
) => {
|
|
|
|
await processors.identify(identity, timestamp)
|
2022-05-23 23:14:44 +02:00
|
|
|
}
|