2017-02-27 18:53:04 +03:00
|
|
|
/**
|
|
|
|
* Settings Lib
|
|
|
|
* A collection of utilities for handling settings including a cache
|
|
|
|
*/
|
2021-06-30 16:26:59 +03:00
|
|
|
const events = require('../../lib/common/events');
|
2019-06-25 19:33:56 +03:00
|
|
|
const models = require('../../models');
|
2021-06-30 16:56:57 +03:00
|
|
|
const SettingsCache = require('../../../shared/settings-cache');
|
2017-02-27 18:53:04 +03:00
|
|
|
|
2021-04-16 19:05:13 +03:00
|
|
|
// 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
|
|
|
|
function hideValueIfSecret(setting) {
|
|
|
|
if (setting.value && isSecretSetting(setting)) {
|
|
|
|
return {...setting, value: obfuscatedSetting};
|
|
|
|
}
|
|
|
|
return setting;
|
|
|
|
}
|
|
|
|
|
2017-02-27 18:53:04 +03:00
|
|
|
module.exports = {
|
2021-06-30 12:32:04 +03:00
|
|
|
/**
|
|
|
|
* Initialise the cache, used in boot and in testing
|
|
|
|
*/
|
2020-07-06 18:09:43 +03:00
|
|
|
async init() {
|
|
|
|
const settingsCollection = await models.Settings.populateDefaults();
|
2021-06-30 16:26:59 +03:00
|
|
|
SettingsCache.init(events, settingsCollection);
|
2020-07-01 19:16:57 +03:00
|
|
|
},
|
|
|
|
|
2021-06-30 12:32:04 +03:00
|
|
|
/**
|
|
|
|
* Shutdown the cache, used in force boot during testing
|
|
|
|
*/
|
|
|
|
shutdown() {
|
2021-06-30 16:26:59 +03:00
|
|
|
SettingsCache.reset(events);
|
2021-06-30 12:32:04 +03:00
|
|
|
},
|
|
|
|
|
2020-09-09 15:28:12 +03:00
|
|
|
/**
|
|
|
|
* Handles syncronization 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}});
|
|
|
|
}
|
2021-04-16 18:05:16 +03:00
|
|
|
},
|
|
|
|
|
2021-04-16 19:05:13 +03:00
|
|
|
obfuscatedSetting,
|
|
|
|
isSecretSetting,
|
|
|
|
hideValueIfSecret
|
2017-02-27 18:53:04 +03:00
|
|
|
};
|