Files
lanhongguangandClaude Sonnet 4.6 dfa1eed726 feat: 审批员权限分区、资源库重构与UI全面优化
权限与角色:
- 审批员自动拥有其管辖卡(allowed_modem_ids)的使用权限,无需申请
- 审批员申请列表按 allowed_modem_ids 过滤,只显示其管辖范围内的申请
- 头部及个人信息页显示实际 RBAC 角色名,而非固定"普通用户"

资源库(ResourceLibrary):
- 新增独立页面展示所有 SIM 卡,支持申请权限
- getEffectiveStatus() 单一函数统一计算各角色的卡访问状态
- Modal 改用 createPortal 渲染至 document.body,修复 CSS stacking context 问题

任务监控:
- /admin/tasks 对所有非只读用户开放,管理员看全部,普通用户只看自己的
- 菜单文案区分:管理员显示"任务监控",普通用户显示"我的任务记录"

导航与页面:
- 移除"我的SIM权限"菜单,功能并入资源库
- 短信记录对所有登录用户可见
- SIM卡管理空状态区分管理员和普通用户提示语
- 新增 RequireApprove 路由守卫

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-25 03:02:02 +08:00

80 lines
3.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List
from app.core.database import get_db
from app.core.security import get_current_user, require_admin, hash_password, verify_password
from app.models.user import User
from app.schemas.user import UserCreate, UserUpdate, UserOut, PasswordChange, AdminPasswordReset
from app.services.notify import push
router = APIRouter(prefix="/users", tags=["users"])
@router.get("/", response_model=List[UserOut], dependencies=[Depends(require_admin)])
def list_users(db: Session = Depends(get_db)):
return db.query(User).order_by(User.id).all()
@router.post("/", response_model=UserOut, dependencies=[Depends(require_admin)])
def create_user(data: UserCreate, db: Session = Depends(get_db)):
if db.query(User).filter(User.username == data.username).first():
raise HTTPException(status_code=400, detail="用户名已存在")
if len(data.password) < 6:
raise HTTPException(status_code=400, detail="密码至少 6 位")
user = User(username=data.username, password_hash=hash_password(data.password), role=data.role)
db.add(user)
db.commit()
db.refresh(user)
push("new_user", "新用户注册", f"新用户 {user.username} 已创建(角色:{user.role.value})")
return user
@router.patch("/{user_id}", response_model=UserOut, dependencies=[Depends(require_admin)])
def update_user(user_id: int, data: UserUpdate, db: Session = Depends(get_db)):
user = db.query(User).filter(User.id == user_id).first()
if not user:
raise HTTPException(status_code=404, detail="用户不存在")
if data.role is not None:
user.role = data.role
if data.is_active is not None:
user.is_active = data.is_active
db.commit()
db.refresh(user)
return user
@router.delete("/{user_id}", dependencies=[Depends(require_admin)])
def delete_user(user_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
if user_id == current_user.id:
raise HTTPException(status_code=400, detail="不能删除自己")
user = db.query(User).filter(User.id == user_id).first()
if not user:
raise HTTPException(status_code=404, detail="用户不存在")
db.delete(user)
db.commit()
return {"ok": True}
@router.post("/{user_id}/reset-password", response_model=UserOut, dependencies=[Depends(require_admin)])
def reset_password(user_id: int, data: AdminPasswordReset, db: Session = Depends(get_db)):
user = db.query(User).filter(User.id == user_id).first()
if not user:
raise HTTPException(status_code=404, detail="用户不存在")
if len(data.new_password) < 6:
raise HTTPException(status_code=400, detail="密码至少 6 位")
user.password_hash = hash_password(data.new_password)
db.commit()
db.refresh(user)
return user
@router.post("/me/change-password")
def change_password(data: PasswordChange, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
if not verify_password(data.old_password, current_user.password_hash):
raise HTTPException(status_code=400, detail="原密码错误")
if len(data.new_password) < 6:
raise HTTPException(status_code=400, detail="密码至少 6 位")
current_user.password_hash = hash_password(data.new_password)
db.commit()
return {"ok": True}