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