2015-07-08 19:11:57 +08:00
|
|
|
var step = require('step');
|
|
|
|
var assert = require('assert');
|
|
|
|
var templateName = require('../backends/template_maps').templateName;
|
2015-09-16 22:18:26 +08:00
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var BaseController = require('./base');
|
|
|
|
|
2015-07-08 19:27:56 +08:00
|
|
|
var cors = require('../middleware/cors');
|
2015-09-30 23:17:01 +08:00
|
|
|
var userMiddleware = require('../middleware/user');
|
2015-07-08 19:11:57 +08:00
|
|
|
|
|
|
|
|
2015-07-13 21:05:03 +08:00
|
|
|
/**
|
|
|
|
* @param {AuthApi} authApi
|
2015-09-16 22:18:26 +08:00
|
|
|
* @param {PgConnection} pgConnection
|
2015-10-01 00:00:54 +08:00
|
|
|
* @param {TemplateMaps} templateMaps
|
2015-07-13 21:05:03 +08:00
|
|
|
* @constructor
|
|
|
|
*/
|
2017-09-26 01:40:27 +08:00
|
|
|
function NamedMapsAdminController(authApi, templateMaps) {
|
|
|
|
BaseController.call(this);
|
2015-09-16 22:18:26 +08:00
|
|
|
|
2015-07-13 21:05:03 +08:00
|
|
|
this.authApi = authApi;
|
2015-10-01 00:00:54 +08:00
|
|
|
this.templateMaps = templateMaps;
|
2015-07-08 19:11:57 +08:00
|
|
|
}
|
|
|
|
|
2015-09-17 00:09:39 +08:00
|
|
|
util.inherits(NamedMapsAdminController, BaseController);
|
|
|
|
|
2015-07-08 19:11:57 +08:00
|
|
|
module.exports = NamedMapsAdminController;
|
|
|
|
|
2017-09-22 22:45:34 +08:00
|
|
|
NamedMapsAdminController.prototype.register = function (router) {
|
|
|
|
router.options('/:template_id', cors('Content-Type'));
|
|
|
|
|
|
|
|
router.use(
|
|
|
|
cors(),
|
|
|
|
userMiddleware
|
|
|
|
);
|
|
|
|
|
|
|
|
router.post('/', this.create.bind(this));
|
|
|
|
router.put('/:template_id', this.update.bind(this));
|
|
|
|
router.get('/:template_id', this.retrieve.bind(this));
|
|
|
|
router.delete('/:template_id', this.destroy.bind(this));
|
|
|
|
router.get('/', this.list.bind(this));
|
2015-07-08 19:11:57 +08:00
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
NamedMapsAdminController.prototype.create = function(req, res, next) {
|
2015-07-08 19:11:57 +08:00
|
|
|
var self = this;
|
|
|
|
|
2015-07-08 21:34:46 +08:00
|
|
|
var cdbuser = req.context.user;
|
2015-07-08 19:11:57 +08:00
|
|
|
|
|
|
|
step(
|
|
|
|
function checkPerms(){
|
2015-07-13 21:05:03 +08:00
|
|
|
self.authApi.authorizedByAPIKey(cdbuser, req, this);
|
2015-07-08 19:11:57 +08:00
|
|
|
},
|
2016-03-12 01:28:14 +08:00
|
|
|
function addTemplate(err, authenticated) {
|
2015-07-08 19:11:57 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
ifUnauthenticated(authenticated, 'Only authenticated users can get template maps');
|
|
|
|
ifInvalidContentType(req, 'template POST data must be of type application/json');
|
|
|
|
var cfg = req.body;
|
|
|
|
self.templateMaps.addTemplate(cdbuser, cfg, this);
|
|
|
|
},
|
|
|
|
function prepareResponse(err, tpl_id){
|
|
|
|
assert.ifError(err);
|
|
|
|
return { template_id: tpl_id };
|
|
|
|
},
|
2017-09-21 17:46:31 +08:00
|
|
|
finishFn(self, req, res, 'POST TEMPLATE', null, next)
|
2015-07-08 19:11:57 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
NamedMapsAdminController.prototype.update = function(req, res, next) {
|
2015-07-08 19:11:57 +08:00
|
|
|
var self = this;
|
|
|
|
|
2015-07-08 21:34:46 +08:00
|
|
|
var cdbuser = req.context.user;
|
2015-07-08 19:11:57 +08:00
|
|
|
var template;
|
|
|
|
var tpl_id;
|
2016-03-11 18:06:51 +08:00
|
|
|
|
2015-07-08 19:11:57 +08:00
|
|
|
step(
|
|
|
|
function checkPerms(){
|
2015-07-13 21:05:03 +08:00
|
|
|
self.authApi.authorizedByAPIKey(cdbuser, req, this);
|
2015-07-08 19:11:57 +08:00
|
|
|
},
|
2016-03-12 01:28:14 +08:00
|
|
|
function updateTemplate(err, authenticated) {
|
2015-07-08 19:11:57 +08:00
|
|
|
assert.ifError(err);
|
2016-03-14 18:18:32 +08:00
|
|
|
ifUnauthenticated(authenticated, 'Only authenticated user can update templated maps');
|
|
|
|
ifInvalidContentType(req, 'template PUT data must be of type application/json');
|
2016-03-11 18:06:51 +08:00
|
|
|
|
2015-07-08 19:11:57 +08:00
|
|
|
template = req.body;
|
|
|
|
tpl_id = templateName(req.params.template_id);
|
2016-03-12 01:28:14 +08:00
|
|
|
self.templateMaps.updTemplate(cdbuser, tpl_id, template, this);
|
2015-07-08 19:11:57 +08:00
|
|
|
},
|
|
|
|
function prepareResponse(err){
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
return { template_id: tpl_id };
|
|
|
|
},
|
2017-09-21 17:46:31 +08:00
|
|
|
finishFn(self, req, res, 'PUT TEMPLATE', null, next)
|
2015-07-08 19:11:57 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
NamedMapsAdminController.prototype.retrieve = function(req, res, next) {
|
2015-07-08 19:11:57 +08:00
|
|
|
var self = this;
|
|
|
|
|
2017-03-31 02:31:53 +08:00
|
|
|
req.profiler.start('windshaft-cartodb.get_template');
|
2015-07-08 19:11:57 +08:00
|
|
|
|
2015-07-08 21:34:46 +08:00
|
|
|
var cdbuser = req.context.user;
|
2015-07-08 19:11:57 +08:00
|
|
|
var tpl_id;
|
|
|
|
step(
|
|
|
|
function checkPerms(){
|
2015-07-13 21:05:03 +08:00
|
|
|
self.authApi.authorizedByAPIKey(cdbuser, req, this);
|
2015-07-08 19:11:57 +08:00
|
|
|
},
|
|
|
|
function getTemplate(err, authenticated) {
|
|
|
|
assert.ifError(err);
|
|
|
|
ifUnauthenticated(authenticated, 'Only authenticated users can get template maps');
|
|
|
|
|
|
|
|
tpl_id = templateName(req.params.template_id);
|
|
|
|
self.templateMaps.getTemplate(cdbuser, tpl_id, this);
|
|
|
|
},
|
|
|
|
function prepareResponse(err, tpl_val) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2015-07-08 19:11:57 +08:00
|
|
|
if ( ! tpl_val ) {
|
|
|
|
err = new Error("Cannot find template '" + tpl_id + "' of user '" + cdbuser + "'");
|
|
|
|
err.http_status = 404;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
// auth_id was added by ourselves,
|
|
|
|
// so we remove it before returning to the user
|
|
|
|
delete tpl_val.auth_id;
|
|
|
|
return { template: tpl_val };
|
|
|
|
},
|
2017-09-21 17:46:31 +08:00
|
|
|
finishFn(self, req, res, 'GET TEMPLATE', null, next)
|
2015-07-08 19:11:57 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
NamedMapsAdminController.prototype.destroy = function(req, res, next) {
|
2015-07-08 19:11:57 +08:00
|
|
|
var self = this;
|
|
|
|
|
2017-03-31 02:31:53 +08:00
|
|
|
req.profiler.start('windshaft-cartodb.delete_template');
|
2015-07-08 19:11:57 +08:00
|
|
|
|
2015-07-08 21:34:46 +08:00
|
|
|
var cdbuser = req.context.user;
|
2015-07-08 19:11:57 +08:00
|
|
|
var tpl_id;
|
|
|
|
step(
|
|
|
|
function checkPerms(){
|
2015-07-13 21:05:03 +08:00
|
|
|
self.authApi.authorizedByAPIKey(cdbuser, req, this);
|
2015-07-08 19:11:57 +08:00
|
|
|
},
|
|
|
|
function deleteTemplate(err, authenticated) {
|
|
|
|
assert.ifError(err);
|
|
|
|
ifUnauthenticated(authenticated, 'Only authenticated users can delete template maps');
|
|
|
|
|
|
|
|
tpl_id = templateName(req.params.template_id);
|
|
|
|
self.templateMaps.delTemplate(cdbuser, tpl_id, this);
|
|
|
|
},
|
|
|
|
function prepareResponse(err/*, tpl_val*/){
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2015-09-17 08:05:25 +08:00
|
|
|
return '';
|
2015-07-08 19:11:57 +08:00
|
|
|
},
|
2017-09-21 17:46:31 +08:00
|
|
|
finishFn(self, req, res, 'DELETE TEMPLATE', 204, next)
|
2015-07-08 19:11:57 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
NamedMapsAdminController.prototype.list = function(req, res, next) {
|
2015-07-08 19:11:57 +08:00
|
|
|
var self = this;
|
2017-03-31 02:31:53 +08:00
|
|
|
req.profiler.start('windshaft-cartodb.get_template_list');
|
2015-07-08 19:11:57 +08:00
|
|
|
|
2015-07-08 21:34:46 +08:00
|
|
|
var cdbuser = req.context.user;
|
2015-07-08 19:11:57 +08:00
|
|
|
|
|
|
|
step(
|
|
|
|
function checkPerms(){
|
2015-07-13 21:05:03 +08:00
|
|
|
self.authApi.authorizedByAPIKey(cdbuser, req, this);
|
2015-07-08 19:11:57 +08:00
|
|
|
},
|
|
|
|
function listTemplates(err, authenticated) {
|
|
|
|
assert.ifError(err);
|
|
|
|
ifUnauthenticated(authenticated, 'Only authenticated user can list templated maps');
|
|
|
|
|
|
|
|
self.templateMaps.listTemplates(cdbuser, this);
|
|
|
|
},
|
|
|
|
function prepareResponse(err, tpl_ids){
|
|
|
|
assert.ifError(err);
|
|
|
|
return { template_ids: tpl_ids };
|
|
|
|
},
|
2017-09-21 17:46:31 +08:00
|
|
|
finishFn(self, req, res, 'GET TEMPLATE LIST', null, next)
|
2015-07-08 19:11:57 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
function finishFn(controller, req, res, description, status, next) {
|
2015-07-08 19:11:57 +08:00
|
|
|
return function finish(err, response){
|
|
|
|
if (err) {
|
2017-09-21 17:46:31 +08:00
|
|
|
err.label = description;
|
|
|
|
next(err);
|
2015-07-08 19:11:57 +08:00
|
|
|
} else {
|
2015-09-17 08:05:25 +08:00
|
|
|
controller.send(req, res, response, status || 200);
|
2015-07-08 19:11:57 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function ifUnauthenticated(authenticated, description) {
|
|
|
|
if (!authenticated) {
|
|
|
|
var err = new Error(description);
|
|
|
|
err.http_status = 403;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ifInvalidContentType(req, description) {
|
2015-07-10 07:31:06 +08:00
|
|
|
if (!req.is('application/json')) {
|
2015-07-08 19:11:57 +08:00
|
|
|
throw new Error(description);
|
|
|
|
}
|
|
|
|
}
|