settings: move service setting into services.js

this also introduces getJson/setJson
This commit is contained in:
Girish Ramakrishnan
2023-08-03 11:34:33 +05:30
parent 3caf0c3902
commit bbc6ba1a35
10 changed files with 42 additions and 45 deletions

View File

@@ -276,10 +276,10 @@ async function setGhost(user, password, expiresAt) {
debug(`setGhost: ${user.username} expiresAt ${expiresAt}`);
const ghostData = safe.JSON.parse(await settings.get(settings.GHOSTS_CONFIG_KEY)) || {};
const ghostData = await settings.getJson(settings.GHOSTS_CONFIG_KEY) || {};
ghostData[user.username] = { password, expiresAt };
await settings.set(settings.GHOSTS_CONFIG_KEY, JSON.stringify(ghostData));
await settings.setJson(settings.GHOSTS_CONFIG_KEY, ghostData);
}
// returns true if ghost user was matched
@@ -287,7 +287,7 @@ async function verifyGhost(username, password) {
assert.strictEqual(typeof username, 'string');
assert.strictEqual(typeof password, 'string');
const ghostData = safe.JSON.parse(await settings.get(settings.GHOSTS_CONFIG_KEY)) || {};
const ghostData = await settings.getJson(settings.GHOSTS_CONFIG_KEY) || {};
// either the username is an object with { password, expiresAt } or a string with the password which will expire on first match
if (username in ghostData) {
@@ -296,7 +296,7 @@ async function verifyGhost(username, password) {
debug('verifyGhost: password expired');
delete ghostData[username];
await settings.set(settings.GHOSTS_CONFIG_KEY, JSON.stringify(ghostData));
await settings.setJson(settings.GHOSTS_CONFIG_KEY, ghostData);
return false;
} else if (ghostData[username].password === password) {
@@ -309,7 +309,7 @@ async function verifyGhost(username, password) {
debug('verifyGhost: matched ghost user');
delete ghostData[username];
await settings.set(settings.GHOSTS_CONFIG_KEY, JSON.stringify(ghostData));
await settings.setJson(settings.GHOSTS_CONFIG_KEY, ghostData);
return true;
}
}
@@ -952,9 +952,8 @@ async function setBackgroundImage(id, backgroundImage) {
}
async function getProfileConfig() {
const value = await settings.get(settings.PROFILE_CONFIG_KEY);
if (value === null) return { lockUserProfiles: false, mandatory2FA: false };
return JSON.parse(value);
const value = await settings.getJson(settings.PROFILE_CONFIG_KEY);
return value || { lockUserProfiles: false, mandatory2FA: false };
}
async function setProfileConfig(profileConfig) {
@@ -963,7 +962,7 @@ async function setProfileConfig(profileConfig) {
if (settings.isDemo()) throw new BoxError(BoxError.BAD_FIELD, 'Not allowed in demo mode');
const oldConfig = await getProfileConfig();
await settings.set(settings.PROFILE_CONFIG_KEY, JSON.stringify(profileConfig));
await settings.setJson(settings.PROFILE_CONFIG_KEY, profileConfig);
if (profileConfig.mandatory2FA && !oldConfig.mandatory2FA) {
debug('setProfileConfig: logging out non-2FA users to enforce 2FA');