group CRUD

This commit is contained in:
Martin McKeaveney 2021-04-19 16:16:46 +01:00
parent 390b43c122
commit 4b5d302819
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 = {}) => {
if (!email) {

View File

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

View File

@ -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)
}
}

View File

@ -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

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 appRoutes = require("./app")