mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Refactored the settings edit controller
no-issue There was some unused code here, the variable was never used, also we were looping and collecting a list of errors, but only every using the first one, so switched to the `find` method which stops iteration after an element has matched.
This commit is contained in:
parent
7fe945f4dd
commit
88b4c5571d
1 changed files with 27 additions and 42 deletions
|
@ -87,59 +87,44 @@ module.exports = {
|
|||
unsafeAttrsObject(frame) {
|
||||
return _.find(frame.data.settings, {key: 'labs'});
|
||||
},
|
||||
before(frame) {
|
||||
const errors = [];
|
||||
async before(frame) {
|
||||
if (frame.options.context && frame.options.context.internal) {
|
||||
return;
|
||||
}
|
||||
|
||||
frame.data.settings.map((setting) => {
|
||||
if (setting.type === 'core' && !(frame.options.context && frame.options.context.internal)) {
|
||||
errors.push(new NoPermissionError({
|
||||
message: i18n.t('errors.api.settings.accessCoreSettingFromExtReq')
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
if (errors.length) {
|
||||
return Promise.reject(errors[0]);
|
||||
const firstCoreSetting = frame.data.settings.find(setting => setting.type === 'core');
|
||||
if (firstCoreSetting) {
|
||||
throw new NoPermissionError({
|
||||
message: i18n.t('errors.api.settings.accessCoreSettingFromExtReq')
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
query(frame) {
|
||||
let type = frame.data.settings.find((setting) => {
|
||||
return setting.key === 'type';
|
||||
});
|
||||
async query(frame) {
|
||||
const settings = frame.data.settings;
|
||||
|
||||
if (_.isObject(type)) {
|
||||
type = type.value;
|
||||
const getSetting = setting => settingsCache.get(setting.key, {resolve: false});
|
||||
|
||||
const firstUnknownSetting = settings.find(setting => !getSetting(setting));
|
||||
|
||||
if (firstUnknownSetting) {
|
||||
throw new NotFoundError({
|
||||
message: i18n.t('errors.api.settings.problemFindingSetting', {
|
||||
key: firstUnknownSetting.key
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
frame.data.settings = _.reject(frame.data.settings, (setting) => {
|
||||
return setting.key === 'type';
|
||||
});
|
||||
|
||||
const errors = [];
|
||||
|
||||
_.each(frame.data.settings, (setting) => {
|
||||
const settingFromCache = settingsCache.get(setting.key, {resolve: false});
|
||||
|
||||
if (!settingFromCache) {
|
||||
errors.push(new NotFoundError({
|
||||
message: i18n.t('errors.api.settings.problemFindingSetting', {
|
||||
key: setting.key
|
||||
})
|
||||
}));
|
||||
} else if (settingFromCache.type === 'core' && !(frame.options.context && frame.options.context.internal)) {
|
||||
// @TODO: handle in settings model permissible fn
|
||||
errors.push(new NoPermissionError({
|
||||
if (!(frame.options.context && frame.options.context.internal)) {
|
||||
const firstCoreSetting = settings.find(setting => getSetting(setting).type === 'core');
|
||||
if (firstCoreSetting) {
|
||||
throw new NoPermissionError({
|
||||
message: i18n.t('errors.api.settings.accessCoreSettingFromExtReq')
|
||||
}));
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (errors.length) {
|
||||
return Promise.reject(errors[0]);
|
||||
}
|
||||
|
||||
return models.Settings.edit(frame.data.settings, frame.options);
|
||||
return models.Settings.edit(settings, frame.options);
|
||||
}
|
||||
},
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue