BookSystem/backend/auth/models.py
jayhgq 19d6db6ecc feat: 添加登录日志和操作日志功能
1. 新增日志相关数据库模型、API路由、前端页面与权限配置
2. 为现有业务接口添加操作日志记录功能
3. 优化导入顺序与权限初始化数据
2026-06-12 22:41:41 +08:00

140 lines
6.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, func, Table, Index, Text
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
Base = declarative_base()
class Role(Base):
"""角色数据库模型"""
__tablename__ = "role"
id = Column(Integer, primary_key=True, index=True, comment="角色ID")
name = Column(String(50), nullable=False, unique=True, comment="角色名称")
createtime = Column(DateTime, default=datetime.utcnow, comment="创建时间")
updatetime = Column(
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间"
)
creator = Column(String(50), nullable=True, comment="创建人")
# 关联到User
users = relationship("User", back_populates="role_rel")
# 关联到Permission多对多
permissions = relationship(
"Permission",
secondary="role_permission",
back_populates="roles"
)
class User(Base):
"""用户数据库模型"""
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
username = Column(String(50), unique=True, nullable=False, comment="登录名")
nickname = Column(String(50), nullable=False, comment="昵称")
email = Column(String(50), nullable=True, comment="电子邮箱")
phone = Column(String(11), nullable=True, comment="手机号码")
wx_openid = Column(String(100), nullable=True, comment="微信OpenID")
password_hash = Column(String(128), nullable=False, comment="密码哈希值")
avatar = Column(String(255), nullable=True, comment="头像路径")
role_id = Column(Integer, ForeignKey("role.id"), default=2, comment="角色ID")
createtime = Column(DateTime, default=datetime.utcnow, comment="创建时间")
updatetime = Column(
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间"
)
lastlogintime = Column(DateTime, comment="最后登录时间")
# 关联到Role
role_rel = relationship("Role", back_populates="users")
class Permission(Base):
"""权限数据库模型"""
__tablename__ = "permission"
id = Column(Integer, primary_key=True, index=True, comment="权限ID")
name = Column(String(100), nullable=False, comment="权限名称")
code = Column(String(100), nullable=False, unique=True, comment="权限编码")
parentid = Column(Integer, default=0, comment="父权限ID0表示顶级模块")
route = Column(String(255), nullable=True, comment="前端路由路径")
description = Column(String(255), nullable=True, comment="权限描述")
createtime = Column(DateTime, default=datetime.utcnow, comment="创建时间")
updatetime = Column(
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间"
)
# 关联到Role多对多
roles = relationship(
"Role",
secondary="role_permission",
back_populates="permissions"
)
# 角色权限关联表(多对多)
role_permission = Table(
"role_permission",
Base.metadata,
Column("role_id", Integer, ForeignKey("role.id"), primary_key=True, comment="角色ID"),
Column("permission_id", Integer, ForeignKey("permission.id"), primary_key=True, comment="权限ID")
)
class SystemSetting(Base):
"""系统设置数据库模型"""
__tablename__ = "system_setting"
id = Column(Integer, primary_key=True, index=True, comment="设置ID")
key = Column(String(100), nullable=False, unique=True, comment="设置键名")
value = Column(String(2000), nullable=False, comment="设置值")
type = Column(String(20), default="string", comment="类型string/image/select/boolean")
options = Column(String(500), nullable=True, comment="下拉选项(用|分隔)")
description = Column(String(255), nullable=True, comment="设置描述")
createtime = Column(DateTime, default=datetime.utcnow, comment="创建时间")
updatetime = Column(
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间"
)
class LoginLog(Base):
"""登录日志"""
__tablename__ = "login_log"
id = Column(Integer, primary_key=True, index=True, comment="日志ID")
username = Column(String(50), nullable=False, comment="用户名")
login_time = Column(DateTime, default=datetime.utcnow, comment="登录时间")
ip_address = Column(String(50), nullable=True, comment="IP地址")
user_agent = Column(String(500), nullable=True, comment="User-Agent")
status = Column(String(20), nullable=False, comment="状态: success/failure")
failure_reason = Column(String(255), nullable=True, comment="失败原因")
Index("ix_login_log_login_time", LoginLog.login_time)
Index("ix_login_log_username", LoginLog.username)
Index("ix_login_log_status", LoginLog.status)
class OperationLog(Base):
"""操作日志"""
__tablename__ = "operation_log"
id = Column(Integer, primary_key=True, index=True, comment="日志ID")
user_id = Column(Integer, nullable=False, comment="用户ID")
username = Column(String(50), nullable=False, comment="用户名")
action_type = Column(String(20), nullable=False, comment="操作类型: create/update/delete")
target_type = Column(String(50), nullable=False, comment="目标类型: user/role/permission/setting")
target_id = Column(Integer, nullable=True, comment="目标ID")
target_name = Column(String(100), nullable=True, comment="目标名称")
detail = Column(Text, nullable=True, comment="变更详情(JSON)")
ip_address = Column(String(50), nullable=True, comment="IP地址")
user_agent = Column(String(500), nullable=True, comment="User-Agent")
createtime = Column(DateTime, default=datetime.utcnow, comment="创建时间")
Index("ix_operation_log_createtime", OperationLog.createtime)
Index("ix_operation_log_username", OperationLog.username)
Index("ix_operation_log_action_type", OperationLog.action_type)
Index("ix_operation_log_target_type", OperationLog.target_type)