diff --git a/lib/cartodb/cache/named_map_provider_cache.js b/lib/cartodb/cache/named_map_provider_cache.js index dac98b21..ddfea4a2 100644 --- a/lib/cartodb/cache/named_map_provider_cache.js +++ b/lib/cartodb/cache/named_map_provider_cache.js @@ -15,7 +15,7 @@ function NamedMapProviderCache(templateMaps, pgConnection, metadataBackend, anal this.metadataBackend = metadataBackend; this.userLimitsApi = userLimitsApi; - this.namedLayersAdapter = new MapConfigNamedLayersAdapter(templateMaps); + this.namedLayersAdapter = new MapConfigNamedLayersAdapter(templateMaps, pgConnection); this.analysisMapConfigAdapter = new AnalysisMapConfigAdapter(analysisBackend); this.overviewsAdapter = overviewsAdapter; this.turboCartoAdapter = turboCartoAdapter; diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 8a702a32..80150699 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -51,7 +51,7 @@ function MapController(authApi, pgConnection, templateMaps, mapBackend, metadata this.turboCartoAdapter = turboCartoAdapter; this.analysisMapConfigAdapter = new AnalysisMapConfigAdapter(analysisBackend); - this.namedLayersAdapter = new MapConfigNamedLayersAdapter(templateMaps); + this.namedLayersAdapter = new MapConfigNamedLayersAdapter(templateMaps, pgConnection); this.overviewsAdapter = overviewsAdapter; this.sqlWrapMapConfigAdapter = new SqlWrapMapConfigAdapter(); } @@ -169,7 +169,7 @@ MapController.prototype.create = function(req, res, prepareConfigFn) { assert.ifError(err); var next = this; analysesResults = _analysesResults; - self.namedLayersAdapter.getMapConfig(req.context.user, requestMapConfig, self.pgConnection, + self.namedLayersAdapter.getMapConfig(user, requestMapConfig, req.params, context, function(err, requestMapConfig, datasource) { if (err) { return next(err); diff --git a/lib/cartodb/models/mapconfig/adapter/mapconfig-named-layers-adapter.js b/lib/cartodb/models/mapconfig/adapter/mapconfig-named-layers-adapter.js index e8792def..8716bd9d 100644 --- a/lib/cartodb/models/mapconfig/adapter/mapconfig-named-layers-adapter.js +++ b/lib/cartodb/models/mapconfig/adapter/mapconfig-named-layers-adapter.js @@ -2,13 +2,14 @@ var queue = require('queue-async'); var _ = require('underscore'); var Datasource = require('windshaft').model.Datasource; -function MapConfigNamedLayersAdapter(templateMaps) { +function MapConfigNamedLayersAdapter(templateMaps, pgConnection) { this.templateMaps = templateMaps; + this.pgConnection = pgConnection; } module.exports = MapConfigNamedLayersAdapter; -MapConfigNamedLayersAdapter.prototype.getMapConfig = function(username, requestMapConfig, dbMetadata, callback) { +MapConfigNamedLayersAdapter.prototype.getMapConfig = function (user, requestMapConfig, params, context, callback) { var self = this; var layers = requestMapConfig.layers; @@ -30,9 +31,9 @@ MapConfigNamedLayersAdapter.prototype.getMapConfig = function(username, requestM var templateConfigParams = layer.options.config || {}; var templateAuthTokens = layer.options.auth_tokens; - self.templateMaps.getTemplate(username, templateName, function(err, template) { + self.templateMaps.getTemplate(user, templateName, function(err, template) { if (err || !template) { - return done(new Error("Template '" + templateName + "' of user '" + username + "' not found")); + return done(new Error("Template '" + templateName + "' of user '" + user + "' not found")); } if (self.templateMaps.isAuthorized(template, templateAuthTokens)) { @@ -108,7 +109,7 @@ MapConfigNamedLayersAdapter.prototype.getMapConfig = function(username, requestM if (_.some(layers, isNamedTypeLayer)) { // Lazy load dbAuth - dbMetadata.setDBAuth(username, dbAuth, function(err) { + this.pgConnection.setDBAuth(user, dbAuth, function(err) { if (err) { return callback(err); } diff --git a/lib/cartodb/models/mapconfig/provider/named-map-provider.js b/lib/cartodb/models/mapconfig/provider/named-map-provider.js index 0eead347..c9755fed 100644 --- a/lib/cartodb/models/mapconfig/provider/named-map-provider.js +++ b/lib/cartodb/models/mapconfig/provider/named-map-provider.js @@ -126,7 +126,7 @@ NamedMapMapConfigProvider.prototype.getMapConfig = function(callback) { assert.ifError(err); var next = this; self.analysesResults = analysesResults || []; - self.namedLayersAdapter.getMapConfig(self.owner, _mapConfig, self.pgConnection, + self.namedLayersAdapter.getMapConfig(self.owner, _mapConfig, rendererParams, context, function(err, _mapConfig, datasource) { if (err) { return next(err); diff --git a/test/integration/mapconfig_named_layers_datasource.js b/test/integration/mapconfig_named_layers_datasource.js index d372f138..ec37dddd 100644 --- a/test/integration/mapconfig_named_layers_datasource.js +++ b/test/integration/mapconfig_named_layers_datasource.js @@ -14,7 +14,7 @@ var templateMaps = new TemplateMaps(redisPool, { max_user_templates: global.environment.maxUserTemplates }); -var mapConfigNamedLayersAdapter = new MapConfigNamedLayersAdapter(templateMaps); +var mapConfigNamedLayersAdapter = new MapConfigNamedLayersAdapter(templateMaps, pgConnection); var wadusSql = 'select 1 wadusLayer, null::geometry the_geom_webmercator'; var wadusLayer = { @@ -294,7 +294,9 @@ describe('named_layers datasources', function() { testScenarios.forEach(function(testScenario) { it('should return a list of layers ' + testScenario.desc, function(done) { - mapConfigNamedLayersAdapter.getMapConfig(username, testScenario.config, pgConnection, + var params = {}; + var context = {}; + mapConfigNamedLayersAdapter.getMapConfig(username, testScenario.config, params, context, function(err, mapConfig, datasource) { testScenario.test(err, mapConfig.layers, datasource, done); } diff --git a/test/integration/mapconfig_named_layers_expanded.js b/test/integration/mapconfig_named_layers_expanded.js index 90c384f4..54245313 100644 --- a/test/integration/mapconfig_named_layers_expanded.js +++ b/test/integration/mapconfig_named_layers_expanded.js @@ -16,7 +16,7 @@ describe('mapconfig-named-layers-adapter', function() { max_user_templates: global.environment.maxUserTemplates }); - var mapConfigNamedLayersAdapter = new MapConfigNamedLayersAdapter(templateMaps); + var mapConfigNamedLayersAdapter = new MapConfigNamedLayersAdapter(templateMaps, pgConnection); var wadusLayer = { type: 'cartodb', @@ -134,6 +134,8 @@ describe('mapconfig-named-layers-adapter', function() { }; } + var params = {}; + var context = {}; beforeEach(function(done) { templateMaps.addTemplate(username, template, done); @@ -147,7 +149,7 @@ describe('mapconfig-named-layers-adapter', function() { var missingNamedMapLayerConfig = makeNamedMapLayerConfig({ config: {} }); - mapConfigNamedLayersAdapter.getMapConfig(username, missingNamedMapLayerConfig, pgConnection, + mapConfigNamedLayersAdapter.getMapConfig(username, missingNamedMapLayerConfig, params, context, function(err, mapConfig, datasource) { assert.ok(err); assert.ok(!mapConfig); @@ -164,7 +166,7 @@ describe('mapconfig-named-layers-adapter', function() { var nonExistentNamedMapLayerConfig = makeNamedMapLayerConfig({ name: missingTemplateName }); - mapConfigNamedLayersAdapter.getMapConfig(username, nonExistentNamedMapLayerConfig, pgConnection, + mapConfigNamedLayersAdapter.getMapConfig(username, nonExistentNamedMapLayerConfig, params, context, function(err, mapConfig, datasource) { assert.ok(err); assert.ok(!mapConfig); @@ -187,7 +189,7 @@ describe('mapconfig-named-layers-adapter', function() { var nonAuthTokensNamedMapLayerConfig = makeNamedMapLayerConfig({ name: tokenAuthTemplateName }); - mapConfigNamedLayersAdapter.getMapConfig(username, nonAuthTokensNamedMapLayerConfig, pgConnection, + mapConfigNamedLayersAdapter.getMapConfig(username, nonAuthTokensNamedMapLayerConfig, params, context, function(err, mapConfig, datasource) { assert.ok(err); assert.ok(!mapConfig); @@ -209,7 +211,7 @@ describe('mapconfig-named-layers-adapter', function() { var nestedNamedMapLayerConfig = makeNamedMapLayerConfig({ name: nestedNamedMapTemplateName }); - mapConfigNamedLayersAdapter.getMapConfig(username, nestedNamedMapLayerConfig, pgConnection, + mapConfigNamedLayersAdapter.getMapConfig(username, nestedNamedMapLayerConfig, params, context, function(err, mapConfig, datasource) { assert.ok(err); assert.ok(!mapConfig); @@ -226,7 +228,7 @@ describe('mapconfig-named-layers-adapter', function() { var validNamedMapMapLayerConfig = makeNamedMapLayerConfig({ name: templateName }); - mapConfigNamedLayersAdapter.getMapConfig(username, validNamedMapMapLayerConfig, pgConnection, + mapConfigNamedLayersAdapter.getMapConfig(username, validNamedMapMapLayerConfig, params, context, function(err, mapConfig, datasource) { assert.ok(!err); var layers = mapConfig.layers; @@ -249,7 +251,7 @@ describe('mapconfig-named-layers-adapter', function() { name: tokenAuthTemplateName, auth_tokens: ['valid1'] }); - mapConfigNamedLayersAdapter.getMapConfig(username, validAuthTokensNamedMapLayerConfig, pgConnection, + mapConfigNamedLayersAdapter.getMapConfig(username, validAuthTokensNamedMapLayerConfig, params, context, function(err, mapConfig, datasource) { assert.ok(!err); var layers = mapConfig.layers; @@ -272,7 +274,7 @@ describe('mapconfig-named-layers-adapter', function() { name: multipleLayersTemplateName, auth_tokens: ['valid2'] }); - mapConfigNamedLayersAdapter.getMapConfig(username, multipleLayersNamedMapLayerConfig, pgConnection, + mapConfigNamedLayersAdapter.getMapConfig(username, multipleLayersNamedMapLayerConfig, params, context, function(err, mapConfig, datasource) { assert.ok(!err); var layers = mapConfig.layers; @@ -309,7 +311,7 @@ describe('mapconfig-named-layers-adapter', function() { }, auth_tokens: ['valid2'] }); - mapConfigNamedLayersAdapter.getMapConfig(username, multipleLayersNamedMapLayerConfig, pgConnection, + mapConfigNamedLayersAdapter.getMapConfig(username, multipleLayersNamedMapLayerConfig, params, context, function(err, mapConfig, datasource) { assert.ok(!err); var layers = mapConfig.layers;