diff --git a/lib/cartodb/controllers/template_maps.js b/lib/cartodb/controllers/template_maps.js index 8efb722d..21f19931 100644 --- a/lib/cartodb/controllers/template_maps.js +++ b/lib/cartodb/controllers/template_maps.js @@ -44,11 +44,7 @@ TemplateMapsController.prototype.create = function(req, res) { }, function addTemplate(err, authenticated) { assert.ifError(err); - if (authenticated !== 1) { - err = new Error("Only authenticated user can create templated maps"); - err.http_status = 403; - throw 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'); var cfg = req.body; @@ -77,12 +73,7 @@ TemplateMapsController.prototype.update = function(req, res) { }, function updateTemplate(err, authenticated) { assert.ifError(err); - - if (authenticated !== 1) { - err = new Error("Only authenticated user can list templated maps"); - err.http_status = 403; - throw 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'); template = req.body; @@ -115,12 +106,8 @@ TemplateMapsController.prototype.retrieve = function(req, res) { self.serverOptions.authorizedByAPIKey(req, this); }, function updateTemplate(err, authenticated) { - if ( err ) throw err; - if (authenticated !== 1) { - err = new Error("Only authenticated users can get template maps"); - err.http_status = 403; - throw err; - } + 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); }, @@ -156,12 +143,8 @@ TemplateMapsController.prototype.destroy = function(req, res) { self.serverOptions.authorizedByAPIKey(req, this); }, function updateTemplate(err, authenticated) { - if ( err ) throw err; - if (authenticated !== 1) { - err = new Error("Only authenticated users can delete template maps"); - err.http_status = 403; - throw err; - } + 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); }, @@ -188,12 +171,8 @@ TemplateMapsController.prototype.list = function(req, res) { self.serverOptions.authorizedByAPIKey(req, this); }, function listTemplates(err, authenticated) { - if ( err ) throw err; - if (authenticated !== 1) { - err = new Error("Only authenticated user can list templated maps"); - err.http_status = 403; - throw err; - } + assert.ifError(err); + ifUnauthenticated(authenticated, 'Only authenticated user can list templated maps'); self.templateMaps.listTemplates(cdbuser, this); }, function prepareResponse(err, tpl_ids){ @@ -394,6 +373,14 @@ function finishFn(app, res, description, okResponse) { }; } +function ifUnauthenticated(authenticated, description) { + if (authenticated !== 1) { + var err = new Error(description); + err.http_status = 403; + throw err; + } +} + function templateName(templateId) { var templateIdTokens = templateId.split('@'); var name = templateIdTokens[0];