From aebacc223a41ba5a8eb7d87b46698929d4aff5b1 Mon Sep 17 00:00:00 2001 From: Jim Laurie Date: Mon, 10 Jul 2017 11:40:41 +0200 Subject: [PATCH 1/6] Send environments, menu, general settings and securit, server by env --- .../admin/src/translations/en.json | 5 +- .../config/routes.json | 28 ++- .../controllers/SettingsManager.js | 40 +-- .../package.json | 16 +- .../services/SettingsManager.js | 227 +++++++++++++++++- 5 files changed, 281 insertions(+), 35 deletions(-) diff --git a/packages/strapi-plugin-setings-manager/admin/src/translations/en.json b/packages/strapi-plugin-setings-manager/admin/src/translations/en.json index 9e26dfeeb6..6ce9d16df8 100644 --- a/packages/strapi-plugin-setings-manager/admin/src/translations/en.json +++ b/packages/strapi-plugin-setings-manager/admin/src/translations/en.json @@ -1 +1,4 @@ -{} \ No newline at end of file +{ + "menu.section.global-settings": "Globales settings", + "menu.section.environments": "Environments" +} diff --git a/packages/strapi-plugin-setings-manager/config/routes.json b/packages/strapi-plugin-setings-manager/config/routes.json index d0cfcd0846..1a37ba051e 100644 --- a/packages/strapi-plugin-setings-manager/config/routes.json +++ b/packages/strapi-plugin-setings-manager/config/routes.json @@ -2,8 +2,32 @@ "routes": [ { "method": "GET", - "path": "/", - "handler": "SettingsManager.index", + "path": "/menu", + "handler": "SettingsManager.menu", + "config": { + "policies": [] + } + }, + { + "method": "GET", + "path": "/environments", + "handler": "SettingsManager.environments", + "config": { + "policies": [] + } + }, + { + "method": "GET", + "path": "/:slug", + "handler": "SettingsManager.form", + "config": { + "policies": [] + } + }, + { + "method": "GET", + "path": "/:slug/:env", + "handler": "SettingsManager.form", "config": { "policies": [] } diff --git a/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js b/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js index 8234cde92f..c5abe38691 100644 --- a/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js +++ b/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js @@ -1,25 +1,29 @@ 'use strict'; -/** - * SettingsManager.js controller - * - * @description: A set of functions called "actions" of the `settings-manager` plugin. - */ - module.exports = { + menu: async ctx => { + const Service = strapi.plugins['settings-manager'].services.settingsmanager; - /** - * Default action. - * - * @return {Object} - */ + ctx.send(Service.menu); + }, - index: async (ctx) => { - // Add your own logic here. - - // Send 200 `ok` - ctx.send({ - message: 'ok' + environments: async ctx => { + let envs = _.map(_.keys(strapi.config.environments), env => { + return { + name: env, + active: (strapi.config.environment === env) + } }); - } + + ctx.send({ + environments: envs + }); + }, + + form: async ctx => { + const Service = strapi.plugins['settings-manager'].services.settingsmanager; + const { slug, env } = ctx.params; + + ctx.send(env ? Service[slug](env) : Service[slug]); + }, }; diff --git a/packages/strapi-plugin-setings-manager/package.json b/packages/strapi-plugin-setings-manager/package.json index 2ef3a54941..1051ecaa27 100644 --- a/packages/strapi-plugin-setings-manager/package.json +++ b/packages/strapi-plugin-setings-manager/package.json @@ -1,7 +1,7 @@ { "name": "strapi-plugin-settings-manager", - "version": "0.0.0", - "description": "This is the description of the plugin.", + "version": "0.1.0", + "description": "Strapi plugin to manage settings.", "strapi": { "name": "settings-manager", "icon": "ion-document-text", @@ -29,15 +29,15 @@ "strapi-helper-plugin": "3.0.0-alpha.3" }, "author": { - "name": "A Strapi developer", - "email": "", - "url": "" + "name": "Strapi team", + "email": "hi@strapi.io", + "url": "http://strapi.io" }, "maintainers": [ { - "name": "A Strapi developer", - "email": "", - "url": "" + "name": "Strapi team", + "email": "hi@strapi.io", + "url": "http://strapi.io" } ], "engines": { diff --git a/packages/strapi-plugin-setings-manager/services/SettingsManager.js b/packages/strapi-plugin-setings-manager/services/SettingsManager.js index 0d23e9045b..80e72b1f7f 100644 --- a/packages/strapi-plugin-setings-manager/services/SettingsManager.js +++ b/packages/strapi-plugin-setings-manager/services/SettingsManager.js @@ -1,11 +1,226 @@ 'use strict'; -/** - * SettingsManager.js service - * - * @description: A set of functions similar to controller's actions to avoid code duplication. - */ - module.exports = { + menu: { + sections: [ + { + tradKey: 'menu.section.global-settings', + items: [ + { + slug: 'general', + tradKey: 'menu.item.general', + icon: 'globe' + }, + { + slug: 'languages', + tradKey: 'menu.item.languages', + icon: 'language' + }, + { + slug: 'advenced', + tradKey: 'menu.item.advenced', + icon: 'cogs' + } + ] + }, + { + tradKey: 'menu.section.environments', + items: [ + { + slug: 'databases', + tradKey: 'menu.item.databases', + icon: 'databases' + }, + { + slug: 'security', + tradKey: 'menu.item.security', + icon: 'shield' + }, + { + slug: 'server', + tradKey: 'menu.item.server', + icon: 'server' + } + ] + } + ] + }, + general: { + tradKey: 'form.general', + tradKeyDesc: 'form.general.desc', + sections: [ + { + tradKey: '', + items: [ + { + tradKey: 'form.general.name', + target: 'package.name', + type: 'string', + value: strapi.config.name, + validations : { + maxLength: 255, + required: true + } + }, + { + tradKey: 'form.general.description', + target: 'package.name', + type: 'string', + value: strapi.config.description, + validations : { + maxLength: 255, + required: true + } + }, + { + name: 'form.general.version', + target: 'package.name', + type: 'string', + value: strapi.config.version, + validations : { + maxLength: 255, + required: true + } + } + ] + } + ] + }, + + security: env => { + return { + tradKey: 'form.security', + tradKeyDesc: 'form.security.desc', + sections: [ + { + tradKey: 'form.security.session', + items: [ + { + tradKey: 'form.security.session.key', + target: 'security.session.key', + type: 'string', + value: strapi.config.environments[env].security.session.key + }, + { + tradKey: 'form.security.session.maxAge', + target: 'security.session.maxAge', + type: 'number', + value: strapi.config.environments[env].security.session.maxAge + } + ] + }, + { + tradKey: '', + items: [ + { + tradKey: 'form.security.xframe', + target: 'security.xframe', + type: 'enum', + value: strapi.config.environments[env].security.xframe, + items: [ + { + tradKey: 'server.xframe.deny', + value: 'DENY', + }, + { + tradKey: 'server.xframe.sameorigin', + value: 'SAMEORIGIN', + }, + { + tradKey: 'server.xframe.allow-from', + value: 'ALLOW-FROM', + }, + ] + }, + { + tradKey: 'form.security.xssProtection', + target: 'security.xssProtection', + type: 'boolean', + value: strapi.config.environments[env].security.xssProtection + } + ] + }, + { + tradKey: 'form.security.cors', + items: [ + { + tradKey: 'form.security.cors.origin', + target: 'security.cors.origin', + type: 'string', + value: strapi.config.environments[env].security.cors.origin, + } + ] + } + ] + }; + }, + + server: env => { + return { + tradKey: 'form.server', + tradKeyDesc: 'form.server.desc', + sections: [ + { + tradKey: '', + items: [ + { + tradKey: 'form.server.host', + target: 'server.host', + type: 'string', + value: strapi.config.environments[env].server.host + }, + { + tradKey: 'form.server.port', + target: 'server.port', + type: 'number', + value: strapi.config.environments[env].server.port + } + ] + }, + { + tradKey: 'form.server.parser', + items: [ + { + tradKey: 'form.server.parser.xframe', + target: 'server.xframe', + type: 'enum', + value: strapi.config.environments[env].server.xframe, + items: [ + { + tradKey: 'server.xframe.deny', + value: 'DENY', + }, + { + tradKey: 'server.xframe.sameorigin', + value: 'SAMEORIGIN', + }, + { + tradKey: 'server.xframe.allow-from', + value: 'ALLOW-FROM', + }, + ] + }, + { + tradKey: 'form.server.xssProtection', + target: 'server.xssProtection', + type: 'boolean', + value: strapi.config.environments[env].server.xssProtection + } + ] + }, + { + tradKey: 'form.server.cors', + items: [ + { + tradKey: 'form.server.cors.origin', + target: 'server.cors.origin', + type: 'string', + value: strapi.config.environments[env].server.cors.origin + } + ] + } + ] + }; + } }; From bc884151475d772aa97f9ba8c63ec5531b859805 Mon Sep 17 00:00:00 2001 From: Jim Laurie Date: Mon, 10 Jul 2017 12:19:06 +0200 Subject: [PATCH 2/6] Change traduction keys --- .../services/SettingsManager.js | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/packages/strapi-plugin-setings-manager/services/SettingsManager.js b/packages/strapi-plugin-setings-manager/services/SettingsManager.js index 80e72b1f7f..f4edbab3a4 100644 --- a/packages/strapi-plugin-setings-manager/services/SettingsManager.js +++ b/packages/strapi-plugin-setings-manager/services/SettingsManager.js @@ -4,41 +4,41 @@ module.exports = { menu: { sections: [ { - tradKey: 'menu.section.global-settings', + name: 'menu.section.global-settings', items: [ { slug: 'general', - tradKey: 'menu.item.general', + name: 'menu.item.general', icon: 'globe' }, { slug: 'languages', - tradKey: 'menu.item.languages', + name: 'menu.item.languages', icon: 'language' }, { slug: 'advenced', - tradKey: 'menu.item.advenced', + name: 'menu.item.advenced', icon: 'cogs' } ] }, { - tradKey: 'menu.section.environments', + name: 'menu.section.environments', items: [ { slug: 'databases', - tradKey: 'menu.item.databases', + name: 'menu.item.databases', icon: 'databases' }, { slug: 'security', - tradKey: 'menu.item.security', + name: 'menu.item.security', icon: 'shield' }, { slug: 'server', - tradKey: 'menu.item.server', + name: 'menu.item.server', icon: 'server' } ] @@ -47,14 +47,14 @@ module.exports = { }, general: { - tradKey: 'form.general', - tradKeyDesc: 'form.general.desc', + name: 'form.general', + description: 'form.general.desc', sections: [ { - tradKey: '', + name: '', items: [ { - tradKey: 'form.general.name', + name: 'form.general.name', target: 'package.name', type: 'string', value: strapi.config.name, @@ -64,7 +64,7 @@ module.exports = { } }, { - tradKey: 'form.general.description', + name: 'form.general.description', target: 'package.name', type: 'string', value: strapi.config.description, @@ -90,20 +90,20 @@ module.exports = { security: env => { return { - tradKey: 'form.security', - tradKeyDesc: 'form.security.desc', + name: 'form.security', + description: 'form.security.desc', sections: [ { - tradKey: 'form.security.session', + name: 'form.security.session', items: [ { - tradKey: 'form.security.session.key', + name: 'form.security.session.key', target: 'security.session.key', type: 'string', value: strapi.config.environments[env].security.session.key }, { - tradKey: 'form.security.session.maxAge', + name: 'form.security.session.maxAge', target: 'security.session.maxAge', type: 'number', value: strapi.config.environments[env].security.session.maxAge @@ -111,30 +111,30 @@ module.exports = { ] }, { - tradKey: '', + name: '', items: [ { - tradKey: 'form.security.xframe', + name: 'form.security.xframe', target: 'security.xframe', type: 'enum', value: strapi.config.environments[env].security.xframe, items: [ { - tradKey: 'server.xframe.deny', + name: 'server.xframe.deny', value: 'DENY', }, { - tradKey: 'server.xframe.sameorigin', + name: 'server.xframe.sameorigin', value: 'SAMEORIGIN', }, { - tradKey: 'server.xframe.allow-from', + name: 'server.xframe.allow-from', value: 'ALLOW-FROM', }, ] }, { - tradKey: 'form.security.xssProtection', + name: 'form.security.xssProtection', target: 'security.xssProtection', type: 'boolean', value: strapi.config.environments[env].security.xssProtection @@ -142,10 +142,10 @@ module.exports = { ] }, { - tradKey: 'form.security.cors', + name: 'form.security.cors', items: [ { - tradKey: 'form.security.cors.origin', + name: 'form.security.cors.origin', target: 'security.cors.origin', type: 'string', value: strapi.config.environments[env].security.cors.origin, @@ -158,20 +158,20 @@ module.exports = { server: env => { return { - tradKey: 'form.server', - tradKeyDesc: 'form.server.desc', + name: 'form.server', + description: 'form.server.desc', sections: [ { - tradKey: '', + name: '', items: [ { - tradKey: 'form.server.host', + name: 'form.server.host', target: 'server.host', type: 'string', value: strapi.config.environments[env].server.host }, { - tradKey: 'form.server.port', + name: 'form.server.port', target: 'server.port', type: 'number', value: strapi.config.environments[env].server.port @@ -179,30 +179,30 @@ module.exports = { ] }, { - tradKey: 'form.server.parser', + name: 'form.server.parser', items: [ { - tradKey: 'form.server.parser.xframe', + name: 'form.server.parser.xframe', target: 'server.xframe', type: 'enum', value: strapi.config.environments[env].server.xframe, items: [ { - tradKey: 'server.xframe.deny', + name: 'server.xframe.deny', value: 'DENY', }, { - tradKey: 'server.xframe.sameorigin', + name: 'server.xframe.sameorigin', value: 'SAMEORIGIN', }, { - tradKey: 'server.xframe.allow-from', + name: 'server.xframe.allow-from', value: 'ALLOW-FROM', }, ] }, { - tradKey: 'form.server.xssProtection', + name: 'form.server.xssProtection', target: 'server.xssProtection', type: 'boolean', value: strapi.config.environments[env].server.xssProtection @@ -210,10 +210,10 @@ module.exports = { ] }, { - tradKey: 'form.server.cors', + name: 'form.server.cors', items: [ { - tradKey: 'form.server.cors.origin', + name: 'form.server.cors.origin', target: 'server.cors.origin', type: 'string', value: strapi.config.environments[env].server.cors.origin From aaa80e63d73157bbcc45bddeea712f8319714a81 Mon Sep 17 00:00:00 2001 From: Jim Laurie Date: Mon, 10 Jul 2017 18:00:50 +0200 Subject: [PATCH 3/6] Valide received data before update --- .../config/routes.json | 20 +++- .../controllers/SettingsManager.js | 22 ++++- .../package.json | 3 +- .../services/SettingsManager.js | 92 ++++++++++++++++++- 4 files changed, 131 insertions(+), 6 deletions(-) diff --git a/packages/strapi-plugin-setings-manager/config/routes.json b/packages/strapi-plugin-setings-manager/config/routes.json index 1a37ba051e..b595155005 100644 --- a/packages/strapi-plugin-setings-manager/config/routes.json +++ b/packages/strapi-plugin-setings-manager/config/routes.json @@ -19,7 +19,7 @@ { "method": "GET", "path": "/:slug", - "handler": "SettingsManager.form", + "handler": "SettingsManager.get", "config": { "policies": [] } @@ -27,7 +27,23 @@ { "method": "GET", "path": "/:slug/:env", - "handler": "SettingsManager.form", + "handler": "SettingsManager.get", + "config": { + "policies": [] + } + }, + { + "method": "PUT", + "path": "/:slug", + "handler": "SettingsManager.update", + "config": { + "policies": [] + } + }, + { + "method": "PUT", + "path": "/:slug/:env", + "handler": "SettingsManager.update", "config": { "policies": [] } diff --git a/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js b/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js index c5abe38691..3d3b45d434 100644 --- a/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js +++ b/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js @@ -20,10 +20,30 @@ module.exports = { }); }, - form: async ctx => { + get: async ctx => { const Service = strapi.plugins['settings-manager'].services.settingsmanager; const { slug, env } = ctx.params; ctx.send(env ? Service[slug](env) : Service[slug]); }, + + update: async ctx => { + const Service = strapi.plugins['settings-manager'].services.settingsmanager; + const { slug, env } = ctx.params; + let params = ctx.request.body; + let error = false; + + const model = env ? Service[slug](env) : Service[slug]; + const items = Service.getItems(model); + + params = Service.cleanParams(params, items); + + let validationErrors = Service.paramsValidation(params, items); + + if (_.isEmpty(validationErrors)) { + ctx.send(params); + } else { + ctx.send(validationErrors); + } + }, }; diff --git a/packages/strapi-plugin-setings-manager/package.json b/packages/strapi-plugin-setings-manager/package.json index 1051ecaa27..eadc3f2b32 100644 --- a/packages/strapi-plugin-setings-manager/package.json +++ b/packages/strapi-plugin-setings-manager/package.json @@ -24,7 +24,8 @@ "test": "echo Tests are not implemented.", "prepublish": "npm run build" }, - "dependencies": {}, + "dependencies": { + }, "devDependencies": { "strapi-helper-plugin": "3.0.0-alpha.3" }, diff --git a/packages/strapi-plugin-setings-manager/services/SettingsManager.js b/packages/strapi-plugin-setings-manager/services/SettingsManager.js index f4edbab3a4..b1a31a52b0 100644 --- a/packages/strapi-plugin-setings-manager/services/SettingsManager.js +++ b/packages/strapi-plugin-setings-manager/services/SettingsManager.js @@ -60,12 +60,13 @@ module.exports = { value: strapi.config.name, validations : { maxLength: 255, + minLength: 5, required: true } }, { name: 'form.general.description', - target: 'package.name', + target: 'package.description', type: 'string', value: strapi.config.description, validations : { @@ -75,7 +76,7 @@ module.exports = { }, { name: 'form.general.version', - target: 'package.name', + target: 'package.version', type: 'string', value: strapi.config.version, validations : { @@ -222,5 +223,92 @@ module.exports = { } ] }; + }, + + getItems: model => { + let items = []; + _.forEach(model.sections, section => items = _.concat(items, section.items)); + + return items; + }, + + cleanParams: (params, items) => { + const cleanParams = {}; + + _.forEach(items, ({ target }) => _.has(params, target) ? _.set(cleanParams, target, _.get(params, target)) : ''); + + return cleanParams; + }, + + paramsValidation: (params, items) => { + let errors = []; + + const checkType = (input, { type, target }) => { + if ((type === 'string' || type === 'text') && !_.isString(input)) errors.push({ + target: target, + message: 'form.error.string' + }); + + if (type === 'number' && !_.isNumber(input)) errors.push({ + target: target, + message: 'form.error.number' + }); + + if (type === 'boolean' && !_.isBoolean(input)) errors.push({ + target: target, + message: 'form.error.number' + }); + }; + + const checkValidations = (input, item) => { + _.forEach(item.validations, (value, key) => { + if (key === 'required' && (_.isNull(input) || _.isEmpty(input) || _.isUndefined(input))) errors.push({ + target: item.target, + message: 'form.error.required' + }); + + if (key === 'max' && parseInt(input) > value) errors.push({ + target: item.target, + message: 'form.error.max' + }); + + if (key === 'min' && parseInt(input) < value) errors.push({ + target: item.target, + message: 'form.error.min' + }); + + if (key === 'maxLength' && input.length > value) errors.push({ + target: item.target, + message: 'form.error.maxLength' + }); + + if (key === 'minLength' && input.length < value) errors.push({ + target: item.target, + message: 'form.error.minLength' + }); + }); + }; + + _.forEach(items, item => { + if (_.has(params, item.target)) { + const input = _.get(params, item.target, null); + + checkType(input, item) + checkValidations(input, item) + } + }); + + if (!_.isEmpty(errors)) { + const grpTarget = _.groupBy(errors, 'target'); + + errors = _.map(grpTarget, (errs, target) => { + return { + target, + messages: _.map(errs, err => err.message) + } + }); + } + + return errors; } }; From 0eb4fef9f1dd4c1336e80bd54eb7547d16d6ab57 Mon Sep 17 00:00:00 2001 From: Jim Laurie Date: Mon, 10 Jul 2017 18:44:20 +0200 Subject: [PATCH 4/6] Fix 404 for empty send method --- .../configuration/hooks/core/responses/policy.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/strapi/lib/configuration/hooks/core/responses/policy.js b/packages/strapi/lib/configuration/hooks/core/responses/policy.js index d940e9c771..007ac41289 100644 --- a/packages/strapi/lib/configuration/hooks/core/responses/policy.js +++ b/packages/strapi/lib/configuration/hooks/core/responses/policy.js @@ -51,13 +51,19 @@ module.exports = async function(ctx, next) { } // Empty body is considered as `notFound` response. - if (!ctx.body) { - ctx.notFound(); + if (_.isUndefined(ctx.body) && _.isUndefined(ctx.status)) { + return ctx.notFound(); } - // Format `ctx.body` and `ctx.status`. - ctx.status = ctx.body.isBoom ? ctx.body.output.statusCode : ctx.status; - ctx.body = ctx.body.isBoom ? ctx.body.output.payload : ctx.body; + if (_.isObject(ctx.body)) { + if (ctx.body.isBoom && ctx.body.data) { + ctx.body.output.payload.data = ctx.body.data; + } + + // Format `ctx.body` and `ctx.status`. + ctx.status = ctx.body.isBoom ? ctx.body.output.statusCode : ctx.status; + ctx.body = ctx.body.isBoom ? ctx.body.output.payload : ctx.body; + } // Call custom responses. if (_.isFunction(_.get(strapi.config, `responses.${ctx.status}`))) { From 877bece494962aabe9da10d33068b6b33b407726 Mon Sep 17 00:00:00 2001 From: Jim Laurie Date: Tue, 11 Jul 2017 11:41:00 +0200 Subject: [PATCH 5/6] Update settings files after validation --- .../controllers/SettingsManager.js | 15 ++++++------ .../services/SettingsManager.js | 23 ++++++++++++++++++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js b/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js index 3d3b45d434..98d0724e2c 100644 --- a/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js +++ b/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js @@ -15,9 +15,7 @@ module.exports = { } }); - ctx.send({ - environments: envs - }); + ctx.send({ environments: envs }); }, get: async ctx => { @@ -31,7 +29,6 @@ module.exports = { const Service = strapi.plugins['settings-manager'].services.settingsmanager; const { slug, env } = ctx.params; let params = ctx.request.body; - let error = false; const model = env ? Service[slug](env) : Service[slug]; const items = Service.getItems(model); @@ -40,10 +37,12 @@ module.exports = { let validationErrors = Service.paramsValidation(params, items); - if (_.isEmpty(validationErrors)) { - ctx.send(params); - } else { - ctx.send(validationErrors); + if (!_.isEmpty(validationErrors)) { + return ctx.badData(null, validationErrors); } + + Service.updateSettings(params, items, env); + + ctx.send(); }, }; diff --git a/packages/strapi-plugin-setings-manager/services/SettingsManager.js b/packages/strapi-plugin-setings-manager/services/SettingsManager.js index b1a31a52b0..d097157442 100644 --- a/packages/strapi-plugin-setings-manager/services/SettingsManager.js +++ b/packages/strapi-plugin-setings-manager/services/SettingsManager.js @@ -1,5 +1,8 @@ 'use strict'; +const fs = require('fs'); +const path = require('path'); + module.exports = { menu: { sections: [ @@ -60,7 +63,6 @@ module.exports = { value: strapi.config.name, validations : { maxLength: 255, - minLength: 5, required: true } }, @@ -310,5 +312,24 @@ module.exports = { } return errors; + }, + + updateSettings: (params, items, env = '') => { + const appPath = process.cwd(); + + _.forEach(items, ({ target }) => { + if (_.has(params, target)) { + const input = _.get(params, target, null); + const [file, ...objPath] = target.split('.'); + + let filePath = (file === 'package') ? path.join(appPath, 'package.json') : path.join(appPath, 'config', 'environments', env, `${_.replace(file, '.', '/')}.json`); + + const fileContent = require(filePath); + + _.set(fileContent, objPath, input); + + fs.writeFileSync(filePath, JSON.stringify(fileContent, null, 2), 'utf8'); + } + }); } }; From a844392792bdfcc33c01a986c8cdc74de3c58c49 Mon Sep 17 00:00:00 2001 From: Jim Laurie Date: Tue, 11 Jul 2017 14:56:52 +0200 Subject: [PATCH 6/6] Handle config and evironment error --- .../admin/src/translations/en.json | 2 +- .../controllers/SettingsManager.js | 22 ++++++++++------ .../services/SettingsManager.js | 25 +++++++++++++------ 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/packages/strapi-plugin-setings-manager/admin/src/translations/en.json b/packages/strapi-plugin-setings-manager/admin/src/translations/en.json index 6ce9d16df8..0d5058c6af 100644 --- a/packages/strapi-plugin-setings-manager/admin/src/translations/en.json +++ b/packages/strapi-plugin-setings-manager/admin/src/translations/en.json @@ -1,4 +1,4 @@ { - "menu.section.global-settings": "Globales settings", + "menu.section.global-settings": "Global settings", "menu.section.environments": "Environments" } diff --git a/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js b/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js index 98d0724e2c..bb77c74439 100644 --- a/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js +++ b/packages/strapi-plugin-setings-manager/controllers/SettingsManager.js @@ -8,21 +8,22 @@ module.exports = { }, environments: async ctx => { - let envs = _.map(_.keys(strapi.config.environments), env => { - return { - name: env, - active: (strapi.config.environment === env) - } - }); + const Service = strapi.plugins['settings-manager'].services.settingsmanager; - ctx.send({ environments: envs }); + ctx.send({ environments: Service.getEnvironments() }); }, get: async ctx => { const Service = strapi.plugins['settings-manager'].services.settingsmanager; const { slug, env } = ctx.params; - ctx.send(env ? Service[slug](env) : Service[slug]); + if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData('request.error.environment'); + + const model = env ? Service[slug](env) : Service[slug]; + + if (_.isUndefined(model)) return ctx.badData('request.error.config'); + + ctx.send(model); }, update: async ctx => { @@ -30,7 +31,12 @@ module.exports = { const { slug, env } = ctx.params; let params = ctx.request.body; + if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData('request.error.environment'); + const model = env ? Service[slug](env) : Service[slug]; + + if (_.isUndefined(config)) return ctx.badData('request.error.config'); + const items = Service.getItems(model); params = Service.cleanParams(params, items); diff --git a/packages/strapi-plugin-setings-manager/services/SettingsManager.js b/packages/strapi-plugin-setings-manager/services/SettingsManager.js index d097157442..35c4fc8ee8 100644 --- a/packages/strapi-plugin-setings-manager/services/SettingsManager.js +++ b/packages/strapi-plugin-setings-manager/services/SettingsManager.js @@ -227,6 +227,15 @@ module.exports = { }; }, + getEnvironments: () => { + return _.map(_.keys(strapi.config.environments), environment => { + return { + name: environment, + active: (strapi.config.environment === environment) + } + }); + }, + getItems: model => { let items = []; _.forEach(model.sections, section => items = _.concat(items, section.items)); @@ -248,17 +257,17 @@ module.exports = { const checkType = (input, { type, target }) => { if ((type === 'string' || type === 'text') && !_.isString(input)) errors.push({ target: target, - message: 'form.error.string' + message: 'form.error.type.string' }); if (type === 'number' && !_.isNumber(input)) errors.push({ target: target, - message: 'form.error.number' + message: 'form.error.type.number' }); if (type === 'boolean' && !_.isBoolean(input)) errors.push({ target: target, - message: 'form.error.number' + message: 'form.error.type.boolean' }); }; @@ -266,27 +275,27 @@ module.exports = { _.forEach(item.validations, (value, key) => { if (key === 'required' && (_.isNull(input) || _.isEmpty(input) || _.isUndefined(input))) errors.push({ target: item.target, - message: 'form.error.required' + message: 'form.error.validation.required' }); if (key === 'max' && parseInt(input) > value) errors.push({ target: item.target, - message: 'form.error.max' + message: 'form.error.validation.max' }); if (key === 'min' && parseInt(input) < value) errors.push({ target: item.target, - message: 'form.error.min' + message: 'form.error.validation.min' }); if (key === 'maxLength' && input.length > value) errors.push({ target: item.target, - message: 'form.error.maxLength' + message: 'form.error.validation.maxLength' }); if (key === 'minLength' && input.length < value) errors.push({ target: item.target, - message: 'form.error.minLength' + message: 'form.error.validation.minLength' }); }); };