mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
refs https://github.com/TryGhost/Team/issues/694 refs https://linear.app/tryghost/issue/CORE-13 - The controller code is not meant to contain complex business logic. Removed complexity in settings.edit method - Have separated the regular editing from "Stripe Data" editing to keep the dependency on the members service still in the controller reducing coupling of the settings BREAD service to the minimum. - The stripeConnectData passed into `edit` method still feels out of place (maybe it should be passed as an array already that's ready to be merged with the rest of settings, but that was left for another refactor in the future)
91 lines
2.8 KiB
JavaScript
91 lines
2.8 KiB
JavaScript
/**
|
|
* Settings Lib
|
|
* A collection of utilities for handling settings including a cache
|
|
*/
|
|
const events = require('../../lib/common/events');
|
|
const models = require('../../models');
|
|
const SettingsCache = require('../../../shared/settings-cache');
|
|
const SettingsBREADService = require('./settings-bread-service');
|
|
|
|
// The string returned when a setting is set as write-only
|
|
const obfuscatedSetting = '••••••••';
|
|
|
|
// The function used to decide whether a setting is write-only
|
|
function isSecretSetting(setting) {
|
|
return /secret/.test(setting.key);
|
|
}
|
|
|
|
// The function that obfuscates a write-only setting
|
|
// NOTE: move this method into SettingsBREADService once once refactoring is finished
|
|
function hideValueIfSecret(setting) {
|
|
if (setting.value && isSecretSetting(setting)) {
|
|
return {...setting, value: obfuscatedSetting};
|
|
}
|
|
return setting;
|
|
}
|
|
|
|
/**
|
|
* @returns {SettingsBREADService} instance of the PostsService
|
|
*/
|
|
const getSettingsBREADServiceInstance = () => {
|
|
return new SettingsBREADService({
|
|
SettingsModel: models.Settings,
|
|
settingsCache: SettingsCache
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
/**
|
|
* Initialize the cache, used in boot and in testing
|
|
*/
|
|
async init() {
|
|
const settingsCollection = await models.Settings.populateDefaults();
|
|
SettingsCache.init(events, settingsCollection);
|
|
},
|
|
|
|
/**
|
|
* Shutdown the cache, used in force boot during testing
|
|
*/
|
|
shutdown() {
|
|
SettingsCache.reset(events);
|
|
},
|
|
|
|
/**
|
|
* Handles synchronization of routes.yaml hash loaded in the frontend with
|
|
* the value stored in the settings table.
|
|
* getRoutesHash is a function to allow keeping "frontend" decoupled from settings
|
|
*
|
|
* @param {function} getRoutesHash function fetching currently loaded routes file hash
|
|
*/
|
|
async syncRoutesHash(getRoutesHash) {
|
|
const currentRoutesHash = await getRoutesHash();
|
|
|
|
if (SettingsCache.get('routes_hash') !== currentRoutesHash) {
|
|
return await models.Settings.edit([{
|
|
key: 'routes_hash',
|
|
value: currentRoutesHash
|
|
}], {context: {internal: true}});
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Handles email setting synchronization when email has been verified per instance
|
|
*
|
|
* @param {boolean} configValue current email verification value from local config
|
|
*/
|
|
async syncEmailSettings(configValue) {
|
|
const isEmailDisabled = SettingsCache.get('email_verification_required');
|
|
|
|
if (configValue === true && isEmailDisabled) {
|
|
return await models.Settings.edit([{
|
|
key: 'email_verification_required',
|
|
value: false
|
|
}], {context: {internal: true}});
|
|
}
|
|
},
|
|
|
|
obfuscatedSetting,
|
|
isSecretSetting,
|
|
hideValueIfSecret,
|
|
getSettingsBREADServiceInstance
|
|
};
|