mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 12:52:26 +00:00
本次提交包含多项优化: 1. 移除大量冗余的文件头注释与过时的from __future__导入 2. 将CRUD的list方法统一重命名为get_list保持接口一致 3. 修复前后端状态字段类型不匹配问题,将string类型status改为number 4. 修正前端文案错别字,将"代办事项"修正为标准写法 5. 更新sqlalchemy版本并调整依赖配置 6. 新增缓存工具类替代fastapi-cache2,重构缓存调用逻辑 7. 新增开源授权函生成相关工具与数据库字段支持 8. 为多个业务模块添加防重复提交loading状态 9. 修复邮件模型的外键关联缺失问题 10. 优化pdf生成工具的导入时机与文档注释
65 lines
2.3 KiB
Python
65 lines
2.3 KiB
Python
from app.api.v1.module_platform.menu.crud import MenuCRUD
|
|
from app.api.v1.module_system.dept.crud import DeptCRUD
|
|
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) -> None:
|
|
super().__init__(model=RoleModel, auth=auth)
|
|
|
|
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
|
|
"""
|
|
roles = await self.get_list(search={"id": ("in", role_ids)})
|
|
menus = [] if not menu_ids else await MenuCRUD(self.auth).get_list(search={"id": ("in", menu_ids)})
|
|
|
|
from app.api.v1.module_platform.package.service import PackageService
|
|
|
|
if self.auth.user and not self.auth.user.is_superuser and self.auth.tenant_id:
|
|
allowed_menu_ids = await PackageService.get_tenant_available_menu_ids(self.auth, self.auth.tenant_id)
|
|
allowed_set = set(allowed_menu_ids)
|
|
for menu in menus:
|
|
if int(menu.id) not in allowed_set:
|
|
raise CustomException(msg=f"菜单[{menu.name}]不在当前租户的功能组内,无法分配")
|
|
|
|
for obj in roles:
|
|
relationship = obj.menus
|
|
relationship.clear()
|
|
relationship.extend(menus)
|
|
await self.auth.db.flush()
|
|
|
|
async def set_role_depts_crud(self, role_ids: list[int], dept_ids: list[int]) -> None:
|
|
"""
|
|
设置角色的部门权限
|
|
|
|
参数:
|
|
- role_ids (list[int]): 角色ID列表
|
|
- dept_ids (list[int]): 部门ID列表
|
|
|
|
返回:
|
|
- None
|
|
"""
|
|
roles = await self.get_list(search={"id": ("in", role_ids)})
|
|
depts = [] if not dept_ids else await DeptCRUD(self.auth).get_list(search={"id": ("in", dept_ids)})
|
|
|
|
for obj in roles:
|
|
relationship = obj.depts
|
|
relationship.clear()
|
|
relationship.extend(depts)
|
|
await self.auth.db.flush()
|