audit_log.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """追加写审计日志模型。"""
  2. from __future__ import annotations
  3. from datetime import datetime
  4. from sqlalchemy import BigInteger, CheckConstraint, Index, JSON, String, Text, text
  5. from sqlalchemy.dialects.mysql import DATETIME
  6. from sqlalchemy.orm import Mapped, mapped_column
  7. from dms.extensions import db
  8. class AuditLog(db.Model):
  9. __tablename__ = "sys_audit_log"
  10. __table_args__ = (
  11. CheckConstraint(
  12. "action_type IN ("
  13. "'LOGIN', 'LOGOUT', 'VIEW_DOCUMENT', 'DOWNLOAD_DOCUMENT', "
  14. "'UPLOAD_DOCUMENT', 'BATCH_IMPORT', 'EDIT_DOCUMENT', "
  15. "'DELETE_DOCUMENT', 'CREATE_CATEGORY', 'EDIT_CATEGORY', "
  16. "'DELETE_CATEGORY', 'CHANGE_PERMISSION', 'BIND_ATTACHMENT', "
  17. "'UNBIND_ATTACHMENT', 'RESTORE_DOCUMENT', "
  18. "'CREATE_ORGANIZATION', 'EDIT_ORGANIZATION', "
  19. "'DELETE_ORGANIZATION', 'CREATE_USER', 'EDIT_USER', "
  20. "'DELETE_USER', 'RESET_USER_PASSWORD', "
  21. "'REORDER_DOCUMENT', 'REORDER_ATTACHMENT', 'REORDER_CATEGORY', "
  22. "'CREATE_ROLE', 'EDIT_ROLE', 'DELETE_ROLE', "
  23. "'CHANGE_ROLE_PERMISSION', 'CHANGE_ROLE_USER', "
  24. "'DOCUMENT_PASSWORD_SET', 'DOCUMENT_PASSWORD_REMOVE', "
  25. "'CREATE_IP_ENTRY', 'EDIT_IP_ENTRY', 'DELETE_IP_ENTRY', "
  26. "'ASSIGN_ORG_IP'"
  27. ")",
  28. name="audit_action_type",
  29. ),
  30. CheckConstraint(
  31. "target_type IS NULL OR target_type IN ("
  32. "'AUTH', 'CATEGORY', 'DOCUMENT', 'ATTACHMENT', "
  33. "'PERMISSION', 'ATTACHMENT_BINDING', 'ORGANIZATION', 'USER', "
  34. "'ROLE', 'IP_ENTRY'"
  35. ")",
  36. name="audit_target_type",
  37. ),
  38. CheckConstraint(
  39. "operation_result IN ('SUCCESS', 'FAILURE')",
  40. name="audit_operation_result",
  41. ),
  42. Index("ix_sys_audit_log_created_at", "created_at"),
  43. Index("ix_sys_audit_log_user_created", "user_id", "created_at"),
  44. Index(
  45. "ix_sys_audit_log_organization_created",
  46. "organization_id",
  47. "created_at",
  48. ),
  49. Index("ix_sys_audit_log_action_created", "action_type", "created_at"),
  50. Index(
  51. "ix_sys_audit_log_target_created",
  52. "target_type",
  53. "target_id",
  54. "created_at",
  55. ),
  56. Index(
  57. "ix_sys_audit_log_result_created",
  58. "operation_result",
  59. "created_at",
  60. ),
  61. Index("ix_sys_audit_log_request_id", "request_id"),
  62. )
  63. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  64. user_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  65. username: Mapped[str | None] = mapped_column(String(64), nullable=True)
  66. real_name: Mapped[str | None] = mapped_column(String(64), nullable=True)
  67. organization_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  68. organization_name: Mapped[str | None] = mapped_column(String(128), nullable=True)
  69. action_type: Mapped[str] = mapped_column(String(32), nullable=False)
  70. target_type: Mapped[str | None] = mapped_column(String(32), nullable=True)
  71. target_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  72. target_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
  73. operation_result: Mapped[str] = mapped_column(String(20), nullable=False)
  74. failure_reason: Mapped[str | None] = mapped_column(String(1000), nullable=True)
  75. client_ip: Mapped[str | None] = mapped_column(String(64), nullable=True)
  76. user_agent: Mapped[str | None] = mapped_column(String(500), nullable=True)
  77. request_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
  78. operation_detail: Mapped[dict[str, object] | None] = mapped_column(
  79. JSON,
  80. nullable=True,
  81. )
  82. created_at: Mapped[datetime] = mapped_column(
  83. DATETIME(fsp=3),
  84. nullable=False,
  85. server_default=text("CURRENT_TIMESTAMP(3)"),
  86. )