From 8de6eb8d88df6dd3ce2e6789161f15434736b7f8 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Sat, 21 Nov 2026 00:23:52 +0100 Subject: [PATCH] Add account-scoped access for hosting customers --- docs/development.md | 12 ++ src/coulobre/admin.py | 6 +- src/coulobre/hosting/access.py | 36 +++- src/coulobre/hosting/admin.py | 11 +- .../migrations/0011_customer_access.py | 65 ++++++ src/coulobre/hosting/models.py | 21 +- .../templates/hosting/account_detail.html | 39 ++-- .../templates/hosting/account_list.html | 6 +- .../hosting/templates/hosting/base.html | 6 +- src/coulobre/hosting/test_customers.py | 189 ++++++++++++++++++ src/coulobre/hosting/views.py | 50 +++-- 11 files changed, 390 insertions(+), 51 deletions(-) create mode 100644 src/coulobre/hosting/migrations/0011_customer_access.py create mode 100644 src/coulobre/hosting/test_customers.py diff --git a/docs/development.md b/docs/development.md index 1da7d02..93d9b1c 100644 --- a/docs/development.md +++ b/docs/development.md @@ -45,6 +45,18 @@ and node deployment files. The local database defaults to `db.sqlite3`. Set `COULOBRE_SQLITE_PATH` to use a different file. Keep `COULOBRE_DEBUG` for local development. +## Give a customer access to hosting accounts + +In `/admin/`, create a user with a password and add a **Customer** record for +that user. Open **Edit account** in Coulobre Panel and select the customer. +Operators need permission to view customers to make the assignment. Repeat the +assignment for each hosting account the customer should be able to access. + +The customer can sign in to Coulobre Panel with that username and password. +Their account list shows only the hosting accounts assigned to them. Each +account page shows its hosting plan, database requests, DNS zones, and mailbox +requests. Customer access is read-only. + ## Request a MariaDB database Open a hosting account and select **Request database**. Choose a short suffix diff --git a/src/coulobre/admin.py b/src/coulobre/admin.py index 59e9741..3f2679b 100644 --- a/src/coulobre/admin.py +++ b/src/coulobre/admin.py @@ -1,14 +1,12 @@ from django.contrib.admin import AdminSite from django.contrib.admin.apps import AdminConfig -from .hosting.access import reseller_for_user +from .hosting.access import is_tenant class CoulobreAdminSite(AdminSite): def has_permission(self, request): - return ( - super().has_permission(request) and reseller_for_user(request.user) is None - ) + return super().has_permission(request) and not is_tenant(request.user) class CoulobreAdminConfig(AdminConfig): diff --git a/src/coulobre/hosting/access.py b/src/coulobre/hosting/access.py index 05b7d51..e585e32 100644 --- a/src/coulobre/hosting/access.py +++ b/src/coulobre/hosting/access.py @@ -1,30 +1,58 @@ -def reseller_for_user(user): +def customer_for_user(user): if not user.is_authenticated or user.is_superuser: return None + return getattr(user, "hosting_customer", None) + + +def reseller_for_user(user): + if ( + not user.is_authenticated + or user.is_superuser + or customer_for_user(user) is not None + ): + return None return getattr(user, "hosting_reseller", None) +def is_tenant(user): + return customer_for_user(user) is not None or reseller_for_user(user) is not None + + def panel_access(request): - return {"is_reseller": reseller_for_user(request.user) is not None} + customer = customer_for_user(request.user) is not None + reseller = not customer and reseller_for_user(request.user) is not None + return { + "is_reseller": reseller, + "is_tenant": customer or reseller, + "is_operator": request.user.is_authenticated and not (customer or reseller), + } class OperatorOnlyMixin: def dispatch(self, request, *args, **kwargs): - if reseller_for_user(request.user) is not None: + if is_tenant(request.user): return self.handle_no_permission() return super().dispatch(request, *args, **kwargs) -class ResellerScopeMixin: +class TenantScopeMixin: reseller_relation = "reseller" + customer_relation = None def has_permission(self): + if customer_for_user(self.request.user) is not None: + return self.customer_relation is not None return ( reseller_for_user(self.request.user) is not None or super().has_permission() ) def get_queryset(self): queryset = super().get_queryset() + customer = customer_for_user(self.request.user) + if customer is not None: + if self.customer_relation is None: + return queryset.none() + return queryset.filter(**{f"{self.customer_relation}_id": customer.pk}) reseller = reseller_for_user(self.request.user) if reseller is not None: return queryset.filter(**{f"{self.reseller_relation}_id": reseller.pk}) diff --git a/src/coulobre/hosting/admin.py b/src/coulobre/hosting/admin.py index 43439ae..c1ec0a8 100644 --- a/src/coulobre/hosting/admin.py +++ b/src/coulobre/hosting/admin.py @@ -1,7 +1,7 @@ from django.contrib import admin from django.utils.html import format_html -from .models import HostingAccount, HostingPlan, Reseller, WebProvisioningPlan +from .models import Customer, HostingAccount, HostingPlan, Reseller, WebProvisioningPlan @admin.register(HostingAccount) @@ -17,6 +17,8 @@ class HostingAccountAdmin(admin.ModelAdmin): excluded.append("hosting_plan") if not request.user.has_perm("hosting.view_reseller"): excluded.append("reseller") + if not request.user.has_perm("hosting.view_customer"): + excluded.append("customer") return excluded def save_model(self, request, obj, form, change): @@ -54,6 +56,13 @@ class ResellerAdmin(admin.ModelAdmin): return excluded +@admin.register(Customer) +class CustomerAdmin(admin.ModelAdmin): + list_display = ("user",) + list_select_related = ("user",) + search_fields = ("user__username",) + + @admin.register(WebProvisioningPlan) class WebProvisioningPlanAdmin(admin.ModelAdmin): list_display = ("primary_domain", "account", "requested_at", "completed_at") diff --git a/src/coulobre/hosting/migrations/0011_customer_access.py b/src/coulobre/hosting/migrations/0011_customer_access.py new file mode 100644 index 0000000..fd52772 --- /dev/null +++ b/src/coulobre/hosting/migrations/0011_customer_access.py @@ -0,0 +1,65 @@ +# Generated by Django 5.2.19 on 2026-11-20 23:20 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0010_directadminimport"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.AlterField( + model_name="reseller", + name="user", + field=models.OneToOneField( + limit_choices_to={ + "hosting_customer__isnull": True, + "is_superuser": False, + }, + on_delete=django.db.models.deletion.PROTECT, + related_name="hosting_reseller", + to=settings.AUTH_USER_MODEL, + ), + ), + migrations.CreateModel( + name="Customer", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "user", + models.OneToOneField( + limit_choices_to={ + "hosting_reseller__isnull": True, + "is_staff": False, + "is_superuser": False, + }, + on_delete=django.db.models.deletion.PROTECT, + related_name="hosting_customer", + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + migrations.AddField( + model_name="hostingaccount", + name="customer", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + to="hosting.customer", + ), + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 75add91..671fce9 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -21,7 +21,7 @@ class Reseller(models.Model): settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name="hosting_reseller", - limit_choices_to={"is_superuser": False}, + limit_choices_to={"is_superuser": False, "hosting_customer__isnull": True}, ) account_limit = models.PositiveIntegerField(default=0) hosting_plans = models.ManyToManyField("HostingPlan", blank=True) @@ -33,6 +33,22 @@ class Reseller(models.Model): return self.user.get_username() +class Customer(models.Model): + user = models.OneToOneField( + settings.AUTH_USER_MODEL, + on_delete=models.PROTECT, + related_name="hosting_customer", + limit_choices_to={ + "is_staff": False, + "is_superuser": False, + "hosting_reseller__isnull": True, + }, + ) + + def __str__(self): + return self.user.get_username() + + class HostingPlan(models.Model): name = models.CharField(max_length=120, unique=True) description = models.CharField(max_length=240, blank=True) @@ -53,6 +69,9 @@ class HostingAccount(models.Model): reseller = models.ForeignKey( Reseller, on_delete=models.PROTECT, null=True, blank=True ) + customer = models.ForeignKey( + Customer, on_delete=models.PROTECT, null=True, blank=True + ) created_at = models.DateTimeField(auto_now_add=True) def get_absolute_url(self): diff --git a/src/coulobre/hosting/templates/hosting/account_detail.html b/src/coulobre/hosting/templates/hosting/account_detail.html index 3ee9cf2..a5adee4 100644 --- a/src/coulobre/hosting/templates/hosting/account_detail.html +++ b/src/coulobre/hosting/templates/hosting/account_detail.html @@ -7,25 +7,28 @@

