Add operator-owned hosting and fix table card corners #171

Merged
parkel merged 2 commits from feature/operator-own-hosting into main 2026-11-24 01:35:36 +01:00
4 changed files with 137 additions and 9 deletions

No files matched your search

+6
View File
@@ -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 customer** enables customer creation. **Can change customer** enables
sign-in suspension and restoration. 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 If local credential access is enabled, customers can choose **Connection
details** for databases and mailboxes on their assigned accounts. The panel details** for databases and mailboxes on their assigned accounts. The panel
requires password confirmation for each reveal. requires password confirmation for each reveal.
@@ -709,12 +709,14 @@ tbody tr:hover {
border-top: 0; border-top: 0;
} }
.surface > .table-scroll:first-child th:first-child { .surface > .table-scroll:first-child {
border-top-left-radius: var(--radius); border-start-start-radius: calc(var(--radius) - 1px);
border-start-end-radius: calc(var(--radius) - 1px);
} }
.surface > .table-scroll:first-child th:last-child { .surface > .table-scroll:last-child {
border-top-right-radius: var(--radius); border-end-start-radius: calc(var(--radius) - 1px);
border-end-end-radius: calc(var(--radius) - 1px);
} }
.cell-date, .status { .cell-date, .status {
+90 -1
View File
@@ -1,8 +1,10 @@
from django.contrib.admin.models import ADDITION, LogEntry
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
from django.urls import reverse 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( @override_settings(
@@ -102,3 +104,90 @@ class HostingAccountSetupTests(TestCase):
self.assertIn( self.assertIn(
"check me", {record.text for record in zone.spec.txt_records} "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)
+35 -4
View File
@@ -2,6 +2,7 @@ from operator import attrgetter
from django import forms from django import forms
from django.conf import settings 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 import get_user_model
from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin
from django.core.exceptions import PermissionDenied, ValidationError from django.core.exceptions import PermissionDenied, ValidationError
@@ -458,6 +459,15 @@ class AccountCreate(PermissionRequiredMixin, AccountEditor, CreateView):
def get_form(self, form_class=None): def get_form(self, form_class=None):
form = super().get_form(form_class) form = super().get_form(form_class)
actor = self.request.user 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 reseller = reseller_for_user(actor) is not None
defaults = zone_defaults() defaults = zone_defaults()
web = panel_feature_allowed("LOCAL_WEB_ACTIVATION") and ( web = panel_feature_allowed("LOCAL_WEB_ACTIVATION") and (
@@ -516,14 +526,35 @@ class AccountCreate(PermissionRequiredMixin, AccountEditor, CreateView):
return initial return initial
def form_valid(self, form): 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) self.object = form.save(commit=False)
try: try:
with transaction.atomic(): with transaction.atomic():
if reseller is None: if own_hosting:
save_operator_account( customer, created = Customer.objects.get_or_create(
self.object, self.request.user.get_username() 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: else:
create_reseller_account(reseller, self.object) create_reseller_account(reseller, self.object)
setup_new_account(self.object, form.cleaned_data) setup_new_account(self.object, form.cleaned_data)