From 6ed566f1ee981b869ca90c448a7e6cabe73c0f3b Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Tue, 24 Nov 2026 01:33:23 +0100 Subject: [PATCH 1/2] Let operators create hosting for their own login --- docs/development.md | 6 ++ src/coulobre/hosting/test_account_setup.py | 91 +++++++++++++++++++++- src/coulobre/hosting/views.py | 39 +++++++++- 3 files changed, 131 insertions(+), 5 deletions(-) diff --git a/docs/development.md b/docs/development.md index eaf7fe1..9f35fe1 100644 --- a/docs/development.md +++ b/docs/development.md @@ -96,6 +96,12 @@ and select a customer on hosting accounts they can create or edit. **Can add customer** enables customer creation. **Can change customer** enables sign-in suspension and restoration. +To create an account for your own hosting, sign in as a verified server +administrator. On **Add hosting account**, leave **Customer** unassigned and +select **For my own hosting**. The panel links the account to your login and +creates a customer profile if needed. Use the role selector to switch to +**Customer** and manage the hosting assigned to that profile. + If local credential access is enabled, customers can choose **Connection details** for databases and mailboxes on their assigned accounts. The panel requires password confirmation for each reveal. diff --git a/src/coulobre/hosting/test_account_setup.py b/src/coulobre/hosting/test_account_setup.py index dc6b233..a37fa2a 100644 --- a/src/coulobre/hosting/test_account_setup.py +++ b/src/coulobre/hosting/test_account_setup.py @@ -1,8 +1,10 @@ +from django.contrib.admin.models import ADDITION, LogEntry from django.contrib.auth import get_user_model from django.test import TestCase, override_settings from django.urls import reverse +from django_otp.plugins.otp_totp.models import TOTPDevice -from .models import HostingAccount, WebsiteAlias +from .models import Customer, HostingAccount, PanelInvitation, WebsiteAlias @override_settings( @@ -102,3 +104,90 @@ class HostingAccountSetupTests(TestCase): self.assertIn( "check me", {record.text for record in zone.spec.txt_records} ) + + +class OperatorOwnHostingTests(TestCase): + @classmethod + def setUpTestData(cls): + users = get_user_model().objects + cls.operator = users.create_superuser("operator") + cls.device = TOTPDevice.objects.create(user=cls.operator, name="Panel") + cls.customer = Customer.objects.create(user=users.create_user("other")) + cls.other = HostingAccount.objects.create( + name="Other site", + primary_domain="other.example.test", + customer=cls.customer, + ) + + def setUp(self): + self.client.force_login(self.operator) + self.url = reverse("panel:account_create") + self.data = { + "name": "Own website", + "primary_domain": "own.example.test", + "own_hosting": "on", + } + + def verify_operator(self): + session = self.client.session + session["otp_device_id"] = self.device.persistent_id + session.save() + + def test_current_login_can_manage_its_own_accounts_as_customer(self): + self.verify_operator() + self.assertContains(self.client.get(self.url), "For my own hosting") + for domain in ("own.example.test", "second.example.test"): + response = self.client.post( + self.url, self.data | {"primary_domain": domain} + ) + account = HostingAccount.objects.get(primary_domain=domain) + self.assertRedirects(response, account.get_absolute_url()) + self.assertEqual(account.customer.user_id, self.operator.pk) + self.assertEqual( + Customer.objects.filter(user=self.operator).count(), 1 + ) + self.assertEqual( + LogEntry.objects.filter( + user=self.operator, action_flag=ADDITION + ).count(), + 1, + ) + self.assertFalse(PanelInvitation.objects.exists()) + response = self.client.post( + reverse("panel:switch_panel_role"), {"role": "customer"} + ) + self.assertRedirects(response, reverse("panel:accounts")) + page = self.client.get(reverse("panel:accounts")) + self.assertContains(page, "own.example.test") + self.assertContains(page, "second.example.test") + self.assertNotContains(page, "other.example.test") + self.assertEqual( + self.client.get(self.other.get_absolute_url()).status_code, 404 + ) + self.assertEqual(self.client.get(self.url).status_code, 403) + + def test_own_hosting_cannot_override_an_explicit_customer(self): + self.verify_operator() + response = self.client.post( + self.url, self.data | {"customer": self.customer.pk} + ) + self.assertContains(response, "Leave Customer unassigned") + self.assertFalse(Customer.objects.filter(user=self.operator).exists()) + self.assertEqual(HostingAccount.objects.count(), 1) + + def test_reserved_domain_rolls_back_the_customer_role(self): + self.verify_operator() + self.other.website_domains.create(domain=self.data["primary_domain"]) + response = self.client.post(self.url, self.data) + self.assertContains(response, "This domain is unavailable.") + self.assertFalse(Customer.objects.filter(user=self.operator).exists()) + self.assertFalse(LogEntry.objects.filter(user=self.operator).exists()) + self.assertEqual(HostingAccount.objects.count(), 1) + + def test_unverified_operator_cannot_create_a_customer_role(self): + self.assertNotContains(self.client.get(self.url), "For my own hosting") + self.assertEqual( + self.client.post(self.url, self.data).status_code, 403 + ) + self.assertFalse(Customer.objects.filter(user=self.operator).exists()) + self.assertEqual(HostingAccount.objects.count(), 1) diff --git a/src/coulobre/hosting/views.py b/src/coulobre/hosting/views.py index ffbfbfb..10efbf9 100644 --- a/src/coulobre/hosting/views.py +++ b/src/coulobre/hosting/views.py @@ -2,6 +2,7 @@ from operator import attrgetter from django import forms from django.conf import settings +from django.contrib.admin.models import ADDITION, LogEntry from django.contrib.auth import get_user_model from django.contrib.auth.mixins import PermissionRequiredMixin from django.core.exceptions import PermissionDenied, ValidationError @@ -458,6 +459,15 @@ class AccountCreate(PermissionRequiredMixin, AccountEditor, CreateView): def get_form(self, form_class=None): form = super().get_form(form_class) actor = self.request.user + if actor.is_superuser and actor.is_verified(): + form.fields["own_hosting"] = forms.BooleanField( + label="For my own hosting", + help_text=( + "Leave Customer unassigned to use your current login " + "in the Customer workspace." + ), + required=False, + ) reseller = reseller_for_user(actor) is not None defaults = zone_defaults() web = panel_feature_allowed("LOCAL_WEB_ACTIVATION") and ( @@ -516,14 +526,35 @@ class AccountCreate(PermissionRequiredMixin, AccountEditor, CreateView): return initial def form_valid(self, form): - reseller = reseller_for_user(self.request.user) + actor = self.request.user + if self.request.POST.get("own_hosting") and ( + "own_hosting" not in form.fields + ): + raise PermissionDenied + own_hosting = form.cleaned_data.get("own_hosting") + if own_hosting and form.cleaned_data.get("customer"): + form.add_error( + "customer", "Leave Customer unassigned for your own hosting." + ) + return self.form_invalid(form) + reseller = reseller_for_user(actor) self.object = form.save(commit=False) try: with transaction.atomic(): - if reseller is None: - save_operator_account( - self.object, self.request.user.get_username() + if own_hosting: + customer, created = Customer.objects.get_or_create( + user_id=actor.pk ) + self.object.customer = customer + if created: + LogEntry.objects.log_actions( + actor.pk, + [customer], + ADDITION, + "Assigned Customer role for own hosting.", + ) + if reseller is None: + save_operator_account(self.object, actor.get_username()) else: create_reseller_account(reseller, self.object) setup_new_account(self.object, form.cleaned_data) -- 2.43.0 From b76e6c78a3a70a51dc6a617ae06882ad45fa7bac Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Tue, 24 Nov 2026 01:33:23 +0100 Subject: [PATCH 2/2] Clip table surfaces at their outer corners --- src/coulobre/hosting/static/hosting/panel.css | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index d3152e1..6a261c5 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -709,12 +709,14 @@ tbody tr:hover { border-top: 0; } -.surface > .table-scroll:first-child th:first-child { - border-top-left-radius: var(--radius); +.surface > .table-scroll:first-child { + border-start-start-radius: calc(var(--radius) - 1px); + border-start-end-radius: calc(var(--radius) - 1px); } -.surface > .table-scroll:first-child th:last-child { - border-top-right-radius: var(--radius); +.surface > .table-scroll:last-child { + border-end-start-radius: calc(var(--radius) - 1px); + border-end-end-radius: calc(var(--radius) - 1px); } .cell-date, .status { -- 2.43.0