diff --git a/src/coulobre/hosting/migrations/0022_mailbox_quota_updates.py b/src/coulobre/hosting/migrations/0022_mailbox_quota_updates.py new file mode 100644 index 0000000..b634aa9 --- /dev/null +++ b/src/coulobre/hosting/migrations/0022_mailbox_quota_updates.py @@ -0,0 +1,17 @@ +# Generated by Django 5.2.19 on 2026-11-21 12:27 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0021_mailbox_storage_limits"), + ] + + operations = [ + migrations.AddField( + model_name="mailboxprovisioning", + name="quota_update", + field=models.BooleanField(default=False), + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 18d5041..36a7dd6 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -441,3 +441,4 @@ class MailboxProvisioning(ProvisioningJob): mailbox = models.OneToOneField( HostingMailbox, on_delete=models.CASCADE, related_name="provisioning" ) + quota_update = models.BooleanField(default=False) diff --git a/src/coulobre/hosting/node_client.py b/src/coulobre/hosting/node_client.py index 07a08f3..5b3e933 100644 --- a/src/coulobre/hosting/node_client.py +++ b/src/coulobre/hosting/node_client.py @@ -5,6 +5,7 @@ from coulobre.account_backup_request import ( BACKUP_SOCKET, BACKUP_TIMEOUT_SECONDS, ) +from coulobre.mail_node import MailboxQuotaUpdate RESPONSE_LIMIT = 32768 @@ -56,6 +57,15 @@ def provision_local_mailbox(mailbox): ) +def update_local_mailbox_quota(mailbox): + apply_on_local_node( + MailboxQuotaUpdate(mailbox), + "/run/coulobre-mail-apply.sock", + "mail node", + "mailbox quota update", + ) + + def create_local_account_backup(request): apply_on_local_node( request, diff --git a/src/coulobre/hosting/resource_requests.py b/src/coulobre/hosting/resource_requests.py index 811d033..b5cd920 100644 --- a/src/coulobre/hosting/resource_requests.py +++ b/src/coulobre/hosting/resource_requests.py @@ -4,7 +4,12 @@ from django.db.models import F from coulobre.mail_node import MailboxSpec -from .models import HostingAccount, HostingMailbox, HostingMailDomain +from .models import ( + HostingAccount, + HostingMailbox, + HostingMailDomain, + MailboxProvisioning, +) def lock_hosting_account(account_id, customer_id): @@ -69,3 +74,29 @@ def create_mailbox_request(account_id, local_part, *, customer_id=None): local_part=spec.local_part, quota_mib=plan.mailbox_quota_mib if plan is not None else None, ) + + +@transaction.atomic +def queue_mailbox_provisioning(mailbox): + lock_hosting_account(mailbox.mail_domain.account_id, customer_id=None) + MailboxProvisioning.objects.queue(mailbox=mailbox) + + +@transaction.atomic +def change_mailbox_quota(mailbox, quota_mib): + lock_hosting_account(mailbox.mail_domain.account_id, customer_id=None) + current = HostingMailbox.objects.get(pk=mailbox.pk) + if current.quota_mib == quota_mib: + return + + jobs = MailboxProvisioning.objects.filter(mailbox=current) + job = jobs.select_for_update().first() + if job is not None and job.state != "configured": + raise ValidationError( + "Complete the pending or failed mailbox request first." + ) + + current.quota_mib = quota_mib + current.save(update_fields=["quota_mib"]) + if job is not None: + MailboxProvisioning.objects.requeue(jobs, quota_update=True) diff --git a/src/coulobre/hosting/tasks.py b/src/coulobre/hosting/tasks.py index 485b3dd..9035b98 100644 --- a/src/coulobre/hosting/tasks.py +++ b/src/coulobre/hosting/tasks.py @@ -31,6 +31,7 @@ from .node_client import ( provision_local_database, provision_local_mailbox, publish_local_zone, + update_local_mailbox_quota, update_local_sftp, ) from .sftp import site_is_active @@ -275,7 +276,10 @@ def provision_mailbox(provisioning_id): def apply_mailbox_provisioning(provisioning): - provision_local_mailbox(provisioning.mailbox.spec) + if provisioning.quota_update: + update_local_mailbox_quota(provisioning.mailbox.spec) + else: + provision_local_mailbox(provisioning.mailbox.spec) @shared_task(acks_late=True, reject_on_worker_lost=True) diff --git a/src/coulobre/hosting/templates/hosting/account_detail.html b/src/coulobre/hosting/templates/hosting/account_detail.html index 64526dd..c846bb3 100644 --- a/src/coulobre/hosting/templates/hosting/account_detail.html +++ b/src/coulobre/hosting/templates/hosting/account_detail.html @@ -187,6 +187,9 @@
Download manifest + {% if change_mailbox_quota %} + Change storage limit + {% endif %} {% if manage_mailbox_provisioning %} {% url 'panel:mailbox_provision' mailbox.pk as provision_url %} {% include "hosting/resource_provisioning.html" with provisioning=mailbox.provisioning enabled=local_mailbox_enabled action=provision_url resource=mailbox provision_label="Provision mailbox" %} diff --git a/src/coulobre/hosting/templates/hosting/mailbox_quota_form.html b/src/coulobre/hosting/templates/hosting/mailbox_quota_form.html new file mode 100644 index 0000000..92fcd9b --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/mailbox_quota_form.html @@ -0,0 +1,21 @@ +{% extends "hosting/base.html" %} +{% block title %}Change mailbox storage limit · Coulobre Panel{% endblock %} +{% block content %} + ← {{ mailbox.mail_domain.account.name }} +
+
+

