1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +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

@ -3,11 +3,11 @@ from django.contrib.auth.models import Group, Permission, User
from django.core.exceptions import ObjectDoesNotExist
from django.db import connection
from django.shortcuts import reverse
from django.test import TestCase
from django.test import Client, TestCase
from django.test.utils import CaptureQueriesContext
from lxml import html
from accounts.models import UserAttributes
from accounts.models import ApiToken, UserAttributes
from logs.models import Logs
@ -99,6 +99,18 @@ class AdminTestCase(TestCase):
User.objects.create_user(f"more-{i}").user_permissions.add(clone)
self.assertEqual(rows()[1], count)
def test_a_block_deletes_the_api_tokens_an_unblock_would_bring_back(self):
user = User.objects.create_user("taken", password="Wvc-taken-9")
for block in (lambda: self.client.post(reverse("admin:user_block", args=[user.id])),
lambda: self.client.post(reverse("admin:user_update", args=[user.id]),
{"username": "taken", "role": "user", "max_instances": 1, "max_cpus": 1,
"max_memory": 1, "max_disk_size": 1})):
_, token = ApiToken.objects.create(user)
self.assertEqual(block().status_code, 302)
self.client.post(reverse("admin:user_unblock", args=[user.id]))
self.assertEqual(Client().get("/api/v1/instances/", HTTP_AUTHORIZATION=f"Token {token}").status_code, 401)
self.assertFalse(ApiToken.objects.filter(user=user).exists())
def test_users(self):
response = self.client.get(reverse("admin:user_create"))
self.assertEqual(response.status_code, 200)

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")