Updating current app to set up role in middleware.

This commit is contained in:
mike12345567 2021-04-13 14:35:00 +01:00
parent d82516d949
commit 02c9f69143
1 changed files with 15 additions and 18 deletions

View File

@ -1,20 +1,8 @@
const { getAppId, setCookie, getCookie, Cookies } = require("@budibase/auth")
const { getRole } = require("../utilities/security/roles")
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)
@ -44,9 +32,18 @@ module.exports = async (ctx, next) => {
appId = appCookie.appId
roleId = appCookie.roleId || BUILTIN_ROLE_IDS.PUBLIC
}
return finish(ctx, next, {
appId: appId,
roleId: roleId,
cookie: updateCookie,
})
if (appId) {
ctx.appId = appId
if (roleId) {
ctx.roleId = roleId
ctx.user = {
...ctx.user,
role: await getRole(appId, roleId),
}
}
}
if (updateCookie && appId) {
setCookie(ctx, { appId, roleId }, Cookies.CurrentApp)
}
return next()
}