Extract to a middleware user timeout limit from user-database-services

This commit is contained in:
Daniel García Aubert 2018-02-22 12:45:55 +01:00
parent afceac7369
commit 700c64bba3
4 changed files with 25 additions and 35 deletions

View File

@ -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()
];

View File

@ -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();
});

View File

@ -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();
});
};
};

View File

@ -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;