1
0
Fork 0
mirror of synced 2024-06-27 18:40:42 +12:00
budibase/packages/server/src/api/controllers/user.js

111 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-05-07 21:53:34 +12:00
const CouchDB = require("../../db")
const {
generateUserMetadataID,
getUserMetadataParams,
getEmailFromUserMetadataID,
} = require("../../db/utils")
const { InternalTables } = require("../../db/utils")
const { getRole } = require("../../utilities/security/roles")
const {
getGlobalUsers,
saveGlobalUser,
deleteGlobalUser,
} = require("../../utilities/workerRequests")
const { getFullUser } = require("../../utilities/users")
exports.fetchMetadata = async function(ctx) {
const database = new CouchDB(ctx.appId)
const global = await getGlobalUsers(ctx, ctx.appId)
const metadata = (
await database.allDocs(
getUserMetadataParams(null, {
include_docs: true,
})
)
).rows.map(row => row.doc)
const users = []
for (let user of global) {
const info = metadata.find(meta => meta._id.includes(user.email))
// remove these props, not for the correct DB
delete user._id
delete user._rev
users.push({
...user,
...info,
// make sure the ID is always a local ID, not a global one
_id: generateUserMetadataID(user.email),
})
}
ctx.body = users
2020-05-07 21:53:34 +12:00
}
2020-04-08 04:25:09 +12:00
exports.createMetadata = async function(ctx) {
const appId = ctx.appId
const db = new CouchDB(appId)
2021-04-15 04:02:12 +12:00
const { roleId } = ctx.request.body
const email = ctx.request.body.email || ctx.user.email
// check role valid
const role = await getRole(appId, roleId)
if (!role) ctx.throw(400, "Invalid Role")
2020-05-22 01:31:23 +12:00
const metadata = await saveGlobalUser(ctx, appId, email, ctx.request.body)
2020-05-22 01:31:23 +12:00
const user = {
...metadata,
_id: generateUserMetadataID(email),
2020-05-07 21:53:34 +12:00
type: "user",
tableId: InternalTables.USER_METADATA,
}
2020-05-22 01:31:23 +12:00
const response = await db.post(user)
// for automations to make it obvious was successful
ctx.status = 200
ctx.body = {
_id: response.id,
_rev: response.rev,
email,
2020-04-11 03:37:59 +12:00
}
2020-05-07 21:53:34 +12:00
}
2020-04-10 03:53:48 +12:00
exports.updateMetadata = async function(ctx) {
const appId = ctx.appId
const db = new CouchDB(appId)
2020-06-26 21:05:09 +12:00
const user = ctx.request.body
let email = user.email || getEmailFromUserMetadataID(user._id)
const metadata = await saveGlobalUser(ctx, appId, email, ctx.request.body)
if (!metadata._id) {
metadata._id = generateUserMetadataID(email)
}
if (!metadata._rev) {
metadata._rev = ctx.request.body._rev
}
ctx.body = await db.put({
...metadata,
})
2020-06-26 21:05:09 +12:00
}
exports.destroyMetadata = async function(ctx) {
const db = new CouchDB(ctx.appId)
const email =
ctx.params.email || getEmailFromUserMetadataID(ctx.params.userId)
await deleteGlobalUser(ctx, email)
try {
const dbUser = await db.get(generateUserMetadataID(email))
await db.remove(dbUser._id, dbUser._rev)
} catch (err) {
// error just means the global user has no config in this app
}
ctx.body = {
message: `User ${ctx.params.email} deleted.`,
}
2020-05-15 02:12:30 +12:00
}
exports.findMetadata = async function(ctx) {
ctx.body = await getFullUser({
ctx,
email: ctx.params.email,
userId: ctx.params.userId,
})
2020-05-07 21:53:34 +12:00
}