Files
xenia-pos-local/local_backend/core/access/check.py
T
bonaminandClaude Opus 5.5 650fee7873 feat(backend): manager override with one-time PIN approval for cancel, price change, over-limit discount
Platform v2 step 1.9 (APPS_PLATFORM D-10).
- Perm(..., overridable=True). check() answers access.override_possible (not
  access.denied) for such keys, and consumes an X-Xenia-Override token for them.
- POST /api/access/override {key, username, pin}: the approver must hold the key
  (module effective); returns a token valid 60 s, once, for that key and the
  requesting session only (sha256 stored). Wrong PINs throttled per session.
  GET /api/access/overrides: audit list (core.staff.manage).
- access_overrides table (revision 0008): who asked, session, who approved, device,
  key, created/expires/used, target. The revision also grants core.devices.manage and
  the new pricing.discounts.over_limit to existing owners/store managers.
- Overridable: ordering.orders.cancel (the three cancel endpoints; the order audit log
  notes "override #n approved by user m"), ordering.prices.modify (price adjust), and
  new pricing.discounts.over_limit (waiter discounts beyond their limits; the reason
  goes in params). The venue switch discounts.enabled is never overridable.
- /orders/cancel-permissions adds override_possible; permission catalogues expose
  `overridable`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 09:57:37 +02:00

66 lines
2.4 KiB
Python

"""The one access decision (APPS_PLATFORM §3.1)."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Optional
from core.errors import XeniaError
from core.modules.registry import all_permissions
@dataclass
class AccessContext:
user: Any = None # User row, or the support ghost (step 1.10); None for device-only calls
session: Any = None # session id (sid) of the user's login
device: Any = None # Device row when the call comes from a paired device (D-8)
override_token: Optional[str] = None # X-Xenia-Override (D-10); consumed by check() when needed
target: Optional[str] = None # what the request acts on, for the override audit row
def module_of(key: str) -> str:
try:
return all_permissions()[key][1]
except KeyError:
raise ValueError(f"unknown permission key {key!r}: declare it in a module manifest") from None
def holds(user, key: str) -> bool:
"""Does the user have the key (ignoring modules)? Superadmin has every key (D-7)."""
if user is None:
return False
if getattr(user, "role", None) == "superadmin":
return True
return key in user_keys(user)
def user_keys(user) -> frozenset[str]:
rows = getattr(user, "permission_rows", None) or []
return frozenset(r.key for r in rows)
def can(user, key: str, db=None) -> bool:
"""Full check without raising: module effective AND user holds the key."""
from core.modules.state import is_effective
return is_effective(module_of(key), db) and holds(user, key)
def check(ctx: AccessContext, key: str, db=None) -> None:
"""Raise the right XeniaError unless `key` is allowed for this request."""
from core.modules.state import blocked_error, is_effective
module = module_of(key)
if not is_effective(module, db):
raise blocked_error(module, db)
if ctx.device is not None:
from core.access.context import in_scope
if not in_scope(ctx.device.app_key, key):
raise XeniaError("device.out_of_scope", app=ctx.device.app_key, key=key)
if holds(ctx.user, key):
return
from core.access.override import consume, is_overridable
if is_overridable(key):
if ctx.override_token:
consume(db, ctx.override_token, key, ctx.session, ctx.target)
return
raise XeniaError("access.override_possible", key=key)
raise XeniaError("access.denied", key=key)