mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-15 06:33:55 +01:00
Move hasAdminCapability middleware into helpers
This is so that we can test it more easily than embedding it would allow GitOrigin-RevId: be23d945bc7c816d32b18b4990ecd9e0a6592eb5
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
const Settings = require('@overleaf/settings')
|
||||
const Modules = require('../../infrastructure/Modules')
|
||||
const { expressify } = require('@overleaf/promise-utils')
|
||||
const SessionManager = require('../Authentication/SessionManager')
|
||||
const logger = require('@overleaf/logger')
|
||||
|
||||
module.exports = {
|
||||
hasAdminAccess,
|
||||
canRedirectToAdminDomain,
|
||||
getAdminCapabilities,
|
||||
addHasAdminCapabilityToLocals: expressify(addHasAdminCapabilityToLocals),
|
||||
}
|
||||
|
||||
function hasAdminAccess(user) {
|
||||
@@ -25,6 +29,32 @@ async function getAdminCapabilities(user) {
|
||||
}
|
||||
}
|
||||
|
||||
async function addHasAdminCapabilityToLocals(req, res, next) {
|
||||
const user = SessionManager.getSessionUser(req.session)
|
||||
try {
|
||||
const { adminCapabilities, adminCapabilitiesAvailable } =
|
||||
await getAdminCapabilities(user)
|
||||
res.locals.hasAdminCapability = capability => {
|
||||
if (!hasAdminAccess(user)) {
|
||||
return false
|
||||
}
|
||||
if (!adminCapabilitiesAvailable) {
|
||||
// If admin capabilities are not available, then all admins have all capabilities
|
||||
return true
|
||||
}
|
||||
return adminCapabilities.includes(capability)
|
||||
}
|
||||
} catch (error) {
|
||||
if (user) {
|
||||
// This is unexpected, it probably means that the session user does not exist.
|
||||
logger.warn({ error, req, user }, 'Failed to get admin capabilities')
|
||||
}
|
||||
// A module probably threw so adminCapabilitiesAvailable should be true if we are here so deny to be safe
|
||||
res.locals.hasAdminCapability = () => false
|
||||
}
|
||||
next()
|
||||
}
|
||||
|
||||
function canRedirectToAdminDomain(user) {
|
||||
if (Settings.adminPrivilegeAvailable) return false
|
||||
if (!Settings.adminUrl) return false
|
||||
|
||||
@@ -13,15 +13,14 @@ const PackageVersions = require('./PackageVersions')
|
||||
const Modules = require('./Modules')
|
||||
const Errors = require('../Features/Errors/Errors')
|
||||
const {
|
||||
addHasAdminCapabilityToLocals,
|
||||
canRedirectToAdminDomain,
|
||||
getAdminCapabilities,
|
||||
hasAdminAccess,
|
||||
} = require('../Features/Helpers/AdminAuthorizationHelper')
|
||||
const {
|
||||
addOptionalCleanupHandlerAfterDrainingConnections,
|
||||
} = require('./GracefulShutdown')
|
||||
const { sanitizeSessionUserForFrontEnd } = require('./FrontEndUser')
|
||||
const { expressify } = require('@overleaf/promise-utils')
|
||||
|
||||
const IEEE_BRAND_ID = Settings.ieeeBrandId
|
||||
|
||||
@@ -314,34 +313,7 @@ module.exports = function (webRouter, privateApiRouter, publicApiRouter) {
|
||||
next()
|
||||
})
|
||||
|
||||
webRouter.use(
|
||||
expressify(async function (req, res, next) {
|
||||
const user = SessionManager.getSessionUser(req.session)
|
||||
try {
|
||||
const { adminCapabilities, adminCapabilitiesAvailable } =
|
||||
await getAdminCapabilities(user)
|
||||
res.locals.hasAdminCapability = capability => {
|
||||
if (!hasAdminAccess(user)) {
|
||||
return false
|
||||
}
|
||||
if (!adminCapabilitiesAvailable) {
|
||||
// If admin capabilities are not available, then all admins have all capabilities
|
||||
return true
|
||||
}
|
||||
return adminCapabilities.includes(capability)
|
||||
}
|
||||
} catch (error) {
|
||||
if (user) {
|
||||
// This is unexpected, it probably means that the session user does not exist.
|
||||
logger.warn({ error, req, user }, 'Failed to get admin capabilities')
|
||||
}
|
||||
// adminCapabilitiesAvailable should be true if we are here so deny to be safe
|
||||
res.locals.hasAdminCapability = () => false
|
||||
}
|
||||
|
||||
next()
|
||||
})
|
||||
)
|
||||
webRouter.use(addHasAdminCapabilityToLocals)
|
||||
|
||||
webRouter.use(function (req, res, next) {
|
||||
// Clone the nav settings so they can be modified for each request
|
||||
|
||||
Reference in New Issue
Block a user