user.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. 用户相关数据模型
  3. """
  4. from sqlalchemy import Column, String, Boolean, Integer, DateTime, Text, Date, JSON, ForeignKey
  5. from sqlalchemy.orm import relationship
  6. from sqlalchemy.dialects.mysql import CHAR, TINYINT
  7. from .base import BaseModel
  8. from datetime import datetime
  9. from typing import Optional
  10. class User(BaseModel):
  11. """用户表"""
  12. __tablename__ = "users"
  13. username = Column(String(50), unique=True, nullable=False, comment="用户名")
  14. email = Column(String(100), unique=True, nullable=False, comment="邮箱")
  15. phone = Column(String(20), unique=True, nullable=True, comment="手机号")
  16. password_hash = Column(String(255), nullable=False, comment="密码哈希")
  17. avatar_url = Column(String(500), nullable=True, comment="头像URL")
  18. is_active = Column(Boolean, default=True, comment="是否激活")
  19. is_superuser = Column(Boolean, default=False, comment="是否超级管理员")
  20. last_login_at = Column(DateTime, nullable=True, comment="最后登录时间")
  21. last_login_ip = Column(String(45), nullable=True, comment="最后登录IP")
  22. failed_login_attempts = Column(Integer, default=0, comment="失败登录次数")
  23. locked_until = Column(DateTime, nullable=True, comment="锁定直到时间")
  24. # 关联关系
  25. profile = relationship("UserProfile", back_populates="user", uselist=False)
  26. roles = relationship("UserRole", back_populates="user")
  27. tokens = relationship("OAuthAccessToken", back_populates="user")
  28. login_logs = relationship("LoginLog", back_populates="user")
  29. class UserProfile(BaseModel):
  30. """用户详情表"""
  31. __tablename__ = "user_profiles"
  32. user_id = Column(CHAR(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, comment="用户ID")
  33. real_name = Column(String(50), nullable=True, comment="真实姓名")
  34. gender = Column(TINYINT, nullable=True, comment="性别 0:未知 1:男 2:女")
  35. birth_date = Column(Date, nullable=True, comment="出生日期")
  36. address = Column(String(500), nullable=True, comment="地址")
  37. company = Column(String(100), nullable=True, comment="公司")
  38. department = Column(String(100), nullable=True, comment="部门")
  39. position = Column(String(100), nullable=True, comment="职位")
  40. extra_info = Column(JSON, nullable=True, comment="扩展信息")
  41. # 关联关系
  42. user = relationship("User", back_populates="profile")
  43. class Role(BaseModel):
  44. """角色表"""
  45. __tablename__ = "roles"
  46. name = Column(String(50), unique=True, nullable=False, comment="角色名称")
  47. code = Column(String(50), unique=True, nullable=False, comment="角色代码")
  48. description = Column(String(500), nullable=True, comment="角色描述")
  49. is_active = Column(Boolean, default=True, comment="是否激活")
  50. # 关联关系
  51. users = relationship("UserRole", back_populates="role")
  52. permissions = relationship("RolePermission", back_populates="role")
  53. class UserRole(BaseModel):
  54. """用户角色关系表"""
  55. __tablename__ = "user_roles"
  56. user_id = Column(CHAR(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, comment="用户ID")
  57. role_id = Column(CHAR(36), ForeignKey("roles.id", ondelete="CASCADE"), nullable=False, comment="角色ID")
  58. # 关联关系
  59. user = relationship("User", back_populates="roles")
  60. role = relationship("Role", back_populates="users")
  61. class Permission(BaseModel):
  62. """权限表"""
  63. __tablename__ = "permissions"
  64. name = Column(String(100), nullable=False, comment="权限名称")
  65. code = Column(String(100), unique=True, nullable=False, comment="权限代码")
  66. description = Column(String(500), nullable=True, comment="权限描述")
  67. resource = Column(String(100), nullable=True, comment="资源")
  68. action = Column(String(50), nullable=True, comment="操作")
  69. is_active = Column(Boolean, default=True, comment="是否激活")
  70. # 关联关系
  71. roles = relationship("RolePermission", back_populates="permission")
  72. class RolePermission(BaseModel):
  73. """角色权限关系表"""
  74. __tablename__ = "role_permissions"
  75. role_id = Column(CHAR(36), ForeignKey("roles.id", ondelete="CASCADE"), nullable=False, comment="角色ID")
  76. permission_id = Column(CHAR(36), ForeignKey("permissions.id", ondelete="CASCADE"), nullable=False, comment="权限ID")
  77. # 关联关系
  78. role = relationship("Role", back_populates="permissions")
  79. permission = relationship("Permission", back_populates="roles")