mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 01:16:10 +00:00
fix(admin): a new user is not given a VM that has its one owner
NEW_USER_DEFAULT_INSTANCES gives each new user the VMs it names. With "Multiple Owner for VM" off, an owned VM named there was given the new user as a second owner anyway, with its power and console. Creating the user is now refused, as adding a second owner on the VM's Users tab is; the check runs again with the inserts, under the ownership lock.
This commit is contained in:
parent
8263680c48
commit
dde7672375
7 changed files with 121 additions and 83 deletions
|
|
@ -10,7 +10,8 @@ from django.db.models import Q
|
|||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from accounts.forms import UniqueEmailMixin
|
||||
from accounts.models import UserAttributes
|
||||
from accounts.models import UserAttributes, UserInstance
|
||||
from appsettings.settings import app_settings
|
||||
from instances.models import Instance
|
||||
|
||||
from .models import Permission
|
||||
|
|
@ -140,6 +141,10 @@ class UserCreateForm(UserForm):
|
|||
found = list(Instance.objects.filter(name=name)[:2])
|
||||
if len(found) != 1:
|
||||
raise ValidationError(_("NEW_USER_DEFAULT_INSTANCES: %(name)s is not exactly one VM") % {"name": name})
|
||||
# as on the VM's Users tab: with one owner allowed, an owned VM is not given a second
|
||||
if app_settings.ALLOW_INSTANCE_MULTIPLE_OWNER == "False" and UserInstance.objects.filter(instance=found[0]).exists():
|
||||
raise ValidationError(_("NEW_USER_DEFAULT_INSTANCES: %(name)s has an owner, and only one is allowed")
|
||||
% {"name": name})
|
||||
self.default_instances += found
|
||||
return cleaned
|
||||
|
||||
|
|
|
|||
|
|
@ -96,6 +96,18 @@ class DefaultInstancesTests(TestCase):
|
|||
self.assertRedirects(self.create(), reverse("admin:user_list"))
|
||||
self.assertEqual([o.instance for o in UserInstance.objects.filter(user__username="newbie")], [self.web])
|
||||
|
||||
@override_settings(NEW_USER_DEFAULT_INSTANCES=["web"])
|
||||
def test_an_owned_vm_is_not_given_a_second_owner_when_one_is_allowed(self):
|
||||
from appsettings.models import AppSettings
|
||||
|
||||
UserInstance.objects.create(user=User.objects.create_user("alice"), instance=self.web)
|
||||
AppSettings.objects.filter(key="ALLOW_INSTANCE_MULTIPLE_OWNER").update(value="False")
|
||||
self.assertContains(self.create(), "NEW_USER_DEFAULT_INSTANCES: web has an owner, and only one is allowed")
|
||||
self.assertFalse(User.objects.filter(username="newbie").exists())
|
||||
AppSettings.objects.filter(key="ALLOW_INSTANCE_MULTIPLE_OWNER").update(value="True")
|
||||
self.assertRedirects(self.create(), reverse("admin:user_list"))
|
||||
self.assertEqual(UserInstance.objects.filter(instance=self.web).count(), 2)
|
||||
|
||||
def test_a_name_that_is_not_one_vm_is_refused_on_the_form(self):
|
||||
for name in ("no-such-vm", "db"):
|
||||
with self.subTest(name), override_settings(NEW_USER_DEFAULT_INSTANCES=["web", name]):
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from datetime import timedelta
|
|||
|
||||
from accounts.models import ApiToken, Instance, UserAttributes, UserInstance
|
||||
from appsettings.settings import app_settings
|
||||
from computes.utils import lock_ownership
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth import update_session_auth_hash
|
||||
|
|
@ -131,14 +132,20 @@ def user_create(request):
|
|||
user_form = forms.UserCreateForm(request.POST or None)
|
||||
attributes_form = forms.UserAttributesForm(request.POST or None)
|
||||
if user_form.is_valid() and attributes_form.is_valid():
|
||||
with audit(request, None, f"User {user_form.cleaned_data['username']}", "add"), transaction.atomic():
|
||||
user = user_form.save()
|
||||
attributes = attributes_form.save(commit=False)
|
||||
attributes.user = user
|
||||
attributes.save()
|
||||
UserInstance.objects.bulk_create(UserInstance(user=user, instance=instance)
|
||||
for instance in user_form.default_instances)
|
||||
return redirect("admin:user_list")
|
||||
with audit(request, None, f"User {user_form.cleaned_data['username']}", "add") as entry, transaction.atomic():
|
||||
# the form's one-owner check of the default VMs again, with the inserts in one step
|
||||
lock_ownership()
|
||||
user_form.full_clean()
|
||||
entry.skip = not user_form.is_valid()
|
||||
if user_form.is_valid():
|
||||
user = user_form.save()
|
||||
attributes = attributes_form.save(commit=False)
|
||||
attributes.user = user
|
||||
attributes.save()
|
||||
UserInstance.objects.bulk_create(UserInstance(user=user, instance=instance)
|
||||
for instance in user_form.default_instances)
|
||||
if user_form.is_valid():
|
||||
return redirect("admin:user_list")
|
||||
|
||||
return render(
|
||||
request,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue