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
|
|
|
|
2016-05-24 00:03:45 +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);
|
|
|
|
|
2015-02-04 18:30:36 +08:00
|
|
|
function adaptLayer(layer, done) {
|
|
|
|
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;
|
|
|
|
|
2016-05-24 00:03:45 +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)) {
|
|
|
|
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');
|
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
|
|
|
}
|
|
|
|
});
|
2015-02-04 18:30:36 +08:00
|
|
|
|
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
|
|
|
|
2015-02-04 18:30:36 +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'));
|
|
|
|
}
|
|
|
|
|
2015-02-04 18:30:36 +08:00
|
|
|
var layers = [],
|
|
|
|
currentLayerIndex = 0;
|
|
|
|
|
|
|
|
layersResults.forEach(function(layersResult) {
|
|
|
|
|
|
|
|
layersResult.layers.forEach(function(layer) {
|
|
|
|
layers.push(layer);
|
|
|
|
if (layersResult.datasource) {
|
|
|
|
datasourceBuilder.withLayerDatasource(currentLayerIndex, {
|
|
|
|
user: dbAuth.dbuser
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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)) {
|
2018-02-08 18:29:17 +08:00
|
|
|
this.pgConnection.setDBAuth(user, dbAuth, 'master', function(err) {
|
2015-02-04 18:30:36 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
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);
|
2015-01-30 22:31:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2015-02-04 18:30:36 +08:00
|
|
|
|
|
|
|
function isNamedTypeLayer(layer) {
|
|
|
|
return layer.type === 'named';
|
|
|
|
}
|