Files
FabledSteward/steward/models/users.py
T
bvandeusenandClaude Opus 5.5 22090b02d8
CI / lint (push) Successful in 3s
CI / integration (push) Successful in 1m0s
CI / unit (push) Successful in 1m24s
CI / publish (push) Successful in 1m32s
feat(email): each account gets its own email, in its own zone (milestone 495)
Milestone 495 "Email goes to each account, in its own time zone", on the
operator's ruling (System 156): notifications go to each user, times in
their zone, defaults on, toggled per user or per role.

- users.notify_alerts / notify_report (migration 0036): None follows the
  role's default, notify.<kind>.<role> in settings, all on to start.
- mail.pick_recipients: active accounts that want the email and have an
  address mail can reach (an SSO placeholder like name@oidc is skipped and
  flagged), each in its own zone with no UTC beside it, then the Also send
  to addresses in their chosen zone with UTC beside. One copy per address,
  all over one SMTP connection.
- Alerts, the weekly report and the test email (to you, in your zone, plus
  the typed addresses) send that way. The save-time smtp.tz capture is gone.
- Notifications: a role × email table of defaults, who each email reaches
  and who can't be reached, and Also send to with its own zone, which starts
  as the first admin's to open the page.
- Account page: your address and your two switches. Users: an Emailed
  column, and an admin sets an account's switches on its page.
- The Alerts page counts the accounts it emails. zone_select macro and
  formatting.zones_by_region shared by Account and Notifications.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 15:16:17 -04:00

47 lines
2.4 KiB
Python

from __future__ import annotations
import enum
import uuid
from datetime import datetime, timezone
from sqlalchemy import Boolean, DateTime, Enum, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from .base import Base
class UserRole(str, enum.Enum):
admin = "admin"
operator = "operator"
viewer = "viewer"
# Where a user's identity lives. A local user signs in with a Steward
# password; an ldap or oidc user is created at their first sign-in, holds an
# unusable password, and is found again only by (auth_source, external_id) —
# never by a matching username, so a directory cannot sign in as a local account.
AUTH_SOURCES = ("local", "ldap", "oidc")
class User(Base):
__tablename__ = "users"
__table_args__ = (UniqueConstraint("auth_source", "external_id", name="uq_users_auth_source_external_id"),)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
username: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False)
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
role: Mapped[UserRole] = mapped_column(Enum(UserRole), nullable=False, default=UserRole.viewer)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, default=lambda: datetime.now(timezone.utc)
)
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
auth_source: Mapped[str] = mapped_column(String(16), nullable=False, default="local", server_default="local")
# The provider's stable id: the LDAP entry DN, or the OIDC `sub`. None for local users.
external_id: Mapped[str | None] = mapped_column(String(512), nullable=True)
# The IANA zone this user reads times in (milestone 490), e.g.
# "America/New_York". Their browser's zone is saved the first time they
# sign in without one; they change it on the Account page. None reads as UTC.
time_zone: Mapped[str | None] = mapped_column(String(64), nullable=True)
# Whether this account is emailed alerts and the weekly report (milestone
# 495). None follows its role's default (notify.<kind>.<role> in settings).
notify_alerts: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
notify_report: Mapped[bool | None] = mapped_column(Boolean, nullable=True)