diff --git a/app/controllers/query_controller.js b/app/controllers/query_controller.js index 28076405..9c8834b1 100644 --- a/app/controllers/query_controller.js +++ b/app/controllers/query_controller.js @@ -15,6 +15,7 @@ const userMiddleware = require('../middlewares/user'); const errorMiddleware = require('../middlewares/error'); const authorizationMiddleware = require('../middlewares/authorization'); const connectionParamsMiddleware = require('../middlewares/connection-params'); +const timeoutLimitsMiddleware = require('../middlewares/timeout-limits'); const { initializeProfilerMiddleware } = require('../middlewares/profiler'); var ONE_YEAR_IN_SECONDS = 31536000; // 1 year time to live by default @@ -33,6 +34,7 @@ QueryController.prototype.route = function (app) { userMiddleware(), authorizationMiddleware(this.metadataBackend), connectionParamsMiddleware(this.userDatabaseService), + timeoutLimitsMiddleware(this.metadataBackend), this.handleQuery.bind(this), errorMiddleware() ]; diff --git a/app/middlewares/connection-params.js b/app/middlewares/connection-params.js index 9357b0bf..9c033a9a 100644 --- a/app/middlewares/connection-params.js +++ b/app/middlewares/connection-params.js @@ -2,8 +2,7 @@ module.exports = function connectionParams (userDatabaseService) { return function connectionParamsMiddleware (req, res, next) { const { user, api_key: apikeyToken, authenticated } = res.locals; - userDatabaseService.getConnectionParams(user, apikeyToken, authenticated, - function (err, userDbParams, authDbParams, userLimits) { + userDatabaseService.getConnectionParams(user, apikeyToken, authenticated, function (err, userDbParams, authDbParams) { if (req.profiler) { req.profiler.done('getConnectionParams'); } @@ -14,7 +13,6 @@ module.exports = function connectionParams (userDatabaseService) { res.locals.userDbParams = userDbParams; res.locals.authDbParams = authDbParams; - res.locals.userLimits = userLimits; next(); }); diff --git a/app/middlewares/timeout-limits.js b/app/middlewares/timeout-limits.js new file mode 100644 index 00000000..cd8076d5 --- /dev/null +++ b/app/middlewares/timeout-limits.js @@ -0,0 +1,19 @@ +module.exports = function timeoutLimits (metadataBackend) { + return function timeoutLimitsMiddleware (req, res, next) { + const { user, authenticated } = res.locals; + + metadataBackend.getUserTimeoutRenderLimits(user, function (err, timeoutRenderLimit) { + if (err) { + return next(err); + } + + const userLimits = { + timeout: authenticated ? timeoutRenderLimit.render : timeoutRenderLimit.renderPublic + }; + + res.locals.userLimits = userLimits; + + next(); + }); + }; +}; diff --git a/app/services/user_database_service.js b/app/services/user_database_service.js index f8bdbbf7..d2027197 100644 --- a/app/services/user_database_service.js +++ b/app/services/user_database_service.js @@ -100,29 +100,14 @@ UserDatabaseService.prototype.getConnectionParams = function (cdbUsername, apike var authDbOpts = _.defaults({ user: user, pass: pass }, dbopts); - return next(null, isAuthenticated, dbopts, authDbOpts); + return next(null, dbopts, authDbOpts); }, - function getUserLimits (err, isAuthenticated, dbopts, authDbOpts) { - var next = this; - - if (err) { - return next(err); - } - - self.getUserLimits(cdbUsername, isAuthenticated, function (err, userLimits) { - if (err) { - return next(err); - } - - next(null, dbopts, authDbOpts, userLimits); - }); - }, - function errorHandle(err, dbopts, authDbOpts, userLimits) { + function errorHandle(err, dbopts, authDbOpts) { if (err) { return callback(err); } - callback(null, dbopts, authDbOpts, userLimits); + callback(null, dbopts, authDbOpts); } ); }; @@ -141,18 +126,4 @@ UserDatabaseService.prototype.getApiKey = function (cdbUsername, apikeyToken, ca }); }; -UserDatabaseService.prototype.getUserLimits = function (cdbUsername, isAuthenticated, callback) { - this.metadataBackend.getUserTimeoutRenderLimits(cdbUsername, function (err, timeoutRenderLimit) { - if (err) { - return callback(err); - } - - var userLimits = { - timeout: isAuthenticated ? timeoutRenderLimit.render : timeoutRenderLimit.renderPublic - }; - - callback(null, userLimits); - }); -}; - module.exports = UserDatabaseService;