1
0
Fork 0
mirror of synced 2024-09-11 23:16:00 +12:00
budibase/packages/worker/src/api/controllers/admin/configs.js

251 lines
5.8 KiB
JavaScript
Raw Normal View History

const {
generateConfigID,
getConfigParams,
getGlobalUserParams,
getScopedFullConfig,
getGlobalDBFromCtx,
getGlobalDB,
getAllApps,
} = require("@budibase/auth/db")
const { Configs } = require("../../../constants")
const email = require("../../../utilities/email")
const { upload, ObjectStoreBuckets } = require("@budibase/auth").objectStore
2021-05-03 19:31:09 +12:00
exports.save = async function (ctx) {
const db = getGlobalDBFromCtx(ctx)
2021-07-14 04:27:04 +12:00
const { type, workspace, user, config } = ctx.request.body
2021-04-21 05:14:36 +12:00
// Config does not exist yet
2021-05-05 04:31:06 +12:00
if (!ctx.request.body._id) {
ctx.request.body._id = generateConfigID({
2021-04-22 22:45:22 +12:00
type,
2021-07-14 04:27:04 +12:00
workspace,
2021-04-22 22:45:22 +12:00
user,
})
2021-04-21 05:14:36 +12:00
}
try {
// verify the configuration
switch (type) {
case Configs.SMTP:
await email.verifyConfig(config)
break
}
} catch (err) {
ctx.throw(400, err)
}
2021-04-21 05:14:36 +12:00
try {
2021-05-05 04:31:06 +12:00
const response = await db.put(ctx.request.body)
2021-04-21 05:14:36 +12:00
ctx.body = {
2021-04-22 22:45:22 +12:00
type,
2021-04-21 05:14:36 +12:00
_id: response.id,
_rev: response.rev,
}
} catch (err) {
ctx.throw(400, err)
2021-04-21 05:14:36 +12:00
}
}
2021-05-03 19:31:09 +12:00
exports.fetch = async function (ctx) {
const db = getGlobalDBFromCtx(ctx)
2021-04-21 05:14:36 +12:00
const response = await db.allDocs(
2021-05-06 03:00:15 +12:00
getConfigParams(
{ type: ctx.params.type },
{
include_docs: true,
}
)
2021-04-21 05:14:36 +12:00
)
2021-05-04 22:32:22 +12:00
ctx.body = response.rows.map(row => row.doc)
2021-04-21 05:14:36 +12:00
}
2021-04-22 22:45:22 +12:00
/**
* Gets the most granular config for a particular configuration type.
2021-07-14 04:27:04 +12:00
* The hierarchy is type -> workspace -> user.
2021-04-22 22:45:22 +12:00
*/
2021-05-03 19:31:09 +12:00
exports.find = async function (ctx) {
const db = getGlobalDBFromCtx(ctx)
2021-04-22 22:45:22 +12:00
2021-07-14 04:27:04 +12:00
const { userId, workspaceId } = ctx.query
if (workspaceId && userId) {
const workspace = await db.get(workspaceId)
2021-07-14 04:28:05 +12:00
const userInWorkspace = workspace.users.some(
workspaceUser => workspaceUser === userId
)
2021-07-14 04:27:04 +12:00
if (!ctx.user.admin && !userInWorkspace) {
ctx.throw(400, `User is not in specified workspace: ${workspace}.`)
2021-04-22 22:45:22 +12:00
}
}
2021-04-21 05:14:36 +12:00
try {
2021-04-22 22:45:22 +12:00
// Find the config with the most granular scope based on context
const scopedConfig = await getScopedFullConfig(db, {
2021-04-23 00:46:54 +12:00
type: ctx.params.type,
user: userId,
2021-07-14 04:27:04 +12:00
workspace: workspaceId,
2021-04-22 22:45:22 +12:00
})
if (scopedConfig) {
ctx.body = scopedConfig
} else {
// don't throw an error, there simply is nothing to return
ctx.body = {}
2021-04-22 22:45:22 +12:00
}
2021-04-21 05:14:36 +12:00
} catch (err) {
ctx.throw(err.status, err)
}
}
2021-07-14 01:54:20 +12:00
exports.publicOidc = async function (ctx) {
const db = getGlobalDBFromCtx(ctx)
2021-07-14 01:54:20 +12:00
try {
// Find the config with the most granular scope based on context
const oidcConfig = await getScopedFullConfig(db, {
type: Configs.OIDC,
})
if (!oidcConfig) {
ctx.body = {}
} else {
ctx.body = oidcConfig.config.configs.map(config => ({
logo: config.logo,
name: config.name,
uuid: config.uuid,
}))
2021-07-14 01:54:20 +12:00
}
} catch (err) {
ctx.throw(err.status, err)
}
}
exports.publicSettings = async function (ctx) {
const db = getGlobalDBFromCtx(ctx)
try {
// Find the config with the most granular scope based on context
const publicConfig = await getScopedFullConfig(db, {
type: Configs.SETTINGS,
})
const googleConfig = await getScopedFullConfig(db, {
type: Configs.GOOGLE,
})
const oidcConfig = await getScopedFullConfig(db, {
type: Configs.OIDC,
})
let config
if (!publicConfig) {
config = {
config: {},
}
} else {
config = publicConfig
}
config.config.google = !googleConfig
? !!googleConfig
: googleConfig.config.activated
config.config.oidc = !oidcConfig
? !!oidcConfig
: oidcConfig.config.configs[0].activated
ctx.body = config
} catch (err) {
ctx.throw(err.status, err)
}
}
exports.upload = async function (ctx) {
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
const bucket = ObjectStoreBuckets.GLOBAL
const key = `${type}/${name}`
await upload({
bucket,
filename: key,
path: file.path,
type: file.type,
})
// add to configuration structure
// TODO: right now this only does a global level
const db = getGlobalDBFromCtx(ctx)
let cfgStructure = await getScopedFullConfig(db, { type })
if (!cfgStructure) {
cfgStructure = {
_id: generateConfigID({ type }),
config: {},
}
}
const url = `/${bucket}/${key}`
cfgStructure.config[`${name}`] = url
// write back to db with url updated
await db.put(cfgStructure)
ctx.body = {
message: "File has been uploaded and url stored to config.",
url,
}
}
2021-05-03 19:31:09 +12:00
exports.destroy = async function (ctx) {
const db = getGlobalDBFromCtx(ctx)
2021-04-21 05:14:36 +12:00
const { id, rev } = ctx.params
try {
await db.remove(id, rev)
ctx.body = { message: "Config deleted successfully" }
} catch (err) {
ctx.throw(err.status, err)
}
}
2021-05-06 21:57:24 +12:00
exports.configChecklist = async function (ctx) {
const tenantId = ctx.request.query.tenantId
const db = tenantId ? getGlobalDB(tenantId) : getGlobalDBFromCtx(ctx)
try {
// TODO: Watch get started video
// Apps exist
const apps = await getAllApps({ tenantId })
// They have set up SMTP
2021-05-06 23:09:35 +12:00
const smtpConfig = await getScopedFullConfig(db, {
2021-05-06 21:57:24 +12:00
type: Configs.SMTP,
})
2021-05-22 01:55:11 +12:00
// They have set up Google Auth
const googleConfig = await getScopedFullConfig(db, {
2021-05-22 01:55:11 +12:00
type: Configs.GOOGLE,
})
// They have set up OIDC
const oidcConfig = await getScopedFullConfig(db, {
type: Configs.OIDC,
})
// They have set up an admin user
const users = await db.allDocs(
getGlobalUserParams(null, {
include_docs: true,
})
)
const adminUser = users.rows.some(row => row.doc.admin)
2021-05-06 21:57:24 +12:00
ctx.body = {
apps: apps.length,
smtp: !!smtpConfig,
2021-05-06 21:57:24 +12:00
adminUser,
2021-07-14 08:46:50 +12:00
sso: !!googleConfig || !!oidcConfig,
}
} catch (err) {
ctx.throw(err.status, err)
}
}