js: Kill gGlobalSettings. Decouples things a bit.

This commit is contained in:
Cory McWilliams 2024-10-16 18:11:08 -04:00
parent 32db18b0d6
commit e638b155a1
2 changed files with 57 additions and 59 deletions

View File

@ -87,10 +87,6 @@ const k_global_settings = {
}, },
}; };
let gGlobalSettings = {
index: '/~core/apps/',
};
let kPingInterval = 60 * 1000; let kPingInterval = 60 * 1000;
/** /**
@ -345,59 +341,59 @@ async function getProcessBlob(blobId, key, options) {
return []; return [];
} }
}, },
permissionsGranted: function () { permissionsGranted: async function () {
let user = process?.credentials?.session?.name; let user = process?.credentials?.session?.name;
let settings = await loadSettings();
if ( if (
user && user &&
options?.packageOwner && options?.packageOwner &&
options?.packageName && options?.packageName &&
gGlobalSettings.userPermissions && settings.userPermissions &&
gGlobalSettings.userPermissions[user] && settings.userPermissions[user] &&
gGlobalSettings.userPermissions[user][options.packageOwner] settings.userPermissions[user][options.packageOwner]
) { ) {
return gGlobalSettings.userPermissions[user][ return settings.userPermissions[user][
options.packageOwner options.packageOwner
][options.packageName]; ][options.packageName];
} }
}, },
allPermissionsGranted: function () { allPermissionsGranted: async function () {
let user = process?.credentials?.session?.name; let user = process?.credentials?.session?.name;
let settings = await loadSettings();
if ( if (
user && user &&
options?.packageOwner && options?.packageOwner &&
options?.packageName && options?.packageName &&
gGlobalSettings.userPermissions && settings.userPermissions &&
gGlobalSettings.userPermissions[user] settings.userPermissions[user]
) { ) {
return gGlobalSettings.userPermissions[user]; return settings.userPermissions[user];
} }
}, },
permissionsForUser: function (user) { permissionsForUser: async function (user) {
return ( let settings = await loadSettings();
(gGlobalSettings?.permissions return settings?.permissions?.[user] ?? [];
? gGlobalSettings.permissions[user]
: []) ?? []
);
}, },
apps: (user) => getApps(user, process), apps: (user) => getApps(user, process),
getSockets: getSockets, getSockets: getSockets,
permissionTest: function (permission) { permissionTest: async function (permission) {
let user = process?.credentials?.session?.name; let user = process?.credentials?.session?.name;
let settings = await loadSettings();
if (!user || !options?.packageOwner || !options?.packageName) { if (!user || !options?.packageOwner || !options?.packageName) {
return; return;
} else if ( } else if (
gGlobalSettings.userPermissions && settings.userPermissions &&
gGlobalSettings.userPermissions[user] && settings.userPermissions[user] &&
gGlobalSettings.userPermissions[user][options.packageOwner] && settings.userPermissions[user][options.packageOwner] &&
gGlobalSettings.userPermissions[user][options.packageOwner][ settings.userPermissions[user][options.packageOwner][
options.packageName options.packageName
] && ] &&
gGlobalSettings.userPermissions[user][options.packageOwner][ settings.userPermissions[user][options.packageOwner][
options.packageName options.packageName
][permission] !== undefined ][permission] !== undefined
) { ) {
if ( if (
gGlobalSettings.userPermissions[user][options.packageOwner][ settings.userPermissions[user][options.packageOwner][
options.packageName options.packageName
][permission] ][permission]
) { ) {
@ -509,23 +505,24 @@ async function getProcessBlob(blobId, key, options) {
} }
}; };
if (process.credentials?.permissions?.administration) { if (process.credentials?.permissions?.administration) {
imports.core.globalSettingsDescriptions = function () { imports.core.globalSettingsDescriptions = async function () {
let settings = Object.assign({}, k_global_settings); let settings = Object.assign({}, k_global_settings);
for (let [key, value] of Object.entries(gGlobalSettings)) { for (let [key, value] of Object.entries(await loadSettings())) {
if (settings[key]) { if (settings[key]) {
settings[key].value = value; settings[key].value = value;
} }
} }
return settings; return settings;
}; };
imports.core.globalSettingsGet = function (key) { imports.core.globalSettingsGet = async function (key) {
return gGlobalSettings[key]; let settings = await loadSettings();
return settings?.[key];
}; };
imports.core.globalSettingsSet = async function (key, value) { imports.core.globalSettingsSet = async function (key, value) {
print('Setting', key, value); print('Setting', key, value);
await loadSettings(); let settings = await loadSettings();
gGlobalSettings[key] = value; settings[key] = value;
setGlobalSettings(gGlobalSettings); await setGlobalSettings(settings);
print('Done.'); print('Done.');
}; };
imports.core.deleteUser = async function (user) { imports.core.deleteUser = async function (user) {
@ -707,8 +704,9 @@ async function getProcessBlob(blobId, key, options) {
imports.ssb.addEventListener = undefined; imports.ssb.addEventListener = undefined;
imports.ssb.removeEventListener = undefined; imports.ssb.removeEventListener = undefined;
imports.ssb.getIdentityInfo = undefined; imports.ssb.getIdentityInfo = undefined;
imports.fetch = function (url, options) { imports.fetch = async function (url, options) {
return http.fetch(url, options, gGlobalSettings.fetch_hosts); let settings = await loadSettings();
return http.fetch(url, options, settings?.fetch_hosts);
}; };
if ( if (
@ -759,13 +757,13 @@ async function getProcessBlob(blobId, key, options) {
); );
}; };
} }
process.sendPermissions = function sendPermissions() { process.sendPermissions = async function sendPermissions() {
process.app.send({ process.app.send({
action: 'permissions', action: 'permissions',
permissions: imports.core.permissionsGranted(), permissions: await imports.core.permissionsGranted(),
}); });
}; };
process.resetPermission = function resetPermission(permission) { process.resetPermission = async function resetPermission(permission) {
let user = process?.credentials?.session?.name; let user = process?.credentials?.session?.name;
storePermission( storePermission(
user, user,
@ -774,7 +772,7 @@ async function getProcessBlob(blobId, key, options) {
permission, permission,
undefined undefined
); );
process.sendPermissions(); return process.sendPermissions();
}; };
process.task.setImports(imports); process.task.setImports(imports);
process.task.activate(); process.task.activate();
@ -807,7 +805,7 @@ async function getProcessBlob(blobId, key, options) {
broadcastEvent('onSessionBegin', [getUser(process, process)]); broadcastEvent('onSessionBegin', [getUser(process, process)]);
if (process.app) { if (process.app) {
process.app.send({action: 'ready', version: version()}); process.app.send({action: 'ready', version: version()});
process.sendPermissions(); await process.sendPermissions();
} }
await process.task.execute({name: appSourceName, source: appSource}); await process.task.execute({name: appSourceName, source: appSource});
resolveReady(process); resolveReady(process);
@ -837,7 +835,6 @@ async function getProcessBlob(blobId, key, options) {
* @returns * @returns
*/ */
async function setGlobalSettings(settings) { async function setGlobalSettings(settings) {
gGlobalSettings = settings;
try { try {
return await new Database('core').set('settings', JSON.stringify(settings)); return await new Database('core').set('settings', JSON.stringify(settings));
} catch (error) { } catch (error) {
@ -1229,7 +1226,7 @@ async function loadSettings() {
data[key] = value.default_value; data[key] = value.default_value;
} }
} }
gGlobalSettings = data; return data;
} }
/** /**
@ -1254,9 +1251,9 @@ function sendStats() {
* TODOC * TODOC
*/ */
loadSettings() loadSettings()
.then(function () { .then(function (settings) {
if (tildefriends.https_port && gGlobalSettings.http_redirect) { if (tildefriends.https_port && settings.http_redirect) {
httpd.set_http_redirect(gGlobalSettings.http_redirect); httpd.set_http_redirect(settings.http_redirect);
} }
httpd.all('/app/socket', app.socket); httpd.all('/app/socket', app.socket);
httpd.all('', function default_http_handler(request, response) { httpd.all('', function default_http_handler(request, response) {
@ -1332,34 +1329,35 @@ loadSettings()
* @param {*} permission * @param {*} permission
* @param {*} allow * @param {*} allow
*/ */
function storePermission(user, packageOwner, packageName, permission, allow) { async function storePermission(user, packageOwner, packageName, permission, allow) {
if (!gGlobalSettings.userPermissions) { let settings = await loadSettings();
gGlobalSettings.userPermissions = {}; if (!settings.userPermissions) {
settings.userPermissions = {};
} }
if (!gGlobalSettings.userPermissions[user]) { if (!settings.userPermissions[user]) {
gGlobalSettings.userPermissions[user] = {}; settings.userPermissions[user] = {};
} }
if (!gGlobalSettings.userPermissions[user][packageOwner]) { if (!settings.userPermissions[user][packageOwner]) {
gGlobalSettings.userPermissions[user][packageOwner] = {}; settings.userPermissions[user][packageOwner] = {};
} }
if (!gGlobalSettings.userPermissions[user][packageOwner][packageName]) { if (!settings.userPermissions[user][packageOwner][packageName]) {
gGlobalSettings.userPermissions[user][packageOwner][packageName] = {}; settings.userPermissions[user][packageOwner][packageName] = {};
} }
if ( if (
gGlobalSettings.userPermissions[user][packageOwner][packageName][ settings.userPermissions[user][packageOwner][packageName][
permission permission
] !== allow ] !== allow
) { ) {
if (allow === undefined) { if (allow === undefined) {
delete gGlobalSettings.userPermissions[user][packageOwner][packageName][ delete settings.userPermissions[user][packageOwner][packageName][
permission permission
]; ];
} else { } else {
gGlobalSettings.userPermissions[user][packageOwner][packageName][ settings.userPermissions[user][packageOwner][packageName][
permission permission
] = allow; ] = allow;
} }
setGlobalSettings(gGlobalSettings); return setGlobalSettings(settings);
} }
} }

2
deps/c-ares vendored

@ -1 +1 @@
Subproject commit 0c1c60dc60114812eb5950e6b50fa69d923a20e6 Subproject commit a57ff692eeab8d21c853dc1ddaf0164f517074c3