2022-11-22 14:56:01 +01:00
|
|
|
import { InternalTables } from "../db/utils"
|
|
|
|
import { getGlobalUser } from "./global"
|
|
|
|
import { context, db as dbCore, roles } from "@budibase/backend-core"
|
|
|
|
import { BBContext } from "@budibase/types"
|
2021-04-12 16:54:14 +02:00
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export async function getFullUser(ctx: BBContext, userId: string) {
|
2022-01-27 19:18:31 +01:00
|
|
|
const global = await getGlobalUser(userId)
|
2022-11-22 14:56:01 +01:00
|
|
|
let metadata: any = {}
|
2022-10-11 13:42:35 +02:00
|
|
|
|
|
|
|
// always prefer the user metadata _id and _rev
|
|
|
|
delete global._id
|
|
|
|
delete global._rev
|
|
|
|
|
2021-04-14 17:00:58 +02:00
|
|
|
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-11-22 14:56:01 +01:00
|
|
|
const db = context.getAppDB()
|
2021-04-19 17:26:33 +02:00
|
|
|
metadata = await db.get(userId)
|
2021-04-14 17:00:58 +02:00
|
|
|
} catch (err) {
|
2022-10-11 13:42:35 +02:00
|
|
|
// it is fine if there is no user metadata yet
|
2021-04-14 17:00:58 +02:00
|
|
|
}
|
2022-09-23 18:45:26 +02:00
|
|
|
delete metadata.csrfToken
|
2021-04-12 16:54:14 +02:00
|
|
|
return {
|
2021-04-14 17:00:58 +02:00
|
|
|
...metadata,
|
2022-09-23 18:45:26 +02:00
|
|
|
...global,
|
2022-11-22 14:56:01 +01:00
|
|
|
roleId: global.roleId || roles.BUILTIN_ROLE_IDS.PUBLIC,
|
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
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export function publicApiUserFix(ctx: BBContext) {
|
2022-05-22 19:29:02 +02:00
|
|
|
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 {
|
2022-11-22 14:56:01 +01:00
|
|
|
const newRoles: { [key: string]: any } = {}
|
2022-05-22 19:29:02 +02:00
|
|
|
for (let [appId, role] of Object.entries(ctx.request.body.roles)) {
|
2022-11-22 14:56:01 +01:00
|
|
|
newRoles[dbCore.getProdAppID(appId)] = role
|
2022-05-22 19:29:02 +02:00
|
|
|
}
|
|
|
|
ctx.request.body.roles = newRoles
|
|
|
|
}
|
|
|
|
return ctx
|
|
|
|
}
|