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,
|
|
|
|
} = require("../../db/utils")
|
2021-04-08 17:58:33 +02:00
|
|
|
const { InternalTables } = require("../../db/utils")
|
2021-05-19 14:17:50 +02:00
|
|
|
const { BUILTIN_ROLE_IDS } = require("@budibase/auth/roles")
|
|
|
|
const { getGlobalUsers } = 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.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-05-19 14:37:59 +02:00
|
|
|
// make sure to always remove some of the global user props
|
2021-05-19 14:17:50 +02:00
|
|
|
delete user.password
|
|
|
|
delete user.roles
|
|
|
|
delete user.builder
|
2021-04-19 17:26:33 +02:00
|
|
|
const metadata = {
|
2021-04-29 20:06:58 +02:00
|
|
|
tableId: InternalTables.USER_METADATA,
|
2021-05-19 14:17:50 +02:00
|
|
|
_id: user._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-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-05-19 14:17:50 +02:00
|
|
|
message: `User metadata ${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
|
|
|
}
|