fix: 添加角色增加菜单id”
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from fastapi import Query
|
||||
|
||||
from dbhelper.menu import del_menu, get_menus, insert_menu, put_menu
|
||||
from core.utils import list_to_tree
|
||||
from dbhelper.menu import del_menu, get_menus, get_tree_menu, insert_menu, put_menu
|
||||
from schemas import ListAll, MenuIn, MenuRead, Response
|
||||
|
||||
|
||||
@@ -11,10 +12,9 @@ async def menu_add(data: MenuIn) -> Response[MenuRead]:
|
||||
async def menu_arr(
|
||||
offset: int = Query(default=1, description="偏移量"),
|
||||
limit: int = Query(default=10, description="数量"),
|
||||
) -> Response[ListAll[list[MenuRead]]]:
|
||||
skip = (offset - 1) * limit
|
||||
menus, count = await get_menus(skip, limit)
|
||||
return Response(data=ListAll(total=count, items=menus))
|
||||
) -> Response:
|
||||
menus = await get_tree_menu()
|
||||
return Response(data=list_to_tree(menus))
|
||||
|
||||
|
||||
async def menu_del(pk: int) -> Response:
|
||||
|
@@ -16,7 +16,9 @@ from schemas import ListAll, Response, RoleIn, RoleInfo, RoleMenuIn, RoleQuery,
|
||||
|
||||
|
||||
async def role_add(data: RoleIn) -> Response[RoleInfo]:
|
||||
return Response(data=await new_role(data))
|
||||
if result := await new_role(data):
|
||||
return Response(data=result)
|
||||
return Response(code=400, msg="菜单不存在")
|
||||
|
||||
|
||||
async def role_has_menu(rid: int):
|
||||
|
@@ -19,10 +19,10 @@ async def user_add(data: UserAdd) -> Response[UserRead]:
|
||||
"""新增用户并分配角色 一步到位"""
|
||||
if await get_user({"username": data.username}) is not None:
|
||||
return Response(code=400, msg="用户名已存在")
|
||||
roles = data.rids
|
||||
del data.rids
|
||||
rids = data.roles
|
||||
del data.roles
|
||||
data.password = get_password_hash(data.password)
|
||||
result = await insert_user(data, roles)
|
||||
result = await insert_user(data, rids)
|
||||
if isinstance(result, int):
|
||||
return Response(code=400, msg=f"角色{result}不存在")
|
||||
return Response(data=result)
|
||||
|
@@ -25,7 +25,11 @@ async def get_menus(skip: int, limit: int, kwargs: dict = None):
|
||||
else:
|
||||
kwargs = {}
|
||||
result = MenuModel.filter(status__not=9, **kwargs).all().order_by("-created")
|
||||
return await result.offset(skip).limit(limit), await result.count()
|
||||
return await result.offset(skip).limit(limit)
|
||||
|
||||
|
||||
async def get_tree_menu():
|
||||
return await MenuModel.filter(status__not=9).all().values()
|
||||
|
||||
|
||||
async def get_menu(kwargs):
|
||||
|
@@ -1,6 +1,6 @@
|
||||
from tortoise import connections
|
||||
|
||||
from models import RoleModel
|
||||
from models import MenuModel, RoleMenuModel, RoleModel
|
||||
from schemas.role import RoleIn
|
||||
|
||||
|
||||
@@ -20,7 +20,16 @@ async def get_role_menus(rid: int):
|
||||
|
||||
async def new_role(role: RoleIn):
|
||||
"""新增角色"""
|
||||
return await RoleModel.create(**role.dict())
|
||||
# 校验菜单是否存在
|
||||
if not all([await MenuModel.filter(id=mid).first() for mid in role.menus]):
|
||||
return False
|
||||
|
||||
obj = await RoleModel.create(name=role.name, remark=role.remark)
|
||||
# 写入菜单
|
||||
await RoleMenuModel.bulk_create(
|
||||
[RoleMenuModel(rid=obj.id, mid=mid) for mid in role.menus]
|
||||
)
|
||||
return obj
|
||||
|
||||
|
||||
async def get_roles(skip: int, limit: int, kwargs: dict = None):
|
||||
|
@@ -32,19 +32,9 @@ async def get_user_info(user: UserModel):
|
||||
[user.id],
|
||||
)
|
||||
|
||||
# 当前激活角色的按钮权限列表
|
||||
perm_result = await db.execute_query_dict(
|
||||
"""
|
||||
select m.identifier from sys_role_menu as rm, sys_menu as m where rm.mid = m.id and rm.rid = (?) and
|
||||
m.identifier not null and m.identifier != "" and rm.status != 9
|
||||
""",
|
||||
[sql_result[0]["id"]],
|
||||
)
|
||||
|
||||
return {
|
||||
**jsonable_encoder(user),
|
||||
"roles": sql_result,
|
||||
"permissions": [i["identifier"] for i in perm_result],
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +54,6 @@ async def get_users(skip: int, limit: int, kwargs: dict = None):
|
||||
else:
|
||||
kwargs = {}
|
||||
result = UserModel.filter(**kwargs).all().order_by("-created")
|
||||
print(await result.offset(skip).limit(limit))
|
||||
return await result.offset(skip).limit(limit), await result.count()
|
||||
|
||||
|
||||
|
BIN
backend/mini.db
BIN
backend/mini.db
Binary file not shown.
BIN
backend/mini.db-shm
Normal file
BIN
backend/mini.db-shm
Normal file
Binary file not shown.
BIN
backend/mini.db-wal
Normal file
BIN
backend/mini.db-wal
Normal file
Binary file not shown.
@@ -20,7 +20,7 @@ class RoleBasic(BaseModel):
|
||||
|
||||
|
||||
class RoleIn(RoleBasic):
|
||||
pass
|
||||
menus: list[int] = Field(..., description="菜单id列表")
|
||||
|
||||
|
||||
class RoleRead(RoleBasic, ReadBase):
|
||||
|
@@ -41,7 +41,6 @@ class UserInfo(UserRead):
|
||||
"""用户信息模型"""
|
||||
|
||||
roles: list[UserHasRole] = Field(..., description="用户拥有角色")
|
||||
permissions: list[str] = Field(..., description="角色拥有的按钮权限标识")
|
||||
|
||||
|
||||
class RoleActive(BaseModel):
|
||||
@@ -52,7 +51,7 @@ class RoleActive(BaseModel):
|
||||
class UserAdd(UserIn):
|
||||
"""新增用户模型"""
|
||||
|
||||
rids: list[RoleActive] = Field(..., description="选择角色列表")
|
||||
roles: list[RoleActive] = Field(..., description="选择角色列表")
|
||||
|
||||
|
||||
class UserQuery(QueryData):
|
||||
|
Reference in New Issue
Block a user