Serve website aliases over HTTP and HTTPS #38
No files matched your search
@@ -40,6 +40,9 @@
|
||||
{% if can_measure_site_storage or website_storage.measured_at or can_change_site_storage_limit or website_limit.applied_at %}
|
||||
{% include "hosting/site_storage.html" %}
|
||||
{% endif %}
|
||||
{% if website_aliases or can_add_website_alias %}
|
||||
{% include "hosting/website_aliases.html" %}
|
||||
{% endif %}
|
||||
{% if show_account_backups %}
|
||||
{% include "hosting/account_backups.html" %}
|
||||
{% endif %}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
{% extends "hosting/base.html" %}
|
||||
{% block title %}{{ title }} · Coulobre Panel{% endblock %}
|
||||
{% block content %}
|
||||
<a class="back-link" href="{{ account.get_absolute_url }}#website-aliases-heading">← {{ account.name }}</a>
|
||||
<header class="page-heading">
|
||||
<div>
|
||||
<h1>{{ title }}</h1>
|
||||
<p class="heading-meta domain">{% if alias %}{{ alias.reservation.domain }}{% else %}{{ account.primary_domain }}{% endif %}</p>
|
||||
</div>
|
||||
</header>
|
||||
<section class="surface editor-surface">
|
||||
<form method="post" class="editor">
|
||||
{% if action == "https" %}
|
||||
<p>Point the hostname to this web node and make its HTTP-01 challenge reachable before requesting a certificate.</p>
|
||||
{% elif action == "remove" %}
|
||||
<p>This removes the alias from Apache and deletes its dedicated certificate if one exists.</p>
|
||||
{% endif %}
|
||||
{% csrf_token %}
|
||||
{{ form.as_div }}
|
||||
<div class="form-actions">
|
||||
<button class="button primary" type="submit">{{ submit_label }}</button>
|
||||
<a class="button secondary" href="{{ account.get_absolute_url }}#website-aliases-heading">Cancel</a>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,52 @@
|
||||
<section class="surface" aria-labelledby="website-aliases-heading">
|
||||
<header class="section-heading">
|
||||
<div>
|
||||
<h2 id="website-aliases-heading">Website aliases</h2>
|
||||
<p class="heading-meta">Additional hostnames for this website.</p>
|
||||
</div>
|
||||
{% if can_add_website_alias %}
|
||||
<a class="button primary" href="{% url 'panel:website_alias_create' account.pk %}">Add alias</a>
|
||||
{% endif %}
|
||||
</header>
|
||||
{% if website_aliases %}
|
||||
<div class="table-scroll">
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th scope="col">Hostname</th><th scope="col">Status</th>{% if manage_website_aliases %}<th scope="col">Actions</th>{% endif %}</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for alias in website_aliases %}
|
||||
<tr>
|
||||
<td class="domain">{{ alias.reservation.domain }}</td>
|
||||
<td>
|
||||
{% if alias.https_active %}HTTPS active{% elif alias.http_active %}HTTP active{% endif %}
|
||||
{% if alias.state != "configured" %}
|
||||
<div>{{ alias.get_operation_display }} · {{ alias.state|capfirst }}</div>
|
||||
{% if manage_website_aliases and alias.problem %}<div>{{ alias.problem }}</div>{% endif %}
|
||||
{% endif %}
|
||||
</td>
|
||||
{% if manage_website_aliases %}
|
||||
<td>
|
||||
{% if alias.finished_at %}
|
||||
<div class="form-actions">
|
||||
{% if alias.problem %}
|
||||
{% if alias.operation != "https" or can_enable_alias_https %}
|
||||
<a href="{% url 'panel:website_alias_action' account.pk alias.pk 'retry' %}">Retry</a>
|
||||
{% endif %}
|
||||
{% elif can_enable_alias_https and alias.http_active and not alias.https_active %}
|
||||
<a href="{% url 'panel:website_alias_action' account.pk alias.pk 'https' %}">Enable HTTPS</a>
|
||||
{% endif %}
|
||||
<a href="{% url 'panel:website_alias_action' account.pk alias.pk 'remove' %}">Remove</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
</td>
|
||||
{% endif %}
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% else %}
|
||||
<p class="empty">No website aliases.</p>
|
||||
{% endif %}
|
||||
</section>
|
||||
@@ -0,0 +1,360 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import IntegrityError
|
||||
from django.db.models import ProtectedError
|
||||
from django.test import SimpleTestCase, TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
|
||||
from coulobre import web_node_aliases, web_node_apply, web_node_tls
|
||||
from coulobre.web_node import (
|
||||
WebAlias,
|
||||
WebAliasRequest,
|
||||
WebSite,
|
||||
parse_web_request,
|
||||
)
|
||||
|
||||
from .models import (
|
||||
HostingAccount,
|
||||
Reseller,
|
||||
WebActivation,
|
||||
WebProvisioningPlan,
|
||||
WebsiteAlias,
|
||||
WebsiteDomain,
|
||||
)
|
||||
from .tasks import (
|
||||
WEB_ACTIVATION_QUEUE,
|
||||
WEB_HTTPS_QUEUE,
|
||||
dispatch_web_activations,
|
||||
dispatch_web_https,
|
||||
enable_website_alias_https,
|
||||
update_website_alias,
|
||||
)
|
||||
|
||||
|
||||
@override_settings(LOCAL_WEB_ACTIVATION=True, LOCAL_WEB_HTTPS=True)
|
||||
class PanelWebsiteAliasTests(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
users = get_user_model().objects
|
||||
cls.operator = users.create_superuser("operator")
|
||||
cls.reseller = Reseller.objects.create(
|
||||
user=users.create_user("reseller")
|
||||
)
|
||||
cls.account = HostingAccount.objects.create(
|
||||
name="Managed website",
|
||||
primary_domain="primary.example",
|
||||
reseller=cls.reseller,
|
||||
)
|
||||
cls.other = HostingAccount.objects.create(
|
||||
name="Other", primary_domain="other.example"
|
||||
)
|
||||
cls.site = WebSite(cls.account.pk, cls.account.primary_domain)
|
||||
plan = WebProvisioningPlan.objects.create(
|
||||
account=cls.account,
|
||||
primary_domain=cls.site.domain,
|
||||
completed_at=timezone.now(),
|
||||
apache_config=cls.site.render("apache"),
|
||||
)
|
||||
WebActivation.objects.create(plan=plan, finished_at=timezone.now())
|
||||
|
||||
def setUp(self):
|
||||
self.client.force_login(self.operator)
|
||||
self.create_url = reverse(
|
||||
"panel:website_alias_create", args=[self.account.pk]
|
||||
)
|
||||
self.account_url = self.account.get_absolute_url()
|
||||
|
||||
def create_alias(self, name="extra.example"):
|
||||
self.assertRedirects(
|
||||
self.client.post(self.create_url, {"domain": name}),
|
||||
self.account_url,
|
||||
)
|
||||
return WebsiteAlias.objects.get()
|
||||
|
||||
def action(self, alias, verb, account=None):
|
||||
return reverse(
|
||||
"panel:website_alias_action",
|
||||
args=[(account or self.account).pk, alias.pk, verb],
|
||||
)
|
||||
|
||||
def test_alias_lifecycle_dispatches_http_and_https_to_their_workers(self):
|
||||
alias = self.create_alias("Extra.Example.")
|
||||
self.assertEqual(alias.reservation.domain, "extra.example")
|
||||
target = WebAlias(self.site, alias.reservation.domain)
|
||||
https = self.action(alias, "https")
|
||||
self.assertContains(
|
||||
self.client.post(https), "Wait for the current alias request"
|
||||
)
|
||||
|
||||
with patch.object(update_website_alias, "apply_async") as publish:
|
||||
dispatch_web_activations()
|
||||
publish.assert_called_once_with(
|
||||
args=[alias.pk], queue=WEB_ACTIVATION_QUEUE, retry=False
|
||||
)
|
||||
with patch(
|
||||
"coulobre.hosting.tasks.update_local_website_alias"
|
||||
) as node:
|
||||
update_website_alias(alias.pk)
|
||||
node.assert_called_once_with(target, "apply_alias")
|
||||
self.assertContains(self.client.get(self.account_url), "HTTP active")
|
||||
|
||||
self.assertRedirects(self.client.post(https), self.account_url)
|
||||
with patch.object(
|
||||
enable_website_alias_https, "apply_async"
|
||||
) as publish:
|
||||
dispatch_web_https()
|
||||
publish.assert_called_once_with(
|
||||
args=[alias.pk], queue=WEB_HTTPS_QUEUE, retry=False
|
||||
)
|
||||
with (
|
||||
patch("coulobre.hosting.tasks.enable_local_https") as enable,
|
||||
patch("coulobre.hosting.tasks.update_local_website_alias") as http,
|
||||
):
|
||||
update_website_alias(alias.pk)
|
||||
enable_website_alias_https(alias.pk)
|
||||
http.assert_not_called()
|
||||
enable.assert_called_once_with(target)
|
||||
self.assertContains(self.client.get(self.account_url), "HTTPS active")
|
||||
|
||||
self.assertRedirects(
|
||||
self.client.post(self.action(alias, "remove")), self.account_url
|
||||
)
|
||||
with patch(
|
||||
"coulobre.hosting.tasks.update_local_website_alias"
|
||||
) as node:
|
||||
update_website_alias(alias.pk)
|
||||
update_website_alias(alias.pk)
|
||||
node.assert_called_once_with(target, "remove_alias")
|
||||
self.assertFalse(
|
||||
WebsiteDomain.objects.filter(domain=target.domain).exists()
|
||||
)
|
||||
self.assertFalse(WebsiteAlias.objects.exists())
|
||||
self.assertTrue(
|
||||
WebsiteDomain.objects.filter(
|
||||
domain=self.site.domain, is_primary=True
|
||||
).exists()
|
||||
)
|
||||
|
||||
def test_primary_and_alias_hostnames_share_the_same_reservation(self):
|
||||
conflict = self.client.post(
|
||||
self.create_url, {"domain": self.other.primary_domain}
|
||||
)
|
||||
self.assertContains(conflict, "This domain is unavailable.")
|
||||
alias = self.create_alias("Extra.Example")
|
||||
conflict = self.client.post(
|
||||
self.create_url, {"domain": "EXTRA.example."}
|
||||
)
|
||||
self.assertContains(conflict, "This domain is unavailable.")
|
||||
with self.assertRaises(IntegrityError):
|
||||
HostingAccount.objects.create(
|
||||
name="Replacement", primary_domain=alias.reservation.domain
|
||||
)
|
||||
self.assertFalse(
|
||||
HostingAccount.objects.filter(
|
||||
primary_domain=alias.reservation.domain
|
||||
).exists()
|
||||
)
|
||||
|
||||
self.account.primary_domain = "new.example"
|
||||
with self.assertRaisesMessage(
|
||||
ValidationError, "Remove website aliases"
|
||||
):
|
||||
self.account.save(update_fields=["primary_domain"])
|
||||
self.account.refresh_from_db()
|
||||
self.assertEqual(self.account.primary_domain, self.site.domain)
|
||||
|
||||
def test_account_deletion_cannot_release_an_existing_alias(self):
|
||||
alias = self.create_alias()
|
||||
with self.assertRaises(ProtectedError):
|
||||
self.account.delete()
|
||||
self.assertTrue(WebsiteAlias.objects.filter(pk=alias.pk).exists())
|
||||
|
||||
def test_alias_cannot_be_added_to_an_unactivated_site(self):
|
||||
other_url = reverse("panel:website_alias_create", args=[self.other.pk])
|
||||
self.assertContains(
|
||||
self.client.post(other_url, {"domain": "extra.example"}),
|
||||
"Activate the website for the primary domain first.",
|
||||
)
|
||||
self.assertFalse(WebsiteAlias.objects.exists())
|
||||
|
||||
def test_alias_management_and_https_have_separate_permissions(self):
|
||||
alias = self.create_alias()
|
||||
WebsiteAlias.objects.filter(pk=alias.pk).update(
|
||||
finished_at=timezone.now(),
|
||||
http_active=True,
|
||||
operation=WebsiteAlias.Operation.HTTPS,
|
||||
problem="Certificate failed.",
|
||||
)
|
||||
worker = get_user_model().objects.create_user("limited")
|
||||
worker.user_permissions.add(
|
||||
*Permission.objects.filter(
|
||||
codename__in=("view_hostingaccount", "manage_website_aliases")
|
||||
)
|
||||
)
|
||||
self.client.force_login(worker)
|
||||
self.assertEqual(
|
||||
self.client.post(self.action(alias, "retry")).status_code, 403
|
||||
)
|
||||
self.assertEqual(
|
||||
self.client.post(self.action(alias, "https")).status_code, 403
|
||||
)
|
||||
self.assertEqual(
|
||||
self.client.get(
|
||||
self.action(alias, "remove", self.other)
|
||||
).status_code,
|
||||
404,
|
||||
)
|
||||
self.client.force_login(self.reseller.user)
|
||||
self.assertContains(
|
||||
self.client.get(self.account_url), alias.reservation.domain
|
||||
)
|
||||
self.assertNotContains(
|
||||
self.client.get(self.account_url), "Certificate failed."
|
||||
)
|
||||
self.assertEqual(
|
||||
self.client.post(self.action(alias, "remove")).status_code, 403
|
||||
)
|
||||
|
||||
|
||||
class NodeWebsiteAliasTests(SimpleTestCase):
|
||||
def setUp(self):
|
||||
directory = Path(self.enterContext(TemporaryDirectory()))
|
||||
self.state = directory / "state"
|
||||
self.apache = directory / "sites-available"
|
||||
self.enabled = directory / "sites-enabled"
|
||||
for path in (self.state, self.apache, self.enabled):
|
||||
path.mkdir()
|
||||
(self.state / "apply.lock").touch()
|
||||
self.site = WebSite(42, "primary.example")
|
||||
self.alias = WebAlias(self.site, "Extra.Example.")
|
||||
(self.state / "42.json").write_text(
|
||||
json.dumps(self.site.manifest()), encoding="utf-8"
|
||||
)
|
||||
|
||||
for key, value in (
|
||||
("STATE_DIRECTORY", self.state),
|
||||
("APACHE_AVAILABLE", self.apache),
|
||||
("APACHE_ENABLED", self.enabled),
|
||||
):
|
||||
self.enterContext(patch.object(web_node_apply, key, value))
|
||||
self.enterContext(
|
||||
patch.object(web_node_apply.os, "geteuid", return_value=0)
|
||||
)
|
||||
self.enterContext(
|
||||
patch.object(web_node_apply, "require_root_directory")
|
||||
)
|
||||
self.enterContext(patch.object(web_node_apply, "check_root_file"))
|
||||
self.enterContext(patch.object(web_node_apply, "check_installation"))
|
||||
|
||||
def read(path, **kwargs):
|
||||
return path.read_text(encoding="utf-8")
|
||||
|
||||
self.enterContext(
|
||||
patch.object(web_node_apply, "read_root_file", side_effect=read)
|
||||
)
|
||||
self.enterContext(
|
||||
patch.object(web_node_aliases, "read_root_file", side_effect=read)
|
||||
)
|
||||
self.enterContext(
|
||||
patch.object(
|
||||
web_node_aliases,
|
||||
"require_root_directory",
|
||||
side_effect=lambda path, create_mode=None: path.mkdir(
|
||||
exist_ok=True
|
||||
),
|
||||
)
|
||||
)
|
||||
self.runner = self.enterContext(patch.object(web_node_apply, "run"))
|
||||
|
||||
def test_addition_replay_reuses_the_alias_configuration_and_reservation(
|
||||
self,
|
||||
):
|
||||
request = WebAliasRequest(self.alias, "apply_alias")
|
||||
self.assertEqual(parse_web_request(request.manifest()), request)
|
||||
web_node_aliases.activate_alias(self.alias)
|
||||
web_node_aliases.activate_alias(self.alias)
|
||||
apache, enabled = web_node_aliases.alias_paths(self.alias)
|
||||
self.assertEqual(enabled.resolve(), apache)
|
||||
config = apache.read_text(encoding="utf-8")
|
||||
self.assertIn("ServerName extra.example", config)
|
||||
self.assertIn('DocumentRoot "/srv/coulobre/web/42/public"', config)
|
||||
self.assertIn("/run/php/coulobre-42.sock", config)
|
||||
self.assertEqual(
|
||||
json.loads((self.state / "42.json").read_text()),
|
||||
self.site.manifest(),
|
||||
)
|
||||
|
||||
def test_primary_activation_and_aliases_reject_conflicting_hostnames(self):
|
||||
other = WebSite(43, "other.example")
|
||||
(self.state / "43.json").write_text(
|
||||
json.dumps(other.manifest()), encoding="utf-8"
|
||||
)
|
||||
with self.assertRaisesMessage(
|
||||
ValueError, "reserved as a primary domain"
|
||||
):
|
||||
web_node_aliases.activate_alias(WebAlias(self.site, other.domain))
|
||||
web_node_aliases.activate_alias(self.alias)
|
||||
with self.assertRaisesMessage(
|
||||
ValueError, "reserved as another website alias"
|
||||
):
|
||||
web_node_aliases.activate_alias(WebAlias(other, self.alias.domain))
|
||||
with self.assertRaisesMessage(
|
||||
ValueError, "reserved as another website alias"
|
||||
):
|
||||
web_node_apply.reserve_account(
|
||||
WebSite(44, self.alias.domain), "reserved", ()
|
||||
)
|
||||
|
||||
def test_apache_hostname_collision_requires_an_unassigned_name(self):
|
||||
self.runner.return_value = (
|
||||
"VirtualHost configuration:\n"
|
||||
"*:80 is a NameVirtualHost\n"
|
||||
" port 80 namevhost unmanaged.example (/etc/apache2/site.conf:1)\n"
|
||||
" wild alias *.example.net\n"
|
||||
" alias extra.example\n"
|
||||
)
|
||||
for hostname in (
|
||||
"unmanaged.example",
|
||||
"alias.example.net",
|
||||
self.alias.domain,
|
||||
):
|
||||
with (
|
||||
self.subTest(hostname=hostname),
|
||||
self.assertRaisesMessage(ValueError, "configured in Apache"),
|
||||
):
|
||||
web_node_aliases.activate_alias(WebAlias(self.site, hostname))
|
||||
self.assertFalse(web_node_aliases.alias_marker(self.alias).exists())
|
||||
|
||||
def test_https_uses_the_alias_domain_and_dedicated_certificate(self):
|
||||
with patch.object(web_node_tls.subprocess, "run") as certbot:
|
||||
web_node_tls.request_certificate(self.alias)
|
||||
command = certbot.call_args.args[0]
|
||||
self.assertEqual(
|
||||
command[-4:],
|
||||
("--cert-name", self.alias.name, "--domains", self.alias.domain),
|
||||
)
|
||||
config = web_node_tls.render_https_configuration(self.alias)
|
||||
self.assertIn("ServerName extra.example", config)
|
||||
self.assertIn(f"/live/{self.alias.name}/fullchain.pem", config)
|
||||
|
||||
def test_failed_removal_restores_alias_links_before_returning(self):
|
||||
web_node_aliases.activate_alias(self.alias)
|
||||
apache, enabled = web_node_aliases.alias_paths(self.alias)
|
||||
self.runner.side_effect = [ValueError("Config test failed."), ""]
|
||||
with (
|
||||
patch.object(
|
||||
web_node_aliases, "delete_alias_certificate"
|
||||
) as delete,
|
||||
self.assertRaisesMessage(ValueError, "Config test failed."),
|
||||
):
|
||||
web_node_aliases.remove_alias(self.alias)
|
||||
delete.assert_not_called()
|
||||
self.assertEqual(enabled.resolve(), apache)
|
||||
self.assertTrue(web_node_aliases.alias_marker(self.alias).is_file())
|
||||
@@ -21,6 +21,16 @@ urlpatterns = [
|
||||
"accounts/new/", views.AccountCreate.as_view(), name="account_create"
|
||||
),
|
||||
path("accounts/<int:pk>/", views.AccountDetail.as_view(), name="account"),
|
||||
path(
|
||||
"accounts/<int:pk>/aliases/new/",
|
||||
views.WebsiteAliasCreate.as_view(),
|
||||
name="website_alias_create",
|
||||
),
|
||||
path(
|
||||
"accounts/<int:pk>/aliases/<int:alias_pk>/<str:action>/",
|
||||
views.WebsiteAliasAction.as_view(),
|
||||
name="website_alias_action",
|
||||
),
|
||||
path(
|
||||
"accounts/<int:pk>/measure-storage/",
|
||||
views.RequestSiteStorageMeasurement.as_view(),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from operator import attrgetter
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
@@ -85,6 +86,8 @@ from .models import (
|
||||
WebActivation,
|
||||
WebHTTPS,
|
||||
WebProvisioningPlan,
|
||||
WebsiteAlias,
|
||||
WebsiteDomain,
|
||||
WebsiteRestoreJob,
|
||||
)
|
||||
from .reseller_accounts import create_reseller_account
|
||||
@@ -107,6 +110,11 @@ from .web_node import (
|
||||
build_web_smoke_bundle,
|
||||
serialize_deployment_manifest,
|
||||
)
|
||||
from .website_aliases import (
|
||||
WebsiteAliasForm,
|
||||
queue_alias_operation,
|
||||
queue_website_alias,
|
||||
)
|
||||
|
||||
VIEW_ACCOUNTS = "hosting.view_hostingaccount"
|
||||
CHANGE_ACCOUNTS = "hosting.change_hostingaccount"
|
||||
@@ -147,6 +155,7 @@ COMPONENT_RESTORE_PERMISSIONS = {
|
||||
),
|
||||
}
|
||||
ACTIVATE_WEB = (VIEW_ACCOUNTS, VIEW_PLANS, "hosting.add_webactivation")
|
||||
MANAGE_WEBSITE_ALIASES = (VIEW_ACCOUNTS, "hosting.manage_website_aliases")
|
||||
ENABLE_WEB_HTTPS = (VIEW_ACCOUNTS, VIEW_PLANS, "hosting.add_webhttps")
|
||||
PROVISION_DATABASE = (
|
||||
VIEW_ACCOUNTS,
|
||||
@@ -248,18 +257,18 @@ class AccountCreate(PermissionRequiredMixin, AccountEditor, CreateView):
|
||||
|
||||
def form_valid(self, form):
|
||||
reseller = reseller_for_user(self.request.user)
|
||||
if reseller is None:
|
||||
return super().form_valid(form)
|
||||
|
||||
self.object = form.save(commit=False)
|
||||
try:
|
||||
create_reseller_account(reseller, self.object)
|
||||
if reseller is None:
|
||||
self.object.save()
|
||||
else:
|
||||
create_reseller_account(reseller, self.object)
|
||||
except ValidationError as error:
|
||||
form.add_error(None, error)
|
||||
return self.form_invalid(form)
|
||||
except IntegrityError:
|
||||
if not HostingAccount.objects.filter(
|
||||
primary_domain=self.object.primary_domain
|
||||
if not WebsiteDomain.objects.filter(
|
||||
domain=self.object.primary_domain
|
||||
).exists():
|
||||
raise
|
||||
form.add_error("primary_domain", "This domain is unavailable.")
|
||||
@@ -279,7 +288,18 @@ class AccountEdit(
|
||||
|
||||
def form_valid(self, form):
|
||||
self.object = form.save(commit=False)
|
||||
self.object.save(update_fields=form.changed_data)
|
||||
try:
|
||||
self.object.save(update_fields=form.changed_data)
|
||||
except ValidationError as error:
|
||||
form.add_error(None, error)
|
||||
return self.form_invalid(form)
|
||||
except IntegrityError:
|
||||
if not WebsiteDomain.objects.filter(
|
||||
domain=self.object.primary_domain
|
||||
).exists():
|
||||
raise
|
||||
form.add_error("primary_domain", "This domain is unavailable.")
|
||||
return self.form_invalid(form)
|
||||
return redirect(self.object)
|
||||
|
||||
|
||||
@@ -323,6 +343,19 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView):
|
||||
context["website_limit_pending"] = (
|
||||
quota is not None and quota.finished_at is None
|
||||
)
|
||||
if settings.LOCAL_WEB_ACTIVATION:
|
||||
context["website_aliases"] = (
|
||||
WebsiteAlias.objects.filter(reservation__account=self.object)
|
||||
.select_related("reservation")
|
||||
.order_by("reservation__domain")
|
||||
)
|
||||
if not tenant and user.has_perms(MANAGE_WEBSITE_ALIASES):
|
||||
context["manage_website_aliases"] = True
|
||||
context["can_add_website_alias"] = site_is_active(self.object)
|
||||
context["can_enable_alias_https"] = (
|
||||
settings.LOCAL_WEB_HTTPS
|
||||
and user.has_perms(ENABLE_WEB_HTTPS)
|
||||
)
|
||||
context["export_backup_manifest"] = not tenant and user.has_perms(
|
||||
EXPORT_BACKUP_MANIFEST
|
||||
)
|
||||
@@ -470,6 +503,104 @@ class SiteStorageLimitEdit(
|
||||
return redirect(self.account)
|
||||
|
||||
|
||||
class WebsiteAliasEditor(OperatorOnlyMixin, PermissionRequiredMixin, FormView):
|
||||
permission_required = MANAGE_WEBSITE_ALIASES
|
||||
template_name = "hosting/website_alias_form.html"
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if not settings.LOCAL_WEB_ACTIVATION:
|
||||
raise Http404
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
@cached_property
|
||||
def account(self):
|
||||
return get_object_or_404(HostingAccount, pk=self.kwargs["pk"])
|
||||
|
||||
def get_success_url(self):
|
||||
return self.account.get_absolute_url()
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
return super().get_context_data(account=self.account, **kwargs)
|
||||
|
||||
|
||||
class WebsiteAliasCreate(WebsiteAliasEditor):
|
||||
form_class = WebsiteAliasForm
|
||||
extra_context = {"title": "Add website alias", "submit_label": "Add alias"}
|
||||
|
||||
def form_valid(self, form):
|
||||
try:
|
||||
queue_website_alias(self.account.pk, form.cleaned_data["domain"])
|
||||
except HostingAccount.DoesNotExist:
|
||||
raise Http404 from None
|
||||
except ValidationError as error:
|
||||
form.add_error(None, error)
|
||||
return self.form_invalid(form)
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
class WebsiteAliasAction(WebsiteAliasEditor):
|
||||
form_class = forms.Form
|
||||
action_titles = {
|
||||
"https": "Enable HTTPS",
|
||||
"remove": "Remove alias",
|
||||
"retry": "Retry alias request",
|
||||
}
|
||||
|
||||
@property
|
||||
def action(self):
|
||||
value = self.kwargs["action"]
|
||||
if value not in self.action_titles:
|
||||
raise Http404
|
||||
return value
|
||||
|
||||
@cached_property
|
||||
def alias(self):
|
||||
return get_object_or_404(
|
||||
WebsiteAlias.objects.select_related("reservation"),
|
||||
pk=self.kwargs["alias_pk"],
|
||||
reservation__account=self.account,
|
||||
)
|
||||
|
||||
def can_enable_https(self):
|
||||
return settings.LOCAL_WEB_HTTPS and self.request.user.has_perms(
|
||||
ENABLE_WEB_HTTPS
|
||||
)
|
||||
|
||||
def has_permission(self):
|
||||
if not super().has_permission():
|
||||
return False
|
||||
https = self.action == "https" or (
|
||||
self.action == "retry"
|
||||
and self.alias.operation == WebsiteAlias.Operation.HTTPS
|
||||
)
|
||||
return not https or self.can_enable_https()
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
title = self.action_titles[self.action]
|
||||
return super().get_context_data(
|
||||
alias=self.alias,
|
||||
title=title,
|
||||
submit_label=title,
|
||||
action=self.action,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def form_valid(self, form):
|
||||
try:
|
||||
queue_alias_operation(
|
||||
self.account.pk,
|
||||
self.alias.pk,
|
||||
self.action,
|
||||
allow_https=self.can_enable_https(),
|
||||
)
|
||||
except (HostingAccount.DoesNotExist, WebsiteAlias.DoesNotExist):
|
||||
raise Http404 from None
|
||||
except ValidationError as error:
|
||||
form.add_error(None, error)
|
||||
return self.form_invalid(form)
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
class RequestSiteStorageMeasurement(
|
||||
OperatorOnlyMixin, PermissionRequiredMixin, View
|
||||
):
|
||||
|
||||
Reference in new issue
Block a user