feat(backend): link cards to users; charger-qualified gun restrictions; config-driven gun labels

- Allowlist entries take "user": <username> — sessions started with that
  card attribute to the person, and the owner sees their card's history
  even without gun-wide permissions (ownership looked up live)
- "guns": ["eu092025:2"] (fnmatch) replaces the charger-blind
  "connectors": [2] restriction — safe with multiple chargers
- New "chargers" config section provides per-charger/per-gun display
  names; the panel no longer hardcodes the Anfu gun labels
- Panel admin tab: owner dropdown + guns field on card entries
- Smoke test: 42 checks, incl. retroactive history visibility after
  linking a card and rejection of links to unknown users
This commit is contained in:
2026-07-11 20:03:36 +02:00
parent 3326c8d16c
commit 70c2fe1210
8 changed files with 85 additions and 39 deletions
+15 -7
View File
@@ -119,6 +119,7 @@ async def api_status(user: dict = Depends(current_user)):
"active_transactions": csms.ACTIVE_TX,
"recent_auth_events": list(csms.AUTH_EVENTS),
"relays": _visible_relays(user),
"chargers": CONFIG["chargers"], # display labels for the panel
}
@@ -197,11 +198,15 @@ 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, sessions on guns you control, or everything for admin."""
return (
s["id_tag"] == user.get("id_tag")
or users.allowed(user["scopes"], f"charger:{s['cp']}:{s['connector']}")
)
"""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']}"):
return True
if s["id_tag"] == user.get("id_tag"):
return True
entry = CONFIG["authorized"].get(s["id_tag"])
return bool(entry and entry.get("user") == user["name"])
@router.get("/sessions")
@@ -225,8 +230,9 @@ async def session_samples(tx: int, user: dict = Depends(current_user)):
# ---- allowlist management (admin) ----
class AllowlistEntry(BaseModel):
name: str
kind: str = "card" # card | app | car
connectors: Optional[list[int]] = None # e.g. [2] = this card only works on gun 2
kind: str = "card" # card | app | car
user: Optional[str] = None # owning username -> sessions attribute to them
guns: Optional[list[str]] = None # e.g. ["eu092025:2"]; fnmatch, empty = all guns
@router.get("/allowlist")
@@ -243,6 +249,8 @@ async def allowlist_get(user: dict = Depends(current_user)):
async def allowlist_put(id_tag: str, entry: AllowlistEntry,
user: dict = Depends(current_user)):
need(user, "admin")
if entry.user is not None and entry.user not in CONFIG["users"]:
raise HTTPException(400, f"cannot link card to unknown user {entry.user!r}")
CONFIG["authorized"][id_tag] = entry.model_dump(exclude_none=True)
save()
log.info("allowlist: added/updated %r (%s)", id_tag, entry.name)