feat(backend,waiter,terminal,manager): sign-in: ask before signing a waiter's other phone out, usernames on paired screens

- auth.single_session_waiter gains 'ask', the new default (Alembic 0051 moves venues on 'off'
  to it): a second phone gets auth.session_elsewhere {device, since}; Continue retries with
  replace=true and the first phone is signed out at once (live session.ended, reason
  "replaced") and says why on its sign-in page. Only phones count (terminal sessions are
  their own client). refuse_new / end_old unchanged.
- auth.session_ended now carries the reason; the waiter app keeps it until the next sign-in.
- The waiter app sends queued offline ops on every (re)connection, so an order queued on a
  phone that was signed out by another goes out after the next sign-in there (e2e checked).
- auth.terminal_require_username (venue) + Device.require_username (per device, wins):
  GET /api/devices/me/users lists nobody, the screen asks for a username then PIN/password,
  a picked user_id is refused (auth.username_required), "tap" falls back to PIN.
- Manager: Sign-in settings (ask, the new switch), the device dialog's usernames choice.

Plan platform-v2 6F.7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 12:51:37 +02:00
co-authored by Claude Opus 5.5
parent 3eeafb565a
commit 7706bf2470
31 changed files with 494 additions and 41 deletions
+12 -2
View File
@@ -151,6 +151,8 @@ def effective_login_mode(db, device) -> str:
can't use that one, the app's first mode (signage etc.: 'none')."""
modes = _app(device.app_key).device_app.login_modes
mode = device.login_mode or sessions._setting(db, "auth.default_login_mode", "pin")
if mode == "tap" and sessions.terminal_requires_username(db, device):
mode = next((m for m in ("pin", "password") if m in modes), mode) # usernames: no name to tap (6F.7)
return mode if mode in modes else modes[0]
@@ -175,6 +177,8 @@ def _out(d: Device, db) -> dict:
"id": d.id, "uuid": d.uuid, "name": d.name, "app_key": d.app_key,
"config": _effective_config(d, db) if d.app_key in registry() else {},
"login_mode": d.login_mode,
"require_username": d.require_username,
"effective_require_username": sessions.terminal_requires_username(db, d),
"effective_login_mode": effective_login_mode(db, d) if d.app_key in registry() else None,
"linked_device_id": d.linked_device_id, "user_agent": d.user_agent,
"paired_at": iso_z(d.paired_at), "paired_by": d.paired_by,
@@ -191,6 +195,7 @@ def _self_out(d: Device, db) -> dict:
return {
"id": d.id, "uuid": d.uuid, "name": d.name, "app_key": d.app_key, "config": _effective_config(d, db),
"login_mode": effective_login_mode(db, d),
"require_username": sessions.terminal_requires_username(db, d),
"linked_device": ({"id": linked.id, "uuid": linked.uuid, "name": linked.name, "app_key": linked.app_key}
if linked is not None and linked.revoked_at is None else None),
"app_status": status, # "ok" | "module.disabled" | "module.not_licensed": show "app disabled"
@@ -249,6 +254,7 @@ class PairApprove(BaseModel):
name: str = Field(min_length=1, max_length=60)
config: Optional[dict] = None
login_mode: Optional[str] = None # None/"" = site default
require_username: Optional[bool] = None # None = site default (6F.7)
linked_device_id: Optional[int] = None
@@ -256,6 +262,7 @@ class DevicePatch(BaseModel):
name: Optional[str] = Field(None, min_length=1, max_length=60)
config: Optional[dict] = None
login_mode: Optional[str] = None # "" or null = back to the site default
require_username: Optional[bool] = None # null = back to the site default (6F.7)
linked_device_id: Optional[int] = None # null/0 = unlink
@@ -285,6 +292,7 @@ def approve_device(body: PairApprove, db: Session = Depends(get_db), user=Depend
d = Device(uuid=uuidlib.uuid4().hex, name=body.name.strip(), app_key=body.app_key,
config=_config(body.app_key, body.config, db),
login_mode=_check_mode(body.app_key, body.login_mode),
require_username=body.require_username,
linked_device_id=_check_link(db, None, body.linked_device_id),
user_agent=row.user_agent, paired_at=now, paired_by=getattr(user, "id", None) or None)
db.add(d)
@@ -307,8 +315,8 @@ def device_users(db: Session = Depends(get_db), device=Depends(require_device())
"""Who can log in on this device (holders of '<app>.access'), for the name picker."""
from core.access.queries import users_with_key
from models.user import User
if effective_login_mode(db, device) == "none":
return []
if effective_login_mode(db, device) == "none" or sessions.terminal_requires_username(db, device):
return [] # usernames on (6F.7): nobody is listed, the screen asks for a username
users = users_with_key(db, f"{device.app_key}.access").order_by(User.full_name).all()
return [{"id": u.id, "full_name": u.full_name, "nickname": u.nickname, "avatar_url": u.avatar_url}
for u in users if u.role != "superadmin"]
@@ -357,6 +365,8 @@ def update_device(device_id: int, body: DevicePatch, db: Session = Depends(get_d
d.config = _config(d.app_key, body.config, db)
if "login_mode" in sent:
d.login_mode = _check_mode(d.app_key, body.login_mode)
if "require_username" in sent:
d.require_username = body.require_username
if "linked_device_id" in sent:
d.linked_device_id = _check_link(db, d.id, body.linked_device_id)
db.commit()