2021-04-20 19:14:36 +02:00
|
|
|
const CouchDB = require("../../../db")
|
2021-04-22 18:57:38 +02:00
|
|
|
const {
|
|
|
|
generateConfigID,
|
|
|
|
StaticDatabases,
|
|
|
|
getConfigParams,
|
|
|
|
determineScopedConfig,
|
|
|
|
} = require("@budibase/auth").db
|
2021-04-23 14:49:47 +02:00
|
|
|
const { Configs } = require("../../../constants")
|
|
|
|
const email = require("../../../utilities/email")
|
2021-04-20 19:14:36 +02:00
|
|
|
|
|
|
|
const GLOBAL_DB = StaticDatabases.GLOBAL.name
|
|
|
|
|
|
|
|
exports.save = async function(ctx) {
|
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
|
|
|
const configDoc = ctx.request.body
|
2021-04-22 12:45:22 +02:00
|
|
|
const { type, group, user } = configDoc
|
2021-04-20 19:14:36 +02:00
|
|
|
|
|
|
|
// Config does not exist yet
|
|
|
|
if (!configDoc._id) {
|
2021-04-22 18:57:38 +02:00
|
|
|
configDoc._id = generateConfigID({
|
2021-04-22 12:45:22 +02:00
|
|
|
type,
|
|
|
|
group,
|
|
|
|
user,
|
|
|
|
})
|
2021-04-20 19:14:36 +02:00
|
|
|
}
|
|
|
|
|
2021-04-23 14:49:47 +02:00
|
|
|
// verify the configuration
|
|
|
|
switch (type) {
|
|
|
|
case Configs.SMTP:
|
|
|
|
await email.verifyConfig(configDoc)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-04-20 19:14:36 +02:00
|
|
|
try {
|
|
|
|
const response = await db.post(configDoc)
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.fetch = async function(ctx) {
|
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
|
|
|
const response = await db.allDocs(
|
2021-04-22 18:57:38 +02:00
|
|
|
getConfigParams(undefined, {
|
2021-04-20 19:14:36 +02:00
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2021-04-22 18:57:38 +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.
|
|
|
|
* The hierarchy is type -> group -> user.
|
|
|
|
*/
|
2021-04-20 19:14:36 +02:00
|
|
|
exports.find = async function(ctx) {
|
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
2021-04-22 12:45:22 +02:00
|
|
|
const userId = ctx.params.user && ctx.params.user._id
|
|
|
|
|
|
|
|
const { group } = ctx.query
|
|
|
|
if (group) {
|
|
|
|
const group = await db.get(group)
|
|
|
|
const userInGroup = group.users.some(groupUser => groupUser === userId)
|
|
|
|
if (!ctx.user.admin && !userInGroup) {
|
|
|
|
ctx.throw(400, `User is not in specified group: ${group}.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2021-04-22 18:57:38 +02:00
|
|
|
const scopedConfig = await determineScopedConfig(db, {
|
2021-04-22 14:46:54 +02:00
|
|
|
type: ctx.params.type,
|
|
|
|
user: userId,
|
|
|
|
group,
|
2021-04-22 12:45:22 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if (scopedConfig) {
|
|
|
|
ctx.body = scopedConfig
|
|
|
|
} else {
|
|
|
|
ctx.throw(400, "No configuration exists.")
|
|
|
|
}
|
2021-04-20 19:14:36 +02:00
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.destroy = async function(ctx) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|