from req.context to res.locals

This commit is contained in:
Simon Martín 2017-10-03 17:47:57 +02:00
parent 3ce10690d6
commit 21720267cf
15 changed files with 53 additions and 41 deletions

View File

@ -88,7 +88,7 @@ AuthApi.prototype.authorizedByAPIKey = function(user, req, callback) {
*/
AuthApi.prototype.authorize = function(req, params, callback) {
var self = this;
var user = req.context.user;
var user = params.user;
step(
function () {

View File

@ -40,7 +40,7 @@ AnalysesController.prototype.sendResponse = function(req, res, resource) {
AnalysesController.prototype.catalog = function (req, res, next) {
var self = this;
var username = req.context.user;
var username = res.locals.user;
step(
function catalogQuery() {

View File

@ -175,11 +175,11 @@ LayergroupController.prototype.dataview = function(req, res, next) {
step(
function retrieveDataview() {
var mapConfigProvider = new MapStoreMapConfigProvider(
self.mapStore, req.context.user, self.userLimitsApi, res.locals
self.mapStore, res.locals.user, self.userLimitsApi, res.locals
);
self.dataviewBackend.getDataview(
mapConfigProvider,
req.context.user,
res.locals.user,
req.params.dataviewName,
res.locals,
this
@ -204,9 +204,9 @@ LayergroupController.prototype.dataviewSearch = function(req, res, next) {
step(
function searchDataview() {
var mapConfigProvider = new MapStoreMapConfigProvider(
self.mapStore, req.context.user, self.userLimitsApi, res.locals
self.mapStore, res.locals.user, self.userLimitsApi, res.locals
);
self.dataviewBackend.search(mapConfigProvider, req.context.user, req.params.dataviewName, res.locals, this);
self.dataviewBackend.search(mapConfigProvider, res.locals.user, req.params.dataviewName, res.locals, this);
},
function finish(err, searchResult, stats) {
req.profiler.add(stats || {});
@ -230,7 +230,7 @@ LayergroupController.prototype.attributes = function(req, res, next) {
step(
function retrieveFeatureAttributes() {
var mapConfigProvider = new MapStoreMapConfigProvider(
self.mapStore, req.context.user, self.userLimitsApi, res.locals
self.mapStore, res.locals.user, self.userLimitsApi, res.locals
);
self.attributesBackend.getFeatureAttributes(mapConfigProvider, res.locals, false, this);
},
@ -266,7 +266,7 @@ LayergroupController.prototype.tileOrLayer = function (req, res, next) {
step(
function mapController$getTileOrGrid() {
self.tileBackend.getTile(
new MapStoreMapConfigProvider(self.mapStore, req.context.user, self.userLimitsApi, res.locals),
new MapStoreMapConfigProvider(self.mapStore, res.locals.user, self.userLimitsApi, res.locals),
req.params, this
);
},
@ -362,11 +362,11 @@ LayergroupController.prototype.staticMap = function(req, res, width, height, zoo
function getImage() {
if (center) {
self.previewBackend.getImage(
new MapStoreMapConfigProvider(self.mapStore, req.context.user, self.userLimitsApi, res.locals),
new MapStoreMapConfigProvider(self.mapStore, res.locals.user, self.userLimitsApi, res.locals),
format, width, height, zoom, center, this);
} else {
self.previewBackend.getImage(
new MapStoreMapConfigProvider(self.mapStore, req.context.user, self.userLimitsApi, res.locals),
new MapStoreMapConfigProvider(self.mapStore, res.locals.user, self.userLimitsApi, res.locals),
format, width, height, zoom /* bounds */, this);
}
},
@ -405,7 +405,7 @@ LayergroupController.prototype.sendResponse = function(req, res, body, status, h
var dbName = res.locals.dbname;
step(
function getAffectedTables() {
self.getAffectedTables(req.context.user, dbName, res.locals.token, this);
self.getAffectedTables(res.locals.user, dbName, res.locals.token, this);
},
function sendResponse(err, affectedTables) {
req.profiler.done('affectedTables');

View File

@ -161,7 +161,7 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) {
function prepareAdapterMapConfig(err, requestMapConfig) {
assert.ifError(err);
context.analysisConfiguration = {
user: req.context.user,
user: res.locals.user,
db: {
host: res.locals.dbhost,
port: res.locals.dbport,
@ -170,11 +170,11 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) {
pass: res.locals.dbpassword
},
batch: {
username: req.context.user,
username: res.locals.user,
apiKey: res.locals.api_key
}
};
self.mapConfigAdapter.getMapConfig(req.context.user, requestMapConfig, res.locals, context, this);
self.mapConfigAdapter.getMapConfig(res.locals.user, requestMapConfig, res.locals, context, this);
},
function createLayergroup(err, requestMapConfig) {
assert.ifError(err);
@ -183,7 +183,7 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) {
self.mapBackend.createLayergroup(
mapConfig,
res.locals,
new CreateLayergroupMapConfigProvider(mapConfig, req.context.user, self.userLimitsApi, res.locals),
new CreateLayergroupMapConfigProvider(mapConfig, res.locals.user, self.userLimitsApi, res.locals),
this
);
},
@ -215,8 +215,8 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) {
next(err);
} else {
var analysesResults = context.analysesResults || [];
self.addDataviewsAndWidgetsUrls(req.context.user, layergroup, mapConfig.obj());
self.addAnalysesMetadata(req.context.user, layergroup, analysesResults, true);
self.addDataviewsAndWidgetsUrls(res.locals.user, layergroup, mapConfig.obj());
self.addAnalysesMetadata(res.locals.user, layergroup, analysesResults, true);
addContextMetadata(layergroup, mapConfig.obj(), context);
res.set('X-Layergroup-Id', layergroup.layergroupid);
self.send(req, res, layergroup, 200);
@ -239,7 +239,7 @@ function addContextMetadata(layergroup, mapConfig, context) {
MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn, next) {
var self = this;
var cdbuser = req.context.user;
var cdbuser = res.locals.user;
var mapConfigProvider;
var mapConfig;
@ -304,7 +304,7 @@ MapController.prototype.afterLayergroupCreate =
function(req, res, mapconfig, layergroup, analysesResults, callback) {
var self = this;
var username = req.context.user;
var username = res.locals.user;
var tasksleft = 2; // redis key and affectedTables
var errors = [];

View File

@ -47,7 +47,7 @@ NamedMapsController.prototype.register = function(app) {
};
NamedMapsController.prototype.sendResponse = function(req, res, resource, headers, namedMapProvider) {
this.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(req.context.user, namedMapProvider.getTemplateName()));
this.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(res.locals.user, namedMapProvider.getTemplateName()));
res.set('Content-Type', headers['content-type'] || headers['Content-Type'] || 'image/png');
res.set('Cache-Control', 'public,max-age=7200,must-revalidate');
@ -87,7 +87,7 @@ NamedMapsController.prototype.sendResponse = function(req, res, resource, header
NamedMapsController.prototype.tile = function(req, res, next) {
var self = this;
var cdbUser = req.context.user;
var cdbUser = res.locals.user;
var namedMapProvider;
step(
@ -121,7 +121,7 @@ NamedMapsController.prototype.tile = function(req, res, next) {
NamedMapsController.prototype.staticMap = function(req, res, next) {
var self = this;
var cdbUser = req.context.user;
var cdbUser = res.locals.user;
var format = req.params.format === 'jpg' ? 'jpeg' : 'png';
res.locals.format = req.params.format || 'png';

View File

@ -44,7 +44,7 @@ NamedMapsAdminController.prototype.register = function (router) {
NamedMapsAdminController.prototype.create = function(req, res, next) {
var self = this;
var cdbuser = req.context.user;
var cdbuser = res.locals.user;
step(
function checkPerms(){
@ -68,7 +68,7 @@ NamedMapsAdminController.prototype.create = function(req, res, next) {
NamedMapsAdminController.prototype.update = function(req, res, next) {
var self = this;
var cdbuser = req.context.user;
var cdbuser = res.locals.user;
var template;
var tpl_id;
@ -99,7 +99,7 @@ NamedMapsAdminController.prototype.retrieve = function(req, res, next) {
req.profiler.start('windshaft-cartodb.get_template');
var cdbuser = req.context.user;
var cdbuser = res.locals.user;
var tpl_id;
step(
function checkPerms(){
@ -133,7 +133,7 @@ NamedMapsAdminController.prototype.destroy = function(req, res, next) {
req.profiler.start('windshaft-cartodb.delete_template');
var cdbuser = req.context.user;
var cdbuser = res.locals.user;
var tpl_id;
step(
function checkPerms(){
@ -158,7 +158,7 @@ NamedMapsAdminController.prototype.list = function(req, res, next) {
var self = this;
req.profiler.start('windshaft-cartodb.get_template_list');
var cdbuser = req.context.user;
var cdbuser = res.locals.user;
step(
function checkPerms(){

View File

@ -3,7 +3,7 @@ module.exports = function allowQueryParams(params) {
throw new Error('allowQueryParams must receive an Array of params');
}
return function allowQueryParamsMiddleware(req, res, next) {
req.context.allowedQueryParams = params;
res.locals.allowedQueryParams = params;
next();
};
};

View File

@ -18,8 +18,8 @@ module.exports = function cleanUpQueryParamsMiddleware () {
return function cleanUpQueryParams (req, res, next) {
var allowedQueryParams = REQUEST_QUERY_PARAMS_WHITELIST;
if (Array.isArray(req.context.allowedQueryParams)) {
allowedQueryParams = allowedQueryParams.concat(req.context.allowedQueryParams);
if (Array.isArray(res.locals.allowedQueryParams)) {
allowedQueryParams = allowedQueryParams.concat(res.locals.allowedQueryParams);
}
req.query = _.pick(req.query, allowedQueryParams);

View File

@ -2,7 +2,7 @@ const _ = require('underscore');
module.exports = function dbConnSetupMiddleware(pgConnection) {
return function (req, res, next) {
const user = req.context.user;
const user = res.locals.user;
pgConnection.setDBConn(user, res.locals, (err) => {
if (err) {

View File

@ -5,7 +5,7 @@ module.exports = function layergroupTokenMiddleware(req, res, next) {
return next();
}
var user = req.context.user;
var user = res.locals.user;
var layergroupToken = LayergroupToken.parse(res.locals.token);
res.locals.token = layergroupToken.token;

View File

@ -1,7 +1,6 @@
const _ = require('underscore');
module.exports = function localsMiddleware(req, res, next) {
res.locals = {};
_.extend(res.locals, req.params);
next();

View File

@ -2,6 +2,13 @@ var CdbRequest = require('../models/cdb_request');
var cdbRequest = new CdbRequest();
module.exports = function userMiddleware(req, res, next) {
req.context.user = cdbRequest.userByReq(req);
res.locals.user = cdbRequest.userByReq(req);
// avoid a req.params.user equals to undefined
// overwrites res.locals.user
if(!req.params.user) {
delete req.params.user;
}
next();
};

View File

@ -369,7 +369,8 @@ function bootstrap(opts) {
app.use(bodyParser.json());
app.use(function bootstrap$prepareRequestResponse(req, res, next) {
req.context = req.context || {};
res.locals = {};
req.profiler = new Profiler({
statsd_client: global.statsClient,
profile: opts.useProfiler

View File

@ -63,7 +63,7 @@ module.exports = _.extend({}, serverOptions, {
_.extend(req.params, req.query);
req.params.user = 'localhost';
req.context = {user: 'localhost'};
res.locals.user = 'localhost';
req.params.dbhost = global.environment.postgres.host;
req.params.dbport = req.params.dbport || global.environment.postgres.port;

View File

@ -48,8 +48,11 @@ describe('prepare-context', function() {
req.profiler = {
done: function() {}
};
req.context = { user: 'localhost' };
if(!res.locals) {
res.locals = {};
}
res.locals.user = 'localhost';
return {req, res};
}
@ -128,6 +131,8 @@ describe('prepare-context', function() {
}
};
res = {};
({req, res} = prepareRequest(req, res));
dbConnSetup(req, res, function(err) {