from req.context to res.locals
This commit is contained in:
parent
3ce10690d6
commit
21720267cf
@ -88,7 +88,7 @@ AuthApi.prototype.authorizedByAPIKey = function(user, req, callback) {
|
|||||||
*/
|
*/
|
||||||
AuthApi.prototype.authorize = function(req, params, callback) {
|
AuthApi.prototype.authorize = function(req, params, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var user = req.context.user;
|
var user = params.user;
|
||||||
|
|
||||||
step(
|
step(
|
||||||
function () {
|
function () {
|
||||||
|
@ -40,7 +40,7 @@ AnalysesController.prototype.sendResponse = function(req, res, resource) {
|
|||||||
|
|
||||||
AnalysesController.prototype.catalog = function (req, res, next) {
|
AnalysesController.prototype.catalog = function (req, res, next) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var username = req.context.user;
|
var username = res.locals.user;
|
||||||
|
|
||||||
step(
|
step(
|
||||||
function catalogQuery() {
|
function catalogQuery() {
|
||||||
|
@ -175,11 +175,11 @@ LayergroupController.prototype.dataview = function(req, res, next) {
|
|||||||
step(
|
step(
|
||||||
function retrieveDataview() {
|
function retrieveDataview() {
|
||||||
var mapConfigProvider = new MapStoreMapConfigProvider(
|
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(
|
self.dataviewBackend.getDataview(
|
||||||
mapConfigProvider,
|
mapConfigProvider,
|
||||||
req.context.user,
|
res.locals.user,
|
||||||
req.params.dataviewName,
|
req.params.dataviewName,
|
||||||
res.locals,
|
res.locals,
|
||||||
this
|
this
|
||||||
@ -204,9 +204,9 @@ LayergroupController.prototype.dataviewSearch = function(req, res, next) {
|
|||||||
step(
|
step(
|
||||||
function searchDataview() {
|
function searchDataview() {
|
||||||
var mapConfigProvider = new MapStoreMapConfigProvider(
|
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) {
|
function finish(err, searchResult, stats) {
|
||||||
req.profiler.add(stats || {});
|
req.profiler.add(stats || {});
|
||||||
@ -230,7 +230,7 @@ LayergroupController.prototype.attributes = function(req, res, next) {
|
|||||||
step(
|
step(
|
||||||
function retrieveFeatureAttributes() {
|
function retrieveFeatureAttributes() {
|
||||||
var mapConfigProvider = new MapStoreMapConfigProvider(
|
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);
|
self.attributesBackend.getFeatureAttributes(mapConfigProvider, res.locals, false, this);
|
||||||
},
|
},
|
||||||
@ -266,7 +266,7 @@ LayergroupController.prototype.tileOrLayer = function (req, res, next) {
|
|||||||
step(
|
step(
|
||||||
function mapController$getTileOrGrid() {
|
function mapController$getTileOrGrid() {
|
||||||
self.tileBackend.getTile(
|
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
|
req.params, this
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@ -362,11 +362,11 @@ LayergroupController.prototype.staticMap = function(req, res, width, height, zoo
|
|||||||
function getImage() {
|
function getImage() {
|
||||||
if (center) {
|
if (center) {
|
||||||
self.previewBackend.getImage(
|
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);
|
format, width, height, zoom, center, this);
|
||||||
} else {
|
} else {
|
||||||
self.previewBackend.getImage(
|
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);
|
format, width, height, zoom /* bounds */, this);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -405,7 +405,7 @@ LayergroupController.prototype.sendResponse = function(req, res, body, status, h
|
|||||||
var dbName = res.locals.dbname;
|
var dbName = res.locals.dbname;
|
||||||
step(
|
step(
|
||||||
function getAffectedTables() {
|
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) {
|
function sendResponse(err, affectedTables) {
|
||||||
req.profiler.done('affectedTables');
|
req.profiler.done('affectedTables');
|
||||||
|
@ -161,7 +161,7 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) {
|
|||||||
function prepareAdapterMapConfig(err, requestMapConfig) {
|
function prepareAdapterMapConfig(err, requestMapConfig) {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
context.analysisConfiguration = {
|
context.analysisConfiguration = {
|
||||||
user: req.context.user,
|
user: res.locals.user,
|
||||||
db: {
|
db: {
|
||||||
host: res.locals.dbhost,
|
host: res.locals.dbhost,
|
||||||
port: res.locals.dbport,
|
port: res.locals.dbport,
|
||||||
@ -170,11 +170,11 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) {
|
|||||||
pass: res.locals.dbpassword
|
pass: res.locals.dbpassword
|
||||||
},
|
},
|
||||||
batch: {
|
batch: {
|
||||||
username: req.context.user,
|
username: res.locals.user,
|
||||||
apiKey: res.locals.api_key
|
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) {
|
function createLayergroup(err, requestMapConfig) {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
@ -183,7 +183,7 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) {
|
|||||||
self.mapBackend.createLayergroup(
|
self.mapBackend.createLayergroup(
|
||||||
mapConfig,
|
mapConfig,
|
||||||
res.locals,
|
res.locals,
|
||||||
new CreateLayergroupMapConfigProvider(mapConfig, req.context.user, self.userLimitsApi, res.locals),
|
new CreateLayergroupMapConfigProvider(mapConfig, res.locals.user, self.userLimitsApi, res.locals),
|
||||||
this
|
this
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@ -215,8 +215,8 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) {
|
|||||||
next(err);
|
next(err);
|
||||||
} else {
|
} else {
|
||||||
var analysesResults = context.analysesResults || [];
|
var analysesResults = context.analysesResults || [];
|
||||||
self.addDataviewsAndWidgetsUrls(req.context.user, layergroup, mapConfig.obj());
|
self.addDataviewsAndWidgetsUrls(res.locals.user, layergroup, mapConfig.obj());
|
||||||
self.addAnalysesMetadata(req.context.user, layergroup, analysesResults, true);
|
self.addAnalysesMetadata(res.locals.user, layergroup, analysesResults, true);
|
||||||
addContextMetadata(layergroup, mapConfig.obj(), context);
|
addContextMetadata(layergroup, mapConfig.obj(), context);
|
||||||
res.set('X-Layergroup-Id', layergroup.layergroupid);
|
res.set('X-Layergroup-Id', layergroup.layergroupid);
|
||||||
self.send(req, res, layergroup, 200);
|
self.send(req, res, layergroup, 200);
|
||||||
@ -239,7 +239,7 @@ function addContextMetadata(layergroup, mapConfig, context) {
|
|||||||
MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn, next) {
|
MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn, next) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var cdbuser = req.context.user;
|
var cdbuser = res.locals.user;
|
||||||
|
|
||||||
var mapConfigProvider;
|
var mapConfigProvider;
|
||||||
var mapConfig;
|
var mapConfig;
|
||||||
@ -304,7 +304,7 @@ MapController.prototype.afterLayergroupCreate =
|
|||||||
function(req, res, mapconfig, layergroup, analysesResults, callback) {
|
function(req, res, mapconfig, layergroup, analysesResults, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var username = req.context.user;
|
var username = res.locals.user;
|
||||||
|
|
||||||
var tasksleft = 2; // redis key and affectedTables
|
var tasksleft = 2; // redis key and affectedTables
|
||||||
var errors = [];
|
var errors = [];
|
||||||
|
@ -47,7 +47,7 @@ NamedMapsController.prototype.register = function(app) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
NamedMapsController.prototype.sendResponse = function(req, res, resource, headers, namedMapProvider) {
|
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('Content-Type', headers['content-type'] || headers['Content-Type'] || 'image/png');
|
||||||
res.set('Cache-Control', 'public,max-age=7200,must-revalidate');
|
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) {
|
NamedMapsController.prototype.tile = function(req, res, next) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var cdbUser = req.context.user;
|
var cdbUser = res.locals.user;
|
||||||
|
|
||||||
var namedMapProvider;
|
var namedMapProvider;
|
||||||
step(
|
step(
|
||||||
@ -121,7 +121,7 @@ NamedMapsController.prototype.tile = function(req, res, next) {
|
|||||||
NamedMapsController.prototype.staticMap = function(req, res, next) {
|
NamedMapsController.prototype.staticMap = function(req, res, next) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var cdbUser = req.context.user;
|
var cdbUser = res.locals.user;
|
||||||
|
|
||||||
var format = req.params.format === 'jpg' ? 'jpeg' : 'png';
|
var format = req.params.format === 'jpg' ? 'jpeg' : 'png';
|
||||||
res.locals.format = req.params.format || 'png';
|
res.locals.format = req.params.format || 'png';
|
||||||
|
@ -44,7 +44,7 @@ NamedMapsAdminController.prototype.register = function (router) {
|
|||||||
NamedMapsAdminController.prototype.create = function(req, res, next) {
|
NamedMapsAdminController.prototype.create = function(req, res, next) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var cdbuser = req.context.user;
|
var cdbuser = res.locals.user;
|
||||||
|
|
||||||
step(
|
step(
|
||||||
function checkPerms(){
|
function checkPerms(){
|
||||||
@ -68,7 +68,7 @@ NamedMapsAdminController.prototype.create = function(req, res, next) {
|
|||||||
NamedMapsAdminController.prototype.update = function(req, res, next) {
|
NamedMapsAdminController.prototype.update = function(req, res, next) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var cdbuser = req.context.user;
|
var cdbuser = res.locals.user;
|
||||||
var template;
|
var template;
|
||||||
var tpl_id;
|
var tpl_id;
|
||||||
|
|
||||||
@ -99,7 +99,7 @@ NamedMapsAdminController.prototype.retrieve = function(req, res, next) {
|
|||||||
|
|
||||||
req.profiler.start('windshaft-cartodb.get_template');
|
req.profiler.start('windshaft-cartodb.get_template');
|
||||||
|
|
||||||
var cdbuser = req.context.user;
|
var cdbuser = res.locals.user;
|
||||||
var tpl_id;
|
var tpl_id;
|
||||||
step(
|
step(
|
||||||
function checkPerms(){
|
function checkPerms(){
|
||||||
@ -133,7 +133,7 @@ NamedMapsAdminController.prototype.destroy = function(req, res, next) {
|
|||||||
|
|
||||||
req.profiler.start('windshaft-cartodb.delete_template');
|
req.profiler.start('windshaft-cartodb.delete_template');
|
||||||
|
|
||||||
var cdbuser = req.context.user;
|
var cdbuser = res.locals.user;
|
||||||
var tpl_id;
|
var tpl_id;
|
||||||
step(
|
step(
|
||||||
function checkPerms(){
|
function checkPerms(){
|
||||||
@ -158,7 +158,7 @@ NamedMapsAdminController.prototype.list = function(req, res, next) {
|
|||||||
var self = this;
|
var self = this;
|
||||||
req.profiler.start('windshaft-cartodb.get_template_list');
|
req.profiler.start('windshaft-cartodb.get_template_list');
|
||||||
|
|
||||||
var cdbuser = req.context.user;
|
var cdbuser = res.locals.user;
|
||||||
|
|
||||||
step(
|
step(
|
||||||
function checkPerms(){
|
function checkPerms(){
|
||||||
|
@ -3,7 +3,7 @@ module.exports = function allowQueryParams(params) {
|
|||||||
throw new Error('allowQueryParams must receive an Array of params');
|
throw new Error('allowQueryParams must receive an Array of params');
|
||||||
}
|
}
|
||||||
return function allowQueryParamsMiddleware(req, res, next) {
|
return function allowQueryParamsMiddleware(req, res, next) {
|
||||||
req.context.allowedQueryParams = params;
|
res.locals.allowedQueryParams = params;
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -18,15 +18,15 @@ module.exports = function cleanUpQueryParamsMiddleware () {
|
|||||||
return function cleanUpQueryParams (req, res, next) {
|
return function cleanUpQueryParams (req, res, next) {
|
||||||
var allowedQueryParams = REQUEST_QUERY_PARAMS_WHITELIST;
|
var allowedQueryParams = REQUEST_QUERY_PARAMS_WHITELIST;
|
||||||
|
|
||||||
if (Array.isArray(req.context.allowedQueryParams)) {
|
if (Array.isArray(res.locals.allowedQueryParams)) {
|
||||||
allowedQueryParams = allowedQueryParams.concat(req.context.allowedQueryParams);
|
allowedQueryParams = allowedQueryParams.concat(res.locals.allowedQueryParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
req.query = _.pick(req.query, allowedQueryParams);
|
req.query = _.pick(req.query, allowedQueryParams);
|
||||||
|
|
||||||
// bring all query values onto res.locals object
|
// bring all query values onto res.locals object
|
||||||
_.extend(res.locals, req.query);
|
_.extend(res.locals, req.query);
|
||||||
|
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -2,7 +2,7 @@ const _ = require('underscore');
|
|||||||
|
|
||||||
module.exports = function dbConnSetupMiddleware(pgConnection) {
|
module.exports = function dbConnSetupMiddleware(pgConnection) {
|
||||||
return function (req, res, next) {
|
return function (req, res, next) {
|
||||||
const user = req.context.user;
|
const user = res.locals.user;
|
||||||
|
|
||||||
pgConnection.setDBConn(user, res.locals, (err) => {
|
pgConnection.setDBConn(user, res.locals, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -5,7 +5,7 @@ module.exports = function layergroupTokenMiddleware(req, res, next) {
|
|||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
var user = req.context.user;
|
var user = res.locals.user;
|
||||||
|
|
||||||
var layergroupToken = LayergroupToken.parse(res.locals.token);
|
var layergroupToken = LayergroupToken.parse(res.locals.token);
|
||||||
res.locals.token = layergroupToken.token;
|
res.locals.token = layergroupToken.token;
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
const _ = require('underscore');
|
const _ = require('underscore');
|
||||||
|
|
||||||
module.exports = function localsMiddleware(req, res, next) {
|
module.exports = function localsMiddleware(req, res, next) {
|
||||||
res.locals = {};
|
|
||||||
_.extend(res.locals, req.params);
|
_.extend(res.locals, req.params);
|
||||||
|
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,6 +2,13 @@ var CdbRequest = require('../models/cdb_request');
|
|||||||
var cdbRequest = new CdbRequest();
|
var cdbRequest = new CdbRequest();
|
||||||
|
|
||||||
module.exports = function userMiddleware(req, res, next) {
|
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();
|
next();
|
||||||
};
|
};
|
||||||
|
@ -369,7 +369,8 @@ function bootstrap(opts) {
|
|||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
app.use(function bootstrap$prepareRequestResponse(req, res, next) {
|
app.use(function bootstrap$prepareRequestResponse(req, res, next) {
|
||||||
req.context = req.context || {};
|
res.locals = {};
|
||||||
|
|
||||||
req.profiler = new Profiler({
|
req.profiler = new Profiler({
|
||||||
statsd_client: global.statsClient,
|
statsd_client: global.statsClient,
|
||||||
profile: opts.useProfiler
|
profile: opts.useProfiler
|
||||||
|
@ -63,7 +63,7 @@ module.exports = _.extend({}, serverOptions, {
|
|||||||
|
|
||||||
_.extend(req.params, req.query);
|
_.extend(req.params, req.query);
|
||||||
req.params.user = 'localhost';
|
req.params.user = 'localhost';
|
||||||
req.context = {user: 'localhost'};
|
res.locals.user = 'localhost';
|
||||||
|
|
||||||
req.params.dbhost = global.environment.postgres.host;
|
req.params.dbhost = global.environment.postgres.host;
|
||||||
req.params.dbport = req.params.dbport || global.environment.postgres.port;
|
req.params.dbport = req.params.dbport || global.environment.postgres.port;
|
||||||
|
@ -48,8 +48,11 @@ describe('prepare-context', function() {
|
|||||||
req.profiler = {
|
req.profiler = {
|
||||||
done: function() {}
|
done: function() {}
|
||||||
};
|
};
|
||||||
req.context = { user: 'localhost' };
|
|
||||||
res.locals = {};
|
if(!res.locals) {
|
||||||
|
res.locals = {};
|
||||||
|
}
|
||||||
|
res.locals.user = 'localhost';
|
||||||
|
|
||||||
return {req, res};
|
return {req, res};
|
||||||
}
|
}
|
||||||
@ -128,8 +131,10 @@ describe('prepare-context', function() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
res = {};
|
||||||
|
|
||||||
({req, res} = prepareRequest(req, res));
|
({req, res} = prepareRequest(req, res));
|
||||||
|
|
||||||
dbConnSetup(req, res, function(err) {
|
dbConnSetup(req, res, function(err) {
|
||||||
if ( err ) { done(err); return; }
|
if ( err ) { done(err); return; }
|
||||||
// wrong key resets params to no user
|
// wrong key resets params to no user
|
||||||
|
Loading…
Reference in New Issue
Block a user