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