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>
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
"""Permission guard (D-7, plan step 1.5): nothing reads v1 perm_* attributes any more.
|
||||
|
||||
Permissions are keys checked with core.access (holds/can/require). The only file that
|
||||
knows the 13 v1 booleans is core/access/legacy.py (temporary projection for the current
|
||||
frontends). The UserOut schema declares them as output fields, which isn't a read.
|
||||
"""
|
||||
import ast
|
||||
from pathlib import Path
|
||||
|
||||
BACKEND = Path(__file__).resolve().parents[2]
|
||||
ALLOWED = {"core/access/legacy.py"}
|
||||
|
||||
|
||||
def test_no_perm_attribute_reads():
|
||||
bad = []
|
||||
for f in sorted(BACKEND.rglob("*.py")):
|
||||
rel = f.relative_to(BACKEND).as_posix()
|
||||
if rel.startswith(("tests/", "migrations/")) or "__pycache__" in rel or rel in ALLOWED:
|
||||
continue
|
||||
for node in ast.walk(ast.parse(f.read_text(encoding="utf-8"))):
|
||||
if isinstance(node, ast.Attribute) and (node.attr.startswith("perm_") or node.attr == "can_cancel_orders"):
|
||||
bad.append(f"{rel}:{node.lineno}: .{node.attr}")
|
||||
assert bad == [], "use core.access.check.holds()/can() instead:\n" + "\n".join(bad)
|
||||
@@ -1,8 +1,51 @@
|
||||
"""Access guard (D-7, D-17): every route declares require()/require_device()/public(). Filled in by plan step 1.5."""
|
||||
import pytest
|
||||
"""Access guard (D-7, D-17): every route declares who may call it.
|
||||
|
||||
pytestmark = pytest.mark.skip(reason="placeholder: implemented in platform-v2 step 1.5")
|
||||
Each API route must have, somewhere in its dependency tree, one of the markers from
|
||||
core/access/deps.py (require / require_any / require_device / public) or the "signed_in"
|
||||
marker on routers.deps.get_current_user. A route with none fails this test: either
|
||||
protect it, or mark it public() on purpose.
|
||||
|
||||
Also checks every key used by a marker exists in a manifest (keys aren't validated at
|
||||
import time because the registry imports the routers).
|
||||
"""
|
||||
from fastapi.routing import APIRoute, APIWebSocketRoute
|
||||
|
||||
ALLOWLIST = {"/openapi.json", "/docs", "/docs/oauth2-redirect", "/redoc"}
|
||||
|
||||
|
||||
def _markers(dependant, seen=None):
|
||||
seen = set() if seen is None else seen
|
||||
out = []
|
||||
for d in dependant.dependencies:
|
||||
call = d.call
|
||||
marker = getattr(call, "__xenia_access__", None)
|
||||
if marker:
|
||||
out.append(marker)
|
||||
if id(d) not in seen:
|
||||
seen.add(id(d))
|
||||
out += _markers(d, seen)
|
||||
return out
|
||||
|
||||
|
||||
def _routes():
|
||||
import main
|
||||
for r in main.app.routes:
|
||||
if isinstance(r, (APIRoute, APIWebSocketRoute)) and r.path not in ALLOWLIST:
|
||||
yield r
|
||||
|
||||
|
||||
def test_every_route_declares_access():
|
||||
raise NotImplementedError
|
||||
missing = sorted(f"{','.join(sorted(getattr(r, 'methods', None) or {'WS'}))} {r.path}"
|
||||
for r in _routes() if not _markers(r.dependant))
|
||||
assert missing == [], "routes without an access marker (require/require_any/public/signed_in):\n" + "\n".join(missing)
|
||||
|
||||
|
||||
def test_every_marker_key_exists():
|
||||
from core.modules.registry import all_permissions
|
||||
known = set(all_permissions())
|
||||
bad = set()
|
||||
for r in _routes():
|
||||
for kind, *keys in _markers(r.dependant):
|
||||
if kind in ("require", "require_any"):
|
||||
bad |= set(keys) - known
|
||||
assert bad == set(), f"unknown permission keys in route markers: {sorted(bad)}"
|
||||
|
||||
Reference in New Issue
Block a user