2021-06-08 17:06:30 +02:00
|
|
|
const { InternalTables } = require("../db/utils")
|
|
|
|
const { getGlobalUser } = require("../utilities/global")
|
2022-01-27 19:18:31 +01:00
|
|
|
const { getAppDB } = require("@budibase/backend-core/context")
|
2022-05-22 19:29:02 +02:00
|
|
|
const { getProdAppID } = require("@budibase/backend-core/db")
|
2021-04-12 16:54:14 +02:00
|
|
|
|
2021-04-19 17:26:33 +02:00
|
|
|
exports.getFullUser = async (ctx, userId) => {
|
2022-01-27 19:18:31 +01:00
|
|
|
const global = await getGlobalUser(userId)
|
2021-04-14 17:00:58 +02:00
|
|
|
let metadata
|
|
|
|
try {
|
2021-04-15 16:57:55 +02:00
|
|
|
// this will throw an error if the db doesn't exist, or there is no appId
|
2022-01-27 19:18:31 +01:00
|
|
|
const db = getAppDB()
|
2021-04-19 17:26:33 +02:00
|
|
|
metadata = await db.get(userId)
|
2021-04-14 17:00:58 +02:00
|
|
|
} catch (err) {
|
|
|
|
// it is fine if there is no user metadata, just remove global db info
|
|
|
|
delete global._id
|
|
|
|
delete global._rev
|
|
|
|
}
|
2021-04-12 16:54:14 +02:00
|
|
|
return {
|
|
|
|
...global,
|
2021-04-14 17:00:58 +02:00
|
|
|
...metadata,
|
2021-05-19 17:24:20 +02:00
|
|
|
tableId: InternalTables.USER_METADATA,
|
2021-04-12 16:54:14 +02:00
|
|
|
// make sure the ID is always a local ID, not a global one
|
2021-04-19 17:26:33 +02:00
|
|
|
_id: userId,
|
2021-04-12 16:54:14 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-22 19:29:02 +02:00
|
|
|
|
|
|
|
exports.publicApiUserFix = ctx => {
|
|
|
|
if (!ctx.request.body) {
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
if (!ctx.request.body._id && ctx.params.userId) {
|
|
|
|
ctx.request.body._id = ctx.params.userId
|
|
|
|
}
|
|
|
|
if (!ctx.request.body.roles) {
|
|
|
|
ctx.request.body.roles = {}
|
|
|
|
} else {
|
|
|
|
const newRoles = {}
|
|
|
|
for (let [appId, role] of Object.entries(ctx.request.body.roles)) {
|
|
|
|
// @ts-ignore
|
|
|
|
newRoles[getProdAppID(appId)] = role
|
|
|
|
}
|
|
|
|
ctx.request.body.roles = newRoles
|
|
|
|
}
|
|
|
|
return ctx
|
|
|
|
}
|