user.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """系统用户模型。"""
  2. from __future__ import annotations
  3. from datetime import datetime
  4. from sqlalchemy import BigInteger, CheckConstraint, ForeignKey, Index, Integer, String, text
  5. from sqlalchemy.dialects.mysql import DATETIME
  6. from sqlalchemy.orm import Mapped, mapped_column
  7. from dms.database.base import BusinessTableMixin
  8. from dms.extensions import db
  9. class User(BusinessTableMixin, db.Model):
  10. __tablename__ = "sys_user"
  11. __table_args__ = (
  12. CheckConstraint(
  13. "role_code IN ('USER', 'ADMIN')",
  14. name="user_role_code",
  15. ),
  16. CheckConstraint(
  17. "security_level IN "
  18. "('PUBLIC', 'INTERNAL', 'SECRET', 'CONFIDENTIAL', 'TOP_SECRET')",
  19. name="user_security_level",
  20. ),
  21. CheckConstraint(
  22. "status IN ('ENABLED', 'DISABLED')",
  23. name="user_status",
  24. ),
  25. Index(
  26. "ix_sys_user_organization_active_status",
  27. "organization_id",
  28. "is_deleted",
  29. "status",
  30. ),
  31. Index(
  32. "ix_sys_user_role_status_active",
  33. "role_code",
  34. "status",
  35. "is_deleted",
  36. ),
  37. )
  38. username: Mapped[str] = mapped_column(String(64), nullable=False, unique=True)
  39. password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
  40. real_name: Mapped[str] = mapped_column(String(64), nullable=False)
  41. organization_id: Mapped[int | None] = mapped_column(
  42. BigInteger,
  43. ForeignKey("sys_organization.id", ondelete="RESTRICT"),
  44. nullable=True,
  45. )
  46. organization_name: Mapped[str | None] = mapped_column(String(128), nullable=True)
  47. role_code: Mapped[str] = mapped_column(
  48. String(32),
  49. nullable=False,
  50. server_default=text("'USER'"),
  51. )
  52. security_level: Mapped[str] = mapped_column(
  53. String(32),
  54. nullable=False,
  55. server_default=text("'INTERNAL'"),
  56. )
  57. status: Mapped[str] = mapped_column(
  58. String(20),
  59. nullable=False,
  60. server_default=text("'ENABLED'"),
  61. )
  62. auth_version: Mapped[int] = mapped_column(
  63. Integer,
  64. nullable=False,
  65. server_default=text("0"),
  66. )
  67. last_login_at: Mapped[datetime | None] = mapped_column(
  68. DATETIME(fsp=3),
  69. nullable=True,
  70. )
  71. last_login_ip: Mapped[str | None] = mapped_column(String(64), nullable=True)