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
+14 -13
View File
@@ -1,3 +1,4 @@
from core.access.check import holds
from core.errors import XeniaError
from core.timeutil import parse_api_datetime, venue_strftime, utcnow
import json
@@ -62,13 +63,13 @@ def _waiter_can_cancel(user: User, db: Session) -> bool:
"""True only if both the global setting and the per-waiter flag are on."""
row = db.query(PosSettings).filter(PosSettings.key == "orders.waiter_cancellations_allowed").first()
global_on = row is not None and row.value == "true"
return global_on and bool(user.can_cancel_orders)
return global_on and holds(user, "ordering.orders.cancel")
def _can_access_order(order: Order, user: User, db: Session) -> bool:
"""Zone-based access: any waiter whose zone covers the order's table group may act on it.
Standalone orders (table_id=None) are accessible to all waiters with any zone assignment."""
if user.role == "superadmin" or user.perm_access_dashboard:
if holds(user, "core.dashboard.access"):
return True
zones = db.query(WaiterZone).filter(WaiterZone.waiter_id == user.id).all()
if not zones:
@@ -114,7 +115,7 @@ def shift_order_log(
from models.shift import WaiterShift
from models.business_day import BusinessDay
is_manager = user.role == "superadmin" or user.perm_access_dashboard
is_manager = holds(user, "core.dashboard.access")
if is_manager:
bd = db.query(BusinessDay).filter(BusinessDay.closed_at == None).order_by(BusinessDay.opened_at.desc()).first() # noqa: E711
@@ -203,7 +204,7 @@ def shift_order_log(
@router.get("/cancel-permissions")
def cancel_permissions(db: Session = Depends(get_db), user: User = Depends(get_current_user)):
"""Return whether the current user is allowed to cancel orders."""
is_manager = user.role == "superadmin" or user.perm_access_dashboard
is_manager = holds(user, "core.dashboard.access")
if is_manager:
return {"can_cancel": True, "reason": None}
can = _waiter_can_cancel(user, db)
@@ -213,7 +214,7 @@ def cancel_permissions(db: Session = Depends(get_db), user: User = Depends(get_c
global_on = row is not None and row.value == "true"
if not global_on:
reason = "global_disabled"
elif not user.can_cancel_orders:
elif not holds(user, "ordering.orders.cancel"):
reason = "user_disabled"
return {"can_cancel": can, "reason": reason}
@@ -426,14 +427,14 @@ def open_order(body: OrderCreate, db: Session = Depends(get_db), user: User = De
from models.business_day import BusinessDay
from models.shift import WaiterShift
if not (user.role == "superadmin" or user.perm_access_dashboard or user.perm_open_orders):
if not (holds(user, "core.dashboard.access") or holds(user, "ordering.orders.open")):
raise HTTPException(status_code=403, detail="Δεν έχετε δικαίωμα ανοίγματος παραγγελιών")
active_day = db.query(BusinessDay).filter(BusinessDay.status == "open").first()
if not active_day:
raise HTTPException(status_code=403, detail="Restaurant is not open — manager must open the business day first")
if user.perm_access_waiter_app and not user.perm_access_dashboard and user.role != "superadmin":
if holds(user, "waiter_app.access") and not holds(user, "core.dashboard.access"):
active_shift = db.query(WaiterShift).filter(
WaiterShift.waiter_id == user.id,
WaiterShift.ended_at == None,
@@ -749,7 +750,7 @@ def cancel_item(
if not order:
raise XeniaError("order.not_found", order_id=order_id)
is_manager = user.role == "superadmin" or user.perm_access_dashboard
is_manager = holds(user, "core.dashboard.access")
if not is_manager and not _waiter_can_cancel(user, db):
raise HTTPException(status_code=403, detail="Δεν επιτρέπεται η ακύρωση παραγγελίας")
if not is_manager and not _can_access_order(order, user, db):
@@ -934,7 +935,7 @@ def close_order(order_id: int, db: Session = Depends(get_db), user: User = Depen
order = db.query(Order).filter(Order.id == order_id).first()
if not order:
raise XeniaError("order.not_found", order_id=order_id)
if not (user.role == "superadmin" or user.perm_access_dashboard or user.perm_close_orders):
if not (holds(user, "core.dashboard.access") or holds(user, "ordering.orders.close")):
raise HTTPException(status_code=403, detail="Δεν έχετε δικαίωμα κλεισίματος παραγγελιών")
if not _can_access_order(order, user, db):
raise HTTPException(status_code=403, detail="Access denied")
@@ -1080,7 +1081,7 @@ def cancel_order(
if not order:
raise XeniaError("order.not_found", order_id=order_id)
is_manager = user.role == "superadmin" or user.perm_access_dashboard
is_manager = holds(user, "core.dashboard.access")
if not is_manager and not _waiter_can_cancel(user, db):
raise HTTPException(status_code=403, detail="Δεν επιτρέπεται η ακύρωση παραγγελίας")
if not is_manager and not _can_access_order(order, user, db):
@@ -1132,7 +1133,7 @@ def cancel_items_bulk(
if not order:
raise XeniaError("order.not_found", order_id=order_id)
is_manager = user.role == "superadmin" or user.perm_access_dashboard
is_manager = holds(user, "core.dashboard.access")
if not is_manager and not _waiter_can_cancel(user, db):
raise HTTPException(status_code=403, detail="Δεν επιτρέπεται η ακύρωση παραγγελίας")
if not is_manager and not _can_access_order(order, user, db):
@@ -1471,7 +1472,7 @@ def adjust_item_price(
user: User = Depends(get_current_user),
):
"""Apply an on-the-fly price adjustment to an active order item."""
if not (user.role == "superadmin" or user.perm_access_dashboard or user.perm_modify_prices):
if not (holds(user, "core.dashboard.access") or holds(user, "ordering.prices.modify")):
raise HTTPException(status_code=403, detail="Δεν έχετε δικαίωμα τροποποίησης τιμών")
setting = db.query(PosSettings).filter(PosSettings.key == "orders.waiter_price_adjust_allowed").first()
if not setting or setting.value != "true":
@@ -1517,7 +1518,7 @@ def apply_waiter_discount(
order = db.query(Order).filter(Order.id == order_id).first()
if not order:
raise XeniaError("order.not_found", order_id=order_id)
if not (user.role == "superadmin" or user.perm_access_dashboard or user.perm_apply_discounts):
if not (holds(user, "core.dashboard.access") or holds(user, "pricing.discounts.apply")):
raise HTTPException(status_code=403, detail="Δεν έχετε δικαίωμα εφαρμογής εκπτώσεων")
if not _can_access_order(order, user, db):
raise HTTPException(status_code=403, detail="Access denied")