feat(ui): Key entfernen, Symbolknöpfe bei den Keys (#1068)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GmHhg3osejhFSpUz472iQ1
This commit is contained in:
2026-11-16 13:39:49 +01:00
parent 190dbe8c9f
commit 1dd3903768
10 changed files with 232 additions and 14 deletions

View File

@ -50,6 +50,10 @@ const ICON_PLAY: &str = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><polygo
const ICON_STOP: &str = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><rect x=\"6\" y=\"6\" width=\"12\" height=\"12\" rx=\"2\"/></svg>";
const ICON_EDIT: &str = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><path d=\"M12 20h9\"/><path d=\"M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4Z\"/></svg>";
const ICON_LOCK: &str = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><rect x=\"5\" y=\"11\" width=\"14\" height=\"9\" rx=\"2\"/><path d=\"M8 11V8a4 4 0 0 1 8 0v3\"/></svg>";
const ICON_UNLOCK: &str = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><rect x=\"5\" y=\"11\" width=\"14\" height=\"9\" rx=\"2\"/><path d=\"M8 11V8a4 4 0 0 1 7.5-1.5\"/></svg>";
const ICON_TRASH: &str = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><path d=\"M4 7h16\"/><path d=\"M9 7V4h6v3\"/><path d=\"M6 7l1 13h10l1-13\"/></svg>";
/// Wie [`button`], aber als Zeichenknopf mit Name für Screenreader und Tooltip.
fn icon_button(
viewer: &Viewer,
@ -659,20 +663,24 @@ fn keys_body(state: &AppState, viewer: &Viewer, top: &str) -> String {
let action = if k.name == viewer.key.name {
"<span class=\"klein\">eigener Key</span>".to_string()
} else if k.active {
button(
icon_button(
viewer,
"/ui/keys/status",
&[("name", &k.name), ("an", "0")],
ICON_LOCK,
"Sperren",
"danger",
)
} else {
button(
viewer,
"/ui/keys/status",
&[("name", &k.name), ("an", "1")],
"Freigeben",
"",
format!(
"{}<button class=\"sm icon\" type=\"button\" data-dialog=\"{}\" title=\"Entfernen\" aria-label=\"Entfernen\">{ICON_TRASH}</button>",
icon_button(
viewer,
"/ui/keys/status",
&[("name", &k.name), ("an", "1")],
ICON_UNLOCK,
"Freigeben",
),
dialog_id(&k.name)
)
};
format!(
@ -690,6 +698,30 @@ fn keys_body(state: &AppState, viewer: &Viewer, top: &str) -> String {
)
})
.collect();
let dialogs: String = state
.store()
.list_api_keys()
.unwrap_or_default()
.iter()
.filter(|k| !k.active && k.name != viewer.key.name)
.map(|k| {
format!(
"<dialog class=\"dialog\" id=\"{id}\" aria-labelledby=\"{id}-t\">\
<div class=\"dlg-kopf\"><h2 id=\"{id}-t\">Key {name} entfernen</h2>\
<button class=\"sm\" type=\"button\" data-close aria-label=\"Schließen\">×</button></div>\
<form class=\"dlg-form\" method=\"post\" action=\"/ui/keys/entfernen\">{csrf}\
<input type=\"hidden\" name=\"name\" value=\"{name}\">\
<p>Der Key ist gesperrt und wird gelöscht. Das lässt sich nicht rückgängig machen. \
Bisherige Anfragen dieses Keys bleiben im Anfrage-Log, zeigen aber keinen Key-Namen mehr.</p>\
<label class=\"haken\"><input type=\"checkbox\" name=\"bestaetigt\" value=\"1\" required> Ja, diesen Key entfernen</label>\
<div class=\"dlg-fuss\"><button class=\"danger\" type=\"submit\">Entfernen</button>\
<button type=\"button\" data-close>Abbrechen</button></div></form></dialog>",
id = dialog_id(&k.name),
name = esc(&k.name),
csrf = csrf_field(viewer),
)
})
.collect();
let options = |selected: &str| -> String {
["low", "normal", "high"]
.iter()
@ -703,7 +735,7 @@ fn keys_body(state: &AppState, viewer: &Viewer, top: &str) -> String {
};
format!(
"{top}<section class=\"card\"><div class=\"breit\"><table><thead><tr><th>Name</th><th>Status</th><th>Standard</th><th>Höchstens</th>\
<th>Modelle</th><th>Rechte</th><th>Aktion</th></tr></thead><tbody>{rows}</tbody></table></div></section>\
<th>Modelle</th><th>Rechte</th><th></th></tr></thead><tbody>{rows}</tbody></table></div></section>{dialogs}\
<section class=\"card\"><h2>Neuen Key anlegen</h2>\
<form class=\"formular\" method=\"post\" action=\"/ui/keys/anlegen\">{csrf}\
<label><span>Name (Buchstaben, Ziffern, <code>-</code>, <code>_</code>, <code>.</code>)</span>\
@ -717,7 +749,7 @@ fn keys_body(state: &AppState, viewer: &Viewer, top: &str) -> String {
<label class=\"haken\"><input type=\"checkbox\" name=\"admin\" value=\"1\">Verwaltung (admin)</label>\
<div><button class=\"primary\" type=\"submit\">Key anlegen</button></div></form>\
<p class=\"klein\">Der Key wird nach dem Anlegen genau einmal angezeigt. Gespeichert wird nur ein Hash. \
Keys lassen sich sperren, aber nicht löschen.</p></section>",
Gesperrte Keys lassen sich entfernen.</p></section>",
csrf = csrf_field(viewer),
default = options("normal"),
max = options("normal"),
@ -848,6 +880,38 @@ pub(super) async fn key_status(
)
}
pub(super) async fn key_remove(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
body: Bytes,
) -> Response {
use nexus_store::RemoveKeyOutcome;
let viewer = match admin(&state, &headers, &body) {
Ok(v) => v,
Err(response) => return *response,
};
let name = form_field(&body, "name").unwrap_or_default();
if name == viewer.key.name {
return back("/ui/keys", "eigener_key");
}
if form_field(&body, "bestaetigt").as_deref() != Some("1") {
return back("/ui/keys", "eingabe");
}
let outcome = state.store().remove_api_key(&name);
match outcome {
Ok(RemoveKeyOutcome::Removed) => {
audit(&state, &viewer, "key.remove", &name);
back("/ui/keys", "key_entfernt")
}
Ok(RemoveKeyOutcome::NotFound) => back("/ui/keys", "unbekannt"),
Ok(RemoveKeyOutcome::Active) => back("/ui/keys", "key_entfernen_aktiv"),
Err(e) => {
tracing::error!(fehler = %e, "Key konnte nicht entfernt werden");
back("/ui/keys", "eingabe")
}
}
}
#[cfg(test)]
mod tests {
use super::*;