2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-03-16 07:21:55 +08:00
|
|
|
var crypto = require('crypto');
|
2015-09-18 22:40:55 +08:00
|
|
|
var debug = require('debug')('windshaft:templates');
|
2015-03-16 07:21:55 +08:00
|
|
|
var _ = require('underscore');
|
|
|
|
var 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
|
|
|
|
//
|
2019-11-14 18:36:47 +08:00
|
|
|
// @param redisPool an instance of a "redis-mpool"
|
2013-12-18 00:35:12 +08:00
|
|
|
// 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
|
|
|
|
//
|
2016-01-29 02:44:25 +08:00
|
|
|
//
|
2019-11-14 18:36:47 +08:00
|
|
|
function TemplateMaps (redisPool, opts) {
|
2019-10-22 01:07:24 +08:00
|
|
|
if (!(this instanceof TemplateMaps)) {
|
|
|
|
return new TemplateMaps();
|
|
|
|
}
|
2015-01-23 23:35:47 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
EventEmitter.call(this);
|
2015-01-23 23:35:47 +08:00
|
|
|
|
2019-11-14 18:36:47 +08:00
|
|
|
this.redisPool = redisPool;
|
2019-10-22 01:07:24 +08:00
|
|
|
this.opts = opts || {};
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
// Database containing templates
|
|
|
|
// TODO: allow configuring ?
|
|
|
|
// NOTE: currently it is the same as
|
|
|
|
// the one containing layergroups
|
|
|
|
this.db_signatures = 0;
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
//
|
|
|
|
// Map templates are owned by a user that specifies access permissions
|
|
|
|
// for their instances.
|
|
|
|
//
|
|
|
|
// We have the following datastores:
|
|
|
|
//
|
|
|
|
// 1. User templates: set of per-user map templates
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2019-11-14 18:36:47 +08:00
|
|
|
// User templates (HASH:tplId->tpl_val)
|
2019-10-22 01:07:24 +08:00
|
|
|
this.key_usr_tpl = dot.template('map_tpl|{{=it.owner}}');
|
2014-09-25 01:17:51 +08:00
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-23 23:35:47 +08:00
|
|
|
util.inherits(TemplateMaps, EventEmitter);
|
|
|
|
|
|
|
|
module.exports = TemplateMaps;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
// --------------- PRIVATE METHODS --------------------------------
|
2015-01-23 23:35:47 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
TemplateMaps.prototype._userTemplateLimit = function () {
|
|
|
|
return this.opts.max_user_templates || 0;
|
2014-02-13 21:55:31 +08:00
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
*/
|
2019-10-22 01:07:24 +08:00
|
|
|
TemplateMaps.prototype._redisCmd = function (redisFunc, redisArgs, callback) {
|
2019-11-14 18:36:47 +08:00
|
|
|
this.redisPool.acquire(this.db_signatures, (err, redisClient) => {
|
2018-08-01 17:39:07 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2018-08-01 17:48:12 +08:00
|
|
|
redisClient[redisFunc.toUpperCase()](...redisArgs, (err, data) => {
|
2019-11-14 18:36:47 +08:00
|
|
|
this.redisPool.release(this.db_signatures, redisClient);
|
2018-08-02 17:35:54 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
return callback(null, data);
|
2018-08-01 17:25:55 +08:00
|
|
|
});
|
|
|
|
});
|
2013-12-18 00:35:12 +08:00
|
|
|
};
|
|
|
|
|
2019-10-25 01:18:47 +08:00
|
|
|
var _reValidNameIdentifier = /^[a-z0-9][0-9a-z_-]*$/i;
|
2015-06-04 23:58:24 +08:00
|
|
|
var _reValidPlaceholderIdentifier = /^[a-z][0-9a-z_]*$/i;
|
2020-04-04 23:29:33 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
TemplateMaps.prototype._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');
|
|
|
|
}
|
|
|
|
if (!tplname.match(_reValidNameIdentifier)) {
|
|
|
|
return new Error("Invalid characters in template name '" + tplname + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
var invalidError = isInvalidLayergroup(template.layergroup);
|
|
|
|
if (invalidError) {
|
|
|
|
return invalidError;
|
|
|
|
}
|
|
|
|
|
|
|
|
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(_reValidPlaceholderIdentifier)) {
|
|
|
|
return new Error("Invalid characters in placeholder name '" + placeholderKey + "'");
|
|
|
|
}
|
2019-10-22 05:33:27 +08:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(placeholders[placeholderKey], 'default')) {
|
2019-10-22 01:07:24 +08:00
|
|
|
return new Error("Missing default for placeholder '" + placeholderKey + "'");
|
|
|
|
}
|
2019-10-22 05:33:27 +08:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(placeholders[placeholderKey], 'type')) {
|
2019-10-22 01:07:24 +08:00
|
|
|
return new Error("Missing type for placeholder '" + placeholderKey + "'");
|
|
|
|
}
|
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
var auth = template.auth || {};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
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);
|
2015-01-22 22:40:40 +08:00
|
|
|
}
|
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
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function isInvalidLayergroup (layergroup) {
|
2015-01-24 01:24:25 +08:00
|
|
|
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
|
2019-10-22 01:07:24 +08:00
|
|
|
.map(function (layer, layerIndex) {
|
2015-01-26 22:51:10 +08:00
|
|
|
return layer.options ? null : layerIndex;
|
|
|
|
})
|
2019-10-22 01:07:24 +08:00
|
|
|
.filter(function (layerIndex) {
|
2015-01-26 22:51:10 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function templateDefaults (template) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var templateAuth = _.defaults({}, template.auth || {}, {
|
|
|
|
method: 'open'
|
|
|
|
});
|
|
|
|
return _.defaults({ auth: templateAuth }, template, {
|
|
|
|
placeholders: {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-01 19:13:25 +08:00
|
|
|
/**
|
|
|
|
* Checks if the if the user reaches the templetes limit
|
|
|
|
*
|
|
|
|
* @param userTemplatesKey user templat key in Redis
|
|
|
|
* @param owner cartodb username of the template owner
|
|
|
|
* @param callback returns error if the user reaches the limit
|
|
|
|
*/
|
2019-10-22 01:07:24 +08:00
|
|
|
TemplateMaps.prototype._checkUserTemplatesLimit = function (userTemplatesKey, owner, callback) {
|
2018-08-01 19:13:25 +08:00
|
|
|
const limit = this._userTemplateLimit();
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
if (!limit) {
|
2018-08-01 22:02:53 +08:00
|
|
|
return callback();
|
2018-08-01 19:13:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this._redisCmd('HLEN', [userTemplatesKey], (err, numberOfTemplates) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numberOfTemplates >= limit) {
|
|
|
|
const limitReachedError = new Error(
|
|
|
|
`User '${owner}' reached limit on number of templates (${numberOfTemplates}/${limit})`
|
|
|
|
);
|
|
|
|
limitReachedError.http_status = 409;
|
|
|
|
return callback(limitReachedError);
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:02:53 +08:00
|
|
|
return callback();
|
2018-08-01 19:13:25 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
// --------------- PUBLIC API -------------------------------------
|
2015-01-23 02:28:59 +08:00
|
|
|
|
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
|
|
|
|
//
|
2019-11-14 18:36:47 +08:00
|
|
|
// @param callback function(err, tplId)
|
2013-12-18 00:35:12 +08:00
|
|
|
// Return template identifier (only valid for given user)
|
|
|
|
//
|
2019-10-22 01:07:24 +08:00
|
|
|
TemplateMaps.prototype.addTemplate = function (owner, template, callback) {
|
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);
|
2018-08-01 18:33:59 +08:00
|
|
|
if (invalidError) {
|
2015-01-22 22:40:40 +08:00
|
|
|
return callback(invalidError);
|
|
|
|
}
|
|
|
|
|
2018-08-01 18:33:59 +08:00
|
|
|
var userTemplatesKey = this.key_usr_tpl({ owner });
|
2018-08-01 18:20:01 +08:00
|
|
|
|
2018-08-01 19:13:25 +08:00
|
|
|
this._checkUserTemplatesLimit(userTemplatesKey, owner, err => {
|
2018-08-01 18:26:40 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2018-08-01 18:38:50 +08:00
|
|
|
let templateString;
|
|
|
|
try {
|
|
|
|
templateString = JSON.stringify(template);
|
|
|
|
} catch (error) {
|
|
|
|
return callback(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._redisCmd('HSETNX', [userTemplatesKey, template.name, templateString], (err, wasSet) => {
|
2018-08-01 18:26:40 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
2018-08-01 18:20:01 +08:00
|
|
|
}
|
2015-01-23 23:35:47 +08:00
|
|
|
|
2018-08-01 18:26:40 +08:00
|
|
|
if (!wasSet) {
|
2018-08-02 17:38:54 +08:00
|
|
|
var templateExistsError = new Error(`Template '${template.name}' of user '${owner}' already exists`);
|
|
|
|
return callback(templateExistsError);
|
2018-08-01 18:20:01 +08:00
|
|
|
}
|
2018-08-01 18:10:09 +08:00
|
|
|
|
2018-08-01 18:32:20 +08:00
|
|
|
this.emit('add', owner, template.name, template);
|
2018-08-01 22:22:43 +08:00
|
|
|
return callback(null, template.name, template);
|
2018-08-01 18:20:01 +08:00
|
|
|
});
|
|
|
|
});
|
2013-12-18 00:35:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Delete a template
|
|
|
|
//
|
|
|
|
// @param owner cartodb username of the template owner
|
|
|
|
//
|
2019-11-14 18:36:47 +08:00
|
|
|
// @param tplId template identifier as returned
|
2013-12-18 00:35:12 +08:00
|
|
|
// by addTemplate or listTemplates
|
|
|
|
//
|
|
|
|
// @param callback function(err)
|
|
|
|
//
|
2019-11-14 18:36:47 +08:00
|
|
|
TemplateMaps.prototype.delTemplate = function (owner, tplId, callback) {
|
|
|
|
this._redisCmd('HDEL', [this.key_usr_tpl({ owner: owner }), tplId], (err, deleted) => {
|
2018-08-01 19:27:07 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
2018-08-01 19:20:10 +08:00
|
|
|
}
|
|
|
|
|
2018-08-01 19:27:07 +08:00
|
|
|
if (!deleted) {
|
2019-11-14 18:36:47 +08:00
|
|
|
return callback(new Error(`Template '${tplId}' of user '${owner}' does not exist`));
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
2018-08-01 19:20:10 +08:00
|
|
|
|
2019-11-14 18:36:47 +08:00
|
|
|
this.emit('delete', owner, tplId);
|
2018-08-01 19:27:07 +08:00
|
|
|
return callback();
|
2018-08-01 19:20:10 +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
|
|
|
|
//
|
2019-11-14 18:36:47 +08:00
|
|
|
// @param tplId template identifier as returned by addTemplate
|
2013-12-18 00:35:12 +08:00
|
|
|
//
|
|
|
|
// @param template layergroup template, see
|
|
|
|
// http://github.com/CartoDB/Windshaft-cartodb/wiki/Template-maps#template-format
|
|
|
|
//
|
|
|
|
// @param callback function(err)
|
2016-01-29 02:44:25 +08:00
|
|
|
//
|
2019-11-14 18:36:47 +08:00
|
|
|
TemplateMaps.prototype.updTemplate = function (owner, tplId, template, callback) {
|
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);
|
2018-08-01 21:40:40 +08:00
|
|
|
if (invalidError) {
|
2015-01-22 22:40:40 +08:00
|
|
|
return callback(invalidError);
|
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2019-11-14 18:36:47 +08:00
|
|
|
if (tplId !== template.name) {
|
|
|
|
return callback(new Error(`Cannot update name of a map template ('${tplId}' != '${template.name}')`));
|
2015-01-22 22:40:40 +08:00
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2018-08-01 21:40:40 +08:00
|
|
|
var userTemplatesKey = this.key_usr_tpl({ owner });
|
2015-09-24 01:59:39 +08:00
|
|
|
|
2019-11-14 18:36:47 +08:00
|
|
|
this._redisCmd('HGET', [userTemplatesKey, tplId], (err, beforeUpdateTemplate) => {
|
2018-08-01 21:32:44 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2018-08-01 21:43:45 +08:00
|
|
|
if (!beforeUpdateTemplate) {
|
2019-11-14 18:36:47 +08:00
|
|
|
return callback(new Error(`Template '${tplId}' of user '${owner}' does not exist`));
|
2018-08-01 21:28:15 +08:00
|
|
|
}
|
2018-08-01 21:32:44 +08:00
|
|
|
|
2018-08-01 21:48:24 +08:00
|
|
|
let templateString;
|
|
|
|
try {
|
2018-08-01 21:53:41 +08:00
|
|
|
templateString = JSON.stringify(template);
|
2018-08-01 21:48:24 +08:00
|
|
|
} catch (error) {
|
|
|
|
return callback(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._redisCmd('HSET', [userTemplatesKey, template.name, templateString], (err, didSetNewField) => {
|
2018-08-01 21:32:44 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
if (didSetNewField) {
|
2015-09-18 22:40:55 +08:00
|
|
|
debug('New template created on update operation');
|
2015-01-22 22:40:40 +08:00
|
|
|
}
|
2018-08-01 21:28:15 +08:00
|
|
|
|
2018-08-01 21:48:24 +08:00
|
|
|
let beforeUpdateTemplateObject;
|
|
|
|
try {
|
2018-08-01 21:53:41 +08:00
|
|
|
beforeUpdateTemplateObject = JSON.parse(beforeUpdateTemplate);
|
2018-08-01 21:48:24 +08:00
|
|
|
} catch (error) {
|
|
|
|
return callback(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.fingerPrint(beforeUpdateTemplateObject) !== this.fingerPrint(template)) {
|
2018-08-01 21:43:45 +08:00
|
|
|
this.emit('update', owner, template.name, template);
|
2015-01-23 23:35:47 +08:00
|
|
|
}
|
|
|
|
|
2018-08-01 22:02:53 +08:00
|
|
|
return callback(null, template);
|
2018-08-01 21:28:15 +08:00
|
|
|
});
|
|
|
|
});
|
2013-12-18 00:35:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// List user templates
|
|
|
|
//
|
|
|
|
// @param owner cartodb username of the templates owner
|
|
|
|
//
|
2019-11-14 18:36:47 +08:00
|
|
|
// @param callback function(err, tplId_list)
|
2013-12-18 00:35:12 +08:00
|
|
|
// Returns a list of template identifiers
|
2016-01-29 02:44:25 +08:00
|
|
|
//
|
2019-10-22 01:07:24 +08:00
|
|
|
TemplateMaps.prototype.listTemplates = function (owner, callback) {
|
|
|
|
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
|
|
|
|
//
|
2019-11-14 18:36:47 +08:00
|
|
|
// @param tplId template identifier as returned
|
2013-12-18 00:35:12 +08:00
|
|
|
// by addTemplate or listTemplates
|
|
|
|
//
|
|
|
|
// @param callback function(err, template)
|
|
|
|
// Return full template definition
|
|
|
|
//
|
2019-11-14 18:36:47 +08:00
|
|
|
TemplateMaps.prototype.getTemplate = function (owner, tplId, callback) {
|
|
|
|
this._redisCmd('HGET', [this.key_usr_tpl({ owner: owner }), tplId], (err, template) => {
|
2018-08-01 22:00:31 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
let templateObject;
|
|
|
|
try {
|
|
|
|
templateObject = JSON.parse(template);
|
|
|
|
} catch (error) {
|
|
|
|
return callback(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, templateObject);
|
2018-08-01 21:57:22 +08:00
|
|
|
});
|
2015-01-22 22:40:40 +08:00
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
TemplateMaps.prototype.isAuthorized = function (template, authTokens) {
|
2015-01-22 22:40:40 +08:00
|
|
|
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.
|
|
|
|
//
|
2016-01-29 02:44:25 +08:00
|
|
|
// @returns a layergroup configuration
|
2013-12-18 00:35:12 +08:00
|
|
|
//
|
|
|
|
// @throws Error on malformed template or parameter
|
|
|
|
//
|
2019-10-25 01:18:47 +08:00
|
|
|
var _reNumber = /^([-+]?[\d\.]?\d+([eE][+-]?\d+)?)$/; // eslint-disable-line no-useless-escape
|
2019-10-22 01:07:24 +08:00
|
|
|
var _reCSSColorName = /^[a-zA-Z]+$/;
|
|
|
|
var _reCSSColorVal = /^#[0-9a-fA-F]{3,6}$/;
|
2014-09-25 18:16:34 +08:00
|
|
|
|
2015-03-16 06:58:12 +08:00
|
|
|
function _replaceVars (str, params) {
|
2017-04-26 23:01:21 +08:00
|
|
|
// Construct regular expressions for each param
|
2019-10-22 01:07:24 +08:00
|
|
|
Object.keys(params).forEach(function (k) {
|
|
|
|
str = str.replace(new RegExp('<%=\\s*' + k + '\\s*%>', 'g'), params[k]);
|
2014-09-25 18:04:52 +08:00
|
|
|
});
|
|
|
|
return str;
|
2015-03-16 06:58:12 +08:00
|
|
|
}
|
2017-04-26 01:40:12 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function isObject (val) {
|
|
|
|
return (_.isObject(val) && !_.isArray(val) && !_.isFunction(val));
|
2017-04-26 01:27:31 +08:00
|
|
|
}
|
2017-04-26 01:40:12 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
TemplateMaps.prototype.instance = function (template, params) {
|
2019-11-14 18:36:47 +08:00
|
|
|
var allParams = {};
|
2019-10-22 01:07:24 +08:00
|
|
|
var phold = template.placeholders || {};
|
|
|
|
Object.keys(phold).forEach(function (k) {
|
2019-10-22 05:33:27 +08:00
|
|
|
var val = Object.prototype.hasOwnProperty.call(params, k) ? params[k] : phold[k].default;
|
2019-10-22 01:07:24 +08:00
|
|
|
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
|
|
|
|
if (typeof (val) !== 'number' && !val.match(_reNumber)) {
|
|
|
|
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
|
|
|
|
if (!val.match(_reCSSColorName) && !val.match(_reCSSColorVal)) {
|
|
|
|
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 + "'");
|
|
|
|
}
|
2019-11-14 18:36:47 +08:00
|
|
|
allParams[k] = val;
|
2019-10-22 01:07:24 +08:00
|
|
|
});
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
// NOTE: we're deep-cloning the layergroup here
|
|
|
|
var layergroup = JSON.parse(JSON.stringify(template.layergroup));
|
2017-04-25 20:34:17 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
if (layergroup.buffersize && isObject(layergroup.buffersize)) {
|
|
|
|
Object.keys(layergroup.buffersize).forEach(function (k) {
|
2019-11-14 18:36:47 +08:00
|
|
|
layergroup.buffersize[k] = parseInt(_replaceVars(layergroup.buffersize[k], allParams), 10);
|
2019-10-22 01:07:24 +08:00
|
|
|
});
|
|
|
|
}
|
2017-04-25 20:34:17 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
for (var i = 0; i < layergroup.layers.length; ++i) {
|
|
|
|
var lyropt = layergroup.layers[i].options;
|
2016-04-06 23:43:25 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
if (params.styles && params.styles[i]) {
|
2016-04-06 23:43:25 +08:00
|
|
|
// dynamic styling for this layer
|
2019-10-22 01:07:24 +08:00
|
|
|
lyropt.cartocss = params.styles[i];
|
|
|
|
} else if (lyropt.cartocss) {
|
2019-11-14 18:36:47 +08:00
|
|
|
lyropt.cartocss = _replaceVars(lyropt.cartocss, allParams);
|
2019-10-22 01:07:24 +08:00
|
|
|
}
|
|
|
|
if (lyropt.sql) {
|
2019-11-14 18:36:47 +08:00
|
|
|
lyropt.sql = _replaceVars(lyropt.sql, allParams);
|
2019-10-22 01:07:24 +08:00
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
// Anything else ?
|
2019-10-22 01:07:24 +08:00
|
|
|
}
|
2015-01-22 22:40:40 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
// extra information about the template
|
|
|
|
layergroup.template = {
|
|
|
|
name: template.name,
|
|
|
|
auth: template.auth
|
|
|
|
};
|
2015-01-22 22:40:40 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
return layergroup;
|
2013-12-18 00:35:12 +08:00
|
|
|
};
|
|
|
|
|
2014-02-10 22:30:35 +08:00
|
|
|
// Return a fingerPrint of the object
|
2019-10-22 01:07:24 +08:00
|
|
|
TemplateMaps.prototype.fingerPrint = function (template) {
|
|
|
|
return crypto.createHash('md5')
|
|
|
|
.update(JSON.stringify(template))
|
|
|
|
.digest('hex')
|
|
|
|
;
|
2014-02-10 22:30:35 +08:00
|
|
|
};
|
2015-04-27 17:55:05 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
module.exports.templateName = function templateName (templateId) {
|
2015-04-27 17:55:05 +08:00
|
|
|
var templateIdTokens = templateId.split('@');
|
|
|
|
var name = templateIdTokens[0];
|
|
|
|
|
|
|
|
if (templateIdTokens.length > 1) {
|
|
|
|
name = templateIdTokens[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
};
|