2017-07-06 10:02:00 +02:00
|
|
|
'use strict';
|
|
|
|
|
2017-07-11 11:41:00 +02:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
2017-07-06 10:02:00 +02:00
|
|
|
module.exports = {
|
2017-07-10 11:40:41 +02:00
|
|
|
menu: {
|
|
|
|
sections: [
|
|
|
|
{
|
2017-07-10 12:19:06 +02:00
|
|
|
name: 'menu.section.global-settings',
|
2017-07-10 11:40:41 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
slug: 'general',
|
2017-07-10 12:19:06 +02:00
|
|
|
name: 'menu.item.general',
|
2017-07-10 11:40:41 +02:00
|
|
|
icon: 'globe'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
slug: 'languages',
|
2017-07-10 12:19:06 +02:00
|
|
|
name: 'menu.item.languages',
|
2017-07-10 11:40:41 +02:00
|
|
|
icon: 'language'
|
|
|
|
},
|
|
|
|
{
|
2017-07-12 16:37:07 +02:00
|
|
|
slug: 'advanced',
|
|
|
|
name: 'menu.item.advanced',
|
2017-07-10 11:40:41 +02:00
|
|
|
icon: 'cogs'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2017-07-10 12:19:06 +02:00
|
|
|
name: 'menu.section.environments',
|
2017-07-10 11:40:41 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
slug: 'databases',
|
2017-07-10 12:19:06 +02:00
|
|
|
name: 'menu.item.databases',
|
2017-07-10 11:40:41 +02:00
|
|
|
icon: 'databases'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
slug: 'security',
|
2017-07-10 12:19:06 +02:00
|
|
|
name: 'menu.item.security',
|
2017-07-10 11:40:41 +02:00
|
|
|
icon: 'shield'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
slug: 'server',
|
2017-07-10 12:19:06 +02:00
|
|
|
name: 'menu.item.server',
|
2017-07-10 11:40:41 +02:00
|
|
|
icon: 'server'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
general: {
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.general.name',
|
2017-07-12 16:37:07 +02:00
|
|
|
description: 'form.general.description',
|
2017-07-10 11:40:41 +02:00
|
|
|
sections: [
|
|
|
|
{
|
2017-07-10 12:19:06 +02:00
|
|
|
name: '',
|
2017-07-10 11:40:41 +02:00
|
|
|
items: [
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.general.item.name',
|
2017-07-10 11:40:41 +02:00
|
|
|
target: 'package.name',
|
|
|
|
type: 'string',
|
2017-07-12 16:04:36 +02:00
|
|
|
value: _.get(strapi.config, 'name', null),
|
2017-07-10 11:40:41 +02:00
|
|
|
validations : {
|
|
|
|
maxLength: 255,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.general.item.description',
|
2017-07-10 18:00:50 +02:00
|
|
|
target: 'package.description',
|
2017-07-10 11:40:41 +02:00
|
|
|
type: 'string',
|
2017-07-12 16:04:36 +02:00
|
|
|
value: _.get(strapi.config, 'description', null),
|
2017-07-10 11:40:41 +02:00
|
|
|
validations : {
|
|
|
|
maxLength: 255,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.general.item.version',
|
2017-07-10 18:00:50 +02:00
|
|
|
target: 'package.version',
|
2017-07-10 11:40:41 +02:00
|
|
|
type: 'string',
|
2017-07-12 16:04:36 +02:00
|
|
|
value: _.get(strapi.config, 'version', null),
|
2017-07-10 11:40:41 +02:00
|
|
|
validations : {
|
2017-07-12 16:04:36 +02:00
|
|
|
regex: '^(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)$',
|
2017-07-10 11:40:41 +02:00
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
2017-07-13 11:59:56 +02:00
|
|
|
advanced: {
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.advanced.name',
|
2017-07-13 11:59:56 +02:00
|
|
|
description: 'form.advanced.description',
|
|
|
|
sections: [
|
|
|
|
{
|
|
|
|
name: '',
|
|
|
|
items: [
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.advanced.item.admin',
|
2017-07-13 11:59:56 +02:00
|
|
|
target: 'general.admin',
|
|
|
|
type: 'string',
|
|
|
|
value: _.get(strapi.config, 'general.admin', null),
|
|
|
|
validations : {
|
|
|
|
maxLength: 255,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.advanced.item.prefix',
|
2017-07-13 11:59:56 +02:00
|
|
|
target: 'general.prefix',
|
|
|
|
type: 'string',
|
|
|
|
value: _.get(strapi.config, 'general.prefix', null),
|
|
|
|
validations : {
|
|
|
|
maxLength: 255
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
2017-07-10 11:40:41 +02:00
|
|
|
security: env => {
|
|
|
|
return {
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.security.name',
|
2017-07-12 16:37:07 +02:00
|
|
|
description: 'form.security.description',
|
2017-07-10 11:40:41 +02:00
|
|
|
sections: [
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.security.item.session',
|
2017-07-10 11:40:41 +02:00
|
|
|
items: [
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.security.item.session.key',
|
2017-07-10 11:40:41 +02:00
|
|
|
target: 'security.session.key',
|
|
|
|
type: 'string',
|
2017-07-12 16:04:36 +02:00
|
|
|
value: _.get(strapi.config, `environments.${env}.security.session.key`, null),
|
|
|
|
validations: {
|
|
|
|
required: true
|
|
|
|
}
|
2017-07-10 11:40:41 +02:00
|
|
|
},
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.security.item.session.maxAge',
|
2017-07-10 11:40:41 +02:00
|
|
|
target: 'security.session.maxAge',
|
|
|
|
type: 'number',
|
2017-07-12 16:04:36 +02:00
|
|
|
value: _.get(strapi.config, `environments.${env}.security.session.maxAge`, null)
|
2017-07-10 11:40:41 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2017-07-10 12:19:06 +02:00
|
|
|
name: '',
|
2017-07-10 11:40:41 +02:00
|
|
|
items: [
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.security.item.xframe',
|
2017-07-10 11:40:41 +02:00
|
|
|
target: 'security.xframe',
|
|
|
|
type: 'enum',
|
2017-07-12 16:04:36 +02:00
|
|
|
value: _.get(strapi.config, `environments.${env}.security.xframe`, null),
|
2017-07-10 11:40:41 +02:00
|
|
|
items: [
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.security.item.xframe.deny',
|
2017-07-10 11:40:41 +02:00
|
|
|
value: 'DENY',
|
|
|
|
},
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.security.item.xframe.sameorigin',
|
2017-07-10 11:40:41 +02:00
|
|
|
value: 'SAMEORIGIN',
|
|
|
|
},
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.security.item.xframe.allow-from',
|
2017-07-10 11:40:41 +02:00
|
|
|
value: 'ALLOW-FROM',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.security.item.xssProtection',
|
2017-07-10 11:40:41 +02:00
|
|
|
target: 'security.xssProtection',
|
|
|
|
type: 'boolean',
|
2017-07-12 16:04:36 +02:00
|
|
|
value: _.get(strapi.config, `environments.${env}.security.xssProtection`, null)
|
2017-07-10 11:40:41 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.security.item.cors',
|
2017-07-10 11:40:41 +02:00
|
|
|
items: [
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.security.item.cors.origin',
|
2017-07-10 11:40:41 +02:00
|
|
|
target: 'security.cors.origin',
|
|
|
|
type: 'string',
|
2017-07-12 16:04:36 +02:00
|
|
|
value: _.get(strapi.config, `environments.${env}.security.cors.origin`, null)
|
2017-07-10 11:40:41 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
},
|
2017-07-06 10:02:00 +02:00
|
|
|
|
2017-07-10 11:40:41 +02:00
|
|
|
server: env => {
|
|
|
|
return {
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.server.name',
|
2017-07-12 16:37:07 +02:00
|
|
|
description: 'form.server.description',
|
2017-07-10 11:40:41 +02:00
|
|
|
sections: [
|
|
|
|
{
|
2017-07-10 12:19:06 +02:00
|
|
|
name: '',
|
2017-07-10 11:40:41 +02:00
|
|
|
items: [
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.server.item.host',
|
2017-07-10 11:40:41 +02:00
|
|
|
target: 'server.host',
|
|
|
|
type: 'string',
|
2017-07-12 16:04:36 +02:00
|
|
|
value: _.get(strapi.config, `environments.${env}.server.host`, null)
|
2017-07-10 11:40:41 +02:00
|
|
|
},
|
|
|
|
{
|
2017-07-13 12:32:17 +02:00
|
|
|
name: 'form.server.item.port',
|
2017-07-10 11:40:41 +02:00
|
|
|
target: 'server.port',
|
|
|
|
type: 'number',
|
2017-07-12 16:04:36 +02:00
|
|
|
value: _.get(strapi.config, `environments.${env}.server.port`, null)
|
2017-07-10 11:40:41 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
2017-07-10 18:00:50 +02:00
|
|
|
},
|
|
|
|
|
2017-07-11 14:56:52 +02:00
|
|
|
getEnvironments: () => {
|
|
|
|
return _.map(_.keys(strapi.config.environments), environment => {
|
|
|
|
return {
|
|
|
|
name: environment,
|
|
|
|
active: (strapi.config.environment === environment)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-07-10 18:00:50 +02:00
|
|
|
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,
|
2017-07-13 12:32:17 +02:00
|
|
|
message: 'request.error.type.string'
|
2017-07-10 18:00:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (type === 'number' && !_.isNumber(input)) errors.push({
|
|
|
|
target: target,
|
2017-07-13 12:32:17 +02:00
|
|
|
message: 'request.error.type.number'
|
2017-07-10 18:00:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (type === 'boolean' && !_.isBoolean(input)) errors.push({
|
|
|
|
target: target,
|
2017-07-13 12:32:17 +02:00
|
|
|
message: 'request.error.type.boolean'
|
2017-07-10 18:00:50 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const checkValidations = (input, item) => {
|
|
|
|
_.forEach(item.validations, (value, key) => {
|
|
|
|
if (key === 'required' && (_.isNull(input) || _.isEmpty(input) || _.isUndefined(input))) errors.push({
|
|
|
|
target: item.target,
|
2017-07-13 12:32:17 +02:00
|
|
|
message: 'request.error.validation.required'
|
2017-07-10 18:00:50 +02:00
|
|
|
});
|
|
|
|
|
2017-07-12 16:04:36 +02:00
|
|
|
if (key === 'regex' && !new RegExp(value).test(input)) errors.push({
|
|
|
|
target: item.target,
|
2017-07-13 12:32:17 +02:00
|
|
|
message: 'request.error.validation.regex'
|
2017-07-12 16:04:36 +02:00
|
|
|
});
|
|
|
|
|
2017-07-10 18:00:50 +02:00
|
|
|
if (key === 'max' && parseInt(input) > value) errors.push({
|
|
|
|
target: item.target,
|
2017-07-13 12:32:17 +02:00
|
|
|
message: 'request.error.validation.max'
|
2017-07-10 18:00:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (key === 'min' && parseInt(input) < value) errors.push({
|
|
|
|
target: item.target,
|
2017-07-13 12:32:17 +02:00
|
|
|
message: 'request.error.validation.min'
|
2017-07-10 18:00:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (key === 'maxLength' && input.length > value) errors.push({
|
|
|
|
target: item.target,
|
2017-07-13 12:32:17 +02:00
|
|
|
message: 'request.error.validation.maxLength'
|
2017-07-10 18:00:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (key === 'minLength' && input.length < value) errors.push({
|
|
|
|
target: item.target,
|
2017-07-13 12:32:17 +02:00
|
|
|
message: 'request.error.validation.minLength'
|
2017-07-10 18:00:50 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
_.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;
|
2017-07-11 11:41:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
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('.');
|
|
|
|
|
2017-07-13 11:59:56 +02:00
|
|
|
let filePath = (file === 'package') ? path.join(appPath, 'package.json') : path.join(appPath, 'config', `${env ? `environments/${env}` : ''}`, `${_.replace(file, '.', '/')}.json`);
|
2017-07-11 11:41:00 +02:00
|
|
|
|
|
|
|
const fileContent = require(filePath);
|
|
|
|
|
|
|
|
_.set(fileContent, objPath, input);
|
|
|
|
|
|
|
|
fs.writeFileSync(filePath, JSON.stringify(fileContent, null, 2), 'utf8');
|
|
|
|
}
|
|
|
|
});
|
2017-07-10 11:40:41 +02:00
|
|
|
}
|
2017-07-06 10:02:00 +02:00
|
|
|
};
|