budibase/packages/server/src/api/controllers/user.js

145 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-05-07 11:53:34 +02:00
const CouchDB = require("../../db")
const {
generateUserID,
getUserParams,
getEmailFromUserID,
} = require("@budibase/auth")
const { InternalTables } = require("../../db/utils")
const { getRole } = require("../../utilities/security/roles")
const { checkSlashesInUrl } = require("../../utilities")
const env = require("../../environment")
const fetch = require("node-fetch")
2020-04-07 18:25:09 +02:00
async function deleteGlobalUser(email) {
const endpoint = `/api/admin/users/${email}`
const reqCfg = { method: "DELETE" }
const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + endpoint),
reqCfg
)
return response.json()
}
async function getGlobalUsers(email = null) {
const endpoint = email ? `/api/admin/users/${email}` : `/api/admin/users`
const reqCfg = { method: "GET" }
const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + endpoint),
reqCfg
)
return response.json()
}
async function saveGlobalUser(appId, email, body) {
const globalUser = await getGlobalUsers(email)
const roles = globalUser.roles || {}
if (body.roleId) {
roles.appId = body.roleId
}
const endpoint = `/api/admin/users`
const reqCfg = {
method: "POST",
body: {
...globalUser,
email,
password: body.password,
status: body.status,
roles,
},
}
const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + endpoint),
reqCfg
)
await response.json()
delete body.email
delete body.password
delete body.roleId
delete body.status
return body
}
exports.fetchMetadata = async function(ctx) {
const database = new CouchDB(ctx.appId)
const global = await getGlobalUsers()
const metadata = (
await database.allDocs(
getUserParams(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))
users.push({
...user,
...info,
})
}
ctx.body = users
2020-05-07 11:53:34 +02:00
}
2020-04-07 18:25:09 +02:00
exports.createMetadata = async function(ctx) {
const appId = ctx.appId
const db = new CouchDB(appId)
const { email, roleId } = ctx.request.body
// check role valid
const role = await getRole(appId, roleId)
if (!role) ctx.throw(400, "Invalid Role")
2020-05-21 15:31:23 +02:00
const metadata = await saveGlobalUser(appId, email, ctx.request.body)
2020-05-21 15:31:23 +02:00
const user = {
...metadata,
2020-12-04 13:22:45 +01:00
_id: generateUserID(email),
2020-05-07 11:53:34 +02:00
type: "user",
tableId: InternalTables.USER_METADATA,
}
2020-05-21 15:31:23 +02:00
const response = await db.post(user)
ctx.body = {
_rev: response.rev,
email,
2020-04-10 17:37:59 +02:00
}
2020-05-07 11:53:34 +02:00
}
2020-04-09 17:53:48 +02:00
exports.updateMetadata = async function(ctx) {
const appId = ctx.appId
const db = new CouchDB(appId)
2020-06-26 11:05:09 +02:00
const user = ctx.request.body
let email = user.email || getEmailFromUserID(user._id)
const metadata = await saveGlobalUser(appId, email, ctx.request.body)
2020-05-21 15:31:23 +02:00
if (!metadata._id) {
user._id = generateUserID(email)
}
ctx.body = await db.put({
...metadata,
})
2020-06-26 11:05:09 +02:00
}
exports.destroyMetadata = async function(ctx) {
const db = new CouchDB(ctx.appId)
const email = ctx.params.email
await deleteGlobalUser(email)
await db.destroy(generateUserID(email))
ctx.body = {
message: `User ${ctx.params.email} deleted.`,
}
2020-05-14 16:12:30 +02:00
}
exports.findMetadata = async function(ctx) {
const database = new CouchDB(ctx.appId)
let lookup = ctx.params.email
? generateUserID(ctx.params.email)
: ctx.params.userId
const user = await database.get(lookup)
if (user) {
delete user.password
2020-04-10 17:37:59 +02:00
}
ctx.body = user
2020-05-07 11:53:34 +02:00
}