mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-23 05:10:57 +00:00
本次提交完成以下主要变更: 1. 删除所有租户相关的初始化数据、配置和工具代码 2. 移除TicketModel的租户关联字段与校验逻辑 3. 重构菜单表名与关联外键,统一为sys_前缀 4. 为岗位、部门、工作流等模型添加唯一约束 5. 调整导入顺序与冗余代码,优化代码结构 6. 移除支付、PDF生成相关的依赖与工具类 7. 简化OpenAPI文档标签配置 8. 清理无效的枚举与冗余导入
77 lines
3.0 KiB
Python
77 lines
3.0 KiB
Python
from typing import Any
|
||
|
||
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
||
from app.api.v1.module_system.dept.crud import DeptCRUD
|
||
from app.api.v1.module_system.menu.crud import MenuCRUD
|
||
from app.core.base_crud import CRUDBase
|
||
from app.core.base_schema import AuthSchema
|
||
from app.core.exceptions import CustomException
|
||
|
||
from .model import RoleModel
|
||
from .schema import RoleCreateSchema, RoleUpdateSchema
|
||
|
||
|
||
class RoleCRUD(CRUDBase[RoleModel, RoleCreateSchema, RoleUpdateSchema]):
|
||
"""角色模块数据层"""
|
||
|
||
def __init__(self, auth: AuthSchema, db: AsyncSession) -> None:
|
||
super().__init__(model=RoleModel, auth=auth, db=db)
|
||
|
||
async def set_role_menus_crud(self, role_ids: list[int], menu_ids: list[int]) -> None:
|
||
"""设置角色的菜单权限
|
||
|
||
参数:
|
||
- role_ids (list[int]): 角色ID列表
|
||
- menu_ids (list[int]): 菜单ID列表
|
||
|
||
返回:
|
||
- None
|
||
"""
|
||
if not role_ids:
|
||
raise CustomException(msg="角色ID列表不能为空")
|
||
|
||
roles = await self.get_list(search={"id": ("in", role_ids)})
|
||
# 校验:传入的 role_ids 必须全部存在(否则容易被 IDOR silent no-op)
|
||
if len(roles) != len(set(role_ids)):
|
||
missing = sorted(set(role_ids) - {r.id for r in roles})
|
||
raise CustomException(msg=f"角色不存在: {missing}")
|
||
|
||
menus = [] if not menu_ids else await MenuCRUD(self.auth, self.db).get_list(search={"id": ("in", menu_ids)})
|
||
|
||
# 校验:传入的所有菜单必须存在
|
||
if menu_ids and len(menus) != len(set(menu_ids)):
|
||
missing = sorted(set(menu_ids) - {m.id for m in menus})
|
||
raise CustomException(msg=f"菜单不存在: {missing}")
|
||
|
||
for obj in roles:
|
||
obj.menus.clear()
|
||
obj.menus.extend(menus)
|
||
await self.db.flush()
|
||
|
||
async def set_role_depts_crud(self, role_ids: list[int], dept_ids: list[int]) -> None:
|
||
"""设置角色的部门权限(含存在性校验)"""
|
||
if not role_ids:
|
||
raise CustomException(msg="角色ID列表不能为空")
|
||
|
||
roles = await self.get_list(search={"id": ("in", role_ids)})
|
||
if len(roles) != len(set(role_ids)):
|
||
missing = sorted(set(role_ids) - {r.id for r in roles})
|
||
raise CustomException(msg=f"角色不存在: {missing}")
|
||
|
||
depts = [] if not dept_ids else await DeptCRUD(self.auth, self.db).get_list(search={"id": ("in", dept_ids)})
|
||
if dept_ids and len(depts) != len(set(dept_ids)):
|
||
missing = sorted(set(dept_ids) - {d.id for d in depts})
|
||
raise CustomException(msg=f"部门不存在: {missing}")
|
||
|
||
for obj in roles:
|
||
relationship = obj.depts
|
||
relationship.clear()
|
||
relationship.extend(depts)
|
||
await self.db.flush()
|
||
|
||
async def get_options(self) -> list[dict[str, Any]]:
|
||
"""获取角色下拉选项,返回 [{value, label}](自动按状态过滤)"""
|
||
items = await self.get_list(search={"status": 0})
|
||
return [{"value": item.id, "label": item.name} for item in items]
|