2020-05-07 11:53:34 +02:00
|
|
|
const CouchDB = require("../../db")
|
2021-04-08 17:58:33 +02:00
|
|
|
const {
|
|
|
|
generateUserID,
|
|
|
|
getUserParams,
|
|
|
|
getEmailFromUserID,
|
|
|
|
} = require("@budibase/auth")
|
|
|
|
const { InternalTables } = require("../../db/utils")
|
2020-12-02 18:08:25 +01:00
|
|
|
const { getRole } = require("../../utilities/security/roles")
|
2021-04-08 17:58:33 +02:00
|
|
|
const { checkSlashesInUrl } = require("../../utilities")
|
|
|
|
const env = require("../../environment")
|
|
|
|
const fetch = require("node-fetch")
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2021-04-08 17:58:33 +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) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const database = new CouchDB(ctx.appId)
|
2021-04-08 17:58:33 +02:00
|
|
|
const global = await getGlobalUsers()
|
|
|
|
const metadata = (
|
2020-12-08 18:33:08 +01:00
|
|
|
await database.allDocs(
|
|
|
|
getUserParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).rows.map(row => row.doc)
|
2021-04-08 17:58:33 +02:00
|
|
|
const users = []
|
|
|
|
for (let user of global) {
|
|
|
|
const info = metadata.find(meta => meta._id.includes(user.email))
|
|
|
|
users.push({
|
|
|
|
...user,
|
|
|
|
...info,
|
|
|
|
})
|
2020-12-08 18:33:08 +01:00
|
|
|
}
|
|
|
|
ctx.body = users
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2021-04-08 17:58:33 +02:00
|
|
|
exports.createMetadata = async function(ctx) {
|
|
|
|
const appId = ctx.appId
|
|
|
|
const db = new CouchDB(appId)
|
|
|
|
const { email, roleId } = ctx.request.body
|
2020-04-23 15:37:08 +02:00
|
|
|
|
2021-04-08 17:58:33 +02:00
|
|
|
// check role valid
|
|
|
|
const role = await getRole(appId, roleId)
|
2020-12-02 14:20:56 +01:00
|
|
|
if (!role) ctx.throw(400, "Invalid Role")
|
2020-05-21 15:31:23 +02:00
|
|
|
|
2021-04-08 17:58:33 +02:00
|
|
|
const metadata = await saveGlobalUser(appId, email, ctx.request.body)
|
|
|
|
|
2020-05-21 15:31:23 +02:00
|
|
|
const user = {
|
2021-04-08 17:58:33 +02:00
|
|
|
...metadata,
|
2020-12-04 13:22:45 +01:00
|
|
|
_id: generateUserID(email),
|
2020-05-07 11:53:34 +02:00
|
|
|
type: "user",
|
2021-04-08 17:58:33 +02:00
|
|
|
tableId: InternalTables.USER_METADATA,
|
2021-02-22 12:39:58 +01:00
|
|
|
}
|
2020-05-21 15:31:23 +02:00
|
|
|
|
2021-04-08 17:58:33 +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
|
|
|
|
2021-04-08 17:58:33 +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
|
2021-04-08 17:58:33 +02:00
|
|
|
let email = user.email || getEmailFromUserID(user._id)
|
|
|
|
const metadata = await saveGlobalUser(appId, email, ctx.request.body)
|
2020-05-21 15:31:23 +02:00
|
|
|
|
2021-04-08 17:58:33 +02:00
|
|
|
if (!metadata._id) {
|
|
|
|
user._id = generateUserID(email)
|
|
|
|
}
|
|
|
|
ctx.body = await db.put({
|
|
|
|
...metadata,
|
2021-02-22 12:39:58 +01:00
|
|
|
})
|
2020-06-26 11:05:09 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 17:58:33 +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))
|
2021-03-09 18:09:18 +01:00
|
|
|
ctx.body = {
|
|
|
|
message: `User ${ctx.params.email} deleted.`,
|
|
|
|
}
|
2020-05-14 16:12:30 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 17:58:33 +02:00
|
|
|
exports.findMetadata = async function(ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const database = new CouchDB(ctx.appId)
|
2020-12-09 11:52:18 +01:00
|
|
|
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
|
|
|
}
|
2020-12-09 11:52:18 +01:00
|
|
|
ctx.body = user
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|