""" RBAC 权限域 ORM 模型 —— 映射到已有的 sys_departments / sys_roles / sys_users 表 注意: 表已由 schema.sql 创建,这里仅做 ORM 映射,不使用 metadata.create_all """ from __future__ import annotations import uuid from datetime import datetime from sqlalchemy import Boolean, DateTime, ForeignKey, SmallInteger, String, Text, UniqueConstraint, func from sqlalchemy.dialects.postgresql import JSONB, UUID from sqlalchemy.orm import Mapped, mapped_column, relationship from app.models.base import Base class SysDepartment(Base): __tablename__ = "sys_departments" id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 ) parent_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("sys_departments.id"), nullable=True ) name: Mapped[str] = mapped_column(String(100), nullable=False) sort_order: Mapped[int] = mapped_column(SmallInteger, default=0) status: Mapped[int] = mapped_column(SmallInteger, default=1) created_at: Mapped[datetime] = mapped_column( DateTime, server_default=func.now() ) updated_at: Mapped[datetime] = mapped_column( DateTime, server_default=func.now(), onupdate=func.now() ) is_deleted: Mapped[bool] = mapped_column(Boolean, default=False) # 自引用关系 children: Mapped[list[SysDepartment]] = relationship( "SysDepartment", back_populates="parent", lazy="selectin" ) parent: Mapped[SysDepartment | None] = relationship( "SysDepartment", back_populates="children", remote_side=[id], lazy="selectin" ) class SysRole(Base): __tablename__ = "sys_roles" id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 ) role_name: Mapped[str] = mapped_column(String(50), unique=True, nullable=False) data_scope: Mapped[str] = mapped_column(String(20), nullable=False, default="self") menu_keys: Mapped[dict] = mapped_column(JSONB, default=list) description: Mapped[str | None] = mapped_column(String(255), nullable=True) status: Mapped[int] = mapped_column(SmallInteger, default=1) created_at: Mapped[datetime] = mapped_column( DateTime, server_default=func.now() ) updated_at: Mapped[datetime] = mapped_column( DateTime, server_default=func.now(), onupdate=func.now() ) is_deleted: Mapped[bool] = mapped_column(Boolean, default=False) class SysUser(Base): __tablename__ = "sys_users" id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 ) dept_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("sys_departments.id"), nullable=True ) role_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("sys_roles.id"), nullable=True ) username: Mapped[str] = mapped_column(String(50), unique=True, nullable=False) password_hash: Mapped[str] = mapped_column(String(255), nullable=False) real_name: Mapped[str | None] = mapped_column(String(50), nullable=True) phone: Mapped[str | None] = mapped_column(String(20), nullable=True) email: Mapped[str | None] = mapped_column(String(100), nullable=True) avatar_url: Mapped[str | None] = mapped_column(String(500), nullable=True) status: Mapped[int] = mapped_column(SmallInteger, default=1) last_login_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) created_at: Mapped[datetime] = mapped_column( DateTime, server_default=func.now() ) updated_at: Mapped[datetime] = mapped_column( DateTime, server_default=func.now(), onupdate=func.now() ) is_deleted: Mapped[bool] = mapped_column(Boolean, default=False) # 关系 department: Mapped[SysDepartment | None] = relationship( "SysDepartment", lazy="selectin" ) role: Mapped[SysRole | None] = relationship("SysRole", lazy="selectin") class SysCompany(Base): """公司主体表 —— 多租户逻辑隔离核心""" __tablename__ = "sys_companies" id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 ) name: Mapped[str] = mapped_column(String(200), nullable=False) code: Mapped[str] = mapped_column(String(50), unique=True, nullable=False) full_info: Mapped[dict | None] = mapped_column( JSONB, default=dict, nullable=True, comment="公司完整信息: full_name/address/phone/bank_name/bank_account/tax_id" ) is_active: Mapped[bool] = mapped_column(Boolean, default=True) created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now()) updated_at: Mapped[datetime] = mapped_column( DateTime, server_default=func.now(), onupdate=func.now() ) class SysUserCompany(Base): """用户-公司多对多关联 —— IDOR 防护核心""" __tablename__ = "sys_user_companies" id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 ) user_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("sys_users.id"), nullable=False ) company_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("sys_companies.id"), nullable=False ) is_default: Mapped[bool] = mapped_column(Boolean, default=False) created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now()) __table_args__ = ( UniqueConstraint("user_id", "company_id", name="uq_user_company"), )