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>
86 lines
3.8 KiB
Python
86 lines
3.8 KiB
Python
from sqlalchemy import Column, Integer, String, Boolean, Float, ForeignKey, Text
|
|
from core.timeutil import UTCDateTime, utcnow
|
|
from sqlalchemy.orm import relationship
|
|
from database import Base
|
|
|
|
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
username = Column(String, unique=True, nullable=False, index=True)
|
|
pin_hash = Column(String, nullable=False)
|
|
password_hash = Column(String, nullable=True)
|
|
email = Column(String, nullable=True)
|
|
# Role is a label + default-permission template. Valid values: see roles.py
|
|
role = Column(String, nullable=False)
|
|
is_active = Column(Boolean, default=True, nullable=False)
|
|
full_name = Column(String, nullable=True)
|
|
nickname = Column(String, nullable=True)
|
|
# UI + personal print language (D-12). NULL = inherit: device -> venue.language -> en.
|
|
language = Column(String, nullable=True)
|
|
mobile_phone = Column(String, nullable=True)
|
|
note = Column(String, nullable=True)
|
|
avatar_url = Column(String, nullable=True)
|
|
created_at = Column(UTCDateTime, default=utcnow)
|
|
# Phase 2B — payroll
|
|
hourly_rate = Column(Float, nullable=True)
|
|
|
|
# Permissions are keys in user_permissions (D-7); v1's 13 perm_* columns are gone.
|
|
# Read them with core.access.check.holds()/can(), never directly.
|
|
permission_rows = relationship("UserPermission", cascade="all, delete-orphan", lazy="selectin")
|
|
|
|
# Per-waiter app settings + favorites (JSON blob, synced from PWA)
|
|
waiter_settings = Column(Text, nullable=True)
|
|
|
|
orders_opened = relationship("Order", foreign_keys="Order.opened_by", back_populates="opener")
|
|
orders_closed = relationship("Order", foreign_keys="Order.closed_by", back_populates="closer")
|
|
order_items = relationship("OrderItem", foreign_keys="OrderItem.added_by", back_populates="added_by_user")
|
|
items_paid = relationship("OrderItem", foreign_keys="OrderItem.paid_by", back_populates="paid_by_user")
|
|
order_assignments = relationship("OrderWaiter", back_populates="waiter")
|
|
zone_assignments = relationship("WaiterZone", back_populates="waiter", cascade="all, delete-orphan")
|
|
|
|
primary_assignments = relationship(
|
|
"AssistantAssignment",
|
|
foreign_keys="AssistantAssignment.primary_waiter_id",
|
|
back_populates="primary_waiter",
|
|
)
|
|
assistant_assignments = relationship(
|
|
"AssistantAssignment",
|
|
foreign_keys="AssistantAssignment.assistant_waiter_id",
|
|
back_populates="assistant_waiter",
|
|
)
|
|
|
|
@property
|
|
def is_superadmin(self):
|
|
return self.role == "superadmin"
|
|
|
|
|
|
class WaiterZone(Base):
|
|
"""Maps a waiter to a table group they are allowed to operate in.
|
|
If a waiter has NO rows here, they see NOTHING.
|
|
A sentinel row with group_id=NULL means 'all zones'."""
|
|
__tablename__ = "waiter_zones"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
waiter_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
group_id = Column(Integer, ForeignKey("table_groups.id"), nullable=True) # NULL = all zones
|
|
assigned_at = Column(UTCDateTime, default=utcnow)
|
|
|
|
waiter = relationship("User", back_populates="zone_assignments")
|
|
group = relationship("TableGroup", back_populates="waiter_zones")
|
|
|
|
|
|
class AssistantAssignment(Base):
|
|
__tablename__ = "assistant_assignments"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
primary_waiter_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
assistant_waiter_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
assigned_at = Column(UTCDateTime, default=utcnow)
|
|
|
|
primary_waiter = relationship("User", foreign_keys=[primary_waiter_id], back_populates="primary_assignments")
|
|
assistant_waiter = relationship("User", foreign_keys=[assistant_waiter_id], back_populates="assistant_assignments")
|