login_attempt_his.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """
  2. 登录尝试历史记录模型
  3. 用于记录登录尝试的变更历史,支持审计和追溯
  4. """
  5. from datetime import datetime
  6. from app.utils.timezone import beijing_now_naive
  7. from sqlalchemy import Column, Integer, String, DateTime, Boolean, Text
  8. from app.database import Base
  9. class LoginAttemptHis(Base):
  10. """
  11. 登录尝试历史记录表
  12. 用于记录 login_attempts 表的变更历史,支持:
  13. - 记录每次登录尝试的详细信息
  14. - 追踪账号锁定/解锁事件
  15. - 安全审计和异常检测
  16. """
  17. __tablename__ = "login_attempts_his"
  18. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
  19. username = Column(String(100), nullable=False, index=True, comment="用户名")
  20. success = Column(Boolean, default=False, comment="是否成功: 1-成功, 0-失败")
  21. machine_code = Column(String(255), nullable=True, comment="机器码")
  22. ip_address = Column(String(45), nullable=True, comment="IP地址")
  23. user_agent = Column(String(500), nullable=True, comment="用户代理")
  24. failure_reason = Column(String(255), nullable=True, comment="失败原因")
  25. attempted_at = Column(DateTime, default=beijing_now_naive, comment="尝试时间")
  26. # 历史记录字段
  27. action_type = Column(String(20), nullable=False, comment="操作类型: INSERT/UPDATE/DELETE")
  28. changed_fields = Column(Text, nullable=True, comment="变更字段(JSON格式)")
  29. operator = Column(String(100), nullable=True, comment="操作者: system/user_id")
  30. created_at = Column(DateTime, default=beijing_now_naive, comment="记录创建时间")
  31. def __repr__(self):
  32. return f"<LoginAttemptHis(id={self.id}, username='{self.username}', action_type='{self.action_type}', attempted_at={self.attempted_at})>"