diff --git a/docs/development.md b/docs/development.md index 3f1c3f5..95d7833 100644 --- a/docs/development.md +++ b/docs/development.md @@ -23,6 +23,17 @@ Use **Hosting plans** to add named plans with an optional description. Choose a plan when creating or editing an account. Existing accounts start unassigned. Operators need permission to view hosting plans before they can assign one. +## Give a reseller access to assigned accounts + +In `/admin/`, create a normal user and add a **Reseller** record for that user. +Then open a hosting account in Coulobre Panel and select the reseller when +editing the account. Existing accounts start with no reseller assigned. + +The reseller can sign in to Coulobre Panel to see their assigned accounts, +each account's hosting plan, and its web provisioning previews. Platform +operators manage assignments and node deployment files. Reseller access in +this release is read-only. + The local database defaults to `db.sqlite3`. Set `COULOBRE_SQLITE_PATH` to use a different file. Keep `COULOBRE_DEBUG` for local development. diff --git a/src/coulobre/admin.py b/src/coulobre/admin.py new file mode 100644 index 0000000..59e9741 --- /dev/null +++ b/src/coulobre/admin.py @@ -0,0 +1,15 @@ +from django.contrib.admin import AdminSite +from django.contrib.admin.apps import AdminConfig + +from .hosting.access import reseller_for_user + + +class CoulobreAdminSite(AdminSite): + def has_permission(self, request): + return ( + super().has_permission(request) and reseller_for_user(request.user) is None + ) + + +class CoulobreAdminConfig(AdminConfig): + default_site = "coulobre.admin.CoulobreAdminSite" diff --git a/src/coulobre/hosting/access.py b/src/coulobre/hosting/access.py new file mode 100644 index 0000000..05b7d51 --- /dev/null +++ b/src/coulobre/hosting/access.py @@ -0,0 +1,31 @@ +def reseller_for_user(user): + if not user.is_authenticated or user.is_superuser: + return None + return getattr(user, "hosting_reseller", None) + + +def panel_access(request): + return {"is_reseller": reseller_for_user(request.user) is not None} + + +class OperatorOnlyMixin: + def dispatch(self, request, *args, **kwargs): + if reseller_for_user(request.user) is not None: + return self.handle_no_permission() + return super().dispatch(request, *args, **kwargs) + + +class ResellerScopeMixin: + reseller_relation = "reseller" + + def has_permission(self): + return ( + reseller_for_user(self.request.user) is not None or super().has_permission() + ) + + def get_queryset(self): + queryset = super().get_queryset() + reseller = reseller_for_user(self.request.user) + if reseller is not None: + return queryset.filter(**{f"{self.reseller_relation}_id": reseller.pk}) + return queryset diff --git a/src/coulobre/hosting/admin.py b/src/coulobre/hosting/admin.py index 6a0e11d..24ad0b5 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, WebProvisioningPlan +from .models import HostingAccount, HostingPlan, Reseller, WebProvisioningPlan @admin.register(HostingAccount) @@ -12,9 +12,12 @@ class HostingAccountAdmin(admin.ModelAdmin): actions = ("request_web_plans",) def get_exclude(self, request, obj=None): + excluded = list(super().get_exclude(request, obj) or ()) if not request.user.has_perm("hosting.view_hostingplan"): - return ("hosting_plan",) - return super().get_exclude(request, obj) + excluded.append("hosting_plan") + if not request.user.has_perm("hosting.view_reseller"): + excluded.append("reseller") + return excluded def save_model(self, request, obj, form, change): if change: @@ -37,6 +40,13 @@ class HostingPlanAdmin(admin.ModelAdmin): search_fields = ("name",) +@admin.register(Reseller) +class ResellerAdmin(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/0004_reseller_hostingaccount_reseller.py b/src/coulobre/hosting/migrations/0004_reseller_hostingaccount_reseller.py new file mode 100644 index 0000000..385b22a --- /dev/null +++ b/src/coulobre/hosting/migrations/0004_reseller_hostingaccount_reseller.py @@ -0,0 +1,48 @@ +# Generated by Django 5.2.19 on 2026-11-19 21:13 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0003_hostingplan_hostingaccount_hosting_plan"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name="Reseller", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "user", + models.OneToOneField( + limit_choices_to={"is_superuser": False}, + on_delete=django.db.models.deletion.PROTECT, + related_name="hosting_reseller", + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + migrations.AddField( + model_name="hostingaccount", + name="reseller", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + to="hosting.reseller", + ), + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index a6e9643..a33b4d6 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -1,9 +1,22 @@ +from django.conf import settings from django.db import models from django.urls import reverse from .fields import DomainNameField +class Reseller(models.Model): + user = models.OneToOneField( + settings.AUTH_USER_MODEL, + on_delete=models.PROTECT, + related_name="hosting_reseller", + limit_choices_to={"is_superuser": False}, + ) + + 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) @@ -21,6 +34,9 @@ class HostingAccount(models.Model): hosting_plan = models.ForeignKey( HostingPlan, on_delete=models.PROTECT, null=True, blank=True ) + reseller = models.ForeignKey( + Reseller, 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 103b90e..248e494 100644 --- a/src/coulobre/hosting/templates/hosting/account_detail.html +++ b/src/coulobre/hosting/templates/hosting/account_detail.html @@ -7,22 +7,25 @@

{{ account.name }}

{{ account.primary_domain }}

- {% if perms.hosting.change_hostingaccount %} + {% if not is_reseller and perms.hosting.change_hostingaccount %} Edit account {% endif %}
Primary domain
{{ account.primary_domain }}
- {% if perms.hosting.view_hostingplan %} + {% if is_reseller 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 %} +
Reseller
{{ account.reseller|default_if_none:"Unassigned" }}
+ {% endif %}
Added
{{ account.created_at|date:"j M Y, H:i" }} UTC
- {% if perms.hosting.view_webprovisioningplan %} + {% if is_reseller or perms.hosting.view_webprovisioningplan %}

Recent Apache previews

- {% if perms.hosting.change_hostingaccount %} + {% if not is_reseller and perms.hosting.change_hostingaccount %}
{% csrf_token %} diff --git a/src/coulobre/hosting/templates/hosting/account_list.html b/src/coulobre/hosting/templates/hosting/account_list.html index 4601504..748ca07 100644 --- a/src/coulobre/hosting/templates/hosting/account_list.html +++ b/src/coulobre/hosting/templates/hosting/account_list.html @@ -6,7 +6,7 @@

Hosting accounts

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

- {% if perms.hosting.add_hostingaccount %} + {% if not is_reseller and perms.hosting.add_hostingaccount %} Add hosting account {% endif %}
@@ -24,7 +24,7 @@ Account Primary domain - {% if perms.hosting.view_hostingplan %}Hosting plan{% endif %} + {% if is_reseller or perms.hosting.view_hostingplan %}Hosting plan{% endif %} Added @@ -33,7 +33,7 @@ {{ account.name }} {{ account.primary_domain }} - {% if perms.hosting.view_hostingplan %} + {% if is_reseller or perms.hosting.view_hostingplan %} {% if account.hosting_plan %}{{ account.hosting_plan.name }}{% else %}Unassigned{% endif %} {% endif %} {{ account.created_at|date:"j M Y" }} diff --git a/src/coulobre/hosting/templates/hosting/base.html b/src/coulobre/hosting/templates/hosting/base.html index de25d10..4b8b73a 100644 --- a/src/coulobre/hosting/templates/hosting/base.html +++ b/src/coulobre/hosting/templates/hosting/base.html @@ -17,19 +17,19 @@