Enforce per-account database and mailbox quotas
This commit is contained in:
1 parent
22f1e24eed
commit
cba291d4a9
12 files changed
+268
-40
No files matched your search
@@ -11,6 +11,8 @@ See [Local development](docs/development.md) to run it.
|
||||
|
||||
- Self-hosted installation on a single Ubuntu server, with Debian support planned.
|
||||
- Hosting plans and self-service access for administrators, resellers, and customers.
|
||||
- Customer impersonation for administrator support, with a dedicated permission,
|
||||
a visible session indicator, and an audit trail.
|
||||
- Domains, TLS certificates, SFTP, and databases using Apache, PHP-FPM, and MariaDB.
|
||||
- Mail and DNS on the hosting server or on separate servers.
|
||||
- Account backups, restore, and migration from DirectAdmin.
|
||||
|
||||
@@ -23,6 +23,17 @@ Use **Hosting plans** to add named plans with an optional description. Choose a
|
||||
plan when creating or editing an account. Existing accounts start unassigned.
|
||||
Operators need permission to view hosting plans before they can assign one.
|
||||
|
||||
Set a database limit and a mailbox limit on a hosting plan to cap how many
|
||||
requests each assigned account can make through the panel. A blank limit allows
|
||||
unlimited requests. Zero blocks new requests of that type. Accounts without a
|
||||
plan have unlimited requests. The account page shows usage for configured
|
||||
limits. Mailboxes count toward the same limit across all of an account's mail
|
||||
domains, including domains retained after a primary domain change.
|
||||
|
||||
Changing or reassigning a plan preserves existing databases and mailboxes. If
|
||||
the new limit is at or below current usage, the panel blocks additional
|
||||
requests of that type.
|
||||
|
||||
## Give a reseller access to assigned accounts
|
||||
|
||||
In `/admin/`, create a normal user and add a **Reseller** record for that user.
|
||||
|
||||
@@ -38,9 +38,15 @@ class HostingAccountAdmin(admin.ModelAdmin):
|
||||
|
||||
@admin.register(HostingPlan)
|
||||
class HostingPlanAdmin(admin.ModelAdmin):
|
||||
list_display = ("name", "description")
|
||||
list_display = ("name", "description", "database_limit", "mailbox_limit")
|
||||
search_fields = ("name",)
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
if change:
|
||||
obj.save(update_fields=form.changed_data)
|
||||
else:
|
||||
super().save_model(request, obj, form, change)
|
||||
|
||||
|
||||
@admin.register(Reseller)
|
||||
class ResellerAdmin(admin.ModelAdmin):
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# Generated by Django 5.2.19 on 2026-11-20 23:31
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("hosting", "0011_customer_access"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="hostingplan",
|
||||
name="database_limit",
|
||||
field=models.PositiveIntegerField(
|
||||
blank=True,
|
||||
help_text="Leave blank for unlimited. Enter 0 to block new database requests.",
|
||||
null=True,
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="hostingplan",
|
||||
name="mailbox_limit",
|
||||
field=models.PositiveIntegerField(
|
||||
blank=True,
|
||||
help_text="Leave blank for unlimited. Enter 0 to block new mailbox requests.",
|
||||
null=True,
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -52,6 +52,16 @@ class Customer(models.Model):
|
||||
class HostingPlan(models.Model):
|
||||
name = models.CharField(max_length=120, unique=True)
|
||||
description = models.CharField(max_length=240, blank=True)
|
||||
database_limit = models.PositiveIntegerField(
|
||||
blank=True,
|
||||
null=True,
|
||||
help_text="Leave blank for unlimited. Enter 0 to block new database requests.",
|
||||
)
|
||||
mailbox_limit = models.PositiveIntegerField(
|
||||
blank=True,
|
||||
null=True,
|
||||
help_text="Leave blank for unlimited. Enter 0 to block new mailbox requests.",
|
||||
)
|
||||
|
||||
class Meta:
|
||||
ordering = ("name", "pk")
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import connection, transaction
|
||||
from django.db.models import F
|
||||
|
||||
from coulobre.mail_node import MailboxSpec
|
||||
|
||||
from .models import HostingAccount, HostingMailbox, HostingMailDomain
|
||||
|
||||
|
||||
def lock_hosting_account(account_id):
|
||||
accounts = HostingAccount.objects.filter(pk=account_id)
|
||||
if connection.vendor == "sqlite":
|
||||
# Acquire the write lock before reading the account and resource counts.
|
||||
accounts.update(hosting_plan=F("hosting_plan"))
|
||||
return accounts.select_for_update().get()
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def create_database_request(account_id, suffix):
|
||||
account = lock_hosting_account(account_id)
|
||||
databases = account.databases
|
||||
if databases.filter(suffix=suffix).exists():
|
||||
raise ValidationError(
|
||||
{"suffix": "This account already has a database with that suffix."}
|
||||
)
|
||||
|
||||
plan = account.hosting_plan
|
||||
if (
|
||||
plan
|
||||
and plan.database_limit is not None
|
||||
and databases.count() >= plan.database_limit
|
||||
):
|
||||
raise ValidationError("This hosting account has reached its database limit.")
|
||||
return databases.create(suffix=suffix)
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def create_mailbox_request(account_id, local_part):
|
||||
account = lock_hosting_account(account_id)
|
||||
try:
|
||||
spec = MailboxSpec(account.primary_domain, local_part)
|
||||
except ValueError as error:
|
||||
raise ValidationError({"local_part": str(error)}) from error
|
||||
|
||||
mail_domain, _ = HostingMailDomain.objects.get_or_create(
|
||||
domain=spec.domain, defaults={"account": account}
|
||||
)
|
||||
if mail_domain.account_id != account.pk:
|
||||
raise ValidationError("This mail domain belongs to another hosting account.")
|
||||
if mail_domain.mailboxes.filter(local_part=spec.local_part).exists():
|
||||
raise ValidationError(
|
||||
{"local_part": "This mailbox has already been requested."}
|
||||
)
|
||||
|
||||
plan = account.hosting_plan
|
||||
if plan and plan.mailbox_limit is not None:
|
||||
mailboxes = HostingMailbox.objects.filter(mail_domain__account=account)
|
||||
if mailboxes.count() >= plan.mailbox_limit:
|
||||
raise ValidationError("This hosting account has reached its mailbox limit.")
|
||||
return mail_domain.mailboxes.create(local_part=spec.local_part)
|
||||
@@ -41,7 +41,14 @@
|
||||
{% if is_tenant or perms.hosting.view_hostingdatabase %}
|
||||
<section class="surface" aria-labelledby="databases-heading">
|
||||
<header class="section-heading">
|
||||
<h2 id="databases-heading">MariaDB database requests</h2>
|
||||
<div>
|
||||
<h2 id="databases-heading">MariaDB database requests</h2>
|
||||
{% if is_tenant or perms.hosting.view_hostingplan %}
|
||||
{% if account.hosting_plan and account.hosting_plan.database_limit is not None %}
|
||||
<p class="heading-meta">{{ databases|length }} of {{ account.hosting_plan.database_limit }} databases requested</p>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if is_operator and perms.hosting.add_hostingdatabase %}
|
||||
<a class="button primary" href="{% url 'panel:database_create' account.pk %}">Request database</a>
|
||||
{% endif %}
|
||||
@@ -112,7 +119,14 @@
|
||||
{% if is_tenant or perms.hosting.view_hostingmailbox %}
|
||||
<section class="surface" aria-labelledby="mailboxes-heading">
|
||||
<header class="section-heading">
|
||||
<h2 id="mailboxes-heading">Mailbox requests</h2>
|
||||
<div>
|
||||
<h2 id="mailboxes-heading">Mailbox requests</h2>
|
||||
{% if is_tenant or perms.hosting.view_hostingplan %}
|
||||
{% if account.hosting_plan and account.hosting_plan.mailbox_limit is not None %}
|
||||
<p class="heading-meta">{{ mailboxes|length }} of {{ account.hosting_plan.mailbox_limit }} mailboxes requested</p>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if is_operator and perms.hosting.add_hostingmailbox %}
|
||||
<a class="button primary" href="{% url 'panel:mailbox_create' account.pk %}">Request mailbox</a>
|
||||
{% endif %}
|
||||
|
||||
@@ -11,7 +11,14 @@
|
||||
{% if hosting_plans %}
|
||||
<div class="table-scroll">
|
||||
<table>
|
||||
<thead><tr><th scope="col">Plan</th><th scope="col">Description</th></tr></thead>
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Plan</th>
|
||||
<th scope="col">Description</th>
|
||||
<th scope="col">Database limit</th>
|
||||
<th scope="col">Mailbox limit</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for hosting_plan in hosting_plans %}
|
||||
<tr>
|
||||
@@ -23,6 +30,8 @@
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>{{ hosting_plan.description|default:"—" }}</td>
|
||||
<td>{{ hosting_plan.database_limit|default_if_none:"Unlimited" }}</td>
|
||||
<td>{{ hosting_plan.mailbox_limit|default_if_none:"Unlimited" }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
|
||||
@@ -5,7 +5,7 @@ from django.urls import reverse
|
||||
|
||||
from coulobre.database_node import DatabaseSpec
|
||||
|
||||
from .models import HostingAccount, HostingDatabase, Reseller
|
||||
from .models import HostingAccount, HostingDatabase, HostingPlan, Reseller
|
||||
|
||||
|
||||
class DatabaseRequestTests(TestCase):
|
||||
@@ -74,6 +74,37 @@ class DatabaseRequestTests(TestCase):
|
||||
self.assertIn("suffix", response.context["form"].errors)
|
||||
self.assertFalse(HostingDatabase.objects.exists())
|
||||
|
||||
def test_database_limit_is_per_account_and_uses_the_current_plan(self):
|
||||
plan = HostingPlan.objects.create(name="Starter", database_limit=1)
|
||||
HostingAccount.objects.update(hosting_plan=plan)
|
||||
self.assertRedirects(
|
||||
self.client.post(self.request_url, {"suffix": "cms"}),
|
||||
self.account.get_absolute_url(),
|
||||
)
|
||||
capped = self.client.post(self.request_url, {"suffix": "shop"})
|
||||
self.assertContains(
|
||||
capped, "This hosting account has reached its database limit."
|
||||
)
|
||||
|
||||
other_url = reverse("panel:database_create", args=[self.other_account.pk])
|
||||
self.assertRedirects(
|
||||
self.client.post(other_url, {"suffix": "cms"}),
|
||||
self.other_account.get_absolute_url(),
|
||||
)
|
||||
zero = HostingPlan.objects.create(name="No databases", database_limit=0)
|
||||
HostingAccount.objects.filter(pk=self.account.pk).update(hosting_plan=zero)
|
||||
self.assertContains(
|
||||
self.client.get(self.account.get_absolute_url()),
|
||||
"1 of 0 databases requested",
|
||||
)
|
||||
capped = self.client.post(self.request_url, {"suffix": "shop"})
|
||||
self.assertContains(
|
||||
capped, "This hosting account has reached its database limit."
|
||||
)
|
||||
self.assertEqual(
|
||||
list(self.account.databases.values_list("suffix", flat=True)), ["cms"]
|
||||
)
|
||||
|
||||
def test_reseller_view_is_scoped_and_node_actions_require_an_operator(self):
|
||||
owned = HostingDatabase.objects.create(account=self.account, suffix="wordpress")
|
||||
HostingDatabase.objects.create(account=self.other_account, suffix="othersite")
|
||||
|
||||
@@ -30,10 +30,17 @@ class HostingPlanTests(TestCase):
|
||||
list_url = reverse("panel:hosting_plans")
|
||||
response = self.client.post(
|
||||
reverse("panel:hosting_plan_create"),
|
||||
{"name": "Starter", "description": "Shared hosting for small sites"},
|
||||
{
|
||||
"name": "Starter",
|
||||
"description": "Shared hosting for small sites",
|
||||
"database_limit": 0,
|
||||
"mailbox_limit": 5,
|
||||
},
|
||||
)
|
||||
self.assertRedirects(response, list_url)
|
||||
plan = HostingPlan.objects.get()
|
||||
self.assertEqual(plan.database_limit, 0)
|
||||
self.assertEqual(plan.mailbox_limit, 5)
|
||||
|
||||
self.assertContains(
|
||||
self.client.get(self.account.get_absolute_url()), "Unassigned"
|
||||
@@ -52,9 +59,18 @@ class HostingPlanTests(TestCase):
|
||||
|
||||
response = self.client.post(
|
||||
reverse("panel:hosting_plan_edit", args=[plan.pk]),
|
||||
{"name": "Standard", "description": "Updated description"},
|
||||
{
|
||||
"name": "Standard",
|
||||
"description": "Updated description",
|
||||
"database_limit": "",
|
||||
"mailbox_limit": 2,
|
||||
},
|
||||
)
|
||||
self.assertRedirects(response, list_url)
|
||||
plan.refresh_from_db()
|
||||
self.assertIsNone(plan.database_limit)
|
||||
self.assertEqual(plan.mailbox_limit, 2)
|
||||
self.assertContains(self.client.get(list_url), "Unlimited")
|
||||
self.assertContains(self.client.get(reverse("panel:accounts")), "Standard")
|
||||
|
||||
response = self.client.post(
|
||||
@@ -69,6 +85,16 @@ class HostingPlanTests(TestCase):
|
||||
self.assertRedirects(response, new_account.get_absolute_url())
|
||||
self.assertEqual(new_account.hosting_plan_id, plan.pk)
|
||||
|
||||
def test_plan_rejects_negative_limits(self):
|
||||
self.client.force_login(self.operator)
|
||||
response = self.client.post(
|
||||
reverse("panel:hosting_plan_create"),
|
||||
{"name": "Invalid plan", "database_limit": -1, "mailbox_limit": -2},
|
||||
)
|
||||
self.assertIn("database_limit", response.context["form"].errors)
|
||||
self.assertIn("mailbox_limit", response.context["form"].errors)
|
||||
self.assertFalse(HostingPlan.objects.exists())
|
||||
|
||||
def test_editor_without_plan_permission_cannot_assign_in_either_interface(self):
|
||||
current = HostingPlan.objects.create(name="Private plan")
|
||||
other = HostingPlan.objects.create(name="Other plan")
|
||||
|
||||
@@ -10,7 +10,13 @@ from django.urls import reverse
|
||||
|
||||
from coulobre.mail_node import MailboxSpec, main
|
||||
|
||||
from .models import HostingAccount, HostingMailbox, HostingMailDomain, Reseller
|
||||
from .models import (
|
||||
HostingAccount,
|
||||
HostingMailbox,
|
||||
HostingMailDomain,
|
||||
HostingPlan,
|
||||
Reseller,
|
||||
)
|
||||
|
||||
|
||||
class MailboxRequestTests(TestCase):
|
||||
@@ -79,6 +85,41 @@ class MailboxRequestTests(TestCase):
|
||||
self.assertIn("local_part", response.context["form"].errors)
|
||||
self.assertFalse(HostingMailDomain.objects.exists())
|
||||
|
||||
def test_mailbox_quota_counts_old_domains_and_preserves_existing_requests(self):
|
||||
plan = HostingPlan.objects.create(name="Solo mail", mailbox_limit=1)
|
||||
HostingAccount.objects.filter(pk=self.account.pk).update(hosting_plan=plan)
|
||||
original = self.request_mailbox("support")
|
||||
response = self.client.post(
|
||||
reverse("panel:account_edit", args=[self.account.pk]),
|
||||
{
|
||||
"name": self.account.name,
|
||||
"primary_domain": "new.example",
|
||||
"hosting_plan": plan.pk,
|
||||
},
|
||||
)
|
||||
self.assertRedirects(response, self.account.get_absolute_url())
|
||||
self.assertContains(
|
||||
self.client.get(self.account.get_absolute_url()),
|
||||
"1 of 1 mailboxes requested",
|
||||
)
|
||||
capped = self.client.post(self.request_url, {"local_part": "billing"})
|
||||
self.assertContains(
|
||||
capped, "This hosting account has reached its mailbox limit."
|
||||
)
|
||||
self.assertEqual(HostingMailbox.objects.get(), original)
|
||||
self.assertFalse(
|
||||
HostingMailDomain.objects.filter(domain="new.example").exists()
|
||||
)
|
||||
|
||||
HostingPlan.objects.filter(pk=plan.pk).update(mailbox_limit=2)
|
||||
self.assertRedirects(
|
||||
self.client.post(self.request_url, {"local_part": "billing"}),
|
||||
self.account.get_absolute_url(),
|
||||
)
|
||||
self.assertEqual(
|
||||
HostingMailbox.objects.get(local_part="billing").spec.domain, "new.example"
|
||||
)
|
||||
|
||||
def test_renamed_account_keeps_existing_mail_and_prevents_domain_reassignment(self):
|
||||
original = self.request_mailbox("support")
|
||||
mail_domain = original.mail_domain.domain
|
||||
|
||||
@@ -15,7 +15,7 @@ from django.views.generic import CreateView, DetailView, FormView, ListView, Upd
|
||||
from django.views.generic.detail import SingleObjectMixin
|
||||
|
||||
from coulobre.dns_node import MAX_SOA_SERIAL
|
||||
from coulobre.mail_node import INVALID_LOCAL_PART, MailboxSpec
|
||||
from coulobre.mail_node import INVALID_LOCAL_PART
|
||||
|
||||
from .access import (
|
||||
OperatorOnlyMixin,
|
||||
@@ -30,12 +30,12 @@ from .models import (
|
||||
HostingDatabase,
|
||||
HostingDnsZone,
|
||||
HostingMailbox,
|
||||
HostingMailDomain,
|
||||
HostingPlan,
|
||||
Reseller,
|
||||
WebProvisioningPlan,
|
||||
)
|
||||
from .reseller_accounts import create_reseller_account
|
||||
from .resource_requests import create_database_request, create_mailbox_request
|
||||
from .web_node import build_web_smoke_bundle, serialize_deployment_manifest
|
||||
|
||||
VIEW_ACCOUNTS = "hosting.view_hostingaccount"
|
||||
@@ -277,17 +277,14 @@ class DatabaseCreate(OperatorOnlyMixin, PermissionRequiredMixin, CreateView):
|
||||
return super().get_context_data(account=self.account, **kwargs)
|
||||
|
||||
def form_valid(self, form):
|
||||
self.object = form.save(commit=False)
|
||||
self.object.account = self.account
|
||||
try:
|
||||
with transaction.atomic():
|
||||
self.object.save()
|
||||
except IntegrityError:
|
||||
if not self.account.databases.filter(suffix=self.object.suffix).exists():
|
||||
raise
|
||||
form.add_error(
|
||||
"suffix", "This account already has a database with that suffix."
|
||||
self.object = create_database_request(
|
||||
self.account.pk, form.cleaned_data["suffix"]
|
||||
)
|
||||
except HostingAccount.DoesNotExist as error:
|
||||
raise Http404 from error
|
||||
except ValidationError as error:
|
||||
form.add_error(None, error)
|
||||
return self.form_invalid(form)
|
||||
return redirect(self.account)
|
||||
|
||||
@@ -445,27 +442,13 @@ class MailboxCreate(OperatorOnlyMixin, PermissionRequiredMixin, CreateView):
|
||||
|
||||
def form_valid(self, form):
|
||||
try:
|
||||
spec = MailboxSpec(
|
||||
self.account.primary_domain, form.cleaned_data["local_part"]
|
||||
self.object = create_mailbox_request(
|
||||
self.account.pk, form.cleaned_data["local_part"]
|
||||
)
|
||||
except ValueError as error:
|
||||
form.add_error("local_part", str(error))
|
||||
return self.form_invalid(form)
|
||||
|
||||
with transaction.atomic():
|
||||
mail_domain, _ = HostingMailDomain.objects.get_or_create(
|
||||
domain=spec.domain, defaults={"account": self.account}
|
||||
)
|
||||
if mail_domain.account_id != self.account.pk:
|
||||
form.add_error(
|
||||
None, "This mail domain belongs to another hosting account."
|
||||
)
|
||||
return self.form_invalid(form)
|
||||
self.object, created = HostingMailbox.objects.get_or_create(
|
||||
mail_domain=mail_domain, local_part=spec.local_part
|
||||
)
|
||||
if not created:
|
||||
form.add_error("local_part", "This mailbox has already been requested.")
|
||||
except HostingAccount.DoesNotExist as error:
|
||||
raise Http404 from error
|
||||
except ValidationError as error:
|
||||
form.add_error(None, error)
|
||||
return self.form_invalid(form)
|
||||
return redirect(self.account)
|
||||
|
||||
@@ -496,7 +479,7 @@ class HostingPlanList(OperatorOnlyMixin, PermissionRequiredMixin, ListView):
|
||||
|
||||
class HostingPlanEditor:
|
||||
model = HostingPlan
|
||||
fields = ("name", "description")
|
||||
fields = ("name", "description", "database_limit", "mailbox_limit")
|
||||
template_name = "hosting/hosting_plan_form.html"
|
||||
success_url = reverse_lazy("panel:hosting_plans")
|
||||
|
||||
@@ -514,6 +497,11 @@ class HostingPlanEdit(
|
||||
permission_required = (VIEW_HOSTING_PLANS, "hosting.change_hostingplan")
|
||||
extra_context = {"page_title": "Edit hosting plan", "submit_label": "Save changes"}
|
||||
|
||||
def form_valid(self, form):
|
||||
self.object = form.save(commit=False)
|
||||
self.object.save(update_fields=form.changed_data)
|
||||
return redirect(self.get_success_url())
|
||||
|
||||
|
||||
class RequestWebPlan(
|
||||
OperatorOnlyMixin, PermissionRequiredMixin, SingleObjectMixin, View
|
||||
|
||||
Reference in new issue
Block a user