fix: sync removes users that no longer exist on Volcengine
- Track all Volcengine usernames during sync - Delete local users not found on Volcengine (cascade deletes related data) - Report removed users in sync response - Deleted test_audit and tudouceshi from local DB Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
610058ae5f
commit
6b3a0bda34
@ -184,6 +184,7 @@ def iam_user_sync_view(request):
|
||||
|
||||
svc = IAMService(ak, sk)
|
||||
imported = []
|
||||
volc_usernames = set()
|
||||
offset = 0
|
||||
|
||||
while True:
|
||||
@ -199,6 +200,7 @@ def iam_user_sync_view(request):
|
||||
|
||||
for u in users:
|
||||
username = u.get("UserName", "")
|
||||
volc_usernames.add(username)
|
||||
obj, created = IAMUser.objects.update_or_create(
|
||||
volc_account=account,
|
||||
username=username,
|
||||
@ -253,10 +255,22 @@ def iam_user_sync_view(request):
|
||||
if offset >= total:
|
||||
break
|
||||
|
||||
# 删除火山已不存在的用户(本地有但火山没有)
|
||||
removed = []
|
||||
local_users = IAMUser.objects.filter(volc_account=account)
|
||||
for local_user in local_users:
|
||||
if local_user.username not in volc_usernames:
|
||||
removed.append(local_user.username)
|
||||
local_user.delete()
|
||||
|
||||
total_count = IAMUser.objects.filter(volc_account=account).count()
|
||||
msg = f'同步完成,共 {total_count} 个用户,新导入 {len(imported)} 个'
|
||||
if removed:
|
||||
msg += f',清理 {len(removed)} 个已删除用户({", ".join(removed)})'
|
||||
return Response({
|
||||
'message': f'同步完成,共 {total_count} 个用户,新导入 {len(imported)} 个',
|
||||
'message': msg,
|
||||
'imported': imported,
|
||||
'removed': removed,
|
||||
'total': total_count,
|
||||
})
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user