From 657ae2b554c21ac1aca888b80de3bbc91c716404 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 19 Apr 2021 16:16:46 +0100 Subject: [PATCH] group CRUD --- packages/auth/src/db/utils.js | 13 +++- packages/auth/src/index.js | 2 + .../src/api/controllers/admin/groups.js | 70 ++++++++++++------- .../worker/src/api/routes/admin/groups.js | 29 ++++---- packages/worker/src/api/routes/admin/index.js | 7 -- packages/worker/src/api/routes/index.js | 3 +- 6 files changed, 76 insertions(+), 48 deletions(-) delete mode 100644 packages/worker/src/api/routes/admin/index.js diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index 56e1ea2154..89aedb60bf 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -41,7 +41,18 @@ exports.generateGroupID = () => { } /** - * Gets parameters for retrieving users, this is a utility function for the getDocParams function. + * Gets parameters for retrieving groups. + */ +exports.getGroupParams = (id = "", otherProps = {}) => { + return { + ...otherProps, + startkey: `${DocumentTypes.GROUP}${SEPARATOR}${id}`, + endkey: `${DocumentTypes.GROUP}${SEPARATOR}${id}${UNICODE_MAX}`, + } +} + +/** + * Gets parameters for retrieving users. */ exports.getUserParams = (email = "", otherProps = {}) => { if (!email) { diff --git a/packages/auth/src/index.js b/packages/auth/src/index.js index 85b60e4f25..455181d538 100644 --- a/packages/auth/src/index.js +++ b/packages/auth/src/index.js @@ -51,6 +51,8 @@ module.exports = { StaticDatabases, generateUserID, getUserParams, + generateGroupID, + getGroupParams, getEmailFromUserID, hash, compare, diff --git a/packages/worker/src/api/controllers/admin/groups.js b/packages/worker/src/api/controllers/admin/groups.js index 31a1b66e8e..3642c2464d 100644 --- a/packages/worker/src/api/controllers/admin/groups.js +++ b/packages/worker/src/api/controllers/admin/groups.js @@ -1,36 +1,58 @@ const CouchDB = require("../../../db") -const { - hash, - generateUserID, - getUserParams, - StaticDatabases, -} = require("@budibase/auth") -const { UserStatus } = require("../../../constants") +const { getGroupParams, StaticDatabases } = require("@budibase/auth") +const { generateGroupID } = require("@budibase/auth") -const USER_DB = StaticDatabases.USER.name +const GLOBAL_DB = StaticDatabases.GLOBAL.name -exports.save = async function(ctx, next) { - const db = new CouchDB(USER_DB) - const doc = ctx.request.body +exports.save = async function(ctx) { + const db = new CouchDB(GLOBAL_DB) + const groupDoc = ctx.request.body - const groupDoc = { - users: ["us:1234", "us:1234"], - managers: ["us:1234"], - defaultRole: "BASIC", - apps: { - abc123: "ADMIN", - }, + // Group does not exist yet + if (!groupDoc._id) { + groupDoc._id = generateGroupID() + } + + try { + const response = await db.post(groupDoc) + ctx.body = { + _id: response.id, + _rev: response.rev, + } + } catch (err) { + ctx.throw(err.status, err) } } -exports.fetch = async function(ctx, next) { - const db = new CouchDB(USER_DB) +exports.fetch = async function(ctx) { + const db = new CouchDB(GLOBAL_DB) + const response = await db.allDocs( + getGroupParams(undefined, { + include_docs: true, + }) + ) + const groups = response.rows.map(row => row.doc) + ctx.body = groups } -exports.find = async function(ctx, next) { - const db = new CouchDB(USER_DB) +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, next) { - const db = new CouchDB(USER_DB) +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: "Group deleted successfully" } + } catch (err) { + ctx.throw(err.status, err) + } } diff --git a/packages/worker/src/api/routes/admin/groups.js b/packages/worker/src/api/routes/admin/groups.js index ca8c675dc7..e683a01c2b 100644 --- a/packages/worker/src/api/routes/admin/groups.js +++ b/packages/worker/src/api/routes/admin/groups.js @@ -9,20 +9,19 @@ const router = Router() function buildGroupSaveValidation() { // prettier-ignore return joiValidator.body(Joi.object({ - // _id: Joi.string(), - // _rev: Joi.string(), - // email: Joi.string(), - // password: Joi.string().allow(null, ""), - // builder: Joi.object({ - // global: Joi.boolean().optional(), - // apps: Joi.array().optional(), - // }).unknown(true).optional(), - // // maps appId -> roleId for the user - // roles: Joi.object() - // .pattern(/.*/, Joi.string()) - // .required() - // .unknown(true) - }).required().unknown(true).optional()) + _id: Joi.string().optional(), + _rev: Joi.string().optional(), + name: Joi.string().required(), + users: Joi.array().required(), + managers: Joi.array().required(), + roles: Joi.object({ + default: Joi.string().optional(), + app: Joi.object() + .pattern(/.*/, Joi.string()) + .required() + .unknown(true), + }).unknown(true).optional(), + }).required().unknown(true)) } router @@ -34,6 +33,6 @@ router ) .delete("/api/admin/groups/:id", authenticated, controller.destroy) .get("/api/admin/groups", authenticated, controller.fetch) - .get("/api/admin/group/:id", authenticated, controller.find) + .get("/api/admin/groups/:id", authenticated, controller.find) module.exports = router diff --git a/packages/worker/src/api/routes/admin/index.js b/packages/worker/src/api/routes/admin/index.js deleted file mode 100644 index ce52a85d05..0000000000 --- a/packages/worker/src/api/routes/admin/index.js +++ /dev/null @@ -1,7 +0,0 @@ -const groups = require("./groups") -const users = require("./users") - -module.exports = { - groups, - users, -} diff --git a/packages/worker/src/api/routes/index.js b/packages/worker/src/api/routes/index.js index d4a8cfc073..5c6b088443 100644 --- a/packages/worker/src/api/routes/index.js +++ b/packages/worker/src/api/routes/index.js @@ -1,4 +1,5 @@ -const { userRoutes, groupRoutes } = require("./admin") +const userRoutes = require("./admin/users") +const groupRoutes = require("./admin/groups") const authRoutes = require("./auth") const appRoutes = require("./app")