{{ account.name }}

{{ account.primary_domain }}

- {% if not is_reseller and perms.hosting.change_hostingaccount %} + {% if is_operator and perms.hosting.change_hostingaccount %} Edit account {% endif %}
Primary domain
{{ account.primary_domain }}
- {% if is_reseller or perms.hosting.view_hostingplan %} + {% if is_tenant or perms.hosting.view_hostingplan %}
Hosting plan
{% if account.hosting_plan %}{{ account.hosting_plan.name }}{% else %}Unassigned{% endif %}
{% endif %} - {% if not is_reseller and perms.hosting.view_reseller %} + {% if is_operator and perms.hosting.view_reseller %}
Reseller
{{ account.reseller|default_if_none:"Unassigned" }}
{% endif %} + {% if is_operator and perms.hosting.view_customer %} +
Customer
{{ account.customer|default_if_none:"Unassigned" }}
+ {% endif %}
Added
{{ account.created_at|date:"j M Y, H:i" }} UTC
- {% if is_reseller or perms.hosting.view_webprovisioningplan %} + {% if is_reseller or is_operator and perms.hosting.view_webprovisioningplan %}

Recent Apache previews

- {% if not is_reseller and perms.hosting.change_hostingaccount %} + {% if is_operator and perms.hosting.change_hostingaccount %}
{% csrf_token %} @@ -35,23 +38,23 @@ {% include "hosting/plan_table.html" %}
{% endif %} - {% if is_reseller or perms.hosting.view_hostingdatabase %} + {% if is_tenant or perms.hosting.view_hostingdatabase %}

