split routes and model code into user-directory.js

This commit is contained in:
Girish Ramakrishnan
2024-06-12 10:27:59 +02:00
parent e1f87161a8
commit b4e7e394c3
10 changed files with 121 additions and 80 deletions

41
src/user-directory.js Normal file
View File

@@ -0,0 +1,41 @@
'use strict';
exports = module.exports = {
getProfileConfig,
setProfileConfig
};
const assert = require('assert'),
BoxError = require('./boxerror.js'),
constants = require('./constants.js'),
debug = require('debug')('box:user-directory'),
oidc = require('./oidc.js'),
settings = require('./settings.js'),
tokens = require('./tokens.js'),
users = require('./users.js');
async function getProfileConfig() {
const value = await settings.getJson(settings.PROFILE_CONFIG_KEY);
return value || { lockUserProfiles: false, mandatory2FA: false };
}
async function setProfileConfig(profileConfig) {
assert.strictEqual(typeof profileConfig, 'object');
if (constants.DEMO) throw new BoxError(BoxError.BAD_STATE, 'Not allowed in demo mode');
const oldConfig = await getProfileConfig();
await settings.setJson(settings.PROFILE_CONFIG_KEY, profileConfig);
if (profileConfig.mandatory2FA && !oldConfig.mandatory2FA) {
debug('setProfileConfig: logging out non-2FA users to enforce 2FA');
const allUsers = await users.list();
for (const user of allUsers) {
if (user.twoFactorAuthenticationEnabled) continue;
await tokens.delByUserIdAndType(user.id, tokens.ID_WEBADMIN);
await oidc.revokeByUserId(user.id);
}
}
}