2014-09-25 01:17:51 +08:00
|
|
|
var crypto = require('crypto'),
|
|
|
|
Step = require('step'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
dot = require('dot');
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-23 23:35:47 +08:00
|
|
|
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
// Class handling map templates
|
|
|
|
//
|
|
|
|
// See http://github.com/CartoDB/Windshaft-cartodb/wiki/Template-maps
|
|
|
|
//
|
|
|
|
// @param redis_pool an instance of a "redis-mpool"
|
|
|
|
// See https://github.com/CartoDB/node-redis-mpool
|
|
|
|
// Needs version 0.x.x of the API.
|
|
|
|
//
|
2014-02-13 21:55:31 +08:00
|
|
|
// @param opts TemplateMap options. Supported elements:
|
|
|
|
// 'max_user_templates' limit on the number of per-user
|
|
|
|
//
|
2013-12-18 00:35:12 +08:00
|
|
|
//
|
2015-01-22 22:40:40 +08:00
|
|
|
function TemplateMaps(redis_pool, opts) {
|
2015-01-23 23:35:47 +08:00
|
|
|
if (!(this instanceof TemplateMaps)) return new TemplateMaps();
|
|
|
|
|
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
this.redis_pool = redis_pool;
|
2014-02-13 21:55:31 +08:00
|
|
|
this.opts = opts || {};
|
2013-12-18 00:35:12 +08:00
|
|
|
|
|
|
|
// Database containing templates
|
|
|
|
// TODO: allow configuring ?
|
|
|
|
// NOTE: currently it is the same as
|
|
|
|
// the one containing layergroups
|
|
|
|
this.db_signatures = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Map templates are owned by a user that specifies access permissions
|
|
|
|
// for their instances.
|
|
|
|
//
|
|
|
|
// We have the following datastores:
|
|
|
|
//
|
2014-09-25 01:11:53 +08:00
|
|
|
// 1. User templates: set of per-user map templates
|
2013-12-18 00:35:12 +08:00
|
|
|
|
|
|
|
// User templates (HASH:tpl_id->tpl_val)
|
2014-09-25 01:17:51 +08:00
|
|
|
this.key_usr_tpl = dot.template("map_tpl|{{=it.owner}}");
|
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-23 23:35:47 +08:00
|
|
|
util.inherits(TemplateMaps, EventEmitter);
|
|
|
|
|
|
|
|
module.exports = TemplateMaps;
|
|
|
|
|
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
var o = TemplateMaps.prototype;
|
|
|
|
|
|
|
|
//--------------- PRIVATE METHODS --------------------------------
|
|
|
|
|
2014-02-13 21:55:31 +08:00
|
|
|
o._userTemplateLimit = function() {
|
|
|
|
return this.opts['max_user_templates'] || 0;
|
|
|
|
};
|
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
/**
|
|
|
|
* Internal function to communicate with redis
|
|
|
|
*
|
|
|
|
* @param redisFunc - the redis function to execute
|
|
|
|
* @param redisArgs - the arguments for the redis function in an array
|
|
|
|
* @param callback - function to pass results too.
|
|
|
|
*/
|
|
|
|
o._redisCmd = function(redisFunc, redisArgs, callback) {
|
|
|
|
var redisClient;
|
|
|
|
var that = this;
|
|
|
|
var db = that.db_signatures;
|
|
|
|
|
|
|
|
Step(
|
|
|
|
function getRedisClient() {
|
|
|
|
that.redis_pool.acquire(db, this);
|
|
|
|
},
|
|
|
|
function executeQuery(err, data) {
|
|
|
|
if ( err ) throw err;
|
|
|
|
redisClient = data;
|
|
|
|
redisArgs.push(this);
|
|
|
|
redisClient[redisFunc.toUpperCase()].apply(redisClient, redisArgs);
|
|
|
|
},
|
|
|
|
function releaseRedisClient(err, data) {
|
|
|
|
if ( ! _.isUndefined(redisClient) ) that.redis_pool.release(db, redisClient);
|
|
|
|
callback(err, data);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2014-09-25 18:17:32 +08:00
|
|
|
var _reValidIdentifier = /^[a-zA-Z][0-9a-zA-Z_]*$/;
|
2013-12-18 00:35:12 +08:00
|
|
|
o._checkInvalidTemplate = function(template) {
|
|
|
|
if ( template.version != '0.0.1' ) {
|
|
|
|
return new Error("Unsupported template version " + template.version);
|
|
|
|
}
|
|
|
|
var tplname = template.name;
|
|
|
|
if ( ! tplname ) {
|
|
|
|
return new Error("Missing template name");
|
|
|
|
}
|
2014-09-25 18:17:32 +08:00
|
|
|
if ( ! tplname.match(_reValidIdentifier) ) {
|
2013-12-18 00:35:12 +08:00
|
|
|
return new Error("Invalid characters in template name '" + tplname + "'");
|
|
|
|
}
|
|
|
|
|
2015-01-24 01:24:25 +08:00
|
|
|
var invalidError = isInvalidLayergroup(template.layergroup);
|
|
|
|
if (invalidError) {
|
|
|
|
return invalidError;
|
|
|
|
}
|
|
|
|
|
2014-09-25 18:17:32 +08:00
|
|
|
var placeholders = template.placeholders || {};
|
|
|
|
|
|
|
|
var placeholderKeys = Object.keys(placeholders);
|
|
|
|
for (var i = 0, len = placeholderKeys.length; i < len; i++) {
|
|
|
|
var placeholderKey = placeholderKeys[i];
|
|
|
|
|
|
|
|
if (!placeholderKey.match(_reValidIdentifier)) {
|
|
|
|
return new Error("Invalid characters in placeholder name '" + placeholderKey + "'");
|
|
|
|
}
|
|
|
|
if ( ! placeholders[placeholderKey].hasOwnProperty('default') ) {
|
|
|
|
return new Error("Missing default for placeholder '" + placeholderKey + "'");
|
|
|
|
}
|
|
|
|
if ( ! placeholders[placeholderKey].hasOwnProperty('type') ) {
|
|
|
|
return new Error("Missing type for placeholder '" + placeholderKey + "'");
|
|
|
|
}
|
2014-09-25 01:11:53 +08:00
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
var auth = template.auth || {};
|
|
|
|
|
|
|
|
switch ( auth.method ) {
|
|
|
|
case 'open':
|
|
|
|
break;
|
|
|
|
case 'token':
|
|
|
|
if ( ! _.isArray(auth.valid_tokens) )
|
|
|
|
return new Error("Invalid 'token' authentication: missing valid_tokens");
|
|
|
|
if ( ! auth.valid_tokens.length )
|
|
|
|
return new Error("Invalid 'token' authentication: no valid_tokens");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return new Error("Unsupported authentication method: " + auth.method);
|
|
|
|
break;
|
|
|
|
}
|
2014-02-06 19:24:14 +08:00
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
return false;
|
2013-12-18 00:35:12 +08:00
|
|
|
};
|
|
|
|
|
2015-01-24 01:24:25 +08:00
|
|
|
function isInvalidLayergroup(layergroup) {
|
|
|
|
if (!layergroup) {
|
|
|
|
return new Error('Missing layergroup');
|
|
|
|
}
|
|
|
|
|
2015-01-26 22:51:10 +08:00
|
|
|
var layers = layergroup.layers;
|
|
|
|
|
|
|
|
if (!_.isArray(layers) || layers.length === 0) {
|
|
|
|
return new Error('Missing or empty layers array from layergroup config');
|
|
|
|
}
|
|
|
|
|
|
|
|
var invalidLayers = layers
|
|
|
|
.map(function(layer, layerIndex) {
|
|
|
|
return layer.options ? null : layerIndex;
|
|
|
|
})
|
|
|
|
.filter(function(layerIndex) {
|
|
|
|
return layerIndex !== null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (invalidLayers.length) {
|
|
|
|
return new Error('Missing `options` in layergroup config for layers: ' + invalidLayers.join(', '));
|
2015-01-24 01:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
function templateDefaults(template) {
|
|
|
|
var templateAuth = _.defaults({}, template.auth || {}, {
|
|
|
|
method: 'open'
|
|
|
|
});
|
|
|
|
return _.defaults({ auth: templateAuth }, template, {
|
|
|
|
placeholders: {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-01-23 02:28:59 +08:00
|
|
|
//--------------- PUBLIC API -------------------------------------
|
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
// Add a template
|
|
|
|
//
|
|
|
|
// NOTE: locks user+template_name or fails
|
|
|
|
//
|
|
|
|
// @param owner cartodb username of the template owner
|
|
|
|
//
|
|
|
|
// @param template layergroup template, see
|
|
|
|
// http://github.com/CartoDB/Windshaft-cartodb/wiki/Template-maps#template-format
|
|
|
|
//
|
|
|
|
// @param callback function(err, tpl_id)
|
|
|
|
// Return template identifier (only valid for given user)
|
|
|
|
//
|
|
|
|
o.addTemplate = function(owner, template, callback) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var self = this;
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
template = templateDefaults(template);
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
var invalidError = this._checkInvalidTemplate(template);
|
|
|
|
if ( invalidError ) {
|
|
|
|
return callback(invalidError);
|
|
|
|
}
|
|
|
|
|
|
|
|
var templateName = template.name;
|
|
|
|
var userTemplatesKey = this.key_usr_tpl({ owner:owner });
|
|
|
|
var limit = this._userTemplateLimit();
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
Step(
|
|
|
|
function checkLimit() {
|
|
|
|
if ( ! limit ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
self._redisCmd('HLEN', [ userTemplatesKey ], this);
|
|
|
|
},
|
|
|
|
function installTemplateIfDoesNotExist(err, numberOfTemplates) {
|
|
|
|
if ( err ) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
if ( limit && numberOfTemplates >= limit ) {
|
|
|
|
throw new Error("User '" + owner + "' reached limit on number of templates " +
|
|
|
|
"("+ numberOfTemplates + "/" + limit + ")");
|
|
|
|
}
|
|
|
|
self._redisCmd('HSETNX', [ userTemplatesKey, templateName, JSON.stringify(template) ], this);
|
|
|
|
},
|
|
|
|
function validateInstallation(err, wasSet) {
|
|
|
|
if ( err ) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
if ( ! wasSet ) {
|
|
|
|
throw new Error("Template '" + templateName + "' of user '" + owner + "' already exists");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
function finish(err) {
|
2015-01-23 23:35:47 +08:00
|
|
|
if (!err) {
|
|
|
|
self.emit('add', owner, templateName, template);
|
|
|
|
}
|
|
|
|
|
2015-01-23 01:38:42 +08:00
|
|
|
callback(err, templateName, template);
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Delete a template
|
|
|
|
//
|
|
|
|
// @param owner cartodb username of the template owner
|
|
|
|
//
|
|
|
|
// @param tpl_id template identifier as returned
|
|
|
|
// by addTemplate or listTemplates
|
|
|
|
//
|
|
|
|
// @param callback function(err)
|
|
|
|
//
|
|
|
|
o.delTemplate = function(owner, tpl_id, callback) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var self = this;
|
|
|
|
Step(
|
|
|
|
function deleteTemplate() {
|
|
|
|
self._redisCmd('HDEL', [ self.key_usr_tpl({ owner:owner }), tpl_id ], this);
|
|
|
|
},
|
|
|
|
function handleDeletion(err, deleted) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
if (!deleted) {
|
|
|
|
throw new Error("Template '" + tpl_id + "' of user '" + owner + "' does not exist");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
function finish(err) {
|
2015-01-23 23:35:47 +08:00
|
|
|
if (!err) {
|
|
|
|
self.emit('delete', owner, tpl_id);
|
|
|
|
}
|
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
callback(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
2015-01-22 22:40:40 +08:00
|
|
|
);
|
2013-12-18 00:35:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Update a template
|
|
|
|
//
|
|
|
|
// NOTE: locks user+template_name or fails
|
|
|
|
//
|
|
|
|
// Also deletes and re-creates associated authentication certificate,
|
|
|
|
// which in turn deletes all instance signatures
|
|
|
|
//
|
|
|
|
// @param owner cartodb username of the template owner
|
|
|
|
//
|
|
|
|
// @param tpl_id template identifier as returned by addTemplate
|
|
|
|
//
|
|
|
|
// @param template layergroup template, see
|
|
|
|
// http://github.com/CartoDB/Windshaft-cartodb/wiki/Template-maps#template-format
|
|
|
|
//
|
|
|
|
// @param callback function(err)
|
|
|
|
//
|
|
|
|
o.updTemplate = function(owner, tpl_id, template, callback) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var self = this;
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
template = templateDefaults(template);
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
var invalidError = this._checkInvalidTemplate(template);
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
if ( invalidError ) {
|
|
|
|
return callback(invalidError);
|
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
var templateName = template.name;
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
if ( tpl_id != templateName ) {
|
|
|
|
return callback(new Error("Cannot update name of a map template ('" + tpl_id + "' != '" + templateName + "')"));
|
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
var userTemplatesKey = this.key_usr_tpl({ owner:owner });
|
|
|
|
|
|
|
|
Step(
|
|
|
|
function getExistingTemplate() {
|
|
|
|
self._redisCmd('HGET', [ userTemplatesKey, tpl_id ], this);
|
|
|
|
},
|
|
|
|
function updateTemplate(err, currentTemplate) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
if (!currentTemplate) {
|
|
|
|
throw new Error("Template '" + tpl_id + "' of user '" + owner + "' does not exist");
|
|
|
|
}
|
|
|
|
self._redisCmd('HSET', [ userTemplatesKey, templateName, JSON.stringify(template) ], this);
|
|
|
|
},
|
|
|
|
function handleTemplateUpdate(err, didSetNewField) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
if (didSetNewField) {
|
|
|
|
console.warn('New template created on update operation');
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
function finish(err) {
|
2015-01-23 23:35:47 +08:00
|
|
|
if (!err) {
|
|
|
|
self.emit('update', owner, templateName, template);
|
|
|
|
}
|
|
|
|
|
2015-01-23 01:38:42 +08:00
|
|
|
callback(err, template);
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
2015-01-22 22:40:40 +08:00
|
|
|
);
|
2013-12-18 00:35:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// List user templates
|
|
|
|
//
|
|
|
|
// @param owner cartodb username of the templates owner
|
|
|
|
//
|
|
|
|
// @param callback function(err, tpl_id_list)
|
|
|
|
// Returns a list of template identifiers
|
|
|
|
//
|
|
|
|
o.listTemplates = function(owner, callback) {
|
2014-09-25 01:17:51 +08:00
|
|
|
this._redisCmd('HKEYS', [ this.key_usr_tpl({owner:owner}) ], callback);
|
2013-12-18 00:35:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Get a templates
|
|
|
|
//
|
|
|
|
// @param owner cartodb username of the template owner
|
|
|
|
//
|
|
|
|
// @param tpl_id template identifier as returned
|
|
|
|
// by addTemplate or listTemplates
|
|
|
|
//
|
|
|
|
// @param callback function(err, template)
|
|
|
|
// Return full template definition
|
|
|
|
//
|
|
|
|
o.getTemplate = function(owner, tpl_id, callback) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var self = this;
|
|
|
|
Step(
|
|
|
|
function getTemplate() {
|
|
|
|
self._redisCmd('HGET', [ self.key_usr_tpl({owner:owner}), tpl_id ], this);
|
|
|
|
},
|
|
|
|
function parseTemplate(err, tpl_val) {
|
|
|
|
if ( err ) throw err;
|
|
|
|
return JSON.parse(tpl_val);
|
|
|
|
},
|
|
|
|
function finish(err, tpl) {
|
|
|
|
callback(err, tpl);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
o.isAuthorized = function(template, authTokens) {
|
|
|
|
if (!template) {
|
|
|
|
return false;
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
2015-01-22 22:40:40 +08:00
|
|
|
|
|
|
|
authTokens = _.isArray(authTokens) ? authTokens : [authTokens];
|
|
|
|
|
|
|
|
var templateAuth = template.auth;
|
|
|
|
|
|
|
|
if (!templateAuth) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-29 00:29:50 +08:00
|
|
|
if (_.isString(templateAuth) && templateAuth === 'open') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
if (templateAuth.method === 'open') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (templateAuth.method === 'token') {
|
|
|
|
return _.intersection(templateAuth.valid_tokens, authTokens).length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-12-18 00:35:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Perform placeholder substitutions on a template
|
|
|
|
//
|
|
|
|
// @param template a template object (will not be modified)
|
|
|
|
//
|
|
|
|
// @param params an object containing named subsitution parameters
|
|
|
|
// Only the ones found in the template's placeholders object
|
|
|
|
// will be used, with missing ones taking default values.
|
|
|
|
//
|
|
|
|
// @returns a layergroup configuration
|
|
|
|
//
|
|
|
|
// @throws Error on malformed template or parameter
|
|
|
|
//
|
2014-09-25 18:16:34 +08:00
|
|
|
var _reNumber = /^([-+]?[\d\.]?\d+([eE][+-]?\d+)?)$/,
|
|
|
|
_reCSSColorName = /^[a-zA-Z]+$/,
|
|
|
|
_reCSSColorVal = /^#[0-9a-fA-F]{3,6}$/;
|
|
|
|
|
|
|
|
_replaceVars = function(str, params) {
|
2013-12-18 00:35:12 +08:00
|
|
|
//return _.template(str, params); // lazy way, possibly dangerous
|
|
|
|
// Construct regular expressions for each param
|
2014-09-25 18:04:52 +08:00
|
|
|
Object.keys(params).forEach(function(k) {
|
|
|
|
str = str.replace(new RegExp("<%=\\s*" + k + "\\s*%>", "g"), params[k]);
|
|
|
|
});
|
|
|
|
return str;
|
2013-12-18 00:35:12 +08:00
|
|
|
};
|
|
|
|
o.instance = function(template, params) {
|
|
|
|
var all_params = {};
|
2014-09-25 18:17:32 +08:00
|
|
|
var phold = template.placeholders || {};
|
|
|
|
Object.keys(phold).forEach(function(k) {
|
2013-12-18 00:35:12 +08:00
|
|
|
var val = params.hasOwnProperty(k) ? params[k] : phold[k].default;
|
|
|
|
var type = phold[k].type;
|
|
|
|
// properly escape
|
|
|
|
if ( type === 'sql_literal' ) {
|
|
|
|
// duplicate any single-quote
|
|
|
|
val = val.replace(/'/g, "''");
|
|
|
|
}
|
|
|
|
else if ( type === 'sql_ident' ) {
|
|
|
|
// duplicate any double-quote
|
|
|
|
val = val.replace(/"/g, '""');
|
|
|
|
}
|
|
|
|
else if ( type === 'number' ) {
|
|
|
|
// check it's a number
|
2014-09-25 18:16:34 +08:00
|
|
|
if ( typeof(val) !== 'number' && ! val.match(_reNumber) ) {
|
2013-12-18 00:35:12 +08:00
|
|
|
throw new Error("Invalid number value for template parameter '"
|
|
|
|
+ k + "': " + val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( type === 'css_color' ) {
|
|
|
|
// check it only contains letters or
|
|
|
|
// starts with # and only contains hexdigits
|
2014-09-25 18:16:34 +08:00
|
|
|
if ( ! val.match(_reCSSColorName) && ! val.match(_reCSSColorVal) ) {
|
2013-12-18 00:35:12 +08:00
|
|
|
throw new Error("Invalid css_color value for template parameter '"
|
|
|
|
+ k + "': " + val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// NOTE: should be checked at template create/update time
|
|
|
|
throw new Error("Invalid placeholder type '" + type + "'");
|
|
|
|
}
|
|
|
|
all_params[k] = val;
|
2014-09-25 18:17:32 +08:00
|
|
|
});
|
2013-12-18 00:35:12 +08:00
|
|
|
|
|
|
|
// NOTE: we're deep-cloning the layergroup here
|
|
|
|
var layergroup = JSON.parse(JSON.stringify(template.layergroup));
|
|
|
|
for (var i=0; i<layergroup.layers.length; ++i) {
|
|
|
|
var lyropt = layergroup.layers[i].options;
|
2014-09-25 18:16:34 +08:00
|
|
|
if ( lyropt.cartocss ) lyropt.cartocss = _replaceVars(lyropt.cartocss, all_params);
|
|
|
|
if ( lyropt.sql) lyropt.sql = _replaceVars(lyropt.sql, all_params);
|
2013-12-18 00:35:12 +08:00
|
|
|
// Anything else ?
|
|
|
|
}
|
2015-01-22 22:40:40 +08:00
|
|
|
|
|
|
|
// extra information about the template
|
|
|
|
layergroup.template = {
|
|
|
|
name: template.name,
|
|
|
|
auth: template.auth
|
|
|
|
};
|
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
return layergroup;
|
|
|
|
};
|
|
|
|
|
2014-02-10 22:30:35 +08:00
|
|
|
// Return a fingerPrint of the object
|
|
|
|
o.fingerPrint = function(template) {
|
|
|
|
return crypto.createHash('md5')
|
|
|
|
.update(JSON.stringify(template))
|
|
|
|
.digest('hex')
|
|
|
|
;
|
|
|
|
};
|