From 069d544bb5421de62bfdadb8f3b271408b44caad Mon Sep 17 00:00:00 2001 From: Julian Cuni Date: Sun, 12 Jul 2026 09:04:06 +0200 Subject: [PATCH] feat: private session history, own-permissions view, plug-gated start button - non-admins now see ONLY their own sessions in /api/sessions (own app starts + own linked cards); gun scopes grant control, not visibility; admins see everything - Account page shows the logged-in user's permissions in plain words (Administrator badge, or the guns/relays they control), built solely from data already visible to them - Start button arms only when the gun reports Preparing (cable in the car); idle unplugged gun shows an explanatory hint instead - keep owner's app rename to 'Art IoT' - smoke test: 59 checks (gun-scoped user sees no foreign sessions) --- backend/routes.py | 10 +++-- frontend/index.html | 2 +- frontend/public/manifest.json | 2 +- frontend/src/components/Layout.tsx | 2 +- frontend/src/pages/Account.tsx | 64 +++++++++++++++++++++++++++++- frontend/src/pages/Dashboard.tsx | 7 +++- frontend/src/pages/History.tsx | 11 +++-- frontend/src/pages/Login.tsx | 2 +- frontend/src/styles.css | 5 ++- wiki/entities/backend.md | 2 +- wiki/entities/frontend.md | 4 +- wiki/log.md | 4 ++ 12 files changed, 98 insertions(+), 17 deletions(-) diff --git a/backend/routes.py b/backend/routes.py index 44fa256..3cec6cf 100644 --- a/backend/routes.py +++ b/backend/routes.py @@ -221,10 +221,12 @@ async def charger_probe(cp: Optional[str] = None, user: dict = Depends(current_u # ---- session history: who charged when and how many kWh ---- def _may_see_session(user: dict, s: dict) -> bool: - """Own sessions (own app starts or own cards), sessions on guns you - control, or everything for admin. Card ownership is looked up live, so - linking a card to a user retroactively shows them its history.""" - if users.allowed(user["scopes"], f"charger:{s['cp']}:{s['connector']}"): + """Transactions are private: admins see everything, everyone else sees + ONLY their own sessions (their app starts + their linked cards). Card + ownership is looked up live, so linking a card to a user retroactively + shows them its history. (Gun scopes grant CONTROL, not visibility — + owner decision 2026-07-12.)""" + if users.allowed(user["scopes"], "admin"): return True if s["id_tag"] == user.get("id_tag"): return True diff --git a/frontend/index.html b/frontend/index.html index 43d23ca..ddf21f9 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -6,7 +6,7 @@ - Charge Control + Art IoT
diff --git a/frontend/public/manifest.json b/frontend/public/manifest.json index 9d5e3e9..55a1d3d 100644 --- a/frontend/public/manifest.json +++ b/frontend/public/manifest.json @@ -1,5 +1,5 @@ { - "name": "Charge Control", + "name": "Art IoT", "short_name": "Charge", "description": "Home EV charger and relay control", "start_url": "/", diff --git a/frontend/src/components/Layout.tsx b/frontend/src/components/Layout.tsx index 69c0fb8..b2e5eee 100644 --- a/frontend/src/components/Layout.tsx +++ b/frontend/src/components/Layout.tsx @@ -32,7 +32,7 @@ export default function Layout() {
- Charge Control + Art IoT private CSMS
diff --git a/frontend/src/pages/Account.tsx b/frontend/src/pages/Account.tsx index ce74d92..e5faf09 100644 --- a/frontend/src/pages/Account.tsx +++ b/frontend/src/pages/Account.tsx @@ -1,7 +1,66 @@ import { FormEvent, useState } from "react"; -import { api } from "../api"; +import { api, isAdmin, scopeAllowed } from "../api"; +import { useStatus } from "../queries"; import { useApp } from "../store"; +/** The logged-in user's own permissions, in plain words. Built purely from + * data this user may already see (status carries charger gun labels for + * everyone, and only the relays they control). */ +function MyPermissions() { + const me = useApp((s) => s.me)!; + const { data: status } = useStatus(); + + if (isAdmin(me.scopes)) { + return ( +
+
+

Your permissions

+
+ Administrator +

+ Full access: both guns, all relays, cards, users and history of everyone. +

+
+ ); + } + + const guns: string[] = []; + for (const [cp, meta] of Object.entries(status?.chargers ?? {})) + for (const [conn, label] of Object.entries(meta.guns ?? {})) + if (scopeAllowed(me.scopes, `charger:${cp}:${conn}`)) + guns.push(`${meta.label ?? cp} — ${label}`); + const relays = Object.values(status?.relays ?? {}).map((r) => r.label); + + return ( +
+
+

Your permissions

+
+ {guns.length + relays.length === 0 ? ( +

+ No device permissions yet — ask an admin to grant you a gun or a relay. +

+ ) : ( +
    + {guns.map((g) => ( +
  • + Gun {g} +
  • + ))} + {relays.map((r) => ( +
  • + Relay {r} +
  • + ))} +
+ )} +

+ History shows only your own charging sessions. +

+
+ ); +} + export default function Account() { const me = useApp((s) => s.me)!; const toast = useApp((s) => s.toast); @@ -31,6 +90,8 @@ export default function Account() { } return ( + <> +

Account

@@ -75,5 +136,6 @@ export default function Account() {
+ ); } diff --git a/frontend/src/pages/Dashboard.tsx b/frontend/src/pages/Dashboard.tsx index e0d1ab4..214135b 100644 --- a/frontend/src/pages/Dashboard.tsx +++ b/frontend/src/pages/Dashboard.tsx @@ -93,7 +93,9 @@ function gunState(gun: Gun): { label: string; cls: string } { function GunBay({ gun, scopes }: { gun: Gun; scopes: string[] }) { const toast = useApp((s) => s.toast); const may = scopeAllowed(scopes, `charger:${gun.cp}:${gun.connector}`); - const ready = !gun.tx && (gun.ocppStatus === "Preparing" || gun.ocppStatus === "Available"); + // Start arms ONLY with a cable in the car (Preparing / pulsing blue) — + // an idle, unplugged gun keeps the button disabled (owner, 2026-07-12) + const ready = !gun.tx && gun.ocppStatus === "Preparing"; const start = useAction( () => @@ -160,6 +162,9 @@ function GunBay({ gun, scopes }: { gun: Gun; scopes: string[] }) { )} {!may && no permission for this gun} + {may && !gun.tx && !ready && gun.ocppStatus === "Available" && ( + plug the cable into the car to enable + )} ); diff --git a/frontend/src/pages/History.tsx b/frontend/src/pages/History.tsx index 499e3dc..3d4b1a8 100644 --- a/frontend/src/pages/History.tsx +++ b/frontend/src/pages/History.tsx @@ -1,4 +1,6 @@ +import { isAdmin } from "../api"; import { useSessions, useStatus } from "../queries"; +import { useApp } from "../store"; import type { Session } from "../types"; const fmtKwh = (wh: number | null) => (wh == null ? "—" : (wh / 1000).toFixed(2)); @@ -23,6 +25,8 @@ function gunLabel(s: Session, chargers: Record s.me)!; + const admin = isAdmin(me.scopes); const { data: sessions, isLoading } = useSessions(); const { data: status } = useStatus(); @@ -30,8 +34,9 @@ export default function History() { if (!sessions?.length) return (

- No charging sessions yet. The first card tap or in-app start will appear here with who, - when, how, and how many kWh. + {admin + ? "No charging sessions yet. The first card tap or in-app start will appear here with who, when, how, and how many kWh." + : "No charging sessions of yours yet. Sessions you start — by card or in-app — appear here."}

); @@ -40,7 +45,7 @@ export default function History() { return (
-

Charging sessions

+

{admin ? "All charging sessions" : "Your charging sessions"}

{sessions.length} session{sessions.length === 1 ? "" : "s"} ·{" "} {totalKwh.toFixed(1)} kWh total diff --git a/frontend/src/pages/Login.tsx b/frontend/src/pages/Login.tsx index 6cd1f5d..d4d2252 100644 --- a/frontend/src/pages/Login.tsx +++ b/frontend/src/pages/Login.tsx @@ -31,7 +31,7 @@ export default function Login() { -

Charge Control

+

Art IoT

Private charging backend — sign in to operate.