1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00

feat(logs): admin changes and logins are recorded on the Logs page

The Logs page shows who added, changed or deleted a compute, from web
or API, and every login, failed login, lockout and logout, with the
client address and a Result column (Done, Failed, Unknown for older
rows). A failed change is recorded with its reason and the error still
reaches the user; a refused one is recorded too.

- logs.audit.write saves each row in its own savepoint and never
  raises, so a log write cannot break or undo the action it records.
  The audit() context manager writes one row when the change ends.
- Logs gains success and ip; user, host and instance are wider and
  date is indexed. The page orders by date then id.
- A compute edit names the changed fields; an edit that changes
  nothing writes no row, and an empty password field is no change.
- A compute's VMs leaving the panel get a system row each: owners
  kept on the Removed VMs page, moved to another compute, restored,
  left without owners, or the removed record expired.
- Job rows carry the result. A job's log entry that cannot be
  computed is logged and skipped instead of rolling back the finish
  (test_service.py changes this on purpose).
- Logins are recorded by signal receivers; CustomLoginView is removed
  and the plain LoginView is used. A wrong OTP code is a failed login,
  counted by django-axes toward the lockout.

Panel records and host-side objects follow in the next layers.
This commit is contained in:
catborise 2026-10-23 09:46:36 +03:00
parent ac6bc48f88
commit 8c0fbff8f7
24 changed files with 639 additions and 55 deletions

View file

@ -10,6 +10,7 @@ from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from instances.models import Instance
from libvirt import libvirtError
from logs.audit import audit, changed_fields, snapshot
from operations import claims
from vrtManager.connection import wvmConnect
from vrtManager.hostdetails import wvmHostDetails
@ -92,7 +93,9 @@ def instances(request, compute_id):
def compute_create(request, FormClass):
form = FormClass(request.POST or None)
if form.is_valid():
form.save()
compute = form.instance
with audit(request, compute, f"Compute {compute.name} ({compute.hostname})", "add"):
form.save()
return redirect(reverse("computes"))
return render(request, "computes/form.html", {"form": form})
@ -111,9 +114,15 @@ def compute_update(request, compute_id):
elif compute.type == 4:
FormClass = SocketComputeForm
subject = f"Compute {compute.name} ({compute.hostname})"
before = snapshot(compute)
form = FormClass(request.POST or None, instance=compute)
if form.is_valid():
form.save()
with audit(request, compute, subject, "change") as entry:
form.save()
fields = changed_fields(before, snapshot(compute))
entry.action = "change " + ", ".join(fields)
entry.skip = not fields
return redirect(reverse("computes"))
return render(request, "computes/form.html", {"form": form})
@ -123,7 +132,8 @@ def compute_update(request, compute_id):
def compute_delete(request, compute_id):
compute = get_object_or_404(Compute, pk=compute_id)
if request.method == "POST":
compute.delete()
with audit(request, compute, f"Compute {compute.name} ({compute.hostname})", "delete"):
compute.delete()
return redirect("computes")
return render(