Windshaft-cartodb/lib/models/mapconfig/adapter/mapconfig-named-layers-adapter.js

131 lines
4.4 KiB
JavaScript
Raw Normal View History

'use strict';
var queue = require('queue-async');
var _ = require('underscore');
2015-07-08 05:52:39 +08:00
var Datasource = require('windshaft').model.Datasource;
2019-10-22 01:07:24 +08:00
function MapConfigNamedLayersAdapter (templateMaps, pgConnection) {
this.templateMaps = templateMaps;
this.pgConnection = pgConnection;
}
module.exports = MapConfigNamedLayersAdapter;
MapConfigNamedLayersAdapter.prototype.getMapConfig = function (user, requestMapConfig, params, context, callback) {
var self = this;
var layers = requestMapConfig.layers;
2015-07-08 05:45:56 +08:00
if (!layers) {
return callback(null, requestMapConfig);
2015-07-08 05:45:56 +08:00
}
var adaptLayersQueue = queue(layers.length);
2019-10-22 01:07:24 +08:00
function adaptLayer (layer, done) {
if (isNamedTypeLayer(layer)) {
if (!layer.options.name) {
return done(new Error('Missing Named Map `name` in layer options'));
}
var templateName = layer.options.name;
var templateConfigParams = layer.options.config || {};
var templateAuthTokens = layer.options.auth_tokens;
2019-10-22 01:07:24 +08:00
self.templateMaps.getTemplate(user, templateName, function (err, template) {
if (err || !template) {
return done(new Error("Template '" + templateName + "' of user '" + user + "' not found"));
}
if (self.templateMaps.isAuthorized(template, templateAuthTokens)) {
2019-10-22 01:07:24 +08:00
var nestedNamedLayers = template.layergroup.layers.filter(function (layer) {
return layer.type === 'named';
});
if (nestedNamedLayers.length > 0) {
var nestedNamedMapsError = new Error('Nested named layers are not allowed');
return done(nestedNamedMapsError);
}
try {
var templateLayergroupConfig = self.templateMaps.instance(template, templateConfigParams);
return done(null, {
datasource: true,
layers: templateLayergroupConfig.layers
});
} catch (err) {
return done(err);
}
} else {
var unauthorizedError = new Error("Unauthorized '" + templateName + "' template instantiation");
unauthorizedError.http_status = 403;
return done(unauthorizedError);
}
});
} else {
return done(null, {
datasource: false,
layers: [layer]
});
}
}
var datasourceBuilder = new Datasource.Builder();
2019-10-22 01:07:24 +08:00
function layersAdaptQueueFinish (err, layersResults) {
if (err) {
return callback(err);
}
if (!layersResults || layersResults.length === 0) {
return callback(new Error('Missing layers array from layergroup config'));
}
2019-10-22 01:07:24 +08:00
var layers = [];
var currentLayerIndex = 0;
2019-10-22 01:07:24 +08:00
layersResults.forEach(function (layersResult) {
layersResult.layers.forEach(function (layer) {
layers.push(layer);
if (layersResult.datasource) {
datasourceBuilder.withLayerDatasource(currentLayerIndex, {
2019-11-12 20:12:01 +08:00
user: dbAuth.dbuser,
2019-11-14 03:07:41 +08:00
// Used internally (PSQL)
2019-11-12 20:12:01 +08:00
pass: dbAuth.dbpassword,
dbpassword: dbAuth.dbpassword,
2019-11-14 03:07:41 +08:00
// Used by Mapnik
2019-11-12 20:12:01 +08:00
password: dbAuth.dbpassword
});
}
currentLayerIndex++;
});
});
requestMapConfig.layers = layers;
2016-05-24 01:09:57 +08:00
context.datasource = datasourceBuilder.build();
2016-05-24 01:09:57 +08:00
return callback(null, requestMapConfig);
}
var dbAuth = {};
if (_.some(layers, isNamedTypeLayer)) {
2019-10-22 01:07:24 +08:00
this.pgConnection.setDBAuth(user, dbAuth, 'master', function (err) {
if (err) {
return callback(err);
}
2019-10-22 01:07:24 +08:00
layers.forEach(function (layer) {
adaptLayersQueue.defer(adaptLayer, layer);
});
adaptLayersQueue.awaitAll(layersAdaptQueueFinish);
});
} else {
2016-05-24 01:09:57 +08:00
context.datasource = datasourceBuilder.build();
return callback(null, requestMapConfig);
}
};
2019-10-22 01:07:24 +08:00
function isNamedTypeLayer (layer) {
return layer.type === 'named';
}