0e6b3b1811
按 ROADMAP §3.1 治理批次推进的后端设计审查整改:
- 批次 0(安全):/api/status/{task_id} 补 JWT 鉴权与任务归属校验;
pythonocc_available 真实探测;bcrypt 超 72 字节显式拒绝;
SECRET_KEY/RUSTFS_* 惰性校验,代码侧弱默认移除
- 批次 1(部署正确性):主处理链路改走 RustFS(分派入参 stp_file_id 化,
worker 按 object_key 下载);AUTO_MIGRATE 开关 + 迁移目录 alembic/→migrations/
修复包遮蔽(自动迁移此前从未真正生效);OCC 镜像改 conda 原生执行 +
基础镜像 tag 锁定;compose 关键项改 ${VAR:?} 强制显式配置
- 批次 2(任务一致性):删除 Redis 进程内存回退,PG 为任务状态单一事实源;
批量元数据入库(processing_tasks.batch_id,迁移 a3f8c2d91e47);
型腔失败任务标 failed 不再静默 completed;事务边界收口
(数据本体写 flush-only、失败先回滚再置 failed、进度更新保留即时 commit)
- 批次 3(API 与代码结构):592 行 advanced_router 拆为 design/cost/machining/
export 四子路由,请求体全量 Pydantic 化;ROUTE_MODULES + route_registry
(/api/health 呈现 degraded,DEBUG fail fast);纯计算端点统一 to_thread;
StorageIntegrationService 按职责三拆;MAX_FILE_SIZE 接线生效、
celery 复用 Settings.redis_url;管理员重置密码改 JSON body(端到端断裂修复);
openapi.json 重导出(76 paths)+ 前端 gen:api
- 批次 4(架构演进):共享 ORM 按模块拆分(shared/models/base.py + identity.py、
moldinsight/models/、inventory/models/,删除三条无使用方的跨模块
relationship,跨模块桥接收敛为裸 FK 硬规则,无兼容 facade);
OCC executor 重建补 cancel_futures=True(消除旧队列被慢恢复线程
并行消化的数据竞争);OCC 吞吐方案设计先行
(docs/topics/performance/OCC_THROUGHPUT.md);顺手清偿 D15
(vite.config.ts 未用参数致 npm run build 失败)
测试基线:125 passed, 2 skipped(pytest + sqlite+aiosqlite;归属边界、
路由契约、配置治理、鉴权回归等随批新增)
文档同步:STATUS / TECH_DEBT / ROADMAP / ARCHITECTURE / API_CONTRACT /
OPERATIONS / AGENTS
Co-Authored-By: Claude Code <noreply@anthropic.com>
183 lines
6.4 KiB
Python
183 lines
6.4 KiB
Python
"""身份与权限模型(平台层,三种部署形态共用)。
|
||
|
||
从旧 shared/models/database.py 拆出(D3,2026-09-17)。
|
||
原 User.stp_files ↔ STPFile.user 跨模块 relationship 已删除(无使用方):
|
||
用户与 STP 文件的关联走 STPFile.user_id 裸 FK,查询由 moldinsight 侧显式 select。
|
||
"""
|
||
from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, ForeignKey, JSON
|
||
from sqlalchemy.sql import func
|
||
from sqlalchemy.orm import relationship
|
||
|
||
from shared.models.base import Base
|
||
|
||
|
||
class User(Base):
|
||
"""用户表"""
|
||
__tablename__ = "users"
|
||
__excluded_fields__ = {'hashed_password'}
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
username = Column(String(50), unique=True, index=True, nullable=False)
|
||
email = Column(String(255), unique=True, index=True, nullable=False)
|
||
hashed_password = Column(String(255), nullable=False)
|
||
full_name = Column(String(100))
|
||
is_active = Column(Boolean, default=True)
|
||
created_at = Column(DateTime, default=func.now())
|
||
last_login = Column(DateTime, nullable=True)
|
||
|
||
user_roles = relationship("UserRole", back_populates="user", cascade="all, delete-orphan")
|
||
|
||
@property
|
||
def roles(self):
|
||
return [ur.role for ur in self.user_roles]
|
||
|
||
@property
|
||
def is_superuser(self):
|
||
return any(r.code == 'admin' for r in self.roles)
|
||
|
||
def has_permission(self, permission_code: str) -> bool:
|
||
if self.is_superuser:
|
||
return True
|
||
for role in self.roles:
|
||
for perm in role.permissions:
|
||
if perm.code == permission_code:
|
||
return True
|
||
return False
|
||
|
||
def safe_dict(self):
|
||
return {k: v for k, v in self.__dict__.items()
|
||
if not k.startswith('_') and k not in self.__excluded_fields__}
|
||
|
||
def __repr__(self):
|
||
return f"<User(id={self.id}, username='{self.username}')>"
|
||
|
||
|
||
class Role(Base):
|
||
"""角色表"""
|
||
__tablename__ = "roles"
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
code = Column(String(50), unique=True, index=True, nullable=False)
|
||
name = Column(String(100), nullable=False)
|
||
description = Column(Text, nullable=True)
|
||
is_system = Column(Boolean, default=False)
|
||
created_at = Column(DateTime, default=func.now())
|
||
|
||
user_roles = relationship("UserRole", back_populates="role", cascade="all, delete-orphan")
|
||
role_permissions = relationship("RolePermission", back_populates="role", cascade="all, delete-orphan")
|
||
|
||
@property
|
||
def permissions(self):
|
||
return [rp.permission for rp in self.role_permissions]
|
||
|
||
def __repr__(self):
|
||
return f"<Role(code='{self.code}', name='{self.name}')>"
|
||
|
||
|
||
class Permission(Base):
|
||
"""权限表"""
|
||
__tablename__ = "permissions"
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
code = Column(String(100), unique=True, index=True, nullable=False)
|
||
name = Column(String(100), nullable=False)
|
||
module = Column(String(50), nullable=True)
|
||
description = Column(Text, nullable=True)
|
||
created_at = Column(DateTime, default=func.now())
|
||
|
||
role_permissions = relationship("RolePermission", back_populates="permission", cascade="all, delete-orphan")
|
||
|
||
def __repr__(self):
|
||
return f"<Permission(code='{self.code}', name='{self.name}')>"
|
||
|
||
|
||
class UserRole(Base):
|
||
"""用户角色关联表"""
|
||
__tablename__ = "user_roles"
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
|
||
role_id = Column(Integer, ForeignKey("roles.id"), nullable=False, index=True)
|
||
created_at = Column(DateTime, default=func.now())
|
||
|
||
user = relationship("User", back_populates="user_roles")
|
||
role = relationship("Role", back_populates="user_roles")
|
||
|
||
def __repr__(self):
|
||
return f"<UserRole(user_id={self.user_id}, role_id={self.role_id})>"
|
||
|
||
|
||
class RolePermission(Base):
|
||
"""角色权限关联表"""
|
||
__tablename__ = "role_permissions"
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
role_id = Column(Integer, ForeignKey("roles.id"), nullable=False, index=True)
|
||
permission_id = Column(Integer, ForeignKey("permissions.id"), nullable=False, index=True)
|
||
created_at = Column(DateTime, default=func.now())
|
||
|
||
role = relationship("Role", back_populates="role_permissions")
|
||
permission = relationship("Permission", back_populates="role_permissions")
|
||
|
||
def __repr__(self):
|
||
return f"<RolePermission(role_id={self.role_id}, permission_id={self.permission_id})>"
|
||
|
||
|
||
class UserActivity(Base):
|
||
"""用户活动日志表"""
|
||
__tablename__ = "user_activities"
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
|
||
|
||
# 活动信息
|
||
activity_type = Column(String(50), nullable=False, index=True) # upload, view, download, delete, export
|
||
resource_type = Column(String(50), nullable=True) # stp_file, geometry_data, mold_cavity
|
||
resource_id = Column(Integer, nullable=True)
|
||
|
||
# 活动详情
|
||
description = Column(Text, nullable=True)
|
||
meta_data = Column(JSON, nullable=True)
|
||
|
||
# 时间戳
|
||
created_at = Column(DateTime, default=func.now(), index=True)
|
||
|
||
# IP和设备信息
|
||
ip_address = Column(String(45), nullable=True)
|
||
user_agent = Column(String(500), nullable=True)
|
||
|
||
def __repr__(self):
|
||
return f"<UserActivity(id={self.id}, user_id={self.user_id}, activity_type='{self.activity_type}')>"
|
||
|
||
|
||
class SystemLog(Base):
|
||
"""系统日志表(重要操作和错误)"""
|
||
__tablename__ = "system_logs"
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
|
||
# 日志级别
|
||
level = Column(String(20), nullable=False, index=True) # INFO, WARNING, ERROR, CRITICAL
|
||
|
||
# 日志信息
|
||
message = Column(Text, nullable=False)
|
||
module = Column(String(100), nullable=True) # 模块名
|
||
function_name = Column(String(100), nullable=True)
|
||
|
||
# 时间戳
|
||
created_at = Column(DateTime, default=func.now(), index=True)
|
||
|
||
# 用户信息(如果有关联用户)
|
||
user_id = Column(Integer, ForeignKey("users.id"), nullable=True)
|
||
|
||
# 额外信息
|
||
request_id = Column(String(100), nullable=True) # 关联的请求ID
|
||
execution_time_ms = Column(Integer, nullable=True) # 执行时间
|
||
|
||
# 关联数据
|
||
resource_type = Column(String(50), nullable=True)
|
||
resource_id = Column(Integer, nullable=True)
|
||
|
||
def __repr__(self):
|
||
return f"<SystemLog(id={self.id}, level='{self.level}', module='{self.module}')>"
|