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) {
|
||||
var self = this;
|
||||
var user = req.context.user;
|
||||
var user = params.user;
|
||||
|
||||
step(
|
||||
function () {
|
||||
|
@ -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() {
|
||||
|
@ -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');
|
||||
|
@ -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 = [];
|
||||
|
@ -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';
|
||||
|
@ -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(){
|
||||
|
@ -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();
|
||||
};
|
||||
};
|
||||
|
@ -18,15 +18,15 @@ 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);
|
||||
|
||||
// bring all query values onto res.locals object
|
||||
_.extend(res.locals, req.query);
|
||||
|
||||
|
||||
next();
|
||||
};
|
||||
};
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -1,9 +1,8 @@
|
||||
const _ = require('underscore');
|
||||
|
||||
module.exports = function localsMiddleware(req, res, next) {
|
||||
res.locals = {};
|
||||
_.extend(res.locals, req.params);
|
||||
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
|
@ -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();
|
||||
};
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -48,8 +48,11 @@ describe('prepare-context', function() {
|
||||
req.profiler = {
|
||||
done: function() {}
|
||||
};
|
||||
req.context = { user: 'localhost' };
|
||||
res.locals = {};
|
||||
|
||||
if(!res.locals) {
|
||||
res.locals = {};
|
||||
}
|
||||
res.locals.user = 'localhost';
|
||||
|
||||
return {req, res};
|
||||
}
|
||||
@ -128,8 +131,10 @@ describe('prepare-context', function() {
|
||||
}
|
||||
};
|
||||
|
||||
res = {};
|
||||
|
||||
({req, res} = prepareRequest(req, res));
|
||||
|
||||
|
||||
dbConnSetup(req, res, function(err) {
|
||||
if ( err ) { done(err); return; }
|
||||
// wrong key resets params to no user
|
||||
|
Loading…
Reference in New Issue
Block a user