修改后台权限的问题

This commit is contained in:
2026-07-13 17:23:56 +08:00
parent 6039b5ee75
commit 6ff8866ca3
2 changed files with 33 additions and 2 deletions
+31
View File
@@ -115,3 +115,34 @@ async def get_backend_user(
detail="需要后台用户权限",
)
return current_user
def require_menu_access(menu_path: str):
"""检查后台用户是否有指定菜单权限。
- 超级管理员 (is_admin=True): 直接放行
- 非管理员后台用户: 检查 allowed_menus 是否包含 menu_path
- 非后台用户: 403 拒绝
用法:
@router.get("/stats")
async def get_stats(admin: User = Depends(require_menu_access("/")), ...):
"""
async def _dependency(
current_user: User = Depends(get_current_user_allow_password_pending),
) -> User:
if current_user.user_type != "admin":
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="需要后台用户权限",
)
if current_user.is_admin:
return current_user
allowed = set(current_user.allowed_menus or [])
if menu_path not in allowed:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="需要管理员权限",
)
return current_user
return _dependency