1
0
Fork 0
mirror of synced 2024-06-30 03:50:37 +12:00
budibase/packages/server/src/middleware/currentapp.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

const { getAppId, setCookie, getCookie, Cookies } = require("@budibase/auth")
const { getGlobalUsers } = require("../utilities/workerRequests")
const { BUILTIN_ROLE_IDS } = require("../utilities/security/roles")
function finish(ctx, next, { appId, roleId, cookie = false }) {
if (appId) {
ctx.appId = appId
}
if (roleId) {
ctx.roleId = roleId
}
if (cookie && appId) {
setCookie(ctx, { appId, roleId }, Cookies.CurrentApp)
}
return next()
}
module.exports = async (ctx, next) => {
// try to get the appID from the request
const requestAppId = getAppId(ctx)
// get app cookie if it exists
const appCookie = getCookie(ctx, Cookies.CurrentApp)
if (!appCookie && !requestAppId) {
return next()
}
let updateCookie = false,
appId,
roleId
if (!ctx.user) {
// not logged in, try to set a cookie for public apps
updateCookie = true
appId = requestAppId
roleId = BUILTIN_ROLE_IDS.PUBLIC
} else if (
requestAppId != null &&
(appCookie == null || requestAppId !== appCookie.appId)
) {
const globalUser = await getGlobalUsers(ctx, requestAppId, ctx.user.email)
updateCookie = true
appId = requestAppId
roleId = globalUser.roles[requestAppId] || BUILTIN_ROLE_IDS.PUBLIC
} else if (appCookie != null) {
appId = appCookie.appId
roleId = appCookie.roleId || BUILTIN_ROLE_IDS.PUBLIC
}
return finish(ctx, next, {
appId: appId,
roleId: roleId,
cookie: updateCookie,
})
}