Change storage limits for existing mailboxes #31
No files matched your search
@@ -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),
|
||||
),
|
||||
]
|
||||
@@ -441,3 +441,4 @@ class MailboxProvisioning(ProvisioningJob):
|
||||
mailbox = models.OneToOneField(
|
||||
HostingMailbox, on_delete=models.CASCADE, related_name="provisioning"
|
||||
)
|
||||
quota_update = models.BooleanField(default=False)
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
|
||||
@@ -187,6 +187,9 @@
|
||||
<td>
|
||||
<div class="form-actions">
|
||||
<a class="record-link" href="{% url 'panel:mailbox_manifest' mailbox.pk %}" aria-label="Download mailbox manifest for {{ mailbox }}">Download manifest</a>
|
||||
{% if change_mailbox_quota %}
|
||||
<a class="record-link" href="{% url 'panel:mailbox_quota' mailbox.pk %}" aria-label="Change storage limit for {{ mailbox }}">Change storage limit</a>
|
||||
{% 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" %}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
{% extends "hosting/base.html" %}
|
||||
{% block title %}Change mailbox storage limit · Coulobre Panel{% endblock %}
|
||||
{% block content %}
|
||||
<a class="back-link" href="{{ mailbox.mail_domain.account.get_absolute_url }}">← {{ mailbox.mail_domain.account.name }}</a>
|
||||
<header class="page-heading">
|
||||
<div>
|
||||
<h1>Change storage limit</h1>
|
||||
<p class="heading-meta domain">{{ mailbox }}</p>
|
||||
</div>
|
||||
</header>
|
||||
<section class="surface editor-surface">
|
||||
<form method="post" class="editor">
|
||||
{% csrf_token %}
|
||||
{{ form.as_div }}
|
||||
<div class="form-actions">
|
||||
<button class="button primary" type="submit">Save storage limit</button>
|
||||
<a class="button secondary" href="{{ mailbox.mail_domain.account.get_absolute_url }}">Cancel</a>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
{% endblock %}
|
||||
@@ -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"
|
||||
)
|
||||
@@ -106,6 +106,11 @@ urlpatterns = [
|
||||
views.DownloadMailboxManifest.as_view(),
|
||||
name="mailbox_manifest",
|
||||
),
|
||||
path(
|
||||
"mailboxes/<int:pk>/quota/",
|
||||
views.MailboxQuotaEdit.as_view(),
|
||||
name="mailbox_quota",
|
||||
),
|
||||
path(
|
||||
"mailboxes/<int:pk>/provision/",
|
||||
views.RequestMailboxProvisioning.as_view(),
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user