"""登录IP限制:IP条目池与部门分配模型。""" from __future__ import annotations from sqlalchemy import ( BigInteger, Computed, ForeignKey, Index, Integer, String, UniqueConstraint, text, ) from sqlalchemy.orm import Mapped, mapped_column from dms.database.base import BusinessTableMixin from dms.extensions import db class IpEntry(BusinessTableMixin, db.Model): """全局单一IP池:每条为规范化后的精确IP或CIDR网段文本。""" __tablename__ = "sys_ip_entry" __table_args__ = ( UniqueConstraint( "ip_value", "active_marker", name="uq_sys_ip_entry_ip_value_active", ), Index( "ix_sys_ip_entry_active", "is_deleted", "id", ), ) ip_value: Mapped[str] = mapped_column(String(64), nullable=False) remark: Mapped[str] = mapped_column( String(128), nullable=False, server_default=text("''"), ) active_marker: Mapped[int | None] = mapped_column( Integer(), Computed("IF(is_deleted = 0, 1, NULL)", persisted=False), nullable=True, ) class OrgIpAssignment(BusinessTableMixin, db.Model): """部门可登录IP分配:整体替换语义,至少一行未删记录即视为"该部门受限"。""" __tablename__ = "sys_org_ip_assignment" __table_args__ = ( UniqueConstraint( "organization_id", "ip_entry_id", "active_marker", name="uq_sys_org_ip_assignment_active", ), Index( "ix_sys_org_ip_assignment_org_active", "organization_id", "is_deleted", ), Index( "ix_sys_org_ip_assignment_entry_active", "ip_entry_id", "is_deleted", ), ) organization_id: Mapped[int] = mapped_column( BigInteger, ForeignKey("sys_organization.id", ondelete="RESTRICT"), nullable=False, ) ip_entry_id: Mapped[int] = mapped_column( BigInteger, ForeignKey("sys_ip_entry.id", ondelete="RESTRICT"), nullable=False, ) active_marker: Mapped[int | None] = mapped_column( Integer(), Computed("IF(is_deleted = 0, 1, NULL)", persisted=False), nullable=True, )