From 446c432484e5383caec637066ffc5761258b4f15 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 23 Apr 2015 12:05:52 +0200 Subject: [PATCH] dry content type validation --- lib/cartodb/controllers/template_maps.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/cartodb/controllers/template_maps.js b/lib/cartodb/controllers/template_maps.js index 21f19931..29026c41 100644 --- a/lib/cartodb/controllers/template_maps.js +++ b/lib/cartodb/controllers/template_maps.js @@ -45,8 +45,7 @@ TemplateMapsController.prototype.create = function(req, res) { function addTemplate(err, authenticated) { assert.ifError(err); ifUnauthenticated(authenticated, 'Only authenticated users can get template maps'); - if ( ! req.headers['content-type'] || req.headers['content-type'].split(';')[0] != 'application/json' ) - throw new Error('template POST data must be of type application/json'); + ifInvalidContentType(req, 'template POST data must be of type application/json'); var cfg = req.body; self.templateMaps.addTemplate(cdbuser, cfg, this); }, @@ -74,8 +73,8 @@ TemplateMapsController.prototype.update = function(req, res) { function updateTemplate(err, authenticated) { assert.ifError(err); ifUnauthenticated(authenticated, 'Only authenticated user can update templated maps'); - if ( ! req.headers['content-type'] || req.headers['content-type'].split(';')[0] != 'application/json' ) - throw new Error('template PUT data must be of type application/json'); + ifInvalidContentType(req, 'template PUT data must be of type application/json'); + template = req.body; tpl_id = templateName(req.params.template_id); self.templateMaps.updTemplate(cdbuser, tpl_id, template, this); @@ -108,6 +107,7 @@ TemplateMapsController.prototype.retrieve = function(req, res) { function updateTemplate(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); }, @@ -145,6 +145,7 @@ TemplateMapsController.prototype.destroy = function(req, res) { function updateTemplate(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); }, @@ -173,6 +174,7 @@ TemplateMapsController.prototype.list = function(req, res) { 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){ @@ -191,9 +193,8 @@ TemplateMapsController.prototype.instantiate = function(req, res) { } step( function() { - if ( ! req.headers['content-type'] || req.headers['content-type'].split(';')[0] != 'application/json') { - throw new Error('template POST data must be of type application/json, it is instead '); - } + ifInvalidContentType(req, 'template POST data must be of type application/json'); + self.instantiateTemplate(req, res, req.body, this); }, function(err, response) { self.finish_instantiation(err, response, res); @@ -381,6 +382,12 @@ function ifUnauthenticated(authenticated, description) { } } +function ifInvalidContentType(req, description) { + if ( ! req.headers['content-type'] || req.headers['content-type'].split(';')[0] != 'application/json' ) { + throw new Error(description); + } +} + function templateName(templateId) { var templateIdTokens = templateId.split('@'); var name = templateIdTokens[0];