2021-08-04 11:02:24 +02:00
|
|
|
const CouchDB = require("../../../db")
|
2021-04-22 18:57:38 +02:00
|
|
|
const {
|
|
|
|
generateConfigID,
|
2021-08-04 11:02:24 +02:00
|
|
|
StaticDatabases,
|
2021-04-22 18:57:38 +02:00
|
|
|
getConfigParams,
|
2021-05-05 21:58:31 +02:00
|
|
|
getGlobalUserParams,
|
2021-05-06 11:51:21 +02:00
|
|
|
getScopedFullConfig,
|
2021-08-04 11:02:24 +02:00
|
|
|
} = require("@budibase/auth").db
|
2021-04-23 14:49:47 +02:00
|
|
|
const { Configs } = require("../../../constants")
|
|
|
|
const email = require("../../../utilities/email")
|
2021-05-07 14:55:30 +02:00
|
|
|
const { upload, ObjectStoreBuckets } = require("@budibase/auth").objectStore
|
2021-08-04 11:02:24 +02:00
|
|
|
|
|
|
|
const APP_PREFIX = "app_"
|
|
|
|
|
|
|
|
const GLOBAL_DB = StaticDatabases.GLOBAL.name
|
2021-05-05 21:58:31 +02:00
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.save = async function (ctx) {
|
2021-08-04 11:02:24 +02:00
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
|
|
|
const { type, group, user, config } = 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) {
|
|
|
|
ctx.request.body._id = generateConfigID({
|
2021-04-22 12:45:22 +02:00
|
|
|
type,
|
2021-08-04 11:02:24 +02:00
|
|
|
group,
|
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) {
|
|
|
|
case Configs.SMTP:
|
|
|
|
await email.verifyConfig(config)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
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)
|
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,
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-06-09 16:45:54 +02:00
|
|
|
ctx.throw(400, err)
|
2021-04-20 19:14:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.fetch = async function (ctx) {
|
2021-08-04 11:02:24 +02:00
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
2021-04-20 19:14:36 +02:00
|
|
|
const response = await db.allDocs(
|
2021-05-05 17:00:15 +02:00
|
|
|
getConfigParams(
|
|
|
|
{ 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-04 11:02:24 +02:00
|
|
|
* The hierarchy is type -> group -> user.
|
2021-04-22 12:45:22 +02:00
|
|
|
*/
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.find = async function (ctx) {
|
2021-08-04 11:02:24 +02:00
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
|
|
|
|
|
|
|
const { userId, groupId } = ctx.query
|
|
|
|
if (groupId && userId) {
|
|
|
|
const group = await db.get(groupId)
|
|
|
|
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-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
|
2021-05-06 11:51:21 +02:00
|
|
|
const scopedConfig = await getScopedFullConfig(db, {
|
2021-04-22 14:46:54 +02:00
|
|
|
type: ctx.params.type,
|
|
|
|
user: userId,
|
2021-08-04 11:02:24 +02:00
|
|
|
group: groupId,
|
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
|
|
|
}
|
2021-04-20 19:14:36 +02:00
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 15:54:20 +02:00
|
|
|
exports.publicOidc = async function (ctx) {
|
2021-08-04 11:02:24 +02:00
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
2021-07-13 15:54:20 +02: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 {
|
2021-08-04 11:02:24 +02:00
|
|
|
const partialOidcCofig = oidcConfig.config.configs.map(config => {
|
|
|
|
return {
|
|
|
|
logo: config.logo,
|
|
|
|
name: config.name,
|
|
|
|
uuid: config.uuid,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
ctx.body = partialOidcCofig
|
2021-07-13 15:54:20 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 19:01:25 +02:00
|
|
|
exports.publicSettings = async function (ctx) {
|
2021-08-04 11:02:24 +02:00
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
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
|
2021-07-09 10:49:16 +02:00
|
|
|
const publicConfig = await getScopedFullConfig(db, {
|
2021-06-21 19:01:25 +02:00
|
|
|
type: Configs.SETTINGS,
|
|
|
|
})
|
2021-07-09 10:49:16 +02:00
|
|
|
|
2021-07-15 16:49:06 +02:00
|
|
|
const googleConfig = await getScopedFullConfig(db, {
|
|
|
|
type: Configs.GOOGLE,
|
|
|
|
})
|
|
|
|
|
|
|
|
const oidcConfig = await getScopedFullConfig(db, {
|
|
|
|
type: Configs.OIDC,
|
|
|
|
})
|
|
|
|
|
2021-08-04 11:02:24 +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-04 11:02:24 +02:00
|
|
|
const googleActivated =
|
|
|
|
googleConfig.config.activated == undefined || // activated by default for configs pre-activated flag
|
|
|
|
googleConfig.config.activated
|
|
|
|
config.config.google = googleActivated
|
2021-07-23 15:40:22 +02:00
|
|
|
} else {
|
|
|
|
config.config.google = false
|
|
|
|
}
|
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-04 11:02:24 +02:00
|
|
|
const oidcActivated = oidcConfig.config.configs[0].activated
|
|
|
|
config.config.oidc = oidcActivated
|
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
|
2021-06-21 19:01:25 +02:00
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 14:55:30 +02:00
|
|
|
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
|
2021-07-09 10:49:16 +02:00
|
|
|
const key = `${type}/${name}`
|
2021-05-07 14:55:30 +02:00
|
|
|
await upload({
|
|
|
|
bucket,
|
|
|
|
filename: key,
|
|
|
|
path: file.path,
|
|
|
|
type: file.type,
|
|
|
|
})
|
|
|
|
|
|
|
|
// add to configuration structure
|
|
|
|
// TODO: right now this only does a global level
|
2021-08-04 11:02:24 +02:00
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
2021-05-12 15:27:33 +02:00
|
|
|
let cfgStructure = await getScopedFullConfig(db, { type })
|
|
|
|
if (!cfgStructure) {
|
|
|
|
cfgStructure = {
|
2021-05-07 14:55:30 +02:00
|
|
|
_id: generateConfigID({ type }),
|
2021-05-12 15:27:33 +02:00
|
|
|
config: {},
|
2021-05-07 14:55:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const url = `/${bucket}/${key}`
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.destroy = async function (ctx) {
|
2021-08-04 11:02:24 +02:00
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
2021-04-20 19:14:36 +02: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-05 21:58:31 +02:00
|
|
|
|
2021-05-06 11:57:24 +02:00
|
|
|
exports.configChecklist = async function (ctx) {
|
2021-08-04 11:02:24 +02:00
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
2021-05-05 21:58:31 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
// TODO: Watch get started video
|
|
|
|
|
|
|
|
// Apps exist
|
2021-08-04 11:02:24 +02:00
|
|
|
let allDbs = await CouchDB.allDbs()
|
|
|
|
const appDbNames = allDbs.filter(dbName => dbName.startsWith(APP_PREFIX))
|
2021-05-05 21:58:31 +02:00
|
|
|
|
|
|
|
// They have set up SMTP
|
2021-05-06 13:09:35 +02:00
|
|
|
const smtpConfig = await getScopedFullConfig(db, {
|
2021-05-06 11:57:24 +02:00
|
|
|
type: Configs.SMTP,
|
2021-05-05 21:58:31 +02:00
|
|
|
})
|
|
|
|
|
2021-05-21 15:55:11 +02:00
|
|
|
// They have set up Google Auth
|
2021-07-13 18:30:17 +02:00
|
|
|
const googleConfig = await getScopedFullConfig(db, {
|
2021-05-21 15:55:11 +02:00
|
|
|
type: Configs.GOOGLE,
|
|
|
|
})
|
|
|
|
|
2021-07-05 15:27:19 +02:00
|
|
|
// They have set up OIDC
|
|
|
|
const oidcConfig = await getScopedFullConfig(db, {
|
|
|
|
type: Configs.OIDC,
|
|
|
|
})
|
2021-08-04 11:02:24 +02:00
|
|
|
// They have set up an admin user
|
2021-05-05 21:58:31 +02:00
|
|
|
const users = await db.allDocs(
|
|
|
|
getGlobalUserParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const adminUser = users.rows.some(row => row.doc.admin)
|
|
|
|
|
2021-05-06 11:57:24 +02:00
|
|
|
ctx.body = {
|
2021-08-04 11:02:24 +02:00
|
|
|
apps: appDbNames.length,
|
2021-05-05 21:58:31 +02:00
|
|
|
smtp: !!smtpConfig,
|
2021-05-06 11:57:24 +02:00
|
|
|
adminUser,
|
2021-07-13 22:46:50 +02:00
|
|
|
sso: !!googleConfig || !!oidcConfig,
|
2021-05-05 21:58:31 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|