Files
xenia-pos-local/local_backend/core/access/deps.py
T
bonaminandClaude Opus 5.5 b7fda58af9 feat(backend): permission keys replace the 13 perm_* columns
Platform v2 step 1.5 (APPS_PLATFORM D-7, D-17, §3, §6.1).
- user_permissions(user_id, key); revision 0005 copies existing v1 flags into
  keys (frozen mapping) and drops the perm_* columns; can_cancel_orders gone
- core/access: check() = module effective + key held (superadmin implicit,
  override keys wired for 1.9); holds()/can(); markers require/require_any/
  public/require_device; role templates from manifests; queries.holders_filter
  replaces User.perm_x == True; legacy.py projects/accepts the v1 booleans
  for the current manager/waiter UIs only (§3.3, temporary)
- routers.deps: require_manager/... are now aliases of require("<key>");
  history endpoints use history.view; superadmin-only = core.system.admin;
  get_current_user carries the "signed_in" marker
- every inline perm_* read in routers/services converted to holds()
- staff API: permission catalogue by module with role templates, get/set a
  user's keys, reset to role template; legacy flag toggles still work; a
  non-superadmin can only grant keys they hold (access.cannot_grant)
- setup wizard creates the first account as owner (v1: store_manager, who
  couldn't change settings); demo/seed manager is owner
- roles.py keeps only the role list and labels
- guards: every route has an access marker (19 open routes marked public()
  on purpose; /waiters/roles now needs sign-in), no perm_* attribute reads
- tests: check matrix, templates, projection, staff permission API, grant
  guard, 0005 data copy; pack rule CS-14

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:51:06 +02:00

66 lines
2.1 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
from sqlalchemy.orm import Session
from core.access.check import AccessContext, check
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(user=Depends(get_current_user), db: Session = Depends(get_db)):
check(AccessContext(user=user), 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(user=Depends(get_current_user), db: Session = Depends(get_db)):
last = None
for k in keys:
try:
check(AccessContext(user=user), 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):
"""Device-only endpoint for paired devices running `app_key` (implemented in step 1.8)."""
def _dep():
raise XeniaError("device.not_paired")
return _mark(_dep, "require_device", app_key)