2022-11-23 19:25:20 +01:00
|
|
|
import * as email from "../../../utilities/email"
|
|
|
|
import env from "../../../environment"
|
|
|
|
import { googleCallbackUrl, oidcCallbackUrl } from "./auth"
|
|
|
|
import {
|
|
|
|
events,
|
2022-06-29 14:08:48 +02:00
|
|
|
cache,
|
2022-11-23 19:25:20 +01:00
|
|
|
objectStore,
|
|
|
|
tenancy,
|
|
|
|
db as dbCore,
|
|
|
|
} from "@budibase/backend-core"
|
|
|
|
import { checkAnyUserExists } from "../../../utilities/users"
|
|
|
|
import {
|
|
|
|
Database,
|
|
|
|
Config as ConfigDoc,
|
|
|
|
ConfigType,
|
|
|
|
SSOType,
|
|
|
|
GoogleConfig,
|
|
|
|
OIDCConfig,
|
|
|
|
SettingsConfig,
|
|
|
|
BBContext,
|
|
|
|
} from "@budibase/types"
|
|
|
|
|
|
|
|
const getEventFns = async (db: Database, config: ConfigDoc) => {
|
2022-04-06 00:14:53 +02:00
|
|
|
const fns = []
|
|
|
|
const type = config.type
|
2022-04-05 17:56:28 +02:00
|
|
|
|
2022-04-06 00:14:53 +02:00
|
|
|
let existing
|
|
|
|
if (config._id) {
|
|
|
|
existing = await db.get(config._id)
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
const ssoType = type as SSOType
|
2022-04-06 00:14:53 +02:00
|
|
|
if (!existing) {
|
|
|
|
switch (config.type) {
|
2022-11-23 19:25:20 +01:00
|
|
|
case ConfigType.SMTP: {
|
2022-04-06 00:14:53 +02:00
|
|
|
fns.push(events.email.SMTPCreated)
|
|
|
|
break
|
2022-04-06 23:53:33 +02:00
|
|
|
}
|
2022-11-23 19:25:20 +01:00
|
|
|
case ConfigType.GOOGLE: {
|
|
|
|
const googleCfg = config as GoogleConfig
|
|
|
|
fns.push(() => events.auth.SSOCreated(ssoType))
|
|
|
|
if (googleCfg.config.activated) {
|
|
|
|
fns.push(() => events.auth.SSOActivated(ssoType))
|
2022-04-06 01:54:07 +02:00
|
|
|
}
|
2022-04-06 00:14:53 +02:00
|
|
|
break
|
2022-04-06 23:53:33 +02:00
|
|
|
}
|
2022-11-23 19:25:20 +01:00
|
|
|
case ConfigType.OIDC: {
|
|
|
|
const oidcCfg = config as OIDCConfig
|
|
|
|
fns.push(() => events.auth.SSOCreated(ssoType))
|
|
|
|
if (oidcCfg.config.configs[0].activated) {
|
|
|
|
fns.push(() => events.auth.SSOActivated(ssoType))
|
2022-04-06 01:54:07 +02:00
|
|
|
}
|
2022-04-06 00:14:53 +02:00
|
|
|
break
|
2022-04-06 23:53:33 +02:00
|
|
|
}
|
2022-11-23 19:25:20 +01:00
|
|
|
case ConfigType.SETTINGS: {
|
2022-04-06 23:53:33 +02:00
|
|
|
// company
|
2022-11-23 19:25:20 +01:00
|
|
|
const settingsCfg = config as SettingsConfig
|
|
|
|
const company = settingsCfg.config.company
|
2022-04-06 23:53:33 +02:00
|
|
|
if (company && company !== "Budibase") {
|
2022-04-06 00:14:53 +02:00
|
|
|
fns.push(events.org.nameUpdated)
|
|
|
|
}
|
2022-04-06 23:53:33 +02:00
|
|
|
|
|
|
|
// logo
|
2022-11-23 19:25:20 +01:00
|
|
|
const logoUrl = settingsCfg.config.logoUrl
|
2022-04-06 23:53:33 +02:00
|
|
|
if (logoUrl) {
|
2022-04-06 00:14:53 +02:00
|
|
|
fns.push(events.org.logoUpdated)
|
|
|
|
}
|
2022-04-06 23:53:33 +02:00
|
|
|
|
|
|
|
// platform url
|
2022-11-23 19:25:20 +01:00
|
|
|
const platformUrl = settingsCfg.config.platformUrl
|
2022-04-07 01:38:18 +02:00
|
|
|
if (
|
|
|
|
platformUrl &&
|
|
|
|
platformUrl !== "http://localhost:10000" &&
|
|
|
|
env.SELF_HOSTED
|
|
|
|
) {
|
2022-04-06 00:14:53 +02:00
|
|
|
fns.push(events.org.platformURLUpdated)
|
|
|
|
}
|
|
|
|
break
|
2022-04-06 23:53:33 +02:00
|
|
|
}
|
2022-04-06 00:14:53 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (config.type) {
|
2022-11-23 19:25:20 +01:00
|
|
|
case ConfigType.SMTP: {
|
2022-04-06 00:14:53 +02:00
|
|
|
fns.push(events.email.SMTPUpdated)
|
|
|
|
break
|
2022-04-06 23:53:33 +02:00
|
|
|
}
|
2022-11-23 19:25:20 +01:00
|
|
|
case ConfigType.GOOGLE: {
|
|
|
|
const googleCfg = config as GoogleConfig
|
|
|
|
fns.push(() => events.auth.SSOUpdated(ssoType))
|
|
|
|
if (!existing.config.activated && googleCfg.config.activated) {
|
|
|
|
fns.push(() => events.auth.SSOActivated(ssoType))
|
|
|
|
} else if (existing.config.activated && !googleCfg.config.activated) {
|
|
|
|
fns.push(() => events.auth.SSODeactivated(ssoType))
|
2022-04-06 01:54:07 +02:00
|
|
|
}
|
2022-04-06 00:14:53 +02:00
|
|
|
break
|
2022-04-06 23:53:33 +02:00
|
|
|
}
|
2022-11-23 19:25:20 +01:00
|
|
|
case ConfigType.OIDC: {
|
|
|
|
const oidcCfg = config as OIDCConfig
|
|
|
|
fns.push(() => events.auth.SSOUpdated(ssoType))
|
2022-04-06 01:54:07 +02:00
|
|
|
if (
|
|
|
|
!existing.config.configs[0].activated &&
|
2022-11-23 19:25:20 +01:00
|
|
|
oidcCfg.config.configs[0].activated
|
2022-04-06 01:54:07 +02:00
|
|
|
) {
|
2022-11-23 19:25:20 +01:00
|
|
|
fns.push(() => events.auth.SSOActivated(ssoType))
|
2022-04-06 01:54:07 +02:00
|
|
|
} else if (
|
|
|
|
existing.config.configs[0].activated &&
|
2022-11-23 19:25:20 +01:00
|
|
|
!oidcCfg.config.configs[0].activated
|
2022-04-06 01:54:07 +02:00
|
|
|
) {
|
2022-11-23 19:25:20 +01:00
|
|
|
fns.push(() => events.auth.SSODeactivated(ssoType))
|
2022-04-06 01:54:07 +02:00
|
|
|
}
|
2022-04-06 00:14:53 +02:00
|
|
|
break
|
2022-04-06 23:53:33 +02:00
|
|
|
}
|
2022-11-23 19:25:20 +01:00
|
|
|
case ConfigType.SETTINGS: {
|
2022-04-06 23:53:33 +02:00
|
|
|
// company
|
2022-11-23 19:25:20 +01:00
|
|
|
const settingsCfg = config as SettingsConfig
|
2022-04-06 23:53:33 +02:00
|
|
|
const existingCompany = existing.config.company
|
2022-11-23 19:25:20 +01:00
|
|
|
const company = settingsCfg.config.company
|
2022-04-06 23:53:33 +02:00
|
|
|
if (company && company !== "Budibase" && existingCompany !== company) {
|
2022-04-06 00:14:53 +02:00
|
|
|
fns.push(events.org.nameUpdated)
|
|
|
|
}
|
2022-04-06 23:53:33 +02:00
|
|
|
|
|
|
|
// logo
|
|
|
|
const existingLogoUrl = existing.config.logoUrl
|
2022-11-23 19:25:20 +01:00
|
|
|
const logoUrl = settingsCfg.config.logoUrl
|
2022-04-06 23:53:33 +02:00
|
|
|
if (logoUrl && existingLogoUrl !== logoUrl) {
|
2022-04-06 00:14:53 +02:00
|
|
|
fns.push(events.org.logoUpdated)
|
|
|
|
}
|
2022-04-06 23:53:33 +02:00
|
|
|
|
|
|
|
// platform url
|
|
|
|
const existingPlatformUrl = existing.config.platformUrl
|
2022-11-23 19:25:20 +01:00
|
|
|
const platformUrl = settingsCfg.config.platformUrl
|
2022-04-06 23:53:33 +02:00
|
|
|
if (
|
|
|
|
platformUrl &&
|
|
|
|
platformUrl !== "http://localhost:10000" &&
|
2022-04-07 01:38:18 +02:00
|
|
|
existingPlatformUrl !== platformUrl &&
|
|
|
|
env.SELF_HOSTED
|
2022-04-06 23:53:33 +02:00
|
|
|
) {
|
2022-04-06 00:14:53 +02:00
|
|
|
fns.push(events.org.platformURLUpdated)
|
|
|
|
}
|
|
|
|
break
|
2022-04-06 23:53:33 +02:00
|
|
|
}
|
2022-04-06 00:14:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fns
|
|
|
|
}
|
2022-04-05 17:56:28 +02:00
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
export async function save(ctx: BBContext) {
|
|
|
|
const db = tenancy.getGlobalDB()
|
2021-08-05 10:59:08 +02:00
|
|
|
const { type, workspace, user, config } = ctx.request.body
|
2022-04-06 00:14:53 +02:00
|
|
|
let eventFns = await getEventFns(db, ctx.request.body)
|
2021-04-20 19:14:36 +02:00
|
|
|
// Config does not exist yet
|
2021-05-04 18:31:06 +02:00
|
|
|
if (!ctx.request.body._id) {
|
2022-11-23 19:25:20 +01:00
|
|
|
ctx.request.body._id = dbCore.generateConfigID({
|
2021-04-22 12:45:22 +02:00
|
|
|
type,
|
2021-08-05 10:59:08 +02:00
|
|
|
workspace,
|
2021-04-22 12:45:22 +02:00
|
|
|
user,
|
|
|
|
})
|
2021-04-20 19:14:36 +02:00
|
|
|
}
|
2021-06-09 16:45:54 +02:00
|
|
|
try {
|
|
|
|
// verify the configuration
|
|
|
|
switch (type) {
|
2022-11-23 19:25:20 +01:00
|
|
|
case ConfigType.SMTP:
|
2021-06-09 16:45:54 +02:00
|
|
|
await email.verifyConfig(config)
|
|
|
|
break
|
|
|
|
}
|
2022-11-23 19:25:20 +01:00
|
|
|
} catch (err: any) {
|
2021-06-09 16:45:54 +02:00
|
|
|
ctx.throw(400, err)
|
2021-04-23 14:49:47 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 19:14:36 +02:00
|
|
|
try {
|
2021-05-04 18:31:06 +02:00
|
|
|
const response = await db.put(ctx.request.body)
|
2022-11-24 19:48:51 +01:00
|
|
|
await cache.bustCache(cache.CacheKey.CHECKLIST)
|
|
|
|
await cache.bustCache(cache.CacheKey.ANALYTICS_ENABLED)
|
2022-05-29 00:03:31 +02:00
|
|
|
|
2022-04-06 00:14:53 +02:00
|
|
|
for (const fn of eventFns) {
|
2022-05-23 23:14:44 +02:00
|
|
|
await fn()
|
2022-04-06 00:14:53 +02:00
|
|
|
}
|
2022-05-29 00:03:31 +02:00
|
|
|
|
2021-04-20 19:14:36 +02:00
|
|
|
ctx.body = {
|
2021-04-22 12:45:22 +02:00
|
|
|
type,
|
2021-04-20 19:14:36 +02:00
|
|
|
_id: response.id,
|
|
|
|
_rev: response.rev,
|
|
|
|
}
|
2022-11-23 19:25:20 +01:00
|
|
|
} catch (err: any) {
|
2021-06-09 16:45:54 +02:00
|
|
|
ctx.throw(400, err)
|
2021-04-20 19:14:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
export async function fetch(ctx: BBContext) {
|
|
|
|
const db = tenancy.getGlobalDB()
|
2021-04-20 19:14:36 +02:00
|
|
|
const response = await db.allDocs(
|
2022-11-23 19:25:20 +01:00
|
|
|
dbCore.getConfigParams(
|
2021-05-05 17:00:15 +02:00
|
|
|
{ type: ctx.params.type },
|
|
|
|
{
|
|
|
|
include_docs: true,
|
|
|
|
}
|
|
|
|
)
|
2021-04-20 19:14:36 +02:00
|
|
|
)
|
2021-05-04 12:32:22 +02:00
|
|
|
ctx.body = response.rows.map(row => row.doc)
|
2021-04-20 19:14:36 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 12:45:22 +02:00
|
|
|
/**
|
|
|
|
* Gets the most granular config for a particular configuration type.
|
2021-08-05 10:59:08 +02:00
|
|
|
* The hierarchy is type -> workspace -> user.
|
2021-04-22 12:45:22 +02:00
|
|
|
*/
|
2022-11-23 19:25:20 +01:00
|
|
|
export async function find(ctx: BBContext) {
|
|
|
|
const db = tenancy.getGlobalDB()
|
2021-08-05 10:59:08 +02:00
|
|
|
|
|
|
|
const { userId, workspaceId } = ctx.query
|
|
|
|
if (workspaceId && userId) {
|
2022-11-23 19:25:20 +01:00
|
|
|
const workspace = await db.get(workspaceId as string)
|
2021-08-05 10:59:08 +02:00
|
|
|
const userInWorkspace = workspace.users.some(
|
2022-11-23 19:25:20 +01:00
|
|
|
(workspaceUser: any) => workspaceUser === userId
|
2021-08-05 10:59:08 +02:00
|
|
|
)
|
2022-11-23 19:25:20 +01:00
|
|
|
if (!ctx.user!.admin && !userInWorkspace) {
|
2021-08-05 10:59:08 +02:00
|
|
|
ctx.throw(400, `User is not in specified workspace: ${workspace}.`)
|
2021-04-22 12:45:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 19:14:36 +02:00
|
|
|
try {
|
2021-04-22 12:45:22 +02:00
|
|
|
// Find the config with the most granular scope based on context
|
2022-11-23 19:25:20 +01:00
|
|
|
const scopedConfig = await dbCore.getScopedFullConfig(db, {
|
2021-04-22 14:46:54 +02:00
|
|
|
type: ctx.params.type,
|
|
|
|
user: userId,
|
2021-08-05 10:59:08 +02:00
|
|
|
workspace: workspaceId,
|
2021-04-22 12:45:22 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if (scopedConfig) {
|
|
|
|
ctx.body = scopedConfig
|
|
|
|
} else {
|
2021-06-21 18:13:06 +02:00
|
|
|
// don't throw an error, there simply is nothing to return
|
|
|
|
ctx.body = {}
|
2021-04-22 12:45:22 +02:00
|
|
|
}
|
2022-11-23 19:25:20 +01:00
|
|
|
} catch (err: any) {
|
|
|
|
ctx.throw(err?.status || 400, err)
|
2021-04-20 19:14:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
export async function publicOidc(ctx: BBContext) {
|
|
|
|
const db = tenancy.getGlobalDB()
|
2021-07-13 15:54:20 +02:00
|
|
|
try {
|
|
|
|
// Find the config with the most granular scope based on context
|
2022-11-23 19:25:20 +01:00
|
|
|
const oidcConfig = await dbCore.getScopedFullConfig(db, {
|
|
|
|
type: ConfigType.OIDC,
|
2021-07-13 15:54:20 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if (!oidcConfig) {
|
|
|
|
ctx.body = {}
|
|
|
|
} else {
|
2022-11-23 19:25:20 +01:00
|
|
|
ctx.body = oidcConfig.config.configs.map((config: any) => ({
|
2021-08-05 10:59:08 +02:00
|
|
|
logo: config.logo,
|
|
|
|
name: config.name,
|
|
|
|
uuid: config.uuid,
|
|
|
|
}))
|
2021-07-13 15:54:20 +02:00
|
|
|
}
|
2022-11-23 19:25:20 +01:00
|
|
|
} catch (err: any) {
|
2021-07-13 15:54:20 +02:00
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
export async function publicSettings(ctx: BBContext) {
|
|
|
|
const db = tenancy.getGlobalDB()
|
2021-07-15 17:50:57 +02:00
|
|
|
|
2021-06-21 19:01:25 +02:00
|
|
|
try {
|
|
|
|
// Find the config with the most granular scope based on context
|
2022-11-23 19:25:20 +01:00
|
|
|
const publicConfig = await dbCore.getScopedFullConfig(db, {
|
|
|
|
type: ConfigType.SETTINGS,
|
2021-06-21 19:01:25 +02:00
|
|
|
})
|
2021-07-09 10:49:16 +02:00
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
const googleConfig = await dbCore.getScopedFullConfig(db, {
|
|
|
|
type: ConfigType.GOOGLE,
|
2021-07-15 16:49:06 +02:00
|
|
|
})
|
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
const oidcConfig = await dbCore.getScopedFullConfig(db, {
|
|
|
|
type: ConfigType.OIDC,
|
2021-07-15 16:49:06 +02:00
|
|
|
})
|
|
|
|
|
2021-08-05 10:59:08 +02:00
|
|
|
let config
|
2021-07-15 17:50:57 +02:00
|
|
|
if (!publicConfig) {
|
|
|
|
config = {
|
|
|
|
config: {},
|
2021-07-15 16:49:06 +02:00
|
|
|
}
|
2021-06-21 19:37:14 +02:00
|
|
|
} else {
|
2021-07-15 17:50:57 +02:00
|
|
|
config = publicConfig
|
2021-06-21 19:37:14 +02:00
|
|
|
}
|
2021-07-15 17:50:57 +02:00
|
|
|
|
2021-07-23 12:38:17 +02:00
|
|
|
// google button flag
|
2021-07-23 15:40:22 +02:00
|
|
|
if (googleConfig && googleConfig.config) {
|
2021-08-05 10:59:08 +02:00
|
|
|
// activated by default for configs pre-activated flag
|
|
|
|
config.config.google =
|
|
|
|
googleConfig.config.activated == null || googleConfig.config.activated
|
2021-07-23 15:40:22 +02:00
|
|
|
} else {
|
|
|
|
config.config.google = false
|
|
|
|
}
|
2021-07-23 12:38:17 +02:00
|
|
|
|
2021-11-12 14:31:55 +01:00
|
|
|
// callback urls
|
|
|
|
config.config.oidcCallbackUrl = await oidcCallbackUrl()
|
|
|
|
config.config.googleCallbackUrl = await googleCallbackUrl()
|
|
|
|
|
2021-07-23 12:38:17 +02:00
|
|
|
// oidc button flag
|
2021-07-23 15:40:22 +02:00
|
|
|
if (oidcConfig && oidcConfig.config) {
|
2021-08-05 10:59:08 +02:00
|
|
|
config.config.oidc = oidcConfig.config.configs[0].activated
|
2021-07-23 15:40:22 +02:00
|
|
|
} else {
|
|
|
|
config.config.oidc = false
|
|
|
|
}
|
2021-07-23 12:38:17 +02:00
|
|
|
|
2021-07-15 17:50:57 +02:00
|
|
|
ctx.body = config
|
2022-11-23 19:25:20 +01:00
|
|
|
} catch (err: any) {
|
2021-06-21 19:01:25 +02:00
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
export async function upload(ctx: BBContext) {
|
2021-05-07 14:55:30 +02:00
|
|
|
if (ctx.request.files == null || ctx.request.files.file.length > 1) {
|
|
|
|
ctx.throw(400, "One file must be uploaded.")
|
|
|
|
}
|
|
|
|
const file = ctx.request.files.file
|
|
|
|
const { type, name } = ctx.params
|
|
|
|
|
2021-09-23 17:22:12 +02:00
|
|
|
let bucket
|
|
|
|
if (env.SELF_HOSTED) {
|
2022-11-23 19:25:20 +01:00
|
|
|
bucket = objectStore.ObjectStoreBuckets.GLOBAL
|
2021-09-23 17:22:12 +02:00
|
|
|
} else {
|
2022-11-23 19:25:20 +01:00
|
|
|
bucket = objectStore.ObjectStoreBuckets.GLOBAL_CLOUD
|
2021-09-23 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 14:31:55 +01:00
|
|
|
let key
|
|
|
|
if (env.MULTI_TENANCY) {
|
2022-11-23 19:25:20 +01:00
|
|
|
key = `${tenancy.getTenantId()}/${type}/${name}`
|
2021-11-12 14:31:55 +01:00
|
|
|
} else {
|
|
|
|
key = `${type}/${name}`
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
await objectStore.upload({
|
2021-05-07 14:55:30 +02:00
|
|
|
bucket,
|
|
|
|
filename: key,
|
|
|
|
path: file.path,
|
|
|
|
type: file.type,
|
|
|
|
})
|
|
|
|
|
|
|
|
// add to configuration structure
|
|
|
|
// TODO: right now this only does a global level
|
2022-11-23 19:25:20 +01:00
|
|
|
const db = tenancy.getGlobalDB()
|
|
|
|
let cfgStructure = await dbCore.getScopedFullConfig(db, { type })
|
2021-05-12 15:27:33 +02:00
|
|
|
if (!cfgStructure) {
|
|
|
|
cfgStructure = {
|
2022-11-23 19:25:20 +01:00
|
|
|
_id: dbCore.generateConfigID({ type }),
|
2021-05-12 15:27:33 +02:00
|
|
|
config: {},
|
2021-05-07 14:55:30 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-12 14:31:55 +01:00
|
|
|
let url
|
|
|
|
if (env.SELF_HOSTED) {
|
|
|
|
url = `/${bucket}/${key}`
|
|
|
|
} else {
|
2022-11-07 10:45:00 +01:00
|
|
|
url = `${env.CDN_URL}/${key}`
|
2021-11-12 14:31:55 +01:00
|
|
|
}
|
|
|
|
|
2021-07-07 14:41:09 +02:00
|
|
|
cfgStructure.config[`${name}`] = url
|
2021-05-07 14:55:30 +02:00
|
|
|
// write back to db with url updated
|
2021-05-12 15:27:33 +02:00
|
|
|
await db.put(cfgStructure)
|
2021-05-07 14:55:30 +02:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
message: "File has been uploaded and url stored to config.",
|
|
|
|
url,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
export async function destroy(ctx: BBContext) {
|
|
|
|
const db = tenancy.getGlobalDB()
|
2021-04-20 19:14:36 +02:00
|
|
|
const { id, rev } = ctx.params
|
|
|
|
try {
|
|
|
|
await db.remove(id, rev)
|
2022-11-24 19:48:51 +01:00
|
|
|
await cache.delete(cache.CacheKey.CHECKLIST)
|
2021-04-20 19:14:36 +02:00
|
|
|
ctx.body = { message: "Config deleted successfully" }
|
2022-11-23 19:25:20 +01:00
|
|
|
} catch (err: any) {
|
2021-04-20 19:14:36 +02:00
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|
2021-05-05 21:58:31 +02:00
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
export async function configChecklist(ctx: BBContext) {
|
|
|
|
const db = tenancy.getGlobalDB()
|
|
|
|
const tenantId = tenancy.getTenantId()
|
2021-05-05 21:58:31 +02:00
|
|
|
|
|
|
|
try {
|
2022-11-23 19:25:20 +01:00
|
|
|
ctx.body = await cache.withCache(
|
2022-11-24 19:48:51 +01:00
|
|
|
cache.CacheKey.CHECKLIST,
|
2022-05-23 17:57:15 +02:00
|
|
|
env.CHECKLIST_CACHE_TTL,
|
|
|
|
async () => {
|
|
|
|
let apps = []
|
|
|
|
if (!env.MULTI_TENANCY || tenantId) {
|
|
|
|
// Apps exist
|
2022-11-23 19:25:20 +01:00
|
|
|
apps = await dbCore.getAllApps({ idsOnly: true, efficient: true })
|
2022-05-23 17:57:15 +02:00
|
|
|
}
|
2021-05-21 15:55:11 +02:00
|
|
|
|
2022-05-23 17:57:15 +02:00
|
|
|
// They have set up SMTP
|
2022-11-23 19:25:20 +01:00
|
|
|
const smtpConfig = await dbCore.getScopedFullConfig(db, {
|
|
|
|
type: ConfigType.SMTP,
|
2022-05-23 17:57:15 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// They have set up Google Auth
|
2022-11-23 19:25:20 +01:00
|
|
|
const googleConfig = await dbCore.getScopedFullConfig(db, {
|
|
|
|
type: ConfigType.GOOGLE,
|
2022-05-23 17:57:15 +02:00
|
|
|
})
|
2022-05-23 01:09:03 +02:00
|
|
|
|
2022-05-23 17:57:15 +02:00
|
|
|
// They have set up OIDC
|
2022-11-23 19:25:20 +01:00
|
|
|
const oidcConfig = await dbCore.getScopedFullConfig(db, {
|
|
|
|
type: ConfigType.OIDC,
|
2022-05-23 01:09:03 +02:00
|
|
|
})
|
2022-05-23 17:57:15 +02:00
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
// They have set up a global user
|
2022-06-30 13:01:15 +02:00
|
|
|
const userExists = await checkAnyUserExists()
|
2022-05-23 17:57:15 +02:00
|
|
|
return {
|
|
|
|
apps: {
|
|
|
|
checked: apps.length > 0,
|
|
|
|
label: "Create your first app",
|
|
|
|
link: "/builder/portal/apps",
|
|
|
|
},
|
|
|
|
smtp: {
|
|
|
|
checked: !!smtpConfig,
|
|
|
|
label: "Set up email",
|
|
|
|
link: "/builder/portal/manage/email",
|
|
|
|
},
|
|
|
|
adminUser: {
|
2022-06-30 13:01:15 +02:00
|
|
|
checked: userExists,
|
2022-05-23 17:57:15 +02:00
|
|
|
label: "Create your first user",
|
|
|
|
link: "/builder/portal/manage/users",
|
|
|
|
},
|
|
|
|
sso: {
|
|
|
|
checked: !!googleConfig || !!oidcConfig,
|
|
|
|
label: "Set up single sign-on",
|
|
|
|
link: "/builder/portal/manage/auth",
|
|
|
|
},
|
|
|
|
}
|
2022-05-23 01:09:03 +02:00
|
|
|
}
|
2021-05-05 21:58:31 +02:00
|
|
|
)
|
2022-11-23 19:25:20 +01:00
|
|
|
} catch (err: any) {
|
2021-05-05 21:58:31 +02:00
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|