40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
|
from typing import List
|
||
|
|
||
|
from fastapi import APIRouter, Depends, HTTPException
|
||
|
from sqlalchemy.orm import Session
|
||
|
|
||
|
from AppUser.Crud import RoleCrud
|
||
|
from AppUser.Model import RoleModel
|
||
|
from AppUser.Schemas import RoleSchema
|
||
|
|
||
|
from Utils.DataBase.SqlAlchemyUtils import get_db, engine
|
||
|
|
||
|
RoleModel.Base.metadata.create_all(bind=engine)
|
||
|
|
||
|
router = APIRouter(
|
||
|
tags=["角色"],
|
||
|
prefix="/api/user/role"
|
||
|
)
|
||
|
|
||
|
|
||
|
@router.get("/view/{role_id}", summary="查看角色", response_model=RoleSchema.RoleFullResBody)
|
||
|
def func01(role_id: str, db: Session = Depends(get_db)):
|
||
|
role_obj = RoleCrud.get_role_by_id(db=db, role_id=role_id)
|
||
|
if not role_obj:
|
||
|
raise HTTPException(status_code=202, detail="Role Not Found")
|
||
|
return role_obj.to_dict()
|
||
|
|
||
|
|
||
|
@router.post("/role_access_policy/view", summary="查看角色权限策略", response_model=List[RoleSchema.RoleAccessPolicyResBody])
|
||
|
def func02(name: str, db: Session = Depends(get_db)):
|
||
|
role_obj = RoleCrud.get_role_by_name(db=db, name=name)
|
||
|
if not role_obj:
|
||
|
raise HTTPException(status_code=202, detail="Role Not Found")
|
||
|
return [RoleSchema.RoleAccessPolicyResBody(**rap).dict() for rap in role_obj.to_dict().get("raps")]
|
||
|
|
||
|
|
||
|
@router.get("/list", summary="角色列表")
|
||
|
def func03(db: Session = Depends(get_db)):
|
||
|
role_list = RoleCrud.get_role_list(db=db)
|
||
|
return role_list
|