Files
bonaminandClaude Opus 5.5 650fee7873 feat(backend): manager override with one-time PIN approval for cancel, price change, over-limit discount
Platform v2 step 1.9 (APPS_PLATFORM D-10).
- Perm(..., overridable=True). check() answers access.override_possible (not
  access.denied) for such keys, and consumes an X-Xenia-Override token for them.
- POST /api/access/override {key, username, pin}: the approver must hold the key
  (module effective); returns a token valid 60 s, once, for that key and the
  requesting session only (sha256 stored). Wrong PINs throttled per session.
  GET /api/access/overrides: audit list (core.staff.manage).
- access_overrides table (revision 0008): who asked, session, who approved, device,
  key, created/expires/used, target. The revision also grants core.devices.manage and
  the new pricing.discounts.over_limit to existing owners/store managers.
- Overridable: ordering.orders.cancel (the three cancel endpoints; the order audit log
  notes "override #n approved by user m"), ordering.prices.modify (price adjust), and
  new pricing.discounts.over_limit (waiter discounts beyond their limits; the reason
  goes in params). The venue switch discounts.enabled is never overridable.
- /orders/cancel-permissions adds override_possible; permission catalogues expose
  `overridable`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 09:57:37 +02:00

82 lines
3.0 KiB
Python

"""Route access markers (APPS_PLATFORM §3.1, D-17).
Each marker is a FastAPI dependency with an `__xenia_access__` attribute. The route
guard (tests/guards/test_route_access.py) walks every route's dependency tree and fails
any route without a marker, so "forgot to protect" can't ship.
"""
from __future__ import annotations
from fastapi import Depends, Request
from sqlalchemy.orm import Session
from core.access.check import AccessContext, check
from core.access.context import context_for, current_device
from core.errors import XeniaError
from database import get_db
def _mark(fn, *marker):
fn.__xenia_access__ = marker
return fn
def require(key: str):
"""The user must hold `key`, and its module must be effective. Returns the user."""
# Keys are not validated here (routers are imported while the registry loads);
# tests/guards/test_route_access.py checks every marker's key exists.
from routers.deps import get_current_user
def _dep(request: Request, user=Depends(get_current_user), db: Session = Depends(get_db)):
check(context_for(request, user, db), key, db)
return user
_dep.__name__ = f"require_{key.replace('.', '_')}"
return _mark(_dep, "require", key)
def require_any(*keys: str):
"""The user must hold at least one of `keys` (in an effective module). Returns the user."""
from routers.deps import get_current_user
def _dep(request: Request, user=Depends(get_current_user), db: Session = Depends(get_db)):
last = None
ctx = context_for(request, user, db)
for k in keys:
try:
check(ctx, k, db)
return user
except XeniaError as e:
last = e
raise last
_dep.__name__ = "require_any_" + "_".join(k.replace(".", "_") for k in keys)
return _mark(_dep, "require_any", *keys)
def public():
"""Deliberately open endpoint (health, identity, login, pairing request...)."""
def _dep():
return None
return _mark(_dep, "public")
def require_device(app_key: str | None = None):
"""Device-only endpoint (D-8): the request must carry a valid X-Xenia-Device token.
With `app_key`, the device must run that app and the app's module must be effective
(licensed and enabled). Without it, any paired device (e.g. GET /api/devices/me, which
must answer even when the app is turned off so the screen can say so). Returns the Device."""
def _dep(request: Request, db: Session = Depends(get_db)):
dev = current_device(request, db)
if dev is None:
raise XeniaError("device.not_paired")
if app_key is not None:
if dev.app_key != app_key:
raise XeniaError("device.out_of_scope", app=dev.app_key, key=app_key)
from core.modules.state import blocked_error, is_effective
if not is_effective(app_key, db):
raise blocked_error(app_key, db)
return dev
_dep.__name__ = f"require_device_{app_key or 'any'}"
return _mark(_dep, "require_device", app_key or "*")