Files
bonaminandClaude Opus 5.5 b7fda58af9 feat(backend): permission keys replace the 13 perm_* columns
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>
2026-11-12 18:51:06 +02:00

89 lines
2.3 KiB
Python

"""
Chat service — system group bootstrap helpers.
"""
from core.access.queries import holders_filter
from core.timeutil import utcnow
from datetime import datetime, timezone
from sqlalchemy.orm import Session
from models.chat import Conversation, ConversationParticipant
from models.user import User
def ensure_system_group(db: Session) -> Conversation:
"""
Guarantee that the single system-wide 'Ομάδα' group conversation exists.
If it is missing, create it and add all currently active users as participants.
Returns the (possibly freshly created) Conversation.
"""
existing = (
db.query(Conversation)
.filter(Conversation.is_system == True) # noqa: E712
.first()
)
if existing:
return existing
# Determine creator — first admin/manager user, or user id=1 as fallback
creator = (
db.query(User)
.filter(holders_filter("core.dashboard.access"), User.is_active == True) # noqa: E712
.order_by(User.id)
.first()
)
creator_id = creator.id if creator else 1
conv = Conversation(
type="group",
name="Ομάδα",
is_system=True,
created_by=creator_id,
)
db.add(conv)
db.flush() # get conv.id before adding participants
active_users = (
db.query(User).filter(User.is_active == True).all() # noqa: E712
)
now = utcnow()
for u in active_users:
participant = ConversationParticipant(
conversation_id=conv.id,
user_id=u.id,
joined_at=now,
)
db.add(participant)
db.commit()
db.refresh(conv)
return conv
def add_user_to_system_group(db: Session, user_id: int) -> None:
"""
Add a user to the system group if not already a participant.
Safe to call even when the system group does not exist yet (it will be created).
"""
conv = ensure_system_group(db)
already = (
db.query(ConversationParticipant)
.filter(
ConversationParticipant.conversation_id == conv.id,
ConversationParticipant.user_id == user_id,
)
.first()
)
if already:
return
participant = ConversationParticipant(
conversation_id=conv.id,
user_id=user_id,
joined_at=utcnow(),
)
db.add(participant)
db.commit()