2020-05-07 11:53:34 +02:00
|
|
|
const CouchDB = require("../../db")
|
2021-04-08 17:58:33 +02:00
|
|
|
const {
|
2021-04-09 16:11:49 +02:00
|
|
|
generateUserMetadataID,
|
|
|
|
getUserMetadataParams,
|
2021-04-19 17:26:33 +02:00
|
|
|
getGlobalIDFromUserMetadataID,
|
2021-04-09 16:11:49 +02:00
|
|
|
} = require("../../db/utils")
|
2021-04-08 17:58:33 +02:00
|
|
|
const { InternalTables } = require("../../db/utils")
|
2021-05-14 16:43:41 +02:00
|
|
|
const { getRole, BUILTIN_ROLE_IDS } = require("@budibase/auth/roles")
|
2021-04-09 16:11:49 +02:00
|
|
|
const {
|
|
|
|
getGlobalUsers,
|
|
|
|
saveGlobalUser,
|
|
|
|
deleteGlobalUser,
|
|
|
|
} = require("../../utilities/workerRequests")
|
2021-04-12 16:54:14 +02:00
|
|
|
const { getFullUser } = require("../../utilities/users")
|
2021-04-08 17:58:33 +02:00
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.fetchMetadata = async function (ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const database = new CouchDB(ctx.appId)
|
2021-04-09 16:11:49 +02:00
|
|
|
const global = await getGlobalUsers(ctx, ctx.appId)
|
2021-04-08 17:58:33 +02:00
|
|
|
const metadata = (
|
2020-12-08 18:33:08 +01:00
|
|
|
await database.allDocs(
|
2021-04-09 16:11:49 +02:00
|
|
|
getUserMetadataParams(null, {
|
2020-12-08 18:33:08 +01:00
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2021-05-04 12:32:22 +02:00
|
|
|
).rows.map(row => row.doc)
|
2021-04-08 17:58:33 +02:00
|
|
|
const users = []
|
|
|
|
for (let user of global) {
|
2021-04-19 17:26:33 +02:00
|
|
|
// find the metadata that matches up to the global ID
|
2021-05-04 12:32:22 +02:00
|
|
|
const info = metadata.find(meta => meta._id.includes(user._id))
|
2021-04-13 18:11:55 +02:00
|
|
|
// remove these props, not for the correct DB
|
2021-04-08 17:58:33 +02:00
|
|
|
users.push({
|
|
|
|
...user,
|
|
|
|
...info,
|
2021-04-09 17:55:56 +02:00
|
|
|
// make sure the ID is always a local ID, not a global one
|
2021-04-19 17:26:33 +02:00
|
|
|
_id: generateUserMetadataID(user._id),
|
2021-04-08 17:58:33 +02:00
|
|
|
})
|
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-05-03 09:31:09 +02:00
|
|
|
exports.createMetadata = async function (ctx) {
|
2021-04-08 17:58:33 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
const db = new CouchDB(appId)
|
2021-04-14 18:02:12 +02:00
|
|
|
const { roleId } = ctx.request.body
|
2020-04-23 15:37:08 +02:00
|
|
|
|
2021-04-20 18:17:44 +02:00
|
|
|
if (ctx.request.body._id) {
|
|
|
|
return exports.updateMetadata(ctx)
|
|
|
|
}
|
|
|
|
|
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-19 17:26:33 +02:00
|
|
|
const globalUser = await saveGlobalUser(ctx, appId, ctx.request.body)
|
2021-04-08 17:58:33 +02:00
|
|
|
|
2020-05-21 15:31:23 +02:00
|
|
|
const user = {
|
2021-04-19 17:26:33 +02:00
|
|
|
...globalUser,
|
|
|
|
_id: generateUserMetadataID(globalUser._id),
|
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)
|
2021-04-09 18:33:21 +02:00
|
|
|
// for automations to make it obvious was successful
|
|
|
|
ctx.status = 200
|
2021-04-08 17:58:33 +02:00
|
|
|
ctx.body = {
|
2021-04-09 18:33:21 +02:00
|
|
|
_id: response.id,
|
2021-04-08 17:58:33 +02:00
|
|
|
_rev: response.rev,
|
2021-04-19 17:26:33 +02:00
|
|
|
email: ctx.request.body.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-05-03 09:31:09 +02:00
|
|
|
exports.updateSelfMetadata = async function (ctx) {
|
2021-04-20 18:17:44 +02:00
|
|
|
// overwrite the ID with current users
|
2021-04-22 12:45:22 +02:00
|
|
|
ctx.request.body._id = ctx.user._id
|
2021-05-13 19:10:09 +02:00
|
|
|
if (ctx.user.builder && ctx.user.builder.global) {
|
|
|
|
ctx.request.body.roleId = BUILTIN_ROLE_IDS.ADMIN
|
|
|
|
}
|
2021-04-20 18:17:44 +02:00
|
|
|
// make sure no stale rev
|
|
|
|
delete ctx.request.body._rev
|
|
|
|
await exports.updateMetadata(ctx)
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.updateMetadata = async function (ctx) {
|
2021-04-08 17:58:33 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
const db = new CouchDB(appId)
|
2020-06-26 11:05:09 +02:00
|
|
|
const user = ctx.request.body
|
2021-04-20 18:17:44 +02:00
|
|
|
const globalUser = await saveGlobalUser(ctx, appId, {
|
|
|
|
...user,
|
|
|
|
_id: getGlobalIDFromUserMetadataID(user._id),
|
|
|
|
})
|
2021-04-19 17:26:33 +02:00
|
|
|
const metadata = {
|
|
|
|
...globalUser,
|
2021-04-29 20:06:58 +02:00
|
|
|
tableId: InternalTables.USER_METADATA,
|
2021-04-19 17:26:33 +02:00
|
|
|
_id: user._id || generateUserMetadataID(globalUser._id),
|
2021-04-20 18:17:44 +02:00
|
|
|
_rev: user._rev,
|
2021-04-08 17:58:33 +02:00
|
|
|
}
|
2021-04-19 17:26:33 +02:00
|
|
|
ctx.body = await db.put(metadata)
|
2020-06-26 11:05:09 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.destroyMetadata = async function (ctx) {
|
2021-04-08 17:58:33 +02:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2021-04-19 17:26:33 +02:00
|
|
|
await deleteGlobalUser(ctx, getGlobalIDFromUserMetadataID(ctx.params.id))
|
2021-04-09 17:55:56 +02:00
|
|
|
try {
|
2021-04-19 17:26:33 +02:00
|
|
|
const dbUser = await db.get(ctx.params.id)
|
2021-04-09 17:55:56 +02:00
|
|
|
await db.remove(dbUser._id, dbUser._rev)
|
|
|
|
} catch (err) {
|
|
|
|
// error just means the global user has no config in this app
|
|
|
|
}
|
2021-03-09 18:09:18 +01:00
|
|
|
ctx.body = {
|
2021-04-19 17:26:33 +02:00
|
|
|
message: `User ${ctx.params.id} deleted.`,
|
2021-03-09 18:09:18 +01:00
|
|
|
}
|
2020-05-14 16:12:30 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.findMetadata = async function (ctx) {
|
2021-04-19 17:26:33 +02:00
|
|
|
ctx.body = await getFullUser(ctx, ctx.params.id)
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|