WIP managing a per-app current identity from the Tilde Friends navigation bar.

This commit is contained in:
Cory McWilliams 2024-04-13 13:22:59 -04:00
parent c8b8a8fc03
commit 0412b97170
4 changed files with 94 additions and 14 deletions

View File

@ -144,6 +144,8 @@ function socket(request, response, client) {
JSON.stringify({ JSON.stringify({
action: 'session', action: 'session',
credentials: credentials, credentials: credentials,
identities: await ssb.getIdentities(credentials?.session?.name),
identity: await core.getActiveIdentity(credentials?.session?.name, packageOwner, packageName),
parentApp: parentApp, parentApp: parentApp,
id: blobId, id: blobId,
}), }),
@ -210,6 +212,10 @@ function socket(request, response, client) {
if (process) { if (process) {
process.resetPermission(message.permission); process.resetPermission(message.permission);
} }
} else if (message.action == 'setActiveIdentity') {
process.setActiveIdentity(message.identity);
} else if (message.action == 'createIdentity') {
process.createIdentity();
} else if (message.message == 'tfrpc') { } else if (message.message == 'tfrpc') {
if (message.id && g_calls[message.id]) { if (message.id && g_calls[message.id]) {
if (message.error !== undefined) { if (message.error !== undefined) {

View File

@ -56,6 +56,8 @@ class TfNavigationElement extends LitElement {
spark_lines: {type: Object}, spark_lines: {type: Object},
version: {type: Object}, version: {type: Object},
show_version: {type: Boolean}, show_version: {type: Boolean},
identity: {type: String},
identities: {type: Array},
}; };
} }
@ -65,6 +67,7 @@ class TfNavigationElement extends LitElement {
this.show_permissions = false; this.show_permissions = false;
this.status = {}; this.status = {};
this.spark_lines = {}; this.spark_lines = {};
this.identities = [];
} }
/** /**
@ -128,6 +131,37 @@ class TfNavigationElement extends LitElement {
} }
} }
set_active_identity(event) {
send({action: 'setActiveIdentity', identity: event.srcElement.value});
}
create_identity(event) {
send({action: 'createIdentity'});
}
render_identity() {
if (this.identities?.length) {
return html`
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
<select
@change=${this.set_active_identity}
class="w3-input"
style="flex: 0 1 auto; width: auto; display: inline-blocktext-overflow: ellipsis; overflow: hidden; white-space: nowrap">
${this.identities.map(x => html`<option ?selected=${x === this.identity}>${x}</option>`)}
</select>
`;
} else {
return html`
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
<button
@click=${this.create_identity}
class="w3-button w3-blue">
Create an Identity
</button>
`;
}
}
/** /**
* TODOC * TODOC
* @returns * @returns
@ -257,6 +291,7 @@ class TfNavigationElement extends LitElement {
x, x,
])}</span ])}</span
> >
${this.render_identity()}
<span style="flex: 0 0; white-space: nowrap" <span style="flex: 0 0; white-space: nowrap"
>${this.render_login()}</span >${this.render_login()}</span
> >
@ -1125,11 +1160,17 @@ function api_setHash(hash) {
function _receive_websocket_message(message) { function _receive_websocket_message(message) {
if (message && message.action == 'session') { if (message && message.action == 'session') {
setStatusMessage('🟢 Executing...', kStatusColor); setStatusMessage('🟢 Executing...', kStatusColor);
document.getElementsByTagName('tf-navigation')[0].credentials = let navigation = document.getElementsByTagName('tf-navigation')[0];
message.credentials; navigation.credentials = message.credentials;
navigation.identities = message.identities;
navigation.identity = message.identity;
} else if (message && message.action == 'permissions') { } else if (message && message.action == 'permissions') {
document.getElementsByTagName('tf-navigation')[0].permissions = let navigation = document.getElementsByTagName('tf-navigation')[0];
message.permissions ?? {}; navigation.permissions = message.permissions ?? {};
} else if (message && message.action == 'identities') {
let navigation = document.getElementsByTagName('tf-navigation')[0];
navigation.identities = message.identities;
navigation.identity = message.identity;
} else if (message && message.action == 'ready') { } else if (message && message.action == 'ready') {
setStatusMessage(null); setStatusMessage(null);
if (window.location.hash) { if (window.location.hash) {

View File

@ -508,6 +508,30 @@ async function getProcessBlob(blobId, key, options) {
url: options?.url, url: options?.url,
}, },
}; };
process.sendIdentities = async function() {
process.app.send({
action: 'identities',
identities: await ssb.getIdentities(process?.credentials?.session?.name),
identity: await getActiveIdentity(process?.credentials?.session?.name, options.packageOwner, options.packageName),
});
};
process.setActiveIdentity = async function(identity) {
if (process?.credentials?.session?.name && options.packageOwner && options.packageName) {
await new Database(process?.credentials?.session?.name).set(`id:${options.packageOwner}:${options.packageName}`, identity);
}
process.sendIdentities();
};
process.createIdentity = async function() {
if (
process.credentials &&
process.credentials.session &&
process.credentials.session.name
) {
let id = ssb.createIdentity(process.credentials.session.name);
await process.sendIdentities();
return id;
}
}
if (process.credentials?.permissions?.administration) { if (process.credentials?.permissions?.administration) {
imports.core.globalSettingsDescriptions = function () { imports.core.globalSettingsDescriptions = function () {
let settings = Object.assign({}, k_global_settings); let settings = Object.assign({}, k_global_settings);
@ -578,15 +602,7 @@ async function getProcessBlob(blobId, key, options) {
Object.keys(ssb).map((key) => [key, ssb[key].bind(ssb)]) Object.keys(ssb).map((key) => [key, ssb[key].bind(ssb)])
); );
imports.ssb.port = tildefriends.ssb_port; imports.ssb.port = tildefriends.ssb_port;
imports.ssb.createIdentity = function () { imports.ssb.createIdentity = () => process.createIdentity();
if (
process.credentials &&
process.credentials.session &&
process.credentials.session.name
) {
return ssb.createIdentity(process.credentials.session.name);
}
};
imports.ssb.addIdentity = function (id) { imports.ssb.addIdentity = function (id) {
if ( if (
process.credentials && process.credentials &&
@ -613,6 +629,8 @@ async function getProcessBlob(blobId, key, options) {
}); });
} }
}; };
imports.ssb.setActiveIdentity = id => process.setActiveIdentity(id);
imports.ssb.getActiveIdentity = () => getActiveIdentity(process.credentials?.session?.name, options.packageOwner, options.packageName);
imports.ssb.getOwnerIdentities = function () { imports.ssb.getOwnerIdentities = function () {
if (options.packageOwner) { if (options.packageOwner) {
return ssb.getIdentities(options.packageOwner); return ssb.getIdentities(options.packageOwner);
@ -1458,10 +1476,25 @@ function storePermission(user, packageOwner, packageName, permission, allow) {
} }
} }
async function getActiveIdentity(user, packageOwner, packageName) {
if (user && packageOwner && packageName) {
let id = await new Database(user).get(`id:${packageOwner}:${packageName}`);
if (!id) {
let ids = await ssb.getIdentities(user);
if (ids) {
id = ids[0];
}
}
return id;
}
}
export { export {
gGlobalSettings as globalSettings, gGlobalSettings as globalSettings,
setGlobalSettings, setGlobalSettings,
enableStats, enableStats,
invoke, invoke,
getSessionProcessBlob, getSessionProcessBlob,
getActiveIdentity,
}; };

View File

@ -474,7 +474,7 @@ static JSValue _httpd_auth_query(JSContext* context, JSValueConst this_val, int
JSValue settings_value = settings ? JS_ParseJSON(context, settings, strlen(settings), NULL) : JS_UNDEFINED; JSValue settings_value = settings ? JS_ParseJSON(context, settings, strlen(settings), NULL) : JS_UNDEFINED;
JSValue permissions = JS_GetPropertyStr(context, settings_value, "permissions"); JSValue permissions = JS_GetPropertyStr(context, settings_value, "permissions");
JSValue user_permissions = JS_GetPropertyStr(context, permissions, name_string); JSValue user_permissions = JS_GetPropertyStr(context, permissions, name_string);
int length = tf_util_get_length(context, user_permissions); int length = !JS_IsUndefined(user_permissions) ? tf_util_get_length(context, user_permissions) : 0 ;
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
{ {
JSValue permission = JS_GetPropertyUint32(context, user_permissions, i); JSValue permission = JS_GetPropertyUint32(context, user_permissions, i);