Updating current app to set up role in middleware.
This commit is contained in:
parent
d82516d949
commit
02c9f69143
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue