feat(waiter): what a waiter may do comes from permission keys (platform-v2 step 4.3)

- Payments: ordering.orders.close checked on pay/close and the counter POS
  (waiters have it by default, owner decision); pay-offline always recorded.
- The venue switches waiter_cancellations / price_adjust / revert are gone:
  ordering.orders.cancel, ordering.prices.modify and the new
  ordering.payments.revert (overridable) decide; moves/split/transfer/merge
  need ordering.orders.open; discounts pricing.discounts.apply; chat chat.use.
- Venue role defaults: role_permissions (Alembic 0024), role_template(role, db),
  GET/PUT /api/waiters/roles/{role}/permissions applying the change to everyone
  with the role; manager Settings -> Operation -> "What each role may do".
  0024 also grants the new key to existing owners and store managers.
- Bootstrap `approvable`; @xenia/access useAllowed(key) -> yes/approve/no.
  The waiter shows each action by key, "with a manager's approval (PIN)" where
  allowed; GET /api/orders/cancel-permissions removed.
- Tests: test_waiter_keys.py (11), access allowed(), waiter.mjs +5, manager.mjs +3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 00:54:08 +02:00
co-authored by Claude Opus 5.5
parent a29a167977
commit 8abe4d92d3
32 changed files with 680 additions and 160 deletions
+54 -5
View File
@@ -1,20 +1,69 @@
"""Role templates (D-7): the default keys of each role, read from the module manifests.
"""Role templates (D-7): the default keys of each role.
A role is a label plus a template. The template is copied into user_permissions when a
user is created and on "reset to role defaults"; after that the user's keys are theirs.
The template starts from the module manifests (Perm(..., roles=...)) and the venue can change
it (plan step 4.3, models/role_permission.py): role_template(role, db) includes those changes,
and set_role_keys() saves a role's new keys and applies the difference to everyone with
that role (a key given to the role is given to each of them; a key taken away is taken
from each of them; their other keys stay as they are).
"""
from __future__ import annotations
from core.modules.registry import all_permissions
def role_template(role: str) -> set[str]:
"""Keys a new user with this role gets. Superadmin: none stored (implicit-all)."""
def manifest_template(role: str) -> set[str]:
"""The role's keys as the manifests declare them. Superadmin: none (implicit-all)."""
if role == "superadmin":
return set()
return {k for k, (p, _m) in all_permissions().items() if role in p.roles}
def role_template(role: str, db=None) -> set[str]:
"""Keys a new user with this role gets: the manifests' plus the venue's changes (with db)."""
keys = manifest_template(role)
if db is None or role == "superadmin":
return keys
from models.role_permission import RolePermission
catalogue = all_permissions()
for row in db.query(RolePermission).filter(RolePermission.role == role).all():
if row.key not in catalogue:
continue # a key whose module is gone
if row.granted:
keys.add(row.key)
else:
keys.discard(row.key)
return keys
def set_role_keys(db, role: str, keys) -> tuple[list[str], list[str]]:
"""Make `keys` the role's template and apply the change to everyone with the role.
Returns (added, removed) compared with the template before. No commit."""
from core.errors import XeniaError
from models.role_permission import RolePermission
from models.user import User
catalogue = all_permissions()
wanted = set(keys)
unknown = wanted - set(catalogue)
if unknown:
raise XeniaError("access.unknown_key", key=sorted(unknown)[0])
before = role_template(role, db)
base = manifest_template(role)
db.query(RolePermission).filter(RolePermission.role == role).delete()
for k in sorted(wanted - base):
db.add(RolePermission(role=role, key=k, granted=True))
for k in sorted(base - wanted):
db.add(RolePermission(role=role, key=k, granted=False))
added, removed = sorted(wanted - before), sorted(before - wanted)
if added or removed:
for user in db.query(User).filter(User.role == role).all():
mine = {r.key for r in user.permission_rows}
set_user_keys(user, (mine | set(added)) - set(removed))
return added, removed
def set_user_keys(user, keys) -> None:
"""Replace a user's grants with `keys` (validated against the catalogue)."""
from models.user_permission import UserPermission
@@ -28,5 +77,5 @@ def set_user_keys(user, keys) -> None:
[UserPermission(key=k) for k in sorted(wanted - {r.key for r in user.permission_rows})]
def apply_role_template(user) -> None:
set_user_keys(user, role_template(user.role))
def apply_role_template(user, db=None) -> None:
set_user_keys(user, role_template(user.role, db))