feat(weibo): 支持管理员强制转入工作台且不影响原使用人

This commit is contained in:
yml
2026-11-12 12:05:26 +08:00
parent 11b5a4d938
commit 31045e0f2e
11 changed files with 604 additions and 56 deletions
+130 -33
View File
@@ -313,8 +313,18 @@ def list_workbench_pool(
"""查看可加入当前用户工作台、且尚未加入的账号池账号。"""
_require_weibo_perm(current, "weibo:workbench")
occupied = db.query(WeiboWorkbenchAccount.account_id)
pool_query = db.query(WeiboAccount).filter(WeiboAccount.id.not_in(occupied))
if current.role == "super_admin":
selected = db.query(WeiboWorkbenchAccount.account_id).filter(
WeiboWorkbenchAccount.user_id == current.id
)
pool_query = db.query(WeiboAccount).filter(WeiboAccount.id.not_in(selected))
else:
exclusively_occupied = db.query(WeiboWorkbenchAccount.account_id).filter(
WeiboWorkbenchAccount.exclusive_slot.is_not(None)
)
pool_query = db.query(WeiboAccount).filter(
WeiboAccount.id.not_in(exclusively_occupied)
)
if not _can_view_all_weibo_accounts(current):
pool_query = pool_query.filter(WeiboAccount.assigned_to == current.id)
query = _apply_account_filters(
@@ -339,6 +349,11 @@ def import_workbench_accounts_by_text(
"""按 UID/用户名/角色批量解析账号并加入当前用户工作台。"""
_require_weibo_perm(current, "weibo:workbench")
if req.force_add_occupied and current.role != "super_admin":
raise HTTPException(
status_code=403,
detail="仅超级管理员可以强制转入其他人的工作台账号",
)
identifiers: list[str] = []
for token in re.split(r"[\s,,;;]+", req.text.strip()):
value = token.strip()
@@ -373,18 +388,42 @@ def import_workbench_accounts_by_text(
by_nickname = grouped("nickname")
by_role = grouped("game_role_name")
account_ids = [account.id for account in accounts]
occupancy = {
account_id: user_id
for account_id, user_id in db.query(
memberships: dict[int, list[tuple[int, str, int | None]]] = {}
for account_id, user_id, username, exclusive_slot in (
db.query(
WeiboWorkbenchAccount.account_id,
WeiboWorkbenchAccount.user_id,
User.username,
WeiboWorkbenchAccount.exclusive_slot,
)
.outerjoin(User, User.id == WeiboWorkbenchAccount.user_id)
.filter(WeiboWorkbenchAccount.account_id.in_(account_ids))
.all()
):
memberships.setdefault(account_id, []).append(
(user_id, username or "", exclusive_slot)
)
current_member_ids = {
account_id
for account_id, rows in memberships.items()
if any(user_id == current.id for user_id, _username, _slot in rows)
}
exclusive_owners = {
account_id: next(
(
(user_id, username)
for user_id, username, exclusive_slot in rows
if exclusive_slot is not None
),
None,
)
for account_id, rows in memberships.items()
}
items: list[WeiboWorkbenchBatchImportItem] = []
to_add: list[WeiboAccount] = []
to_add: list[tuple[WeiboAccount, int | None]] = []
forced_accounts: list[WeiboAccount] = []
planned_accounts: list[WeiboAccount] = []
planned_ids: set[int] = set()
for identifier in identifiers:
candidates = by_uid.get(identifier) or by_nickname.get(identifier) or []
@@ -410,13 +449,19 @@ def import_workbench_accounts_by_text(
continue
account = candidates[0]
exclusive_owner = exclusive_owners.get(account.id)
base = {
"identifier": identifier,
"account_id": account.id,
"uid": account.uid,
"nickname": account.nickname,
"occupied_by_user_id": (
exclusive_owner[0] if exclusive_owner is not None else None
),
"occupied_by_username": (
exclusive_owner[1] if exclusive_owner is not None else ""
),
}
occupant = occupancy.get(account.id)
if not account.is_enabled:
items.append(
WeiboWorkbenchBatchImportItem(
@@ -425,22 +470,6 @@ def import_workbench_accounts_by_text(
**base,
)
)
elif occupant is not None and occupant != current.id:
items.append(
WeiboWorkbenchBatchImportItem(
status="unavailable",
message="账号已在其他人的工作台中使用",
**base,
)
)
elif occupant == current.id:
items.append(
WeiboWorkbenchBatchImportItem(
status="already_added",
message="账号已在本工作台",
**base,
)
)
elif account.id in planned_ids:
items.append(
WeiboWorkbenchBatchImportItem(
@@ -449,9 +478,51 @@ def import_workbench_accounts_by_text(
**base,
)
)
elif account.id in current_member_ids:
items.append(
WeiboWorkbenchBatchImportItem(
status="already_added",
message="账号已在本工作台",
**base,
)
)
elif exclusive_owner is not None and exclusive_owner[0] != current.id:
if req.force_add_occupied:
planned_ids.add(account.id)
to_add.append((account, None))
forced_accounts.append(account)
planned_accounts.append(account)
items.append(
WeiboWorkbenchBatchImportItem(
status="forced_added",
message=(
f"已强制转入,{exclusive_owner[1] or '原使用人'}"
"的工作台保持不变"
),
**base,
)
)
continue
items.append(
WeiboWorkbenchBatchImportItem(
status="unavailable",
message=(
f"账号已在 {exclusive_owner[1]} 的工作台中使用"
if exclusive_owner[1]
else "账号已在其他人的工作台中使用"
),
**base,
)
)
else:
planned_ids.add(account.id)
to_add.append(account)
to_add.append(
(
account,
None if current.role == "super_admin" else 1,
)
)
planned_accounts.append(account)
items.append(
WeiboWorkbenchBatchImportItem(
status="added",
@@ -460,11 +531,23 @@ def import_workbench_accounts_by_text(
)
)
forced_details = [
{
"account_id": account.id,
"existing_owner_user_id": exclusive_owners[account.id][0],
"existing_owner_username": exclusive_owners[account.id][1],
}
for account in forced_accounts
]
try:
db.add_all(
[
WeiboWorkbenchAccount(user_id=current.id, account_id=account.id)
for account in to_add
WeiboWorkbenchAccount(
user_id=current.id,
account_id=account.id,
exclusive_slot=exclusive_slot,
)
for account, exclusive_slot in to_add
]
)
db.commit()
@@ -474,24 +557,31 @@ def import_workbench_accounts_by_text(
status_code=409,
detail="部分账号刚被其他工作台占用,请刷新后重试",
) from exc
if to_add:
if planned_accounts:
_record_audit_safely(
db,
current,
action="weibo:workbench:batch_import",
target=f"weibo_user:{current.id}",
detail={"account_ids": [account.id for account in to_add]},
detail={
"account_ids": [account.id for account in planned_accounts],
"added_account_ids": [account.id for account, _slot in to_add],
"forced_added_account_ids": [account.id for account in forced_accounts],
"existing_owners": forced_details,
},
)
added = sum(item.status == "added" for item in items)
forced_added = sum(item.status == "forced_added" for item in items)
already_added = sum(item.status == "already_added" for item in items)
failed = len(items) - added - already_added
failed = len(items) - added - forced_added - already_added
return WeiboWorkbenchBatchImportOut(
requested=len(identifiers),
added=added,
forced_added=forced_added,
already_added=already_added,
failed=failed,
account_ids=[account.id for account in to_add],
account_ids=[account.id for account in planned_accounts],
items=items,
)
@@ -567,11 +657,12 @@ def add_workbench_accounts(
for (account_id,) in db.query(WeiboWorkbenchAccount.account_id)
.filter(
WeiboWorkbenchAccount.user_id != current.id,
WeiboWorkbenchAccount.exclusive_slot.is_not(None),
WeiboWorkbenchAccount.account_id.in_(account_ids),
)
.all()
}
if taken:
if taken and current.role != "super_admin":
raise HTTPException(
status_code=409,
detail="部分账号已在其他人的工作台中使用,请先让原使用人移出",
@@ -587,7 +678,11 @@ def add_workbench_accounts(
}
db.add_all(
[
WeiboWorkbenchAccount(user_id=current.id, account_id=account_id)
WeiboWorkbenchAccount(
user_id=current.id,
account_id=account_id,
exclusive_slot=(None if current.role == "super_admin" else 1),
)
for account_id in account_ids
if account_id not in selected_ids
]
@@ -763,12 +858,14 @@ def batch_assign_accounts(
previous_assignments = {account.id: account.assigned_to for account in accounts}
if target is None:
db.query(WeiboWorkbenchAccount).filter(
WeiboWorkbenchAccount.account_id.in_(actual_ids)
WeiboWorkbenchAccount.account_id.in_(actual_ids),
WeiboWorkbenchAccount.exclusive_slot.is_not(None),
).delete(synchronize_session=False)
else:
db.query(WeiboWorkbenchAccount).filter(
WeiboWorkbenchAccount.account_id.in_(actual_ids),
WeiboWorkbenchAccount.user_id != target.id,
WeiboWorkbenchAccount.exclusive_slot.is_not(None),
).delete(synchronize_session=False)
for account in accounts:
account.assigned_to = target.id if target else None