2018-10-12 20:44:02 +03:00
|
|
|
const Promise = require('bluebird');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const models = require('../../models');
|
2020-09-09 15:28:12 +03:00
|
|
|
const frontendRouting = require('../../../frontend/services/routing');
|
|
|
|
const frontendSettings = require('../../../frontend/services/settings');
|
2020-05-22 21:22:20 +03:00
|
|
|
const {i18n} = require('../../lib/common');
|
|
|
|
const {NoPermissionError, NotFoundError} = require('@tryghost/errors');
|
2020-09-09 15:28:12 +03:00
|
|
|
const settingsService = require('../../services/settings');
|
2018-10-12 20:44:02 +03:00
|
|
|
const settingsCache = require('../../services/settings/cache');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'settings',
|
|
|
|
|
|
|
|
browse: {
|
|
|
|
options: ['type'],
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
let settings = settingsCache.getAll();
|
|
|
|
|
|
|
|
// CASE: no context passed (functional call)
|
|
|
|
if (!frame.options.context) {
|
|
|
|
return Promise.resolve(settings.filter((setting) => {
|
2020-06-25 07:32:16 +03:00
|
|
|
return setting.group === 'site';
|
2018-10-12 20:44:02 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: omit core settings unless internal request
|
|
|
|
if (!frame.options.context.internal) {
|
|
|
|
settings = _.filter(settings, (setting) => {
|
2020-06-25 07:32:16 +03:00
|
|
|
const isCore = setting.group === 'core';
|
2020-06-24 16:55:50 +03:00
|
|
|
return !isCore;
|
2018-10-12 20:44:02 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
read: {
|
|
|
|
options: ['key'],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
key: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
identifier(frame) {
|
|
|
|
return frame.options.key;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
query(frame) {
|
2020-07-14 11:10:59 +03:00
|
|
|
let setting;
|
|
|
|
if (frame.options.key === 'slack') {
|
|
|
|
const slackURL = settingsCache.get('slack_url', {resolve: false});
|
|
|
|
const slackUsername = settingsCache.get('slack_username', {resolve: false});
|
|
|
|
|
|
|
|
setting = slackURL || slackUsername;
|
|
|
|
setting.key = 'slack';
|
|
|
|
setting.value = [{
|
|
|
|
url: slackURL && slackURL.value,
|
|
|
|
username: slackUsername && slackUsername.value
|
|
|
|
}];
|
|
|
|
} else if (frame.options.key === 'slack_url' || frame.options.key === 'slack_username') {
|
|
|
|
// leave the value empty returning 404 for unknown in current API keys
|
|
|
|
} else {
|
|
|
|
setting = settingsCache.get(frame.options.key, {resolve: false});
|
|
|
|
}
|
2018-10-12 20:44:02 +03:00
|
|
|
|
2019-03-05 00:59:13 +03:00
|
|
|
if (!setting) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new NotFoundError({
|
|
|
|
message: i18n.t('errors.api.settings.problemFindingSetting', {
|
2019-03-05 00:59:13 +03:00
|
|
|
key: frame.options.key
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: handle in settings model permissible fn
|
2020-06-25 07:32:16 +03:00
|
|
|
if (setting.group === 'core' && !(frame.options.context && frame.options.context.internal)) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new NoPermissionError({
|
|
|
|
message: i18n.t('errors.api.settings.accessCoreSettingFromExtReq')
|
2019-03-05 00:59:13 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2018-10-12 20:44:02 +03:00
|
|
|
return {
|
|
|
|
[frame.options.key]: setting
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
edit: {
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
permissions: {
|
2019-10-09 11:26:54 +03:00
|
|
|
unsafeAttrsObject(frame) {
|
|
|
|
return _.find(frame.data.settings, {key: 'labs'});
|
|
|
|
},
|
2018-10-12 20:44:02 +03:00
|
|
|
before(frame) {
|
|
|
|
const errors = [];
|
|
|
|
|
|
|
|
frame.data.settings.map((setting) => {
|
2020-06-25 07:32:16 +03:00
|
|
|
if (setting.group === 'core' && !(frame.options.context && frame.options.context.internal)) {
|
2020-05-22 21:22:20 +03:00
|
|
|
errors.push(new NoPermissionError({
|
|
|
|
message: i18n.t('errors.api.settings.accessCoreSettingFromExtReq')
|
2018-10-12 20:44:02 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (errors.length) {
|
|
|
|
return Promise.reject(errors[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
let type = frame.data.settings.find((setting) => {
|
|
|
|
return setting.key === 'type';
|
|
|
|
});
|
|
|
|
|
|
|
|
if (_.isObject(type)) {
|
|
|
|
type = type.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame.data.settings = _.reject(frame.data.settings, (setting) => {
|
|
|
|
return setting.key === 'type';
|
|
|
|
});
|
|
|
|
|
2019-03-05 00:59:13 +03:00
|
|
|
const errors = [];
|
|
|
|
|
|
|
|
_.each(frame.data.settings, (setting) => {
|
|
|
|
const settingFromCache = settingsCache.get(setting.key, {resolve: false});
|
|
|
|
|
|
|
|
if (!settingFromCache) {
|
2020-05-22 21:22:20 +03:00
|
|
|
errors.push(new NotFoundError({
|
|
|
|
message: i18n.t('errors.api.settings.problemFindingSetting', {
|
2019-03-05 00:59:13 +03:00
|
|
|
key: setting.key
|
|
|
|
})
|
|
|
|
}));
|
2020-06-25 07:32:16 +03:00
|
|
|
} else if (settingFromCache.core === 'core' && !(frame.options.context && frame.options.context.internal)) {
|
2019-03-05 00:59:13 +03:00
|
|
|
// @TODO: handle in settings model permissible fn
|
2020-05-22 21:22:20 +03:00
|
|
|
errors.push(new NoPermissionError({
|
|
|
|
message: i18n.t('errors.api.settings.accessCoreSettingFromExtReq')
|
2019-03-05 00:59:13 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (errors.length) {
|
|
|
|
return Promise.reject(errors[0]);
|
|
|
|
}
|
|
|
|
|
2018-10-12 20:44:02 +03:00
|
|
|
return models.Settings.edit(frame.data.settings, frame.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
upload: {
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
method: 'edit'
|
|
|
|
},
|
2020-09-09 15:28:12 +03:00
|
|
|
async query(frame) {
|
|
|
|
await frontendRouting.settings.setFromFilePath(frame.file.path);
|
|
|
|
const getRoutesHash = () => frontendSettings.getCurrentHash('routes');
|
|
|
|
await settingsService.syncRoutesHash(getRoutesHash);
|
2018-10-12 20:44:02 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
download: {
|
|
|
|
headers: {
|
|
|
|
disposition: {
|
|
|
|
type: 'yaml',
|
2019-01-14 21:05:16 +03:00
|
|
|
value: 'routes.yaml'
|
2018-10-12 20:44:02 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
format: 'plain'
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
method: 'browse'
|
|
|
|
},
|
|
|
|
query() {
|
2020-09-09 15:28:12 +03:00
|
|
|
return frontendRouting.settings.get();
|
2018-10-12 20:44:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|