1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-01 08:26:03 +00:00

fix(admin): a block deletes the user's API tokens

A blocked user's tokens were refused while the block lasted, but an
unblock brought them back: blocking a taken-over account and opening it
again gave its tokens back to whoever had made them. Blocking, from the
user list or by unticking Active on the edit form, now deletes them, as
an administrator's password reset does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-10 08:01:51 +03:00
parent 135839bd66
commit 8c87fe8d32
2 changed files with 20 additions and 4 deletions

View file

@ -167,9 +167,11 @@ def user_update(request, pk):
user_form.add_error(None, OWN_ACCOUNT)
valid = False
if valid:
with audit(request, None, subject, "change") as entry:
with audit(request, None, subject, "change") as entry, transaction.atomic():
user_form.save()
attributes_form.save()
if not user.is_active: # as user_block
ApiToken.objects.filter(user=user).delete()
fields = set(changed_fields(before, snapshot(user))) | set(changed_fields(before_attributes, snapshot(attributes)))
fields = sorted(fields - {"password", "last_login"})
entry.action = "change " + ", ".join(fields)
@ -247,10 +249,12 @@ def user_block(request, pk):
if user == request.user:
messages.error(request, OWN_ACCOUNT)
return redirect("admin:user_list")
with audit(request, None, f"User {user.username}", "block") as entry:
with audit(request, None, f"User {user.username}", "block") as entry, transaction.atomic():
entry.skip = not user.is_active # already blocked
user.is_active = False
user.save()
# an unblock must not bring back the tokens of someone who took the account over
ApiToken.objects.filter(user=user).delete()
return redirect("admin:user_list")