diff --git a/README.md b/README.md index 97c6f63..835a4a8 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ Coulobre Panel is an open-source web hosting control panel for hosting providers and resellers. The project is in early development. -The development panel lets an administrator register hosting accounts and request -Apache configuration previews through a background worker. +The web panel gives operators a workspace to manage hosting accounts, request +Apache configuration previews, and follow their progress. See [Local development](docs/development.md) to run it. ## Planned scope diff --git a/docs/development.md b/docs/development.md index 0414b6f..485f810 100644 --- a/docs/development.md +++ b/docs/development.md @@ -14,10 +14,10 @@ python manage.py createsuperuser python manage.py runserver ``` -Open and sign in with the administrator account. -Under **Hosting accounts**, select **Add hosting account** and enter a name -and a primary domain such as `example.org`. The account is stored in the panel -database. +Open and sign in with the administrator account. +Select **Add hosting account** and enter a name and a primary domain such as +`example.org`. Open an account from the list to edit its details or request an +Apache preview. Manage users, groups, and permissions at `/admin/`. The local database defaults to `db.sqlite3`. Set `COULOBRE_SQLITE_PATH` to use a different file. Keep `COULOBRE_DEBUG` for local development. @@ -36,9 +36,10 @@ celery -A coulobre worker --pool=solo celery -A coulobre beat ``` -Select an account in **Hosting accounts** and run **Request Apache plans**. -Open **Web provisioning plans** to see the request and its configuration once -the worker finishes. Each request captures the primary domain at queue time. +Open an account in **Hosting accounts** and select **Request Apache preview**. +The preview page shows the request status and the configuration once the worker +finishes. Use **Refresh status** for pending requests or **Web provisioning** to +find earlier previews. Each request captures the primary domain at queue time. The preview references the future document root and PHP-FPM socket for the account. Preview generation makes no changes to the hosting services. diff --git a/pyproject.toml b/pyproject.toml index 920dc36..87dbfc6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,12 @@ dev = ["ruff>=0.11,<1"] mariadb = ["mysqlclient>=2.2.1,<3"] [tool.setuptools.package-data] -"coulobre.hosting" = ["templates/hosting/*.conf"] +"coulobre.hosting" = [ + "static/hosting/*.css", + "templates/*.html", + "templates/hosting/*.conf", + "templates/hosting/*.html", +] [tool.ruff] target-version = "py312" diff --git a/src/coulobre/hosting/admin.py b/src/coulobre/hosting/admin.py index 1237463..395052b 100644 --- a/src/coulobre/hosting/admin.py +++ b/src/coulobre/hosting/admin.py @@ -14,7 +14,7 @@ class HostingAccountAdmin(admin.ModelAdmin): @admin.action(permissions=["change"], description="Request Apache plans") def request_web_plans(self, request, queryset): plans = WebProvisioningPlan.objects.bulk_create( - WebProvisioningPlan(account=account, primary_domain=account.primary_domain) + WebProvisioningPlan.for_account(account) for account in queryset.only("primary_domain") ) self.message_user(request, f"Queued {len(plans)} Apache plan(s).") diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 6634f7c..31ad7d5 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.urls import reverse from .fields import DomainNameField @@ -8,6 +9,9 @@ class HostingAccount(models.Model): primary_domain = DomainNameField(max_length=253, unique=True) created_at = models.DateTimeField(auto_now_add=True) + def get_absolute_url(self): + return reverse("panel:account", kwargs={"pk": self.pk}) + def __str__(self): return str(self.primary_domain) @@ -20,5 +24,9 @@ class WebProvisioningPlan(models.Model): completed_at = models.DateTimeField(null=True, blank=True) apache_config = models.TextField(blank=True) + @classmethod + def for_account(cls, account): + return cls(account=account, primary_domain=account.primary_domain) + def __str__(self): return str(self.primary_domain) diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css new file mode 100644 index 0000000..4985ffe --- /dev/null +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -0,0 +1,567 @@ +:root { + color-scheme: light; + --page: #f4f5ef; + --surface: #fffefa; + --ink: #182923; + --muted: #516258; + --line: #dce3d9; + --sidebar: #142b26; + --sidebar-text: #e7f0e9; + --accent: #235440; + --accent-hover: #174332; + --focus: #8c4d2b; + --radius: 12px; + font: 100%/1.5 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; +} + +* { + box-sizing: border-box; +} + +body { + margin: 0; + background: var(--page); + color: var(--ink); +} + +a { + color: var(--accent); + text-underline-offset: 0.2em; +} + +button, input { + font: inherit; +} + +:focus-visible { + outline: 3px solid var(--focus); + outline-offset: 3px; +} + +.sidebar :focus-visible { + outline-color: #f1c68e; +} + +.visually-hidden, .skip-link:not(:focus) { + position: absolute; + width: 1px; + height: 1px; + margin: -1px; + overflow: hidden; + clip-path: inset(50%); + white-space: nowrap; +} + +.skip-link:focus { + position: fixed; + z-index: 1; + top: 1rem; + left: 1rem; + padding: 0.6rem 1rem; + background: var(--surface); +} + +.panel { + display: grid; + min-height: 100vh; + grid-template-columns: 242px minmax(0, 1fr); +} + +.sidebar { + position: sticky; + top: 0; + display: flex; + height: 100dvh; + flex-direction: column; + overflow-y: auto; + padding: 1.65rem 1.1rem 1.1rem; + background: var(--sidebar); + color: var(--sidebar-text); +} + +.brand { + display: flex; + align-items: center; + gap: 0.65rem; + padding: 0.25rem 0.45rem; + color: inherit; + font-size: 1.06rem; + font-weight: 750; + text-decoration: none; + white-space: nowrap; +} + +.brand-mark { + display: grid; + width: 2.15rem; + height: 2.15rem; + place-items: center; + border-radius: 0.55rem; + background: #d8e7bb; + color: #18372c; + font-size: 1.12rem; + font-weight: 800; +} + +.brand-product { + color: #b8c9bd; + font-weight: 500; +} + +.navigation { + display: grid; + gap: 0.35rem; + margin-top: 2.55rem; +} + +.navigation a { + display: flex; + align-items: center; + gap: 0.7rem; + padding: 0.65rem 0.75rem; + border: 1px solid transparent; + border-radius: 8px; + color: #cad8ce; + font-size: 0.9rem; + text-decoration: none; +} + +.navigation a:hover, .navigation a[aria-current="page"] { + border-color: #527369; + background: #28473e; + color: #fff; +} + +.nav-icon { + display: inline-block; + width: 1rem; + height: 0.85rem; + flex: 0 0 auto; + border: 1px solid currentColor; + border-radius: 2px; + color: #b9d5c3; +} + +.nav-icon-accounts { + background: linear-gradient(currentColor, currentColor) center / 100% 1px no-repeat; +} + +.nav-icon-plans { + width: 0.8rem; + height: 1rem; + margin-inline: 0.1rem; + background: + linear-gradient(currentColor, currentColor) center 35% / 55% 1px no-repeat, + linear-gradient(currentColor, currentColor) center 65% / 55% 1px no-repeat; +} + +.sidebar-account { + display: grid; + gap: 0.3rem; + margin-top: auto; + padding: 0.85rem 0.55rem 0; + border-top: 1px solid #3c594e; + font-size: 0.88rem; +} + +.operator { + overflow-wrap: anywhere; + font-weight: 650; +} + +.sidebar-account button { + padding: 0; + border: 0; + background: transparent; + color: #c1d4c7; + cursor: pointer; + text-decoration: underline; + text-underline-offset: 0.2em; +} + +.main { + width: 100%; + max-width: 1230px; + padding: clamp(1.25rem, 4.6vw, 4.5rem); +} + +.page-heading { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 1rem; + margin-bottom: 1.45rem; +} + +.page-heading > div { + min-width: 0; +} + +.page-heading > .button { + flex: 0 0 auto; + margin-top: 0.2rem; +} + +h1, h2, p, dl { + margin: 0; +} + +h1 { + overflow-wrap: anywhere; + font-size: clamp(1.75rem, 4vw, 2.65rem); + font-weight: 750; + letter-spacing: -0.045em; + line-height: 1.12; +} + +h2 { + font-size: 1.06rem; + font-weight: 700; +} + +.heading-meta { + margin-top: 0.5rem; + color: var(--muted); + font-size: 0.92rem; +} + +.surface { + min-width: 0; + border: 1px solid var(--line); + border-radius: var(--radius); + background: var(--surface); + box-shadow: 0 2px 5px #1a2e2010; +} + +.button { + display: inline-flex; + min-height: 2.55rem; + align-items: center; + justify-content: center; + padding: 0.48rem 0.82rem; + border: 1px solid transparent; + border-radius: 7px; + cursor: pointer; + font-size: 0.9rem; + font-weight: 650; + text-decoration: none; +} + +.primary { + background: var(--accent); + color: #fff; +} + +.primary:hover { + background: var(--accent-hover); +} + +.secondary { + border-color: #c5d1c7; + background: var(--surface); + color: var(--ink); +} + +.secondary:hover { + background: #eaf0e9; +} + +.toolbar, .section-heading, .pagination, .form-actions { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 0.6rem; +} + +.toolbar, .section-heading, .pagination { + padding: 1rem 1.2rem; +} + +.section-heading { + justify-content: space-between; +} + +.pagination { + justify-content: flex-end; + border-top: 1px solid var(--line); + color: var(--muted); + font-size: 0.88rem; +} + +input:not([type="hidden"]) { + min-width: 0; + height: 2.55rem; + padding: 0.48rem 0.68rem; + border: 1px solid #bbcbbf; + border-radius: 7px; + background: #fff; + color: var(--ink); +} + +input::placeholder { + color: var(--muted); +} + +.toolbar input { + width: min(100%, 20rem); +} + +.quiet-link, .back-link { + font-size: 0.88rem; + font-weight: 600; +} + +.back-link { + display: inline-block; + margin-bottom: 1.1rem; +} + +.table-scroll { + overflow-x: auto; +} + +table { + width: 100%; + border-collapse: collapse; + text-align: left; +} + +th, td { + padding: 0.84rem 1.2rem; + border-top: 1px solid var(--line); + overflow-wrap: anywhere; + vertical-align: top; +} + +th { + background: #edf1e9; + color: #40594b; + font-size: 0.78rem; + font-weight: 700; +} + +td { + font-size: 0.93rem; +} + +tbody tr:hover { + background: #f6f8f3; +} + +.surface > .table-scroll:first-child thead tr:first-child th { + border-top: 0; +} + +.surface > .table-scroll:first-child th:first-child { + border-top-left-radius: var(--radius); +} + +.surface > .table-scroll:first-child th:last-child { + border-top-right-radius: var(--radius); +} + +.cell-date, .status { + white-space: nowrap; +} + +.record-link { + font-weight: 650; +} + +.domain, code { + font-family: ui-monospace, "Cascadia Code", "SFMono-Regular", Consolas, monospace; + overflow-wrap: anywhere; +} + +.domain { + font-size: 0.88rem; +} + +.muted, .empty { + color: var(--muted); +} + +.empty, .notice { + padding: 1.1rem 1.2rem; + font-size: 0.94rem; +} + +.details { + display: flex; + flex-wrap: wrap; + gap: 1.1rem 3rem; + margin-bottom: 1.5rem; + padding: 1.15rem 1.25rem; +} + +.details > div { + min-width: 0; +} + +dt { + color: var(--muted); + font-size: 0.8rem; + font-weight: 650; +} + +dd { + margin: 0.28rem 0 0; + overflow-wrap: anywhere; +} + +.notice { + margin-bottom: 1.15rem; + border-left: 3px solid #8ca487; + border-radius: 0 6px 6px 0; + background: #e7eee5; +} + +.status { + display: inline-block; + padding: 0.18rem 0.55rem; + border-radius: 0.75rem; + font-size: 0.78rem; + font-weight: 680; +} + +.status.ready { + background: #dcebdc; + color: #205038; +} + +.status.pending { + background: #f5ead6; + color: #6b431b; +} + +.configuration { + margin: 0; + overflow-x: auto; + padding: 1.2rem; + border-radius: 0 0 var(--radius) var(--radius); + background: #192e28; + color: #e6efe3; + font-size: 0.88rem; + line-height: 1.6; + tab-size: 4; +} + +.editor-surface { + max-width: 600px; + padding: 1.35rem; +} + +.editor { + display: grid; + gap: 1rem; +} + +.editor label { + display: block; + margin-bottom: 0.35rem; + font-size: 0.88rem; + font-weight: 680; +} + +.editor input:not([type="hidden"]) { + width: 100%; +} + +.editor .errorlist { + margin: 0; + padding: 0 0 0 1.1rem; + color: #902d29; + font-size: 0.87rem; +} + +.editor [aria-invalid="true"] { + border-color: #902d29; +} + +.sign-in { + display: grid; + min-height: 100vh; + place-items: center; + padding: 1.5rem; + background: radial-gradient(ellipse at 25% 0%, #2b5343, var(--sidebar) 67%); +} + +.sign-in-inner { + width: min(100%, 415px); +} + +.sign-in-brand { + margin-bottom: 1.15rem; + color: var(--sidebar-text); +} + +.sign-in-card { + padding: clamp(1.3rem, 4vw, 2rem); + border-radius: 14px; + background: var(--surface); + box-shadow: 0 16px 44px #08141045; +} + +.sign-in-card h1 { + margin-bottom: 1.25rem; + font-size: 2rem; +} + +@media (max-width: 780px) { + .panel { + display: block; + } + + .sidebar { + position: static; + display: grid; + height: auto; + grid-template-columns: minmax(0, 1fr) auto; + gap: 0.65rem 0.3rem; + padding: 0.65rem 0.75rem; + } + + .brand { + font-size: 1rem; + } + + .navigation { + display: flex; + flex-wrap: wrap; + grid-column: 1 / -1; + grid-row: 2; + margin: 0; + } + + .navigation a { + padding: 0.38rem 0.55rem; + } + + .sidebar-account { + max-width: 8.5rem; + justify-items: end; + margin: 0; + padding: 0 0.35rem; + border-top: 0; + text-align: right; + } +} + +@media (max-width: 520px) { + .page-heading { + flex-direction: column; + } + + .toolbar input { + width: auto; + flex: 1 1 45%; + } + + th, td, .toolbar, .section-heading, .pagination { + padding-inline: 0.8rem; + } + + .cell-date { + display: none; + } +} diff --git a/src/coulobre/hosting/templates/403.html b/src/coulobre/hosting/templates/403.html new file mode 100644 index 0000000..5ccd8fa --- /dev/null +++ b/src/coulobre/hosting/templates/403.html @@ -0,0 +1,10 @@ +{% extends "hosting/base.html" %} +{% block title %}Access restricted · Coulobre Panel{% endblock %} +{% block content %} +
+
+

