55 lines
2.6 KiB
JavaScript
55 lines
2.6 KiB
JavaScript
'use strict';
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const settingsPath = path.join(__dirname, '..', 'data', 'admin-settings.json');
|
|
|
|
function getSettings() {
|
|
try {
|
|
const raw = fs.readFileSync(settingsPath, 'utf8');
|
|
const parsed = JSON.parse(raw);
|
|
return {
|
|
ouDn: typeof parsed.ouDn === 'string' ? parsed.ouDn : '',
|
|
defaultGroups: Array.isArray(parsed.defaultGroups)
|
|
? parsed.defaultGroups.filter((g) => typeof g === 'string')
|
|
: [],
|
|
phoneRequired: !!parsed.phoneRequired,
|
|
authEnabled: parsed.authEnabled === undefined ? true : !!parsed.authEnabled,
|
|
apiKey: typeof parsed.apiKey === 'string' ? parsed.apiKey : '',
|
|
logonScript: typeof parsed.logonScript === 'string' ? parsed.logonScript : 'PST.bat',
|
|
homeDrive: typeof parsed.homeDrive === 'string' ? parsed.homeDrive : 'Z:',
|
|
homeBasePath: typeof parsed.homeBasePath === 'string' ? parsed.homeBasePath : '\\\\dc\\Teachers',
|
|
crmUrl: typeof parsed.crmUrl === 'string' ? parsed.crmUrl : '',
|
|
quickLinksUrl: typeof parsed.quickLinksUrl === 'string' ? parsed.quickLinksUrl : ''
|
|
};
|
|
} catch (err) {
|
|
return {
|
|
ouDn: '', defaultGroups: [], phoneRequired: false, authEnabled: true, apiKey: '',
|
|
logonScript: 'PST.bat', homeDrive: 'Z:', homeBasePath: '\\\\dc\\Teachers',
|
|
crmUrl: '', quickLinksUrl: ''
|
|
};
|
|
}
|
|
}
|
|
|
|
function updateSettings(patch) {
|
|
const current = getSettings();
|
|
const next = { ...current };
|
|
if (patch && typeof patch.ouDn === 'string') next.ouDn = patch.ouDn;
|
|
if (patch && Array.isArray(patch.defaultGroups)) {
|
|
next.defaultGroups = patch.defaultGroups.filter((g) => typeof g === 'string');
|
|
}
|
|
if (patch && typeof patch.phoneRequired === 'boolean') next.phoneRequired = patch.phoneRequired;
|
|
if (patch && typeof patch.authEnabled === 'boolean') next.authEnabled = patch.authEnabled;
|
|
if (patch && typeof patch.apiKey === 'string') next.apiKey = patch.apiKey;
|
|
if (patch && typeof patch.logonScript === 'string') next.logonScript = patch.logonScript;
|
|
if (patch && typeof patch.homeDrive === 'string') next.homeDrive = patch.homeDrive;
|
|
if (patch && typeof patch.homeBasePath === 'string') next.homeBasePath = patch.homeBasePath;
|
|
if (patch && typeof patch.crmUrl === 'string') next.crmUrl = patch.crmUrl;
|
|
if (patch && typeof patch.quickLinksUrl === 'string') next.quickLinksUrl = patch.quickLinksUrl;
|
|
fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
|
|
fs.writeFileSync(settingsPath, JSON.stringify(next, null, 2), 'utf8');
|
|
return next;
|
|
}
|
|
|
|
module.exports = { getSettings, updateSettings, settingsPath }; |