ip_entry.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """登录IP限制:IP条目池与部门分配模型。"""
  2. from __future__ import annotations
  3. from sqlalchemy import (
  4. BigInteger,
  5. Computed,
  6. ForeignKey,
  7. Index,
  8. Integer,
  9. String,
  10. UniqueConstraint,
  11. text,
  12. )
  13. from sqlalchemy.orm import Mapped, mapped_column
  14. from dms.database.base import BusinessTableMixin
  15. from dms.extensions import db
  16. class IpEntry(BusinessTableMixin, db.Model):
  17. """全局单一IP池:每条为规范化后的精确IP或CIDR网段文本。"""
  18. __tablename__ = "sys_ip_entry"
  19. __table_args__ = (
  20. UniqueConstraint(
  21. "ip_value",
  22. "active_marker",
  23. name="uq_sys_ip_entry_ip_value_active",
  24. ),
  25. Index(
  26. "ix_sys_ip_entry_active",
  27. "is_deleted",
  28. "id",
  29. ),
  30. )
  31. ip_value: Mapped[str] = mapped_column(String(64), nullable=False)
  32. remark: Mapped[str] = mapped_column(
  33. String(128),
  34. nullable=False,
  35. server_default=text("''"),
  36. )
  37. active_marker: Mapped[int | None] = mapped_column(
  38. Integer(),
  39. Computed("IF(is_deleted = 0, 1, NULL)", persisted=False),
  40. nullable=True,
  41. )
  42. class OrgIpAssignment(BusinessTableMixin, db.Model):
  43. """部门可登录IP分配:整体替换语义,至少一行未删记录即视为"该部门受限"。"""
  44. __tablename__ = "sys_org_ip_assignment"
  45. __table_args__ = (
  46. UniqueConstraint(
  47. "organization_id",
  48. "ip_entry_id",
  49. "active_marker",
  50. name="uq_sys_org_ip_assignment_active",
  51. ),
  52. Index(
  53. "ix_sys_org_ip_assignment_org_active",
  54. "organization_id",
  55. "is_deleted",
  56. ),
  57. Index(
  58. "ix_sys_org_ip_assignment_entry_active",
  59. "ip_entry_id",
  60. "is_deleted",
  61. ),
  62. )
  63. organization_id: Mapped[int] = mapped_column(
  64. BigInteger,
  65. ForeignKey("sys_organization.id", ondelete="RESTRICT"),
  66. nullable=False,
  67. )
  68. ip_entry_id: Mapped[int] = mapped_column(
  69. BigInteger,
  70. ForeignKey("sys_ip_entry.id", ondelete="RESTRICT"),
  71. nullable=False,
  72. )
  73. active_marker: Mapped[int | None] = mapped_column(
  74. Integer(),
  75. Computed("IF(is_deleted = 0, 1, NULL)", persisted=False),
  76. nullable=True,
  77. )