Access restricted

+

Ask your panel administrator for access to this page.

+
+
+{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/account_detail.html b/src/coulobre/hosting/templates/hosting/account_detail.html new file mode 100644 index 0000000..2865053 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/account_detail.html @@ -0,0 +1,32 @@ +{% extends "hosting/base.html" %} +{% block title %}{{ account.name }} · Coulobre Panel{% endblock %} +{% block content %} + ← Hosting accounts +
+
+

{{ account.name }}

+

{{ account.primary_domain }}

+
+ {% if perms.hosting.change_hostingaccount %} + Edit account + {% endif %} +
+
+
Primary domain
{{ account.primary_domain }}
+
Added
{{ account.created_at|date:"j M Y, H:i" }} UTC
+
+ {% if perms.hosting.view_webprovisioningplan %} +
+
+

Recent Apache previews

+ {% if perms.hosting.change_hostingaccount %} +
+ {% csrf_token %} + +
+ {% endif %} +
+ {% include "hosting/plan_table.html" %} +
+ {% endif %} +{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/account_form.html b/src/coulobre/hosting/templates/hosting/account_form.html new file mode 100644 index 0000000..3c6599e --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/account_form.html @@ -0,0 +1,16 @@ +{% extends "hosting/base.html" %} +{% block title %}{{ page_title }} · Coulobre Panel{% endblock %} +{% block content %} + ← {% if account %}{{ account.name }}{% else %}Hosting accounts{% endif %} +

{{ page_title }}

+
+
+ {% csrf_token %} + {{ form.as_div }} +
+ + Cancel +
+
+
+{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/account_list.html b/src/coulobre/hosting/templates/hosting/account_list.html new file mode 100644 index 0000000..51936b4 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/account_list.html @@ -0,0 +1,44 @@ +{% extends "hosting/base.html" %} +{% block title %}Hosting accounts · Coulobre Panel{% endblock %} +{% block content %} +
+
+

Hosting accounts

+

{{ paginator.count }} account{{ paginator.count|pluralize }}{% if search %} matching your search{% endif %}

+
+ {% if perms.hosting.add_hostingaccount %} + Add hosting account + {% endif %} +
+
+ + {% if accounts %} +
+ + + + + + {% for account in accounts %} + + + + + + {% endfor %} + +
AccountPrimary domainAdded
{{ account.name }}{{ account.primary_domain }}{{ account.created_at|date:"j M Y" }}
+
+ {% elif search %} +

No hosting accounts match this search.

+ {% else %} +

No hosting accounts yet.

+ {% endif %} + {% include "hosting/pagination.html" %} +
+{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/base.html b/src/coulobre/hosting/templates/hosting/base.html new file mode 100644 index 0000000..7e39373 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/base.html @@ -0,0 +1,50 @@ +{% load static %} + + + + + {% block title %}Coulobre Panel{% endblock %} + + + +{% if request.user.is_authenticated %} + +
+ +
+ {% block content %}{% endblock %} +
+
+{% else %} + {% block public_content %}{% endblock %} +{% endif %} + + diff --git a/src/coulobre/hosting/templates/hosting/login.html b/src/coulobre/hosting/templates/hosting/login.html new file mode 100644 index 0000000..d92125e --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/login.html @@ -0,0 +1,21 @@ +{% extends "hosting/base.html" %} +{% block title %}Sign in · Coulobre Panel{% endblock %} +{% block public_content %} +
+ +
+{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/pagination.html b/src/coulobre/hosting/templates/hosting/pagination.html new file mode 100644 index 0000000..6c88c8a --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/pagination.html @@ -0,0 +1,7 @@ +{% if is_paginated %} + +{% endif %} diff --git a/src/coulobre/hosting/templates/hosting/plan_detail.html b/src/coulobre/hosting/templates/hosting/plan_detail.html new file mode 100644 index 0000000..6d2d155 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/plan_detail.html @@ -0,0 +1,30 @@ +{% extends "hosting/base.html" %} +{% block title %}Apache preview · Coulobre Panel{% endblock %} +{% block content %} + ← Web provisioning +
+
+

Apache preview

+

{{ plan.primary_domain }}

+
+ {% if not plan.completed_at %} + Refresh status + {% endif %} +
+
+
Status
{% include "hosting/plan_status.html" %}
+
Hosting account
{% if perms.hosting.view_hostingaccount %}{{ plan.account.name }}{% else %}{{ plan.account.name }}{% endif %}
+
Requested
{{ plan.requested_at|date:"j M Y, H:i" }} UTC
+
+ {% if plan.primary_domain != plan.account.primary_domain %} +

This preview captures the original domain. The hosting account now uses {{ plan.account.primary_domain }}.

+ {% endif %} + {% if plan.completed_at %} +
+

Virtual host configuration

+
{{ plan.apache_config }}
+
+ {% else %} +

The configuration will appear here after the worker completes the request.

+ {% endif %} +{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/plan_list.html b/src/coulobre/hosting/templates/hosting/plan_list.html new file mode 100644 index 0000000..e5a437d --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/plan_list.html @@ -0,0 +1,14 @@ +{% extends "hosting/base.html" %} +{% block title %}Web provisioning · Coulobre Panel{% endblock %} +{% block content %} +
+
+

Web provisioning

+

{{ paginator.count }} Apache preview{{ paginator.count|pluralize }}

+
+
+
+ {% include "hosting/plan_table.html" %} + {% include "hosting/pagination.html" %} +
+{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/plan_status.html b/src/coulobre/hosting/templates/hosting/plan_status.html new file mode 100644 index 0000000..fae3572 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/plan_status.html @@ -0,0 +1,7 @@ +{% if plan.completed_at %} + Ready +{% elif plan.last_dispatched_at %} + Dispatched +{% else %} + Queued +{% endif %} diff --git a/src/coulobre/hosting/templates/hosting/plan_table.html b/src/coulobre/hosting/templates/hosting/plan_table.html new file mode 100644 index 0000000..381386c --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/plan_table.html @@ -0,0 +1,18 @@ +{% if plans %} +
+ + + + {% for plan in plans %} + + + + + + {% endfor %} + +
Primary domainRequestedStatus
{{ plan.primary_domain }}{{ plan.requested_at|date:"j M Y, H:i" }} UTC{% include "hosting/plan_status.html" %}
+
+{% else %} +

No Apache previews yet.

+{% endif %} diff --git a/src/coulobre/hosting/test_panel.py b/src/coulobre/hosting/test_panel.py new file mode 100644 index 0000000..c041404 --- /dev/null +++ b/src/coulobre/hosting/test_panel.py @@ -0,0 +1,117 @@ +from django.contrib.auth import get_user_model +from django.contrib.auth.models import Permission +from django.test import Client, TestCase +from django.urls import reverse + +from .models import HostingAccount, WebProvisioningPlan +from .tasks import render_web_plan + + +class PanelTests(TestCase): + @classmethod + def setUpTestData(cls): + users = get_user_model().objects + cls.login_password = "test-only-coulobre-password" + cls.operator = users.create_superuser("operator", password=cls.login_password) + cls.viewer = users.create_user("viewer") + cls.viewer.user_permissions.add( + Permission.objects.get( + content_type__app_label="hosting", codename="view_hostingaccount" + ) + ) + cls.account = HostingAccount.objects.create( + name="Example hosting", primary_domain="example.org" + ) + + def test_login_returns_operator_to_panel_and_logout_ends_session(self): + account_url = self.account.get_absolute_url() + login_url = reverse("panel:login") + self.assertRedirects( + self.client.get(account_url), f"{login_url}?next={account_url}" + ) + self.assertContains(self.client.get(login_url), "/static/hosting/panel.css") + + response = self.client.post( + login_url, + { + "username": "operator", + "password": self.login_password, + "next": account_url, + }, + ) + self.assertRedirects(response, account_url) + self.assertRedirects(self.client.post(reverse("panel:logout")), login_url) + self.assertRedirects( + self.client.get(account_url), f"{login_url}?next={account_url}" + ) + + def test_operator_can_add_find_and_edit_account(self): + self.client.force_login(self.operator) + response = self.client.post( + reverse("panel:account_create"), + {"name": "Test tenant", "primary_domain": "TEST-TENANT.example."}, + ) + account = HostingAccount.objects.get(name="Test tenant") + self.assertRedirects(response, account.get_absolute_url()) + + response = self.client.get(reverse("panel:accounts"), {"q": "test-tenant"}) + self.assertContains(response, "test-tenant.example") + self.assertNotContains(response, "example.org") + + response = self.client.post( + reverse("panel:account_edit", args=[account.pk]), + {"name": "Updated tenant", "primary_domain": "updated.example"}, + ) + self.assertRedirects(response, account.get_absolute_url()) + account.refresh_from_db() + self.assertEqual(account.primary_domain, "updated.example") + + def test_operator_can_queue_and_view_an_apache_preview(self): + self.client.force_login(self.operator) + request_url = reverse("panel:request_plan", args=[self.account.pk]) + self.assertEqual(self.client.get(request_url).status_code, 405) + response = self.client.post(request_url) + plan = WebProvisioningPlan.objects.get() + preview_url = reverse("panel:plan", args=[plan.pk]) + self.assertRedirects(response, preview_url) + self.assertContains(self.client.get(preview_url), "Queued") + + render_web_plan.run(plan.pk) + + self.assertContains(self.client.get(preview_url), "ServerName example.org") + self.assertContains(self.client.get(reverse("panel:plans")), "Ready") + + def test_viewer_cannot_mutate_accounts_or_read_previews(self): + plan = WebProvisioningPlan.for_account(self.account) + plan.save() + self.client.force_login(self.viewer) + account = self.client.get(self.account.get_absolute_url()) + self.assertContains(account, "Example hosting") + self.assertNotContains(account, "Request Apache preview") + self.assertEqual( + self.client.get(reverse("panel:account_create")).status_code, 403 + ) + self.assertEqual( + self.client.post( + reverse("panel:account_edit", args=[self.account.pk]) + ).status_code, + 403, + ) + self.assertEqual( + self.client.post( + reverse("panel:request_plan", args=[self.account.pk]) + ).status_code, + 403, + ) + self.assertEqual( + self.client.get(reverse("panel:plan", args=[plan.pk])).status_code, 403 + ) + self.assertEqual(WebProvisioningPlan.objects.count(), 1) + + def test_requesting_a_preview_requires_a_csrf_token(self): + browser = Client(enforce_csrf_checks=True) + browser.force_login(self.operator) + response = browser.post(reverse("panel:request_plan", args=[self.account.pk])) + + self.assertEqual(response.status_code, 403) + self.assertFalse(WebProvisioningPlan.objects.exists()) diff --git a/src/coulobre/hosting/urls.py b/src/coulobre/hosting/urls.py new file mode 100644 index 0000000..2202e4d --- /dev/null +++ b/src/coulobre/hosting/urls.py @@ -0,0 +1,27 @@ +from django.contrib.auth.views import LoginView, LogoutView +from django.urls import path + +from . import views + +app_name = "panel" + +urlpatterns = [ + path("", views.AccountList.as_view(), name="accounts"), + path( + "sign-in/", + LoginView.as_view( + template_name="hosting/login.html", + redirect_authenticated_user=True, + ), + name="login", + ), + path("sign-out/", LogoutView.as_view(), name="logout"), + path("accounts/new/", views.AccountCreate.as_view(), name="account_create"), + path("accounts//", views.AccountDetail.as_view(), name="account"), + path("accounts//edit/", views.AccountEdit.as_view(), name="account_edit"), + path( + "accounts//plans/", views.RequestWebPlan.as_view(), name="request_plan" + ), + path("plans/", views.PlanList.as_view(), name="plans"), + path("plans//", views.PlanDetail.as_view(), name="plan"), +] diff --git a/src/coulobre/hosting/views.py b/src/coulobre/hosting/views.py new file mode 100644 index 0000000..f2f1e77 --- /dev/null +++ b/src/coulobre/hosting/views.py @@ -0,0 +1,95 @@ +from django.contrib.auth.mixins import PermissionRequiredMixin +from django.db.models import Q +from django.shortcuts import redirect +from django.views import View +from django.views.generic import CreateView, DetailView, ListView, UpdateView +from django.views.generic.detail import SingleObjectMixin + +from .models import HostingAccount, WebProvisioningPlan + +VIEW_ACCOUNTS = "hosting.view_hostingaccount" +CHANGE_ACCOUNTS = "hosting.change_hostingaccount" +VIEW_PLANS = "hosting.view_webprovisioningplan" + + +class AccountList(PermissionRequiredMixin, ListView): + model = HostingAccount + permission_required = VIEW_ACCOUNTS + template_name = "hosting/account_list.html" + context_object_name = "accounts" + paginate_by = 20 + + def get_queryset(self): + self.search = self.request.GET.get("q", "").strip()[:253] + accounts = HostingAccount.objects.order_by("primary_domain") + if self.search: + accounts = accounts.filter( + Q(name__icontains=self.search) + | Q(primary_domain__icontains=self.search) + ) + return accounts + + def get_context_data(self, **kwargs): + return super().get_context_data(search=self.search, **kwargs) + + +class AccountCreate(PermissionRequiredMixin, CreateView): + model = HostingAccount + permission_required = (VIEW_ACCOUNTS, "hosting.add_hostingaccount") + template_name = "hosting/account_form.html" + fields = ("name", "primary_domain") + extra_context = {"page_title": "Add hosting account", "submit_label": "Add account"} + + +class AccountEdit(PermissionRequiredMixin, UpdateView): + model = HostingAccount + permission_required = (VIEW_ACCOUNTS, CHANGE_ACCOUNTS) + template_name = "hosting/account_form.html" + context_object_name = "account" + fields = ("name", "primary_domain") + extra_context = { + "page_title": "Edit hosting account", + "submit_label": "Save changes", + } + + +class AccountDetail(PermissionRequiredMixin, DetailView): + model = HostingAccount + permission_required = VIEW_ACCOUNTS + template_name = "hosting/account_detail.html" + context_object_name = "account" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + if self.request.user.has_perm(VIEW_PLANS): + context["plans"] = self.object.webprovisioningplan_set.order_by( + "-requested_at", "-pk" + )[:10] + return context + + +class RequestWebPlan(PermissionRequiredMixin, SingleObjectMixin, View): + model = HostingAccount + permission_required = (VIEW_ACCOUNTS, CHANGE_ACCOUNTS, VIEW_PLANS) + + def post(self, request, *args, **kwargs): + plan = WebProvisioningPlan.for_account(self.get_object()) + plan.save() + return redirect("panel:plan", pk=plan.pk) + + +class PlanList(PermissionRequiredMixin, ListView): + queryset = WebProvisioningPlan.objects.select_related("account").order_by( + "-requested_at", "-pk" + ) + permission_required = VIEW_PLANS + template_name = "hosting/plan_list.html" + context_object_name = "plans" + paginate_by = 20 + + +class PlanDetail(PermissionRequiredMixin, DetailView): + queryset = WebProvisioningPlan.objects.select_related("account") + permission_required = VIEW_PLANS + template_name = "hosting/plan_detail.html" + context_object_name = "plan" diff --git a/src/coulobre/settings.py b/src/coulobre/settings.py index 3ed8298..d6ce52e 100644 --- a/src/coulobre/settings.py +++ b/src/coulobre/settings.py @@ -85,6 +85,9 @@ AUTH_PASSWORD_VALIDATORS = [ TIME_ZONE = "UTC" STATIC_URL = "/static/" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" +LOGIN_URL = "panel:login" +LOGIN_REDIRECT_URL = "panel:accounts" +LOGOUT_REDIRECT_URL = "panel:login" CELERY_BROKER_URL = os.environ.get( "COULOBRE_BROKER_URL", "amqp://guest:guest@127.0.0.1:5672//" diff --git a/src/coulobre/urls.py b/src/coulobre/urls.py index 979a96d..9ba6ad0 100644 --- a/src/coulobre/urls.py +++ b/src/coulobre/urls.py @@ -1,12 +1,11 @@ from django.contrib import admin -from django.urls import path -from django.views.generic import RedirectView +from django.urls import include, path admin.site.site_header = "Coulobre Panel" admin.site.site_title = "Coulobre Panel" admin.site.index_title = "Administration" urlpatterns = [ - path("", RedirectView.as_view(pattern_name="admin:index")), path("admin/", admin.site.urls), + path("", include("coulobre.hosting.urls")), ]