优化客服账号标签管理
This commit is contained in:
@@ -92,6 +92,15 @@ def _selected_account_ids(db: Session, current: User, req: AccountBulkSelection)
|
||||
return [account_id for account_id in ids if account_id in allowed]
|
||||
|
||||
|
||||
def _require_account_tag_permission(current: User) -> None:
|
||||
"""标签权限兼容历史上使用账号导入权限的运营人员。"""
|
||||
if not (
|
||||
user_has_permission(current, "account:tag")
|
||||
or user_has_permission(current, "account:import")
|
||||
):
|
||||
raise HTTPException(status_code=403, detail="无权修改账号标签")
|
||||
|
||||
|
||||
@router.get("")
|
||||
def list_accounts(
|
||||
assigned_only: bool = Query(False),
|
||||
@@ -323,13 +332,16 @@ def set_account_tag(
|
||||
account_id: int,
|
||||
req: AccountTag,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("account:import")),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""设置单个账号标签。"""
|
||||
acc = db.query(Account).filter(Account.id == account_id).first()
|
||||
"""设置单个可见账号的标签。"""
|
||||
_require_account_tag_permission(current)
|
||||
acc = _visible_accounts_query(db, current).filter(Account.id == account_id).first()
|
||||
if not acc:
|
||||
raise HTTPException(status_code=404, detail="账号不存在")
|
||||
acc.tag = (req.tag or "").strip()
|
||||
db.add(AuditLog(user_id=current.id, username=current.username,
|
||||
action="account:tag", target=acc.username, detail=acc.tag))
|
||||
db.commit()
|
||||
return {"message": "标签已更新", "success": True}
|
||||
|
||||
@@ -338,13 +350,14 @@ def set_account_tag(
|
||||
def batch_tag(
|
||||
req: AccountTag,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("account:import")),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""批量设置账号标签。"""
|
||||
_require_account_tag_permission(current)
|
||||
if not req.account_ids:
|
||||
raise HTTPException(status_code=400, detail="请选择账号")
|
||||
tag = (req.tag or "").strip()
|
||||
count = db.query(Account).filter(Account.id.in_(req.account_ids)).update(
|
||||
count = _visible_accounts_query(db, current).filter(Account.id.in_(req.account_ids)).update(
|
||||
{Account.tag: tag}, synchronize_session=False
|
||||
)
|
||||
db.commit()
|
||||
@@ -355,9 +368,10 @@ def batch_tag(
|
||||
def batch_tag_selection(
|
||||
req: AccountBulkTag,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("account:import")),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""按显式选择或当前筛选结果批量设置账号标签。"""
|
||||
_require_account_tag_permission(current)
|
||||
ids = _selected_account_ids(db, current, req)
|
||||
if not ids:
|
||||
raise HTTPException(status_code=400, detail="请选择账号")
|
||||
|
||||
@@ -94,6 +94,12 @@ def _require_huya_perm(user: User, permission: str) -> None:
|
||||
raise HTTPException(status_code=403, detail="权限不足")
|
||||
|
||||
|
||||
def _require_huya_tag_permission(user: User) -> None:
|
||||
"""标签权限兼容历史上使用导入权限的运营人员。"""
|
||||
if not (_has_huya_perm(user, "huya:tag") or _has_huya_perm(user, "huya:import")):
|
||||
raise HTTPException(status_code=403, detail="无权修改虎牙账号标签")
|
||||
|
||||
|
||||
def _can_view_huya_all(user: User) -> bool:
|
||||
return _has_huya_perm(user, "huya:view_all")
|
||||
|
||||
@@ -1064,8 +1070,8 @@ def set_account_tag(
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""设置单个虎牙账号标签。"""
|
||||
_require_huya_perm(current, "huya:import")
|
||||
account = db.query(HuyaAccount).filter(HuyaAccount.id == account_id).first()
|
||||
_require_huya_tag_permission(current)
|
||||
account = _visible_huya_accounts_query(db, current).filter(HuyaAccount.id == account_id).first()
|
||||
if not account:
|
||||
raise HTTPException(status_code=404, detail="账号不存在")
|
||||
account.tag = (req.tag or "").strip()
|
||||
@@ -1080,11 +1086,11 @@ def batch_tag(
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""批量设置虎牙账号标签。"""
|
||||
_require_huya_perm(current, "huya:import")
|
||||
_require_huya_tag_permission(current)
|
||||
if not req.account_ids:
|
||||
raise HTTPException(status_code=400, detail="请选择账号")
|
||||
tag = (req.tag or "").strip()
|
||||
count = db.query(HuyaAccount).filter(HuyaAccount.id.in_(req.account_ids)).update(
|
||||
count = _visible_huya_accounts_query(db, current).filter(HuyaAccount.id.in_(req.account_ids)).update(
|
||||
{HuyaAccount.tag: tag},
|
||||
synchronize_session=False,
|
||||
)
|
||||
@@ -1099,12 +1105,12 @@ def batch_tag_selection(
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""按显式选择或当前筛选结果批量设置虎牙账号标签。"""
|
||||
_require_huya_perm(current, "huya:import")
|
||||
_require_huya_tag_permission(current)
|
||||
ids = _selected_huya_account_ids(db, current, req)
|
||||
if not ids:
|
||||
raise HTTPException(status_code=400, detail="请选择虎牙账号")
|
||||
tag = (req.tag_value or "").strip()
|
||||
count = db.query(HuyaAccount).filter(HuyaAccount.id.in_(ids)).update(
|
||||
count = _visible_huya_accounts_query(db, current).filter(HuyaAccount.id.in_(ids)).update(
|
||||
{HuyaAccount.tag: tag},
|
||||
synchronize_session=False,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user