Change storage limit

+

{{ mailbox }}

+
+
+
+
+ {% csrf_token %} + {{ form.as_div }} +
+ + Cancel +
+
+
+{% endblock %} diff --git a/src/coulobre/hosting/test_mailbox_provisioning.py b/src/coulobre/hosting/test_mailbox_provisioning.py index f749b0d..6b4a6cf 100644 --- a/src/coulobre/hosting/test_mailbox_provisioning.py +++ b/src/coulobre/hosting/test_mailbox_provisioning.py @@ -4,6 +4,7 @@ from django.contrib.auth import get_user_model from django.contrib.auth.models import Permission from django.test import TestCase, override_settings from django.urls import reverse +from django.utils import timezone from coulobre.mail_node import MailboxSpec @@ -121,3 +122,102 @@ class MailboxProvisioningTests(TestCase): job.refresh_from_db() self.assertEqual(job.state, "queued") self.assertFalse(job.problem) + + def allow_quota_changes(self, user): + user.user_permissions.add( + Permission.objects.get( + content_type__app_label="hosting", + codename="change_hostingmailbox", + ) + ) + + def test_quota_edit_queues_an_update_for_a_configured_mailbox(self): + self.allow_quota_changes(self.operator) + job = MailboxProvisioning.objects.create( + mailbox=self.mailbox, finished_at=timezone.now() + ) + quota_url = reverse("panel:mailbox_quota", args=[self.mailbox.pk]) + self.assertContains( + self.client.get(self.detail), "Change storage limit" + ) + self.assertRedirects( + self.client.post(quota_url, {"quota_mib": 384}), self.detail + ) + + self.mailbox.refresh_from_db() + self.assertEqual(self.mailbox.quota_mib, 384) + job.refresh_from_db() + self.assertEqual(job.state, "queued") + self.assertTrue(job.quota_update) + with ( + patch( + "coulobre.hosting.tasks.update_local_mailbox_quota" + ) as update, + patch("coulobre.hosting.tasks.provision_local_mailbox") as install, + ): + provision_mailbox.run(job.pk) + update.assert_called_once_with( + MailboxSpec("site.example", "support", 384) + ) + install.assert_not_called() + job.refresh_from_db() + self.assertEqual(job.state, "configured") + + def test_failed_quota_update_keeps_the_target_for_retry(self): + self.allow_quota_changes(self.operator) + job = MailboxProvisioning.objects.create( + mailbox=self.mailbox, finished_at=timezone.now() + ) + quota_url = reverse("panel:mailbox_quota", args=[self.mailbox.pk]) + self.client.post(quota_url, {"quota_mib": 384}) + with patch( + "coulobre.hosting.tasks.update_local_mailbox_quota", + side_effect=OSError, + ): + provision_mailbox.run(job.pk) + + blocked = self.client.post(quota_url, {"quota_mib": 128}) + self.assertContains( + blocked, "Complete the pending or failed mailbox request first." + ) + self.assertEqual( + HostingMailbox.objects.get(pk=self.mailbox.pk).quota_mib, 384 + ) + self.assertRedirects(self.client.post(self.url), self.detail) + with patch( + "coulobre.hosting.tasks.update_local_mailbox_quota" + ) as update: + provision_mailbox.run(job.pk) + update.assert_called_once_with( + MailboxSpec("site.example", "support", 384) + ) + + @override_settings(LOCAL_MAILBOX_PROVISIONING=False) + def test_separate_mail_node_quota_edit_is_saved_in_the_manifest(self): + self.allow_quota_changes(self.operator) + quota_url = reverse("panel:mailbox_quota", args=[self.mailbox.pk]) + manifest_url = reverse( + "panel:mailbox_manifest", args=[self.mailbox.pk] + ) + self.assertRedirects( + self.client.post(quota_url, {"quota_mib": 256}), self.detail + ) + self.assertEqual( + self.client.get(manifest_url).json()["quota_mib"], 256 + ) + self.assertRedirects( + self.client.post(quota_url, {"quota_mib": ""}), self.detail + ) + self.assertEqual(self.client.get(manifest_url).json()["version"], 1) + self.assertFalse(MailboxProvisioning.objects.exists()) + + def test_reseller_cannot_edit_quota_even_with_mailbox_permissions(self): + self.allow_quota_changes(self.reseller) + self.client.force_login(self.reseller) + quota_url = reverse("panel:mailbox_quota", args=[self.mailbox.pk]) + self.assertEqual( + self.client.post(quota_url, {"quota_mib": 128}).status_code, 403 + ) + self.assertNotContains( + self.client.get(self.detail), "Change storage limit" + ) diff --git a/src/coulobre/hosting/urls.py b/src/coulobre/hosting/urls.py index c6eb71d..fea27c2 100644 --- a/src/coulobre/hosting/urls.py +++ b/src/coulobre/hosting/urls.py @@ -106,6 +106,11 @@ urlpatterns = [ views.DownloadMailboxManifest.as_view(), name="mailbox_manifest", ), + path( + "mailboxes//quota/", + views.MailboxQuotaEdit.as_view(), + name="mailbox_quota", + ), path( "mailboxes//provision/", views.RequestMailboxProvisioning.as_view(), diff --git a/src/coulobre/hosting/views.py b/src/coulobre/hosting/views.py index fc94ba9..db773c6 100644 --- a/src/coulobre/hosting/views.py +++ b/src/coulobre/hosting/views.py @@ -54,14 +54,18 @@ from .models import ( HostingDnsZone, HostingMailbox, HostingPlan, - MailboxProvisioning, Reseller, WebActivation, WebHTTPS, WebProvisioningPlan, ) from .reseller_accounts import create_reseller_account -from .resource_requests import create_database_request, create_mailbox_request +from .resource_requests import ( + change_mailbox_quota, + create_database_request, + create_mailbox_request, + queue_mailbox_provisioning, +) from .sftp import ( SFTPKeyUploadForm, can_manage_sftp, @@ -98,6 +102,7 @@ PROVISION_MAILBOX = ( VIEW_MAILBOXES, "hosting.add_mailboxprovisioning", ) +CHANGE_MAILBOX_QUOTA = (*PROVISION_MAILBOX, "hosting.change_hostingmailbox") class AccountList(TenantScopeMixin, PermissionRequiredMixin, ListView): @@ -311,6 +316,9 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView): context["local_mailbox_enabled"] = ( settings.LOCAL_MAILBOX_PROVISIONING ) + context["change_mailbox_quota"] = user.has_perms( + CHANGE_MAILBOX_QUOTA + ) context["mailboxes"] = mailboxes return context @@ -801,6 +809,29 @@ class DownloadMailboxManifest( ) +class MailboxQuotaEdit(OperatorOnlyMixin, PermissionRequiredMixin, UpdateView): + queryset = HostingMailbox.objects.select_related("mail_domain__account") + permission_required = CHANGE_MAILBOX_QUOTA + fields = ("quota_mib",) + template_name = "hosting/mailbox_quota_form.html" + context_object_name = "mailbox" + + def get_form(self, form_class=None): + form = super().get_form(form_class) + form.fields[ + "quota_mib" + ].help_text = "Leave blank for unlimited storage." + return form + + def form_valid(self, form): + try: + change_mailbox_quota(self.object, form.cleaned_data["quota_mib"]) + except ValidationError as error: + form.add_error(None, error) + return self.form_invalid(form) + return redirect(self.object.mail_domain.account) + + class RequestMailboxProvisioning( OperatorOnlyMixin, PermissionRequiredMixin, View ): @@ -813,7 +844,7 @@ class RequestMailboxProvisioning( HostingMailbox.objects.select_related("mail_domain__account"), pk=pk, ) - MailboxProvisioning.objects.queue(mailbox=mailbox) + queue_mailbox_provisioning(mailbox) return redirect(mailbox.mail_domain.account)