Files
FastapiAdmin/backend/app/api/v1/module_system/user/crud.py
T
zhangtao 6a5f8cf0dd refactor: 完成项目大规模重构与功能优化
这是一次综合性的项目迭代,包含以下核心变更:
1.  **目录与模块重构**
    - 调整工作流节点类型模块目录结构,迁移节点类型相关代码
    - 重命名platform模块为system模块,更新插件配置信息
    - 重构代码生成模块导入路径
2.  **数据库与CRUD优化**
    - 统一所有CRUD类构造函数,新增数据库会话参数
    - 修复权限过滤器数据库会话使用问题
    - 更新模板生成器的CRUD代码模板
3.  **认证与安全改进**
    - 重构JWT密钥配置,移除默认密钥强制要求环境变量
    - 重命名密码工具类,统一密码加密校验逻辑
    - 优化OAuth认证流程,修复匿名认证使用问题
4.  **前端与静态资源**
    - 重构前端挂载逻辑,增加目录存在性校验
    - 使用标准StaticFiles替换自定义前端挂载实现
5.  **工具类与依赖更新**
    - 修复导入工具的表名重复检测逻辑
    - 优化限流回调代码,移除冗余依赖
    - 更新用户、租户等模块的响应模型字段
6.  **数据与配置修正**
    - 修复系统版本数据字段命名不统一问题
    - 简化枚举类校验逻辑,移除冗余注释
    - 修复测试用例中的密码工具类导入错误
2026-07-11 13:03:28 +08:00

104 lines
3.5 KiB
Python

from datetime import datetime
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.v1.module_system.position.crud import PositionCRUD
from app.api.v1.module_system.role.crud import RoleCRUD
from app.core.base_crud import CRUDBase
from app.core.base_schema import AuthSchema
from .model import UserModel
from .schema import (
UserCreateSchema,
UserUpdateSchema,
)
class UserCRUD(CRUDBase[UserModel, UserCreateSchema, UserUpdateSchema]):
"""用户模块数据层"""
def __init__(self, auth: AuthSchema, db: AsyncSession) -> None:
super().__init__(model=UserModel, auth=auth, db=db)
async def update_last_login(self, id: int) -> None:
"""更新用户最后登录时间
参数:
- id (int): 用户ID
"""
await self.set([id], last_login=datetime.now())
async def set_user_roles(self, user_ids: list[int], role_ids: list[int]) -> None:
"""批量设置用户角色(带租户隔离验证)
参数:
- user_ids (list[int]): 用户ID列表
- role_ids (list[int]): 角色ID列表
返回:
- None
"""
from app.core.exceptions import CustomException
user_objs = await self.get_list(search={"id": ("in", user_ids)})
if role_ids:
role_objs = await RoleCRUD(self.auth, self.db).get_list(search={"id": ("in", role_ids)})
auth_user = self.auth.user
if auth_user and not auth_user.is_superuser:
for role in role_objs:
if role.tenant_id != auth_user.tenant_id:
raise CustomException(msg=f"角色 {role.name} 不属于当前租户")
else:
role_objs = []
for obj in user_objs:
relationship = obj.roles
relationship.clear()
relationship.extend(role_objs)
await self.db.flush()
async def set_user_positions(self, user_ids: list[int], position_ids: list[int]) -> None:
"""批量设置用户岗位(带租户隔离验证)
参数:
- user_ids (list[int]): 用户ID列表
- position_ids (list[int]): 岗位ID列表
返回:
- None
"""
from app.core.exceptions import CustomException
user_objs = await self.get_list(search={"id": ("in", user_ids)})
if position_ids:
position_objs = await PositionCRUD(self.auth, self.db).get_list(search={"id": ("in", position_ids)})
auth_user = self.auth.user
if auth_user and not auth_user.is_superuser:
for position in position_objs:
if position.tenant_id != auth_user.tenant_id:
raise CustomException(msg=f"岗位 {position.name} 不属于当前租户")
else:
position_objs = []
for obj in user_objs:
relationship = obj.positions
relationship.clear()
relationship.extend(position_objs)
await self.db.flush()
async def change_password(self, id: int, password_hash: str) -> UserModel:
"""修改用户密码
参数:
- id (int): 用户ID
- password_hash (str): 密码哈希值
返回:
- UserModel: 更新后的用户信息
"""
return await self.update(id=id, data=UserUpdateSchema(password=password_hash))
async def forget_password(self, id: int, password_hash: str) -> UserModel:
"""重置密码(与 change_password 逻辑相同)"""
return await self.change_password(id=id, password_hash=password_hash)