@ -5,6 +5,7 @@ const {
} = require ( "@budibase/backend-core/roles" )
const {
PermissionTypes ,
PermissionLevels ,
doesHaveBasePermission ,
} = require ( "@budibase/backend-core/permissions" )
const builderMiddleware = require ( "./builder" )
@ -64,7 +65,7 @@ const checkAuthorizedResource = async (
}
module . exports =
( permType , permLevel = null ) =>
( permType , permLevel = null , opts = { schema : false } ) =>
async ( ctx , next ) => {
// webhooks don't need authentication, each webhook unique
// also internal requests (between services) don't need authorized
@ -81,15 +82,25 @@ module.exports =
await builderMiddleware ( ctx , permType )
// get the resource roles
let resourceRoles = [ ]
let resourceRoles = [ ] ,
otherLevelRoles
const otherLevel =
permLevel === PermissionLevels . READ
? PermissionLevels . WRITE
: PermissionLevels . READ
const appId = getAppId ( )
if ( appId && hasResource ( ctx ) ) {
resourceRoles = await getRequiredResourceRole ( permLevel , ctx )
if ( opts && opts . schema ) {
otherLevelRoles = await getRequiredResourceRole ( otherLevel , ctx )
}
}
// if the resource is public, proceed
const isPublicResource = resourceRoles . includes ( BUILTIN_ROLE_IDS . PUBLIC )
if ( isPublicResource ) {
if (
resourceRoles . includes ( BUILTIN_ROLE_IDS . PUBLIC ) ||
( otherLevelRoles && otherLevelRoles . includes ( BUILTIN_ROLE_IDS . PUBLIC ) )
) {
return next ( )
}
@ -98,8 +109,17 @@ module.exports =
return ctx . throw ( 403 , "Session not authenticated" )
}
// check authorized
await checkAuthorized ( ctx , resourceRoles , permType , permLevel )
try {
// check authorized
await checkAuthorized ( ctx , resourceRoles , permType , permLevel )
} catch ( err ) {
// this is a schema, check if
if ( opts && opts . schema && permLevel ) {
await checkAuthorized ( ctx , otherLevelRoles , permType , otherLevel )
} else {
throw err
}
}
// csrf protection
return csrf ( ctx , next )