|
|
@ -14,15 +14,21 @@ import { |
|
|
SettingsConfig, |
|
|
SettingsConfig, |
|
|
CloudAccount, |
|
|
CloudAccount, |
|
|
UserIdentity, |
|
|
UserIdentity, |
|
|
|
|
|
InstallationIdentity, |
|
|
|
|
|
Installation, |
|
|
|
|
|
isInstallation, |
|
|
} from "@budibase/types" |
|
|
} from "@budibase/types" |
|
|
import { analyticsProcessor } from "./processors" |
|
|
import { processors } from "./processors" |
|
|
import * as dbUtils from "../db/utils" |
|
|
import * as dbUtils from "../db/utils" |
|
|
import { Configs } from "../constants" |
|
|
import { Configs } from "../constants" |
|
|
import * as hashing from "../hashing" |
|
|
import * as hashing from "../hashing" |
|
|
|
|
|
|
|
|
|
|
|
const pkg = require("../../package.json") |
|
|
|
|
|
|
|
|
export const getCurrentIdentity = async (): Promise<Identity> => { |
|
|
export const getCurrentIdentity = async (): Promise<Identity> => { |
|
|
const user: SessionUser | undefined = context.getUser() |
|
|
const user: SessionUser | undefined = context.getUser() |
|
|
let tenantId = context.getTenantId() |
|
|
|
|
|
|
|
|
const tenantId = await getGlobalTenantId(context.getTenantId()) |
|
|
let id: string |
|
|
let id: string |
|
|
let type: IdentityType |
|
|
let type: IdentityType |
|
|
|
|
|
|
|
|
@ -30,12 +36,14 @@ export const getCurrentIdentity = async (): Promise<Identity> => { |
|
|
id = user._id |
|
|
id = user._id |
|
|
type = IdentityType.USER |
|
|
type = IdentityType.USER |
|
|
} else { |
|
|
} else { |
|
|
const global = await getGlobalIdentifiers(tenantId) |
|
|
id = tenantId |
|
|
id = global.id |
|
|
|
|
|
tenantId = global.tenantId |
|
|
|
|
|
type = IdentityType.TENANT |
|
|
type = IdentityType.TENANT |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (user && isInstallation(user)) { |
|
|
|
|
|
type = IdentityType.INSTALLATION |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
id, |
|
|
id, |
|
|
tenantId, |
|
|
tenantId, |
|
|
@ -43,36 +51,29 @@ export const getCurrentIdentity = async (): Promise<Identity> => { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const getGlobalId = async (): Promise<string> => { |
|
|
const getGlobalId = async (tenantId: string): Promise<string> => { |
|
|
const db = context.getGlobalDB() |
|
|
const db = context.getGlobalDB() |
|
|
const config: SettingsConfig = await dbUtils.getScopedFullConfig(db, { |
|
|
const config: SettingsConfig = await dbUtils.getScopedFullConfig(db, { |
|
|
type: Configs.SETTINGS, |
|
|
type: Configs.SETTINGS, |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
let globalId: string |
|
|
if (config.config.globalId) { |
|
|
if (config.config.globalId) { |
|
|
return config.config.globalId |
|
|
return config.config.globalId |
|
|
} else { |
|
|
} else { |
|
|
const globalId = `global_${hashing.newid()}` |
|
|
globalId = `${hashing.newid()}_${tenantId}` |
|
|
config.config.globalId = globalId |
|
|
config.config.globalId = globalId |
|
|
await db.put(config) |
|
|
await db.put(config) |
|
|
return globalId |
|
|
return globalId |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const getGlobalIdentifiers = async ( |
|
|
const getGlobalTenantId = async (tenantId: string): Promise<string> => { |
|
|
tenantId: string |
|
|
|
|
|
): Promise<{ id: string; tenantId: string }> => { |
|
|
|
|
|
if (env.SELF_HOSTED) { |
|
|
if (env.SELF_HOSTED) { |
|
|
const globalId = await getGlobalId() |
|
|
return getGlobalId(tenantId) |
|
|
return { |
|
|
|
|
|
id: globalId, |
|
|
|
|
|
tenantId: `${globalId}-${tenantId}`, |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
// tenant id's in the cloud are already unique
|
|
|
// tenant id's in the cloud are already unique
|
|
|
return { |
|
|
return tenantId |
|
|
id: tenantId, |
|
|
|
|
|
tenantId: tenantId, |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -80,13 +81,34 @@ const getHostingFromEnv = () => { |
|
|
return env.SELF_HOSTED ? Hosting.SELF : Hosting.CLOUD |
|
|
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) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
export const identifyTenant = async ( |
|
|
export const identifyTenant = async ( |
|
|
tenantId: string, |
|
|
tenantId: string, |
|
|
account: CloudAccount | undefined, |
|
|
account: CloudAccount | undefined, |
|
|
timestamp?: string | number |
|
|
timestamp?: string | number |
|
|
) => { |
|
|
) => { |
|
|
const global = await getGlobalIdentifiers(tenantId) |
|
|
const globalTenantId = await getGlobalTenantId(tenantId) |
|
|
const id = global.id |
|
|
const id = globalTenantId |
|
|
const hosting = getHostingFromEnv() |
|
|
const hosting = getHostingFromEnv() |
|
|
const type = IdentityType.TENANT |
|
|
const type = IdentityType.TENANT |
|
|
const profession = account?.profession |
|
|
const profession = account?.profession |
|
|
@ -94,7 +116,7 @@ export const identifyTenant = async ( |
|
|
|
|
|
|
|
|
const identity: TenantIdentity = { |
|
|
const identity: TenantIdentity = { |
|
|
id, |
|
|
id, |
|
|
tenantId: global.tenantId, |
|
|
tenantId: globalTenantId, |
|
|
hosting, |
|
|
hosting, |
|
|
type, |
|
|
type, |
|
|
profession, |
|
|
profession, |
|
|
@ -116,7 +138,8 @@ export const identifyUser = async ( |
|
|
let admin = user.admin?.global |
|
|
let admin = user.admin?.global |
|
|
let providerType = user.providerType |
|
|
let providerType = user.providerType |
|
|
const accountHolder = account?.budibaseUserId === user._id |
|
|
const accountHolder = account?.budibaseUserId === user._id |
|
|
const verified = account ? account.verified : false |
|
|
const verified = |
|
|
|
|
|
account && account?.budibaseUserId === user._id ? account.verified : false |
|
|
const profession = account?.profession |
|
|
const profession = account?.profession |
|
|
const companySize = account?.size |
|
|
const companySize = account?.size |
|
|
|
|
|
|
|
|
@ -170,6 +193,9 @@ export const identifyAccount = async (account: Account) => { |
|
|
await identify(identity) |
|
|
await identify(identity) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const identify = async (identity: Identity, timestamp?: string | number) => { |
|
|
export const identify = async ( |
|
|
await analyticsProcessor.identify(identity, timestamp) |
|
|
identity: Identity, |
|
|
|
|
|
timestamp?: string | number |
|
|
|
|
|
) => { |
|
|
|
|
|
await processors.identify(identity, timestamp) |
|
|
} |
|
|
} |
|
|
|