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

123 lines
3.6 KiB
JavaScript
Raw Normal View History

2021-06-16 06:39:40 +12:00
const { getAppId, setCookie, getCookie, clearCookie } =
require("@budibase/auth").utils
const { Cookies } = require("@budibase/auth").constants
const { getRole } = require("@budibase/auth/roles")
const { BUILTIN_ROLE_IDS } = require("@budibase/auth/roles")
const { generateUserMetadataID, isDevAppID } = require("../db/utils")
const { dbExists } = require("@budibase/auth/db")
const { isUserInAppTenant } = require("@budibase/auth/tenancy")
2021-07-07 05:10:04 +12:00
const { getCachedSelf } = require("../utilities/global")
const CouchDB = require("../db")
2021-10-07 10:16:50 +13:00
const env = require("../environment")
const { isWebhookEndpoint } = require("./utils")
const { Headers } = require("@budibase/auth/constants")
2021-10-07 10:16:50 +13:00
module.exports = async (ctx, next) => {
// try to get the appID from the request
let requestAppId = getAppId(ctx)
// get app cookie if it exists
let appCookie = null
try {
appCookie = getCookie(ctx, Cookies.CurrentApp)
} catch (err) {
clearCookie(ctx, Cookies.CurrentApp)
}
if (!appCookie && !requestAppId) {
return next()
}
// check the app exists referenced in cookie
if (appCookie) {
const appId = appCookie.appId
const exists = await dbExists(CouchDB, appId)
if (!exists) {
clearCookie(ctx, Cookies.CurrentApp)
return next()
}
// if the request app ID wasn't set, update it with the cookie
requestAppId = requestAppId || appId
}
// deny access to application preview
2021-10-26 21:42:19 +13:00
if (
isDevAppID(requestAppId) &&
!isWebhookEndpoint(ctx) &&
2021-10-26 21:42:19 +13:00
(!ctx.user || !ctx.user.builder || !ctx.user.builder.global)
) {
clearCookie(ctx, Cookies.CurrentApp)
return ctx.redirect("/")
}
let appId,
roleId = BUILTIN_ROLE_IDS.PUBLIC
if (!ctx.user) {
// not logged in, try to set a cookie for public apps
appId = requestAppId
2021-07-07 05:10:04 +12:00
} else if (requestAppId != null) {
// Different App ID means cookie needs reset, or if the same public user has logged in
2021-07-07 05:10:04 +12:00
const globalUser = await getCachedSelf(ctx, requestAppId)
appId = requestAppId
// retrieving global user gets the right role
2021-10-13 02:03:47 +13:00
roleId = globalUser.roleId || roleId
}
2021-10-07 10:16:50 +13:00
// nothing more to do
if (!appId) {
return next()
}
// Allow builders to specify their role via a header
const isBuilder = ctx.user && ctx.user.builder && ctx.user.builder.global
const isDevApp = appId && isDevAppID(appId)
const roleHeader = ctx.request.headers[Headers.PREVIEW_ROLE]
if (isBuilder && isDevApp && roleHeader) {
// Ensure the role is valid ensuring a definition exists
try {
await getRole(appId, roleHeader)
roleId = roleHeader
} catch (error) {
// Swallow error and do nothing
}
}
let noCookieSet = false
// if the user not in the right tenant then make sure they have no permissions
// need to judge this only based on the request app ID,
if (
env.MULTI_TENANCY &&
ctx.user &&
requestAppId &&
!isUserInAppTenant(requestAppId)
) {
// don't error, simply remove the users rights (they are a public user)
delete ctx.user.builder
delete ctx.user.admin
delete ctx.user.roles
roleId = BUILTIN_ROLE_IDS.PUBLIC
noCookieSet = true
2021-10-07 10:16:50 +13:00
}
ctx.appId = appId
if (roleId) {
ctx.roleId = roleId
2021-04-22 22:45:22 +12:00
const userId = ctx.user ? generateUserMetadataID(ctx.user._id) : null
ctx.user = {
...ctx.user,
// override userID with metadata one
_id: userId,
userId,
roleId,
role: await getRole(appId, roleId),
}
}
2021-07-08 11:30:55 +12:00
if (
(requestAppId !== appId ||
appCookie == null ||
appCookie.appId !== requestAppId) &&
!noCookieSet
2021-07-08 11:30:55 +12:00
) {
2021-07-07 05:10:04 +12:00
setCookie(ctx, { appId }, Cookies.CurrentApp)
}
return next()
}