group CRUD

This commit is contained in:
Martin McKeaveney 2021-04-19 16:16:46 +01:00
parent 07ac411b12
commit 657ae2b554
6 changed files with 76 additions and 48 deletions

View File

@ -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 = {}) => { exports.getUserParams = (email = "", otherProps = {}) => {
if (!email) { if (!email) {

View File

@ -51,6 +51,8 @@ module.exports = {
StaticDatabases, StaticDatabases,
generateUserID, generateUserID,
getUserParams, getUserParams,
generateGroupID,
getGroupParams,
getEmailFromUserID, getEmailFromUserID,
hash, hash,
compare, compare,

View File

@ -1,36 +1,58 @@
const CouchDB = require("../../../db") const CouchDB = require("../../../db")
const { const { getGroupParams, StaticDatabases } = require("@budibase/auth")
hash, const { generateGroupID } = require("@budibase/auth")
generateUserID,
getUserParams,
StaticDatabases,
} = require("@budibase/auth")
const { UserStatus } = require("../../../constants")
const USER_DB = StaticDatabases.USER.name const GLOBAL_DB = StaticDatabases.GLOBAL.name
exports.save = async function(ctx, next) { exports.save = async function(ctx) {
const db = new CouchDB(USER_DB) const db = new CouchDB(GLOBAL_DB)
const doc = ctx.request.body const groupDoc = ctx.request.body
const groupDoc = { // Group does not exist yet
users: ["us:1234", "us:1234"], if (!groupDoc._id) {
managers: ["us:1234"], groupDoc._id = generateGroupID()
defaultRole: "BASIC", }
apps: {
abc123: "ADMIN", 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) { exports.fetch = async function(ctx) {
const db = new CouchDB(USER_DB) 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) { exports.find = async function(ctx) {
const db = new CouchDB(USER_DB) 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) { exports.destroy = async function(ctx) {
const db = new CouchDB(USER_DB) 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)
}
} }

View File

@ -9,20 +9,19 @@ const router = Router()
function buildGroupSaveValidation() { function buildGroupSaveValidation() {
// prettier-ignore // prettier-ignore
return joiValidator.body(Joi.object({ return joiValidator.body(Joi.object({
// _id: Joi.string(), _id: Joi.string().optional(),
// _rev: Joi.string(), _rev: Joi.string().optional(),
// email: Joi.string(), name: Joi.string().required(),
// password: Joi.string().allow(null, ""), users: Joi.array().required(),
// builder: Joi.object({ managers: Joi.array().required(),
// global: Joi.boolean().optional(), roles: Joi.object({
// apps: Joi.array().optional(), default: Joi.string().optional(),
// }).unknown(true).optional(), app: Joi.object()
// // maps appId -> roleId for the user .pattern(/.*/, Joi.string())
// roles: Joi.object() .required()
// .pattern(/.*/, Joi.string()) .unknown(true),
// .required() }).unknown(true).optional(),
// .unknown(true) }).required().unknown(true))
}).required().unknown(true).optional())
} }
router router
@ -34,6 +33,6 @@ router
) )
.delete("/api/admin/groups/:id", authenticated, controller.destroy) .delete("/api/admin/groups/:id", authenticated, controller.destroy)
.get("/api/admin/groups", authenticated, controller.fetch) .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 module.exports = router

View File

@ -1,7 +0,0 @@
const groups = require("./groups")
const users = require("./users")
module.exports = {
groups,
users,
}

View File

@ -1,4 +1,5 @@
const { userRoutes, groupRoutes } = require("./admin") const userRoutes = require("./admin/users")
const groupRoutes = require("./admin/groups")
const authRoutes = require("./auth") const authRoutes = require("./auth")
const appRoutes = require("./app") const appRoutes = require("./app")