Neue Sektion 'Debug-Modus' auf der Auftragskatalog-Seite (Kunden- und Admin-Bereich, gleiches zurueck_url-Praefix-Muster wie die bestehenden select/deselect-Formulare) mit Aktivieren/Beenden-Toggle. Geräteliste zeigt jetzt ein kleines Debug-Badge. Hardware-Seite bekommt eine neue Sektion 'Aktueller Zustand (letzter Checkin)' mit den Health-Metriken, nur sichtbar wenn device.health vorhanden ist. Log-Seite zeigt jetzt 'Zuletzt gemeldet' oberhalb der Ereignistabelle - vorher nur in der Geräteliste sichtbar, nicht in der Detail-Historie. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
309 lines
12 KiB
Python
309 lines
12 KiB
Python
from fastapi import APIRouter, Depends, Form, HTTPException, Request
|
|
from fastapi.responses import RedirectResponse
|
|
|
|
from anode_client import anode_request
|
|
from auth import get_current_user
|
|
from routers.gruppen import fetch_gruppen
|
|
from templating import templates
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
# Merkmale, für die die UI eine zusätzliche Auswahl-Option anbietet (siehe
|
|
# ADR-0010 optionen-Mechanismus). Bewusst hier als kleine, explizite Map
|
|
# statt eines generischen Options-Schemas - nur ein Fall existiert aktuell.
|
|
OPTIONEN_FELDER = {
|
|
"browser-brave": [
|
|
{"key": "standardbrowser", "label": "Als Standardbrowser festlegen"},
|
|
],
|
|
}
|
|
|
|
|
|
def find_device_in_organization(device_id: str, organization_id: str) -> dict | None:
|
|
"""
|
|
Besitz-Validierung (siehe ADR-0011): ein device_id-Wert aus der URL wird
|
|
nur akzeptiert, wenn er tatsächlich in der Geräteliste der eigenen
|
|
Organisation auftaucht - verhindert, dass ein eingeloggter Kunde per
|
|
manipulierter URL auf fremde Geräte zugreift.
|
|
"""
|
|
|
|
result = anode_request("GET", f"/api/v1/organizations/{organization_id}/devices")
|
|
devices = result.get("devices", []) if result.get("success") else []
|
|
|
|
for device in devices:
|
|
if device["id"] == device_id:
|
|
return device
|
|
|
|
return None
|
|
|
|
|
|
def gruppiere_nach_kategorie(katalog: list[dict]) -> dict[str, list[dict]]:
|
|
# Bereits nach kategorie.sort_order sortiert (siehe anode-Query) - hier
|
|
# nur noch gruppieren, kein erneutes Sortieren (würde die Reihenfolge
|
|
# durcheinanderbringen).
|
|
kategorien: dict[str, list[dict]] = {}
|
|
for eintrag in katalog:
|
|
kategorie_name = eintrag["kategorie"]["name"] if eintrag["kategorie"] else "Ohne Kategorie"
|
|
kategorien.setdefault(kategorie_name, []).append(eintrag)
|
|
|
|
return kategorien
|
|
|
|
|
|
def anreichere_mit_quelle_url(katalog: list[dict], struktur_praefix: str, gruppen_praefix: str) -> list[dict]:
|
|
"""
|
|
Ergänzt jeden Auftragskatalog-Eintrag um quelle_url - ein Sprungziel zur
|
|
verantwortlichen OE/Gruppe (siehe ADR-0012), passend zum jeweiligen
|
|
Kontext (Kundenportal-Self-Service vs. Admin-Bereich, siehe Aufrufer).
|
|
None für die Fälle "workspace"/"geraet", die kein sinnvolles Linkziel
|
|
haben.
|
|
"""
|
|
|
|
for eintrag in katalog:
|
|
quelle, quelle_id = eintrag.get("quelle"), eintrag.get("quelle_id")
|
|
|
|
if quelle == "oe" and quelle_id:
|
|
eintrag["quelle_url"] = f"{struktur_praefix}/{quelle_id}/merkmale"
|
|
elif quelle == "gruppe" and quelle_id:
|
|
eintrag["quelle_url"] = f"{gruppen_praefix}/{quelle_id}"
|
|
else:
|
|
eintrag["quelle_url"] = None
|
|
|
|
return katalog
|
|
|
|
|
|
@router.get("/")
|
|
def index(user: dict = Depends(get_current_user)):
|
|
return RedirectResponse("/geraete", status_code=303)
|
|
|
|
|
|
@router.get("/geraete")
|
|
def geraete_liste(request: Request, user: dict = Depends(get_current_user)):
|
|
result = anode_request("GET", f"/api/v1/organizations/{user.organization_id}/devices")
|
|
devices = result.get("devices", []) if result.get("success") else []
|
|
|
|
oe_result = anode_request("GET", f"/api/v1/organizations/{user.organization_id}/organisationseinheiten")
|
|
organisationseinheiten = oe_result.get("organisationseinheiten", []) if oe_result.get("success") else []
|
|
|
|
gruppen = fetch_gruppen(user.organization_id)
|
|
|
|
# anode bietet keinen direkten "Gruppen eines Geräts, für alle Geräte
|
|
# auf einmal"-Endpunkt an - daher hier je Gerät einzeln abfragen
|
|
# (gleiches N+1-Muster wie admin_kunden.py:gruppe_detail(), nur in
|
|
# umgekehrter Richtung: dort Geräte einer Gruppe, hier Gruppen eines
|
|
# Geräts). Bei der aktuellen Kundenzahl/Gerätezahl unproblematisch.
|
|
for device in devices:
|
|
dg_result = anode_request("GET", f"/api/v1/devices/{device['id']}/gruppen")
|
|
device_gruppen = dg_result.get("gruppen", []) if dg_result.get("success") else []
|
|
device["gruppen_ids"] = {g["id"] for g in device_gruppen}
|
|
device["gruppen_namen"] = [g["name"] for g in device_gruppen]
|
|
|
|
return templates.TemplateResponse(
|
|
request,
|
|
"geraete_liste.html",
|
|
{
|
|
"user": user,
|
|
"devices": devices,
|
|
"organisationseinheiten": organisationseinheiten,
|
|
"gruppen": gruppen,
|
|
},
|
|
)
|
|
|
|
|
|
@router.post("/geraete/{device_id}/gruppen")
|
|
async def gruppen_aktualisieren(request: Request, device_id: str, user: dict = Depends(get_current_user)):
|
|
"""
|
|
Setzt die Gruppen-Mitgliedschaft eines Geräts direkt aus der
|
|
Geräteliste heraus (Checkbox-Auswahl aller Gruppen der Organisation),
|
|
als Gegenstück zur bestehenden Verwaltung von der Gruppen-Seite aus
|
|
(/gruppen/{id}, siehe ADR-0012-Nachtrag). Berechnet die Differenz
|
|
zwischen aktueller und gewünschter Mitgliedschaft und ruft die
|
|
bestehenden anode-Endpunkte entsprechend auf - kein neuer
|
|
Bulk-Endpunkt nötig.
|
|
"""
|
|
|
|
if find_device_in_organization(device_id, user.organization_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
form = await request.form()
|
|
ausgewaehlt = set(form.getlist("gruppen_ids"))
|
|
|
|
aktuell_result = anode_request("GET", f"/api/v1/devices/{device_id}/gruppen")
|
|
aktuell = (
|
|
{g["id"] for g in aktuell_result.get("gruppen", [])}
|
|
if aktuell_result.get("success") else set()
|
|
)
|
|
|
|
for gruppe_id in ausgewaehlt - aktuell:
|
|
anode_request("POST", f"/api/v1/devices/{device_id}/gruppen/{gruppe_id}")
|
|
|
|
for gruppe_id in aktuell - ausgewaehlt:
|
|
anode_request("DELETE", f"/api/v1/devices/{device_id}/gruppen/{gruppe_id}")
|
|
|
|
return RedirectResponse("/geraete", status_code=303)
|
|
|
|
|
|
@router.get("/geraete/{device_id}")
|
|
def auftragskatalog_ansicht(request: Request, device_id: str, user: dict = Depends(get_current_user)):
|
|
device = find_device_in_organization(device_id, user.organization_id)
|
|
|
|
if device is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
result = anode_request("GET", f"/api/v1/devices/{device_id}/auftragskatalog")
|
|
katalog = result.get("auftragskatalog", []) if result.get("success") else []
|
|
fehler = None if result.get("success") else result.get("message")
|
|
|
|
katalog = anreichere_mit_quelle_url(katalog, "/struktur", "/gruppen")
|
|
|
|
return templates.TemplateResponse(
|
|
request,
|
|
"auftragskatalog.html",
|
|
{
|
|
"user": user,
|
|
"device": device,
|
|
"kategorien": gruppiere_nach_kategorie(katalog),
|
|
"fehler": fehler,
|
|
"optionen_felder": OPTIONEN_FELDER,
|
|
"zurueck_url": "/geraete",
|
|
},
|
|
)
|
|
|
|
|
|
@router.post("/geraete/{device_id}/auftragskatalog/{merkmal_key}/select")
|
|
async def auftrag_select(
|
|
request: Request, device_id: str, merkmal_key: str, user: dict = Depends(get_current_user)
|
|
):
|
|
if find_device_in_organization(device_id, user.organization_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
form = await request.form()
|
|
optionen = {
|
|
feld["key"]: (form.get(f"optionen.{feld['key']}") == "on")
|
|
for feld in OPTIONEN_FELDER.get(merkmal_key, [])
|
|
}
|
|
|
|
anode_request(
|
|
"POST",
|
|
f"/api/v1/devices/{device_id}/auftragskatalog/{merkmal_key}/select",
|
|
json={"optionen": optionen},
|
|
)
|
|
|
|
return RedirectResponse(f"/geraete/{device_id}", status_code=303)
|
|
|
|
|
|
@router.post("/geraete/{device_id}/auftragskatalog/{merkmal_key}/deselect")
|
|
def auftrag_deselect(device_id: str, merkmal_key: str, user: dict = Depends(get_current_user)):
|
|
if find_device_in_organization(device_id, user.organization_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
anode_request(
|
|
"POST", f"/api/v1/devices/{device_id}/auftragskatalog/{merkmal_key}/deselect"
|
|
)
|
|
|
|
return RedirectResponse(f"/geraete/{device_id}", status_code=303)
|
|
|
|
|
|
@router.post("/geraete/{device_id}/debug-modus/aktivieren")
|
|
def debug_modus_aktivieren(device_id: str, user: dict = Depends(get_current_user)):
|
|
if find_device_in_organization(device_id, user.organization_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
anode_request("POST", f"/api/v1/devices/{device_id}/debug-mode")
|
|
|
|
return RedirectResponse(f"/geraete/{device_id}", status_code=303)
|
|
|
|
|
|
@router.post("/geraete/{device_id}/debug-modus/deaktivieren")
|
|
def debug_modus_deaktivieren(device_id: str, user: dict = Depends(get_current_user)):
|
|
if find_device_in_organization(device_id, user.organization_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
anode_request("DELETE", f"/api/v1/devices/{device_id}/debug-mode")
|
|
|
|
return RedirectResponse(f"/geraete/{device_id}", status_code=303)
|
|
|
|
|
|
@router.get("/geraete/{device_id}/hardware")
|
|
def hardware_ansicht(request: Request, device_id: str, user: dict = Depends(get_current_user)):
|
|
device = find_device_in_organization(device_id, user.organization_id)
|
|
|
|
if device is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
result = anode_request("GET", f"/api/v1/devices/{device_id}/hardware")
|
|
hardware = result.get("hardware") if result.get("success") else None
|
|
fehler = None if result.get("success") else result.get("message")
|
|
|
|
return templates.TemplateResponse(
|
|
request,
|
|
"geraete_hardware.html",
|
|
{"user": user, "device": device, "hardware": hardware, "fehler": fehler},
|
|
)
|
|
|
|
|
|
@router.get("/geraete/{device_id}/log")
|
|
def log_ansicht(request: Request, device_id: str, user: dict = Depends(get_current_user)):
|
|
device = find_device_in_organization(device_id, user.organization_id)
|
|
|
|
if device is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
result = anode_request("GET", f"/api/v1/devices/{device_id}/events")
|
|
events = result.get("events", []) if result.get("success") else []
|
|
|
|
return templates.TemplateResponse(
|
|
request, "geraete_log.html", {"user": user, "device": device, "events": events}
|
|
)
|
|
|
|
|
|
@router.post("/geraete/{device_id}/deprovisionieren")
|
|
def deprovisionieren(device_id: str, user: dict = Depends(get_current_user)):
|
|
if find_device_in_organization(device_id, user.organization_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
anode_request("POST", f"/api/v1/devices/{device_id}/deprovision")
|
|
|
|
return RedirectResponse("/geraete", status_code=303)
|
|
|
|
|
|
@router.post("/geraete/{device_id}/reprovisionieren")
|
|
def reprovisionieren(device_id: str, user: dict = Depends(get_current_user)):
|
|
if find_device_in_organization(device_id, user.organization_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
anode_request("POST", f"/api/v1/devices/{device_id}/reprovision")
|
|
|
|
return RedirectResponse("/geraete", status_code=303)
|
|
|
|
|
|
@router.get("/geraete/{device_id}/loeschen")
|
|
def loeschen_bestaetigen(request: Request, device_id: str, user: dict = Depends(get_current_user)):
|
|
device = find_device_in_organization(device_id, user.organization_id)
|
|
|
|
if device is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
return templates.TemplateResponse(
|
|
request, "geraete_loeschen.html", {"user": user, "device": device}
|
|
)
|
|
|
|
|
|
@router.post("/geraete/{device_id}/loeschen")
|
|
def loeschen(device_id: str, user: dict = Depends(get_current_user)):
|
|
if find_device_in_organization(device_id, user.organization_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
anode_request("POST", f"/api/v1/devices/{device_id}/archive")
|
|
|
|
return RedirectResponse("/geraete", status_code=303)
|
|
|
|
|
|
@router.post("/geraete/{device_id}/oe")
|
|
def oe_verschieben(device_id: str, oe_id: str = Form(...), user: dict = Depends(get_current_user)):
|
|
if find_device_in_organization(device_id, user.organization_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
anode_request("POST", f"/api/v1/devices/{device_id}/oe", json={"oe_id": oe_id})
|
|
|
|
return RedirectResponse("/geraete", status_code=303)
|