| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- """
- 用户相关数据模型
- """
- from sqlalchemy import Column, String, Boolean, Integer, DateTime, Text, Date, JSON, ForeignKey
- from sqlalchemy.orm import relationship
- from sqlalchemy.dialects.mysql import CHAR, TINYINT
- from .base import BaseModel
- from datetime import datetime
- from typing import Optional
- class User(BaseModel):
- """用户表"""
- __tablename__ = "users"
-
- username = Column(String(50), unique=True, nullable=False, comment="用户名")
- email = Column(String(100), unique=True, nullable=False, comment="邮箱")
- phone = Column(String(20), unique=True, nullable=True, comment="手机号")
- password_hash = Column(String(255), nullable=False, comment="密码哈希")
- avatar_url = Column(String(500), nullable=True, comment="头像URL")
- is_active = Column(Boolean, default=True, comment="是否激活")
- is_superuser = Column(Boolean, default=False, comment="是否超级管理员")
- last_login_at = Column(DateTime, nullable=True, comment="最后登录时间")
- last_login_ip = Column(String(45), nullable=True, comment="最后登录IP")
- failed_login_attempts = Column(Integer, default=0, comment="失败登录次数")
- locked_until = Column(DateTime, nullable=True, comment="锁定直到时间")
-
- # 关联关系
- profile = relationship("UserProfile", back_populates="user", uselist=False)
- roles = relationship("UserRole", back_populates="user")
- tokens = relationship("OAuthAccessToken", back_populates="user")
- login_logs = relationship("LoginLog", back_populates="user")
- class UserProfile(BaseModel):
- """用户详情表"""
- __tablename__ = "user_profiles"
-
- user_id = Column(CHAR(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, comment="用户ID")
- real_name = Column(String(50), nullable=True, comment="真实姓名")
- gender = Column(TINYINT, nullable=True, comment="性别 0:未知 1:男 2:女")
- birth_date = Column(Date, nullable=True, comment="出生日期")
- address = Column(String(500), nullable=True, comment="地址")
- company = Column(String(100), nullable=True, comment="公司")
- department = Column(String(100), nullable=True, comment="部门")
- position = Column(String(100), nullable=True, comment="职位")
- extra_info = Column(JSON, nullable=True, comment="扩展信息")
-
- # 关联关系
- user = relationship("User", back_populates="profile")
- class Role(BaseModel):
- """角色表"""
- __tablename__ = "roles"
-
- name = Column(String(50), unique=True, nullable=False, comment="角色名称")
- code = Column(String(50), unique=True, nullable=False, comment="角色代码")
- description = Column(String(500), nullable=True, comment="角色描述")
- is_active = Column(Boolean, default=True, comment="是否激活")
-
- # 关联关系
- users = relationship("UserRole", back_populates="role")
- permissions = relationship("RolePermission", back_populates="role")
- class UserRole(BaseModel):
- """用户角色关系表"""
- __tablename__ = "user_roles"
-
- user_id = Column(CHAR(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, comment="用户ID")
- role_id = Column(CHAR(36), ForeignKey("roles.id", ondelete="CASCADE"), nullable=False, comment="角色ID")
-
- # 关联关系
- user = relationship("User", back_populates="roles")
- role = relationship("Role", back_populates="users")
- class Permission(BaseModel):
- """权限表"""
- __tablename__ = "permissions"
-
- name = Column(String(100), nullable=False, comment="权限名称")
- code = Column(String(100), unique=True, nullable=False, comment="权限代码")
- description = Column(String(500), nullable=True, comment="权限描述")
- resource = Column(String(100), nullable=True, comment="资源")
- action = Column(String(50), nullable=True, comment="操作")
- is_active = Column(Boolean, default=True, comment="是否激活")
-
- # 关联关系
- roles = relationship("RolePermission", back_populates="permission")
- class RolePermission(BaseModel):
- """角色权限关系表"""
- __tablename__ = "role_permissions"
-
- role_id = Column(CHAR(36), ForeignKey("roles.id", ondelete="CASCADE"), nullable=False, comment="角色ID")
- permission_id = Column(CHAR(36), ForeignKey("permissions.id", ondelete="CASCADE"), nullable=False, comment="权限ID")
-
- # 关联关系
- role = relationship("Role", back_populates="permissions")
- permission = relationship("Permission", back_populates="roles")
|