fix(accounts): 安全删除账号并保护履约租约
This commit is contained in:
@@ -15,8 +15,6 @@ from ..models import (
|
||||
AccountEvent,
|
||||
AuditLog,
|
||||
DouyuTask,
|
||||
DouyuWorkbenchAccount,
|
||||
LoginTask,
|
||||
User,
|
||||
)
|
||||
from ..permissions import ROLE_LABELS, user_has_permission
|
||||
@@ -35,6 +33,7 @@ from ..schemas import (
|
||||
)
|
||||
from ..services.account_binding import bind_payload
|
||||
from ..services.account_capability_service import account_capability_counts
|
||||
from ..services.account_deletion import AccountDeleteBlocked, delete_accounts
|
||||
from ..services.account_events_service import (
|
||||
CATEGORY_OPS,
|
||||
EVENT_ASSIGNED,
|
||||
@@ -1474,21 +1473,10 @@ def batch_delete_accounts(
|
||||
if not ids:
|
||||
raise HTTPException(status_code=400, detail="无效的账号ID")
|
||||
|
||||
# 先删除关联的登录任务
|
||||
db.query(LoginTask).filter(LoginTask.account_id.in_(ids)).delete(
|
||||
synchronize_session=False
|
||||
)
|
||||
db.query(DouyuTask).filter(DouyuTask.account_id.in_(ids)).delete(
|
||||
synchronize_session=False
|
||||
)
|
||||
db.query(DouyuWorkbenchAccount).filter(
|
||||
DouyuWorkbenchAccount.account_id.in_(ids)
|
||||
).delete(synchronize_session=False)
|
||||
|
||||
# 删除账号
|
||||
deleted = (
|
||||
db.query(Account).filter(Account.id.in_(ids)).delete(synchronize_session=False)
|
||||
)
|
||||
try:
|
||||
deleted = delete_accounts(db, account_ids=ids)
|
||||
except AccountDeleteBlocked as exc:
|
||||
raise HTTPException(status_code=409, detail=str(exc)) from exc
|
||||
|
||||
# 账号删除会**连带删掉它的事件时间线**(外键),所以留下的痕迹只有审计。
|
||||
# 批量删时 account_id 无法表达 N 个账号,改成把删掉的 id 记进 detail ——
|
||||
@@ -1515,20 +1503,10 @@ def batch_delete_accounts_selection(
|
||||
if not ids:
|
||||
raise HTTPException(status_code=400, detail="请选择账号")
|
||||
|
||||
db.query(LoginTask).filter(LoginTask.account_id.in_(ids)).delete(
|
||||
synchronize_session=False
|
||||
)
|
||||
db.query(DouyuTask).filter(DouyuTask.account_id.in_(ids)).delete(
|
||||
synchronize_session=False
|
||||
)
|
||||
db.query(DouyuWorkbenchAccount).filter(
|
||||
DouyuWorkbenchAccount.account_id.in_(ids)
|
||||
).delete(synchronize_session=False)
|
||||
deleted = (
|
||||
_visible_accounts_query(db, current)
|
||||
.filter(Account.id.in_(ids))
|
||||
.delete(synchronize_session=False)
|
||||
)
|
||||
try:
|
||||
deleted = delete_accounts(db, account_ids=ids)
|
||||
except AccountDeleteBlocked as exc:
|
||||
raise HTTPException(status_code=409, detail=str(exc)) from exc
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
@@ -1918,21 +1896,10 @@ def delete_account(
|
||||
if not acc:
|
||||
raise HTTPException(status_code=404, detail="账号不存在")
|
||||
|
||||
# 先删除关联的登录任务,避免外键约束失败
|
||||
db.query(LoginTask).filter(LoginTask.account_id == account_id).delete(
|
||||
synchronize_session=False
|
||||
)
|
||||
db.query(DouyuTask).filter(DouyuTask.account_id == account_id).delete(
|
||||
synchronize_session=False
|
||||
)
|
||||
db.query(DouyuWorkbenchAccount).filter(
|
||||
DouyuWorkbenchAccount.account_id == account_id
|
||||
).delete(synchronize_session=False)
|
||||
# 事件表有指向 accounts 的外键,不清会直接删不掉账号。
|
||||
# 账号都没了,它的时间线也没有保留意义(审计仍在 audit_logs 里,那份不带外键)。
|
||||
db.query(AccountEvent).filter(AccountEvent.account_id == account_id).delete(
|
||||
synchronize_session=False
|
||||
)
|
||||
try:
|
||||
delete_accounts(db, account_ids=[account_id])
|
||||
except AccountDeleteBlocked as exc:
|
||||
raise HTTPException(status_code=409, detail=str(exc)) from exc
|
||||
|
||||
db.add(
|
||||
AuditLog(
|
||||
@@ -1943,6 +1910,5 @@ def delete_account(
|
||||
account_id=account_id,
|
||||
)
|
||||
)
|
||||
db.delete(acc)
|
||||
db.commit()
|
||||
return {"message": "已删除", "success": True}
|
||||
|
||||
Reference in New Issue
Block a user