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:
2026-11-12 18:51:06 +02:00
co-authored by Claude Opus 5.5
parent 0d43e6a111
commit b7fda58af9
40 changed files with 842 additions and 404 deletions
+48
View File
@@ -0,0 +1,48 @@
"""TEMPORARY v1 projection (APPS_PLATFORM §3.3). Delete with the last frontend that reads it.
The current manager dashboard and waiter app still read and write 13 boolean fields
(perm_access_dashboard ...). This file is the ONLY place that knows them:
- project(user): booleans computed from the user's keys, for UserOut
- apply_booleans(user, {field: bool}): a frontend toggled a boolean -> grant/revoke keys
Not for deployed v1 sites (D-21): it only keeps our own unconverted screens usable on
the branch. Plan: removed when the manager (phase 2/6) and waiter app (phase 4) use keys.
"""
from __future__ import annotations
from core.access.check import holds, user_keys
# boolean -> keys it stands for. Projection: True if the user holds the FIRST key.
LEGACY_MAP: dict[str, tuple[str, ...]] = {
"perm_access_dashboard": ("core.dashboard.access",),
"perm_access_waiter_app": ("waiter_app.access",),
"perm_access_kds": ("kds.access", "kds.bump"),
"perm_cancel_orders": ("ordering.orders.cancel",),
"perm_apply_discounts": ("pricing.discounts.apply",),
"perm_modify_prices": ("ordering.prices.modify",),
"perm_open_orders": ("ordering.orders.open",),
"perm_close_orders": ("ordering.orders.close",),
"perm_view_reports": ("reports.view", "history.view"),
"perm_manage_staff": ("core.staff.manage",),
"perm_manage_tables": ("catalog.tables.manage",),
"perm_manage_menu": ("catalog.menu.manage",),
"perm_manage_settings": ("core.settings.manage",),
}
LEGACY_FIELDS = tuple(LEGACY_MAP)
def project(user) -> dict[str, bool]:
return {field: holds(user, keys[0]) for field, keys in LEGACY_MAP.items()}
def apply_booleans(user, values: dict) -> None:
"""Grant/revoke the keys behind each boolean present in `values` (None = untouched)."""
from core.access.templates import set_user_keys
keys = set(user_keys(user))
for field, val in values.items():
if field not in LEGACY_MAP or val is None:
continue
if val:
keys |= set(LEGACY_MAP[field])
else:
keys -= set(LEGACY_MAP[field])
set_user_keys(user, keys)