2022-09-23 01:59:28 +12:00
|
|
|
import { outputProcessing } from "../../utilities/rowProcessor"
|
|
|
|
import { InternalTables } from "../../db/utils"
|
|
|
|
import { getFullUser } from "../../utilities/users"
|
|
|
|
import { roles, context } from "@budibase/backend-core"
|
2022-09-23 02:18:10 +12:00
|
|
|
import { groups } from "@budibase/pro"
|
|
|
|
|
|
|
|
const PUBLIC_ROLE = roles.BUILTIN_ROLE_IDS.PUBLIC
|
2020-04-08 07:34:21 +12:00
|
|
|
|
2022-04-26 22:28:31 +12:00
|
|
|
/**
|
|
|
|
* Add the attributes that are session based to the current user.
|
|
|
|
*/
|
2022-09-23 01:59:28 +12:00
|
|
|
const addSessionAttributesToUser = (ctx: any) => {
|
2022-04-26 22:28:31 +12:00
|
|
|
if (ctx.user) {
|
|
|
|
ctx.body.license = ctx.user.license
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 01:59:28 +12:00
|
|
|
export async function fetchSelf(ctx: any) {
|
2021-06-04 23:13:29 +12:00
|
|
|
let userId = ctx.user.userId || ctx.user._id
|
2021-03-11 01:20:07 +13:00
|
|
|
/* istanbul ignore next */
|
2022-03-25 02:04:49 +13:00
|
|
|
if (!userId || !ctx.isAuthenticated) {
|
2021-01-29 03:29:35 +13:00
|
|
|
ctx.body = {}
|
2021-02-19 23:32:24 +13:00
|
|
|
return
|
|
|
|
}
|
2021-04-13 22:56:57 +12:00
|
|
|
|
2022-09-24 04:45:26 +12:00
|
|
|
const appId = context.getAppId()
|
2021-04-20 03:26:33 +12:00
|
|
|
const user = await getFullUser(ctx, userId)
|
2021-11-04 11:23:00 +13:00
|
|
|
// this shouldn't be returned by the app self
|
|
|
|
delete user.roles
|
2022-01-26 11:54:50 +13:00
|
|
|
// forward the csrf token from the session
|
|
|
|
user.csrfToken = ctx.user.csrfToken
|
2021-04-14 04:11:55 +12:00
|
|
|
|
2022-09-23 02:18:10 +12:00
|
|
|
if (appId) {
|
2022-09-23 01:59:28 +12:00
|
|
|
const db = context.getAppDB()
|
2022-09-23 02:18:10 +12:00
|
|
|
// check for group permissions
|
|
|
|
if (!user.roleId || user.roleId === PUBLIC_ROLE) {
|
|
|
|
const groupRoleId = await groups.getGroupRoleId(user, appId)
|
|
|
|
user.roleId = groupRoleId || user.roleId
|
|
|
|
}
|
2021-04-14 04:11:55 +12:00
|
|
|
// remove the full roles structure
|
|
|
|
delete user.roles
|
|
|
|
try {
|
|
|
|
const userTable = await db.get(InternalTables.USER_METADATA)
|
|
|
|
// specifically needs to make sure is enriched
|
2022-09-24 04:45:26 +12:00
|
|
|
ctx.body = await outputProcessing(userTable, user)
|
2022-09-23 01:59:28 +12:00
|
|
|
} catch (err: any) {
|
2021-10-22 05:23:10 +13:00
|
|
|
let response
|
2021-10-08 03:49:26 +13:00
|
|
|
// user didn't exist in app, don't pretend they do
|
2022-09-23 02:18:10 +12:00
|
|
|
if (user.roleId === PUBLIC_ROLE) {
|
2021-10-22 05:23:10 +13:00
|
|
|
response = {}
|
2021-10-08 03:49:26 +13:00
|
|
|
}
|
|
|
|
// user has a role of some sort, return them
|
2021-10-22 05:23:10 +13:00
|
|
|
else if (err.status === 404) {
|
|
|
|
const metadata = {
|
2021-11-04 11:23:00 +13:00
|
|
|
...user,
|
2021-10-22 05:23:10 +13:00
|
|
|
_id: userId,
|
|
|
|
}
|
|
|
|
const dbResp = await db.put(metadata)
|
|
|
|
user._rev = dbResp.rev
|
|
|
|
response = user
|
|
|
|
} else {
|
|
|
|
response = user
|
2021-10-08 03:49:26 +13:00
|
|
|
}
|
2021-10-22 05:23:10 +13:00
|
|
|
ctx.body = response
|
2021-04-14 04:11:55 +12:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.body = user
|
2021-01-29 03:29:35 +13:00
|
|
|
}
|
2022-04-26 22:28:31 +12:00
|
|
|
|
|
|
|
addSessionAttributesToUser(ctx)
|
2021-01-29 03:29:35 +13:00
|
|
|
}
|