--
This commit is contained in:
+37
-2
@@ -22,8 +22,8 @@ class User(Base, UUIDMixin, TimestampMixin):
|
||||
UniqueConstraint("company_id", "employee_code", name="uq_user_company_empcode"),
|
||||
# An MR must have a headquarters; nobody else may have one (R4, R8).
|
||||
CheckConstraint(
|
||||
"(role = 'MR' AND headquarters_id IS NOT NULL) "
|
||||
"OR (role <> 'MR' AND headquarters_id IS NULL)",
|
||||
"(role IN ('MR', 'SFE') AND headquarters_id IS NOT NULL) "
|
||||
"OR (role NOT IN ('MR', 'SFE') AND headquarters_id IS NULL)",
|
||||
name="ck_user_mr_requires_hq",
|
||||
),
|
||||
)
|
||||
@@ -45,6 +45,41 @@ class User(Base, UUIDMixin, TimestampMixin):
|
||||
)
|
||||
#: Checked on EVERY request, not only at login (SEC-5).
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
||||
#: The one protected owner account. Nobody — not even itself — can edit, deactivate,
|
||||
#: re-role or delete it through the API; a DB trigger backs that up. It acts as an
|
||||
#: ADMIN everywhere (role is ADMIN), and is created only by `scripts/create_superadmin.py`.
|
||||
is_superadmin: Mapped[bool] = mapped_column(
|
||||
Boolean, default=False, server_default="false", nullable=False
|
||||
)
|
||||
failed_login_attempts: Mapped[int] = mapped_column(default=0, nullable=False)
|
||||
|
||||
headquarters: Mapped["Headquarters | None"] = relationship() # noqa: F821
|
||||
#: SFE only — the other headquarters (districts) covered besides the primary one.
|
||||
extra_headquarters: Mapped[list["UserExtraHeadquarters"]] = relationship(
|
||||
lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
@property
|
||||
def extra_headquarter_ids(self) -> list[uuid.UUID]:
|
||||
return [e.headquarters_id for e in self.extra_headquarters]
|
||||
|
||||
@property
|
||||
def covered_headquarter_ids(self) -> set[uuid.UUID]:
|
||||
"""Every headquarters this field user works in: the primary one plus the extra ones."""
|
||||
ids = set(self.extra_headquarter_ids)
|
||||
if self.headquarters_id:
|
||||
ids.add(self.headquarters_id)
|
||||
return ids
|
||||
|
||||
|
||||
class UserExtraHeadquarters(Base):
|
||||
"""An SFE covers more than one district: each extra headquarters is one row here."""
|
||||
|
||||
__tablename__ = "user_extra_headquarters"
|
||||
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("user.id", ondelete="CASCADE"), primary_key=True
|
||||
)
|
||||
headquarters_id: Mapped[uuid.UUID] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("headquarters.id", ondelete="RESTRICT"), primary_key=True
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user