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>
This commit is contained in:
2026-11-13 09:57:37 +02:00
co-authored by Claude Opus 5.5
parent c699542565
commit 650fee7873
23 changed files with 589 additions and 32 deletions
@@ -0,0 +1,61 @@
"""access_overrides (manager override audit + tokens) (D-10, step 1.9)
Hand-edited: existing users whose role template now includes a key added since their
grants were written get it (role templates are only copied at user creation):
core.devices.manage (step 1.8) and pricing.discounts.over_limit (this step), for
owner and store_manager. The role lists are frozen here on purpose.
Revision ID: 0008_access_overrides
Revises: 0007_devices
Create Date: 2026-11-13 09:49:51.024839
"""
from alembic import op
import sqlalchemy as sa
from core.timeutil import UTCDateTime
revision = '0008_access_overrides'
down_revision = '0007_devices'
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('access_overrides',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('key', sa.String(), nullable=False),
sa.Column('token_hash', sa.String(), nullable=False),
sa.Column('requested_by', sa.Integer(), nullable=True),
sa.Column('requested_session', sa.String(), nullable=True),
sa.Column('approved_by', sa.Integer(), nullable=True),
sa.Column('device_id', sa.Integer(), nullable=True),
sa.Column('created_at', UTCDateTime(), nullable=False),
sa.Column('expires_at', UTCDateTime(), nullable=False),
sa.Column('used_at', UTCDateTime(), nullable=True),
sa.Column('target', sa.String(), nullable=True),
sa.ForeignKeyConstraint(['approved_by'], ['users.id'], ondelete='SET NULL'),
sa.ForeignKeyConstraint(['device_id'], ['devices.id'], ondelete='SET NULL'),
sa.ForeignKeyConstraint(['requested_by'], ['users.id'], ondelete='SET NULL'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('token_hash'),
sqlite_autoincrement=True
)
with op.batch_alter_table('access_overrides', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_access_overrides_created_at'), ['created_at'], unique=False)
batch_op.create_index(batch_op.f('ix_access_overrides_key'), ['key'], unique=False)
# ### end Alembic commands ###
conn = op.get_bind()
for key in ("core.devices.manage", "pricing.discounts.over_limit"):
conn.execute(sa.text("insert or ignore into user_permissions (user_id, key) "
"select id, :k from users where role in ('owner', 'store_manager')"), {"k": key})
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('access_overrides', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_access_overrides_key'))
batch_op.drop_index(batch_op.f('ix_access_overrides_created_at'))
op.drop_table('access_overrides')
# ### end Alembic commands ###