config creation and management APIs
This commit is contained in:
parent
b5e19e4216
commit
301f681c88
|
@ -10,6 +10,7 @@ const DocumentTypes = {
|
|||
USER: "us",
|
||||
APP: "app",
|
||||
GROUP: "group",
|
||||
CONFIG: "config",
|
||||
}
|
||||
|
||||
exports.DocumentTypes = DocumentTypes
|
||||
|
@ -52,7 +53,7 @@ exports.getGroupParams = (id = "", otherProps = {}) => {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets parameters for retrieving users.
|
||||
* Gets parameters for retrieving users, this is a utility function for the getDocParams function.
|
||||
*/
|
||||
exports.getUserParams = (email = "", otherProps = {}) => {
|
||||
if (!email) {
|
||||
|
@ -64,3 +65,26 @@ exports.getUserParams = (email = "", otherProps = {}) => {
|
|||
endkey: `${DocumentTypes.USER}${SEPARATOR}${email}${UNICODE_MAX}`,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new configuration ID.
|
||||
* @returns {string} The new configuration ID which the config doc can be stored under.
|
||||
*/
|
||||
exports.generateConfigID = (type = "", group = "") => {
|
||||
group += SEPARATOR
|
||||
|
||||
return `${
|
||||
DocumentTypes.CONFIG
|
||||
}${SEPARATOR}${type}${SEPARATOR}${group}${newid()}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets parameters for retrieving configurations.
|
||||
*/
|
||||
exports.getConfigParams = (type = "", group = "", otherProps = {}) => {
|
||||
return {
|
||||
...otherProps,
|
||||
startkey: `${DocumentTypes.CONFIG}${SEPARATOR}${type}${SEPARATOR}${group}`,
|
||||
endkey: `${DocumentTypes.CONFIG}${SEPARATOR}${type}${SEPARATOR}${group}${UNICODE_MAX}`,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,8 @@ const {
|
|||
generateGroupID,
|
||||
getGroupParams,
|
||||
getEmailFromUserID,
|
||||
generateConfigID,
|
||||
getConfigParams,
|
||||
} = require("./db/utils")
|
||||
|
||||
// Strategies
|
||||
|
@ -54,6 +56,8 @@ module.exports = {
|
|||
generateGroupID,
|
||||
getGroupParams,
|
||||
getEmailFromUserID,
|
||||
generateConfigID,
|
||||
getConfigParams,
|
||||
hash,
|
||||
compare,
|
||||
getAppId,
|
||||
|
|
|
@ -6,7 +6,13 @@ exports.options = {
|
|||
callbackURL: env.GOOGLE_AUTH_CALLBACK_URL,
|
||||
}
|
||||
|
||||
// exports.authenticate = async function(token, tokenSecret, profile, done) {
|
||||
// // retrieve user ...
|
||||
// fetchUser().then(user => done(null, user))
|
||||
// }
|
||||
exports.authenticate = async function(token, tokenSecret, profile, done) {
|
||||
console.log({
|
||||
token,
|
||||
tokenSecret,
|
||||
profile,
|
||||
done,
|
||||
})
|
||||
// retrieve user ...
|
||||
// fetchUser().then(user => done(null, user))
|
||||
}
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
const CouchDB = require("../../../db")
|
||||
const { StaticDatabases } = require("@budibase/auth")
|
||||
const { generateConfigID } = require("@budibase/auth")
|
||||
const { getConfigParams } = require("@budibase/auth/src/db/utils")
|
||||
|
||||
const GLOBAL_DB = StaticDatabases.GLOBAL.name
|
||||
|
||||
exports.save = async function(ctx) {
|
||||
const db = new CouchDB(GLOBAL_DB)
|
||||
const configDoc = ctx.request.body
|
||||
|
||||
// Config does not exist yet
|
||||
if (!configDoc._id) {
|
||||
configDoc._id = generateConfigID(configDoc.type, configDoc.group)
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await db.post(configDoc)
|
||||
ctx.body = {
|
||||
type: configDoc.type,
|
||||
_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(
|
||||
getConfigParams(undefined, {
|
||||
include_docs: true,
|
||||
})
|
||||
)
|
||||
const groups = response.rows.map(row => row.doc)
|
||||
ctx.body = groups
|
||||
}
|
||||
|
||||
exports.find = async function(ctx) {
|
||||
const db = new CouchDB(GLOBAL_DB)
|
||||
try {
|
||||
const record = await db.get(ctx.params.id)
|
||||
ctx.body = record
|
||||
} 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)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
const Router = require("@koa/router")
|
||||
const controller = require("../../controllers/admin/configs")
|
||||
const joiValidator = require("../../../middleware/joi-validator")
|
||||
const { authenticated } = require("@budibase/auth")
|
||||
const Joi = require("joi")
|
||||
const { Configs } = require("../../../constants")
|
||||
|
||||
const router = Router()
|
||||
|
||||
function buildConfigSaveValidation() {
|
||||
// prettier-ignore
|
||||
return joiValidator.body(Joi.object({
|
||||
type: Joi.string().valid(...Object.values(Configs)).required()
|
||||
}).required().unknown(true))
|
||||
}
|
||||
|
||||
router
|
||||
.post(
|
||||
"/api/admin/configs",
|
||||
buildConfigSaveValidation(),
|
||||
authenticated,
|
||||
controller.save
|
||||
)
|
||||
.delete("/api/admin/configs/:id", authenticated, controller.destroy)
|
||||
.get("/api/admin/configs", authenticated, controller.fetch)
|
||||
.get("/api/admin/configs/:id", authenticated, controller.find)
|
||||
|
||||
module.exports = router
|
|
@ -1,6 +1,7 @@
|
|||
const userRoutes = require("./admin/users")
|
||||
const configRoutes = require("./admin/configs")
|
||||
const groupRoutes = require("./admin/groups")
|
||||
const authRoutes = require("./auth")
|
||||
const appRoutes = require("./app")
|
||||
|
||||
exports.routes = [userRoutes, groupRoutes, authRoutes, appRoutes]
|
||||
exports.routes = [configRoutes, userRoutes, groupRoutes, authRoutes, appRoutes]
|
||||
|
|
|
@ -6,3 +6,9 @@ exports.UserStatus = {
|
|||
exports.Groups = {
|
||||
ALL_USERS: "all_users",
|
||||
}
|
||||
|
||||
exports.Configs = {
|
||||
SETTINGS: "settings",
|
||||
ACCOUNT: "account",
|
||||
SMTP: "smtp",
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue