1
0
Fork 0
mirror of synced 2024-07-07 15:25:52 +12:00
budibase/packages/worker/src/api/controllers/admin/configs.js

146 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-04-21 05:14:36 +12:00
const CouchDB = require("../../../db")
const {
generateConfigID,
StaticDatabases,
getConfigParams,
getGlobalUserParams,
getScopedFullConfig,
} = require("@budibase/auth").db
const fetch = require("node-fetch")
const { Configs } = require("../../../constants")
const email = require("../../../utilities/email")
const env = require("../../../environment")
const APP_PREFIX = "app_"
2021-04-21 05:14:36 +12:00
const GLOBAL_DB = StaticDatabases.GLOBAL.name
2021-05-03 19:31:09 +12:00
exports.save = async function (ctx) {
2021-04-21 05:14:36 +12:00
const db = new CouchDB(GLOBAL_DB)
2021-05-05 04:31:06 +12:00
const { type, group, 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,
group,
user,
})
2021-04-21 05:14:36 +12:00
}
// verify the configuration
switch (type) {
case Configs.SMTP:
2021-04-24 01:58:06 +12:00
await email.verifyConfig(config)
2021-04-27 01:16:05 +12:00
break
}
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(err.status, err)
}
}
2021-05-03 19:31:09 +12:00
exports.fetch = async function (ctx) {
2021-04-21 05:14:36 +12:00
const db = new CouchDB(GLOBAL_DB)
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.
* The hierarchy is type -> group -> user.
*/
2021-05-03 19:31:09 +12:00
exports.find = async function (ctx) {
2021-04-21 05:14:36 +12:00
const db = new CouchDB(GLOBAL_DB)
2021-04-22 22:45:22 +12:00
const { userId, groupId } = ctx.query
if (groupId && userId) {
const group = await db.get(groupId)
2021-05-04 22:32:22 +12:00
const userInGroup = group.users.some(groupUser => groupUser === userId)
2021-04-22 22:45:22 +12:00
if (!ctx.user.admin && !userInGroup) {
ctx.throw(400, `User is not in specified group: ${group}.`)
}
}
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,
group: groupId,
2021-04-22 22:45:22 +12:00
})
if (scopedConfig) {
ctx.body = scopedConfig
} else {
ctx.throw(400, "No configuration exists.")
}
2021-04-21 05:14:36 +12:00
} catch (err) {
ctx.throw(err.status, err)
}
}
2021-05-03 19:31:09 +12:00
exports.destroy = async function (ctx) {
2021-04-21 05:14:36 +12:00
const db = new CouchDB(GLOBAL_DB)
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 db = new CouchDB(GLOBAL_DB)
try {
// TODO: Watch get started video
// Apps exist
2021-05-06 21:57:24 +12:00
let allDbs
if (env.COUCH_DB_URL) {
allDbs = await (await fetch(`${env.COUCH_DB_URL}/_all_dbs`)).json()
} else {
allDbs = await CouchDB.allDbs()
}
const appDbNames = allDbs.filter(dbName => dbName.startsWith(APP_PREFIX))
// 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,
})
// 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: appDbNames.length,
smtp: !!smtpConfig,
2021-05-06 21:57:24 +12:00
adminUser,
}
} catch (err) {
ctx.throw(err.status, err)
}
}