Move userByReq to its own model

This commit is contained in:
Raul Ochoa 2015-03-23 17:35:09 +01:00
parent 09b3f0a862
commit f5f7be627f
3 changed files with 43 additions and 37 deletions

View File

@ -1,5 +1,6 @@
var step = require('step'); var step = require('step');
var _ = require('underscore'); var _ = require('underscore');
var CdbRequest = require('../models/cdb_request');
function TemplateMapsController(app, serverOptions, templateMaps, metadataBackend, templateBaseUrl, surrogateKeysCache, function TemplateMapsController(app, serverOptions, templateMaps, metadataBackend, templateBaseUrl, surrogateKeysCache,
NamedMapsCacheEntry, pgConnection) { NamedMapsCacheEntry, pgConnection) {
@ -15,6 +16,7 @@ function TemplateMapsController(app, serverOptions, templateMaps, metadataBacken
module.exports = TemplateMapsController; module.exports = TemplateMapsController;
var cdbRequest = new CdbRequest();
TemplateMapsController.prototype.register = function(app) { TemplateMapsController.prototype.register = function(app) {
app.get(this.templateBaseUrl + '/:template_id/jsonp', this.jsonp.bind(this)); app.get(this.templateBaseUrl + '/:template_id/jsonp', this.jsonp.bind(this));
@ -33,7 +35,7 @@ TemplateMapsController.prototype.create = function(req, res) {
this.app.doCORS(res); this.app.doCORS(res);
var cdbuser = self.serverOptions.userByReq(req); var cdbuser = cdbRequest.userByReq(req);
step( step(
function checkPerms(){ function checkPerms(){
@ -80,7 +82,7 @@ TemplateMapsController.prototype.update = function(req, res) {
this.app.doCORS(res); this.app.doCORS(res);
var cdbuser = this.serverOptions.userByReq(req); var cdbuser = cdbRequest.userByReq(req);
var template; var template;
var tpl_id; var tpl_id;
step( step(
@ -140,7 +142,7 @@ TemplateMapsController.prototype.retrieve = function(req, res) {
this.app.doCORS(res); this.app.doCORS(res);
var cdbuser = this.serverOptions.userByReq(req); var cdbuser = cdbRequest.userByReq(req);
var tpl_id; var tpl_id;
step( step(
function checkPerms(){ function checkPerms(){
@ -201,7 +203,7 @@ TemplateMapsController.prototype.destroy = function(req, res) {
} }
this.app.doCORS(res); this.app.doCORS(res);
var cdbuser = this.serverOptions.userByReq(req); var cdbuser = cdbRequest.userByReq(req);
var tpl_id; var tpl_id;
step( step(
function checkPerms(){ function checkPerms(){
@ -254,7 +256,7 @@ TemplateMapsController.prototype.list = function(req, res) {
} }
this.app.doCORS(res); this.app.doCORS(res);
var cdbuser = this.serverOptions.userByReq(req); var cdbuser = cdbRequest.userByReq(req);
step( step(
function checkPerms(){ function checkPerms(){
@ -353,7 +355,7 @@ TemplateMapsController.prototype.instantiateTemplate = function(req, res, templa
var template; var template;
var layergroup; var layergroup;
var fakereq; // used for call to createLayergroup var fakereq; // used for call to createLayergroup
var cdbuser = self.serverOptions.userByReq(req); var cdbuser = cdbRequest.userByReq(req);
// Format of template_id: [<template_owner>]@<template_id> // Format of template_id: [<template_owner>]@<template_id>
var tpl_id = req.params.template_id.split('@'); var tpl_id = req.params.template_id.split('@');
if ( tpl_id.length > 1 ) { if ( tpl_id.length > 1 ) {

View File

@ -0,0 +1,26 @@
function CdbRequest() {
}
module.exports = CdbRequest;
var RE_USER_FROM_HOST = new RegExp(global.environment.user_from_host ||
'^([^\\.]+)\\.' // would extract "strk" from "strk.cartodb.com"
);
CdbRequest.prototype.userByReq = function(req) {
var host = req.headers.host;
if (req.params.user) {
return req.params.user;
}
var mat = host.match(RE_USER_FROM_HOST);
if ( ! mat ) {
console.error("Pattern '" + RE_USER_FROM_HOST + "' does not match hostname '" + host + "'");
return;
}
// console.log("Matches: "); console.dir(mat);
if ( mat.length !== 2 ) {
console.error("Pattern '" + RE_USER_FROM_HOST + "' gave unexpected matches against '" + host + "': ", mat);
return;
}
return mat[1];
};

View File

@ -6,6 +6,7 @@ var crypto = require('crypto');
var LZMA = require('lzma').LZMA; var LZMA = require('lzma').LZMA;
var TemplateMaps = require('./template_maps.js'); var TemplateMaps = require('./template_maps.js');
var MapConfigNamedLayersAdapter = require('./models/mapconfig_named_layers_adapter'); var MapConfigNamedLayersAdapter = require('./models/mapconfig_named_layers_adapter');
var CdbRequest = require('./models/cdb_request');
// Whitelist query parameters and attach format // Whitelist query parameters and attach format
var REQUEST_QUERY_PARAMS_WHITELIST = [ var REQUEST_QUERY_PARAMS_WHITELIST = [
@ -33,7 +34,8 @@ module.exports = function(redisPool) {
var cartoData = require('cartodb-redis')({ pool: redisPool }), var cartoData = require('cartodb-redis')({ pool: redisPool }),
lzmaWorker = new LZMA(), lzmaWorker = new LZMA(),
pgConnection = new PgConnection(cartoData), pgConnection = new PgConnection(cartoData),
queryTablesApi = new QueryTablesApi(pgConnection, cartoData); queryTablesApi = new QueryTablesApi(pgConnection, cartoData),
cdbRequest = new CdbRequest();
var rendererConfig = _.defaults(global.environment.renderer || {}, { var rendererConfig = _.defaults(global.environment.renderer || {}, {
cache_ttl: 60000, // milliseconds cache_ttl: 60000, // milliseconds
@ -201,7 +203,7 @@ module.exports = function(redisPool) {
return [req.params.table]; return [req.params.table];
} }
queryTablesApi.getAffectedTablesInQuery(that.userByReq(req), sql, this); // in addCacheChannel queryTablesApi.getAffectedTablesInQuery(cdbRequest.userByReq(req), sql, this); // in addCacheChannel
}, },
function buildCacheChannel(err, tableNames) { function buildCacheChannel(err, tableNames) {
if ( err ) throw err; if ( err ) throw err;
@ -273,7 +275,7 @@ module.exports = function(redisPool) {
}; };
me.beforeLayergroupCreate = function(req, requestMapConfig, callback) { me.beforeLayergroupCreate = function(req, requestMapConfig, callback) {
mapConfigNamedLayersAdapter.getLayers(this.userByReq(req), requestMapConfig.layers, pgConnection, mapConfigNamedLayersAdapter.getLayers(cdbRequest.userByReq(req), requestMapConfig.layers, pgConnection,
function(err, layers, datasource) { function(err, layers, datasource) {
if (err) { if (err) {
return callback(err); return callback(err);
@ -288,7 +290,7 @@ module.exports = function(redisPool) {
me.afterLayergroupCreate = function(req, mapconfig, response, callback) { me.afterLayergroupCreate = function(req, mapconfig, response, callback) {
var token = response.layergroupid; var token = response.layergroupid;
var username = this.userByReq(req); var username = cdbRequest.userByReq(req);
var tasksleft = 2; // redis key and affectedTables var tasksleft = 2; // redis key and affectedTables
var errors = []; var errors = [];
@ -362,30 +364,6 @@ module.exports = function(redisPool) {
/* X-Cache-Channel generation } */ /* X-Cache-Channel generation } */
me.re_userFromHost = new RegExp(
global.environment.user_from_host ||
'^([^\\.]+)\\.' // would extract "strk" from "strk.cartodb.com"
);
me.userByReq = function(req) {
var host = req.headers.host;
if (req.params.user) {
return req.params.user;
}
var mat = host.match(this.re_userFromHost);
if ( ! mat ) {
console.error("ERROR: user pattern '" + this.re_userFromHost + "' does not match hostname '" + host + "'");
return;
}
// console.log("Matches: "); console.dir(mat);
if ( mat.length !== 2 ) {
console.error("ERROR: pattern '" + this.re_userFromHost + "' gave unexpected matches against '" + host + "': " +
mat);
return;
}
return mat[1];
};
// Check if a request is authorized by a signer // Check if a request is authorized by a signer
// //
// @param req express request object // @param req express request object
@ -441,7 +419,7 @@ module.exports = function(redisPool) {
return; return;
} }
//console.log("given ApiKey: " + givenKey); //console.log("given ApiKey: " + givenKey);
var user = me.userByReq(req); var user = cdbRequest.userByReq(req);
step( step(
function (){ function (){
cartoData.getUserMapKey(user, this); cartoData.getUserMapKey(user, this);
@ -464,7 +442,7 @@ module.exports = function(redisPool) {
*/ */
me.authorize = function(req, callback) { me.authorize = function(req, callback) {
var that = this; var that = this;
var user = me.userByReq(req); var user = cdbRequest.userByReq(req);
step( step(
function (){ function (){
@ -589,7 +567,7 @@ module.exports = function(redisPool) {
_.each(bad_query, function(key){ delete req.query[key]; }); _.each(bad_query, function(key){ delete req.query[key]; });
req.params = _.extend({}, req.params); // shuffle things as request is a strange array/object req.params = _.extend({}, req.params); // shuffle things as request is a strange array/object
var user = me.userByReq(req); var user = cdbRequest.userByReq(req);
if ( req.params.token ) { if ( req.params.token ) {
//console.log("Request parameters include token " + req.params.token); //console.log("Request parameters include token " + req.params.token);