mirror of https://github.com/Budibase/budibase.git
6 changed files with 79 additions and 51 deletions
@ -1,36 +1,58 @@ |
|||
const CouchDB = require("../../../db") |
|||
const { |
|||
hash, |
|||
generateUserID, |
|||
getUserParams, |
|||
StaticDatabases, |
|||
} = require("@budibase/auth") |
|||
const { UserStatus } = require("../../../constants") |
|||
|
|||
const USER_DB = StaticDatabases.USER.name |
|||
|
|||
exports.save = async function(ctx, next) { |
|||
const db = new CouchDB(USER_DB) |
|||
const doc = ctx.request.body |
|||
|
|||
const groupDoc = { |
|||
users: ["us:1234", "us:1234"], |
|||
managers: ["us:1234"], |
|||
defaultRole: "BASIC", |
|||
apps: { |
|||
abc123: "ADMIN", |
|||
}, |
|||
const { getGroupParams, StaticDatabases } = require("@budibase/auth") |
|||
const { generateGroupID } = require("@budibase/auth") |
|||
|
|||
const GLOBAL_DB = StaticDatabases.GLOBAL.name |
|||
|
|||
exports.save = async function(ctx) { |
|||
const db = new CouchDB(GLOBAL_DB) |
|||
const groupDoc = ctx.request.body |
|||
|
|||
// 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) |
|||
} |
|||
} |
|||
|
|||
@ -1,7 +0,0 @@ |
|||
const groups = require("./groups") |
|||
const users = require("./users") |
|||
|
|||
module.exports = { |
|||
groups, |
|||
users, |
|||
} |
|||
Loading…
Reference in new issue