MariaDB database requests

- {% if not is_reseller and perms.hosting.add_hostingdatabase %} + {% if is_operator and perms.hosting.add_hostingdatabase %} Request database {% endif %}
{% if databases %}
- {% if not is_reseller %}{% endif %} + {% if is_operator %}{% endif %} {% for database in databases %} - {% if not is_reseller %} + {% if is_operator %} {% endif %} @@ -64,11 +67,11 @@ {% endif %} {% endif %} - {% if is_reseller or perms.hosting.view_hostingdnszone %} + {% if is_tenant or perms.hosting.view_hostingdnszone %}

DNS zones

- {% if not is_reseller and perms.hosting.add_hostingdnszone and not has_primary_zone %} + {% if is_operator and perms.hosting.add_hostingdnszone and not has_primary_zone %} Add DNS zone {% endif %}
@@ -79,21 +82,21 @@
- {% if not is_reseller %}{% endif %} + {% if is_operator %}{% endif %} {% for zone in dns_zones %} - {% if not is_reseller %} + {% if is_operator %} {% endif %} @@ -106,11 +109,11 @@ {% endif %} {% endif %} - {% if is_reseller or perms.hosting.view_hostingmailbox %} + {% if is_tenant or perms.hosting.view_hostingmailbox %}

Mailbox requests

- {% if not is_reseller and perms.hosting.add_hostingmailbox %} + {% if is_operator and perms.hosting.add_hostingmailbox %} Request mailbox {% endif %}
@@ -120,14 +123,14 @@
- {% if not is_reseller %}{% endif %} + {% if is_operator %}{% endif %} {% for mailbox in mailboxes %} - {% if not is_reseller %} + {% if is_operator %} {% endif %} diff --git a/src/coulobre/hosting/templates/hosting/account_list.html b/src/coulobre/hosting/templates/hosting/account_list.html index ba3a507..669bcc0 100644 --- a/src/coulobre/hosting/templates/hosting/account_list.html +++ b/src/coulobre/hosting/templates/hosting/account_list.html @@ -7,7 +7,7 @@

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

{% if account_allowance %}

{{ account_allowance.used }} of {{ account_allowance.limit }} accounts allocated

{% endif %} - {% if account_allowance and account_allowance.used < account_allowance.limit or not is_reseller and perms.hosting.add_hostingaccount %} + {% if account_allowance and account_allowance.used < account_allowance.limit or is_operator and perms.hosting.add_hostingaccount %} Add hosting account {% endif %} @@ -25,7 +25,7 @@ - {% if is_reseller or perms.hosting.view_hostingplan %}{% endif %} + {% if is_tenant or perms.hosting.view_hostingplan %}{% endif %} @@ -34,7 +34,7 @@ - {% if is_reseller or perms.hosting.view_hostingplan %} + {% if is_tenant or perms.hosting.view_hostingplan %} {% endif %} diff --git a/src/coulobre/hosting/templates/hosting/base.html b/src/coulobre/hosting/templates/hosting/base.html index efde25d..b44bfae 100644 --- a/src/coulobre/hosting/templates/hosting/base.html +++ b/src/coulobre/hosting/templates/hosting/base.html @@ -17,19 +17,19 @@
DatabaseDeployment
DatabaseDeployment
{{ database }}Download manifest
Domain SOA serialDeploymentDeployment
- {% if not is_reseller and perms.hosting.change_hostingdnszone %} + {% if is_operator and perms.hosting.change_hostingdnszone %} {{ zone.domain }} {% else %} {{ zone.domain }} {% endif %} {{ zone.serial }}Download manifest
Email addressDeploymentDeployment
{{ mailbox }}Download manifest
Account Primary domainHosting planHosting planAdded
{{ account.name }} {{ account.primary_domain }}{% if account.hosting_plan %}{{ account.hosting_plan.name }}{% else %}Unassigned{% endif %}{{ account.created_at|date:"j M Y" }}