2019-08-09 17:11:24 +03:00
|
|
|
const Promise = require('bluebird');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const models = require('../../models');
|
|
|
|
const routing = require('../../../frontend/services/routing');
|
2020-05-22 21:22:20 +03:00
|
|
|
const {i18n} = require('../../lib/common');
|
2020-05-28 13:40:50 +03:00
|
|
|
const {BadRequestError, NoPermissionError, NotFoundError} = require('@tryghost/errors');
|
2019-08-09 17:11:24 +03:00
|
|
|
const settingsCache = require('../../services/settings/cache');
|
2020-05-28 13:40:50 +03:00
|
|
|
const membersService = require('../../services/members');
|
2019-08-09 17:11:24 +03:00
|
|
|
|
|
|
|
const SETTINGS_BLACKLIST = [
|
|
|
|
'members_public_key',
|
|
|
|
'members_private_key',
|
|
|
|
'members_session_secret'
|
|
|
|
];
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
return setting.type === 'blog';
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: omit core settings unless internal request
|
|
|
|
if (!frame.options.context.internal) {
|
|
|
|
settings = _.filter(settings, (setting) => {
|
|
|
|
const isCore = setting.type === 'core';
|
|
|
|
const isBlacklisted = SETTINGS_BLACKLIST.includes(setting.key);
|
|
|
|
return !isBlacklisted && !isCore;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
read: {
|
|
|
|
options: ['key'],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
key: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
identifier(frame) {
|
|
|
|
return frame.options.key;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
let setting = settingsCache.get(frame.options.key, {resolve: false});
|
|
|
|
|
|
|
|
if (!setting) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new NotFoundError({
|
|
|
|
message: i18n.t('errors.api.settings.problemFindingSetting', {
|
2019-08-09 17:11:24 +03:00
|
|
|
key: frame.options.key
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: handle in settings model permissible fn
|
|
|
|
if (setting.type === '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-08-09 17:11:24 +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'});
|
|
|
|
},
|
2020-05-28 13:40:47 +03:00
|
|
|
async before(frame) {
|
|
|
|
if (frame.options.context && frame.options.context.internal) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-09 17:11:24 +03:00
|
|
|
|
2020-05-28 13:40:47 +03:00
|
|
|
const firstCoreSetting = frame.data.settings.find(setting => setting.type === 'core');
|
|
|
|
if (firstCoreSetting) {
|
|
|
|
throw new NoPermissionError({
|
|
|
|
message: i18n.t('errors.api.settings.accessCoreSettingFromExtReq')
|
|
|
|
});
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-05-28 13:40:47 +03:00
|
|
|
async query(frame) {
|
2020-05-28 13:40:50 +03:00
|
|
|
const stripeConnectIntegrationToken = frame.data.settings.find(setting => setting.key === 'stripe_connect_integration_token');
|
|
|
|
|
|
|
|
// The `stripe_connect_integration_token` "setting" is only used to set the `stripe_connect_integration` setting.
|
|
|
|
// The `stripe_connect_integration` setting is not allowed to be set directly.
|
|
|
|
const settings = frame.data.settings.filter((setting) => {
|
|
|
|
return !['stripe_connect_integration', 'stripe_connect_integration_token'].includes(setting.key);
|
|
|
|
});
|
2019-08-09 17:11:24 +03:00
|
|
|
|
2020-05-28 13:40:47 +03:00
|
|
|
const getSetting = setting => settingsCache.get(setting.key, {resolve: false});
|
2019-08-09 17:11:24 +03:00
|
|
|
|
2020-05-28 13:40:47 +03:00
|
|
|
const firstUnknownSetting = settings.find(setting => !getSetting(setting));
|
2019-08-09 17:11:24 +03:00
|
|
|
|
2020-05-28 13:40:47 +03:00
|
|
|
if (firstUnknownSetting) {
|
|
|
|
throw new NotFoundError({
|
|
|
|
message: i18n.t('errors.api.settings.problemFindingSetting', {
|
|
|
|
key: firstUnknownSetting.key
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
2019-08-09 17:11:24 +03:00
|
|
|
|
2020-05-28 13:40:47 +03:00
|
|
|
if (!(frame.options.context && frame.options.context.internal)) {
|
|
|
|
const firstCoreSetting = settings.find(setting => getSetting(setting).type === 'core');
|
|
|
|
if (firstCoreSetting) {
|
|
|
|
throw new NoPermissionError({
|
2020-05-22 21:22:20 +03:00
|
|
|
message: i18n.t('errors.api.settings.accessCoreSettingFromExtReq')
|
2020-05-28 13:40:47 +03:00
|
|
|
});
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:40:50 +03:00
|
|
|
if (stripeConnectIntegrationToken) {
|
|
|
|
const getSessionProp = prop => frame.original.session[prop];
|
|
|
|
try {
|
|
|
|
const data = await membersService.stripeConnect.getStripeConnectTokenData(stripeConnectIntegrationToken.value, getSessionProp);
|
|
|
|
settings.push({
|
|
|
|
key: 'stripe_connect_integration',
|
|
|
|
value: JSON.stringify(data)
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
throw new BadRequestError({
|
|
|
|
message: 'The Stripe Connect token could not be parsed.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:40:47 +03:00
|
|
|
return models.Settings.edit(settings, frame.options);
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
upload: {
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
method: 'edit'
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return routing.settings.setFromFilePath(frame.file.path);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
download: {
|
|
|
|
headers: {
|
|
|
|
disposition: {
|
|
|
|
type: 'yaml',
|
|
|
|
value: 'routes.yaml'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
format: 'plain'
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
method: 'browse'
|
|
|
|
},
|
|
|
|
query() {
|
|
|
|
return routing.settings.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|