diff --git a/docs/development.md b/docs/development.md index bbaab14..907f516 100644 --- a/docs/development.md +++ b/docs/development.md @@ -19,6 +19,29 @@ 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/`. +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. + +To let the reseller add accounts, set a positive account limit and select the +available hosting plans in the reseller's admin record. The reseller's account +list shows their allowance. The limit applies to self-service creation, and +operator assignments also count toward the allowance. New reseller accounts +automatically belong to that reseller and require an allowed plan. A limit of +zero keeps the reseller's access read-only. + +Platform operators manage existing accounts, assignments, reseller allowances, +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. 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 395052b..43439ae 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, WebProvisioningPlan +from .models import HostingAccount, HostingPlan, Reseller, WebProvisioningPlan @admin.register(HostingAccount) @@ -11,6 +11,20 @@ class HostingAccountAdmin(admin.ModelAdmin): ordering = ("primary_domain",) 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"): + 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: + obj.save(update_fields=form.changed_data) + else: + super().save_model(request, obj, form, change) + @admin.action(permissions=["change"], description="Request Apache plans") def request_web_plans(self, request, queryset): plans = WebProvisioningPlan.objects.bulk_create( @@ -20,6 +34,26 @@ class HostingAccountAdmin(admin.ModelAdmin): self.message_user(request, f"Queued {len(plans)} Apache plan(s).") +@admin.register(HostingPlan) +class HostingPlanAdmin(admin.ModelAdmin): + list_display = ("name", "description") + search_fields = ("name",) + + +@admin.register(Reseller) +class ResellerAdmin(admin.ModelAdmin): + list_display = ("user", "account_limit") + list_select_related = ("user",) + search_fields = ("user__username",) + filter_horizontal = ("hosting_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"): + excluded.append("hosting_plans") + return excluded + + @admin.register(WebProvisioningPlan) class WebProvisioningPlanAdmin(admin.ModelAdmin): list_display = ("primary_domain", "account", "requested_at", "completed_at") diff --git a/src/coulobre/hosting/migrations/0003_hostingplan_hostingaccount_hosting_plan.py b/src/coulobre/hosting/migrations/0003_hostingplan_hostingaccount_hosting_plan.py new file mode 100644 index 0000000..12fbd4f --- /dev/null +++ b/src/coulobre/hosting/migrations/0003_hostingplan_hostingaccount_hosting_plan.py @@ -0,0 +1,42 @@ +# Generated by Django 5.2.19 on 2026-11-19 21:07 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0002_webprovisioningplan"), + ] + + operations = [ + migrations.CreateModel( + name="HostingPlan", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=120, unique=True)), + ("description", models.CharField(blank=True, max_length=240)), + ], + options={ + "ordering": ("name", "pk"), + }, + ), + migrations.AddField( + model_name="hostingaccount", + name="hosting_plan", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + to="hosting.hostingplan", + ), + ), + ] 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/migrations/0005_reseller_account_limit_reseller_hosting_plans.py b/src/coulobre/hosting/migrations/0005_reseller_account_limit_reseller_hosting_plans.py new file mode 100644 index 0000000..7653dc4 --- /dev/null +++ b/src/coulobre/hosting/migrations/0005_reseller_account_limit_reseller_hosting_plans.py @@ -0,0 +1,22 @@ +# Generated by Django 5.2.19 on 2026-11-19 21:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0004_reseller_hostingaccount_reseller"), + ] + + operations = [ + migrations.AddField( + model_name="reseller", + name="account_limit", + field=models.PositiveIntegerField(default=0), + ), + migrations.AddField( + model_name="reseller", + name="hosting_plans", + field=models.ManyToManyField(blank=True, to="hosting.hostingplan"), + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 23eb19a..9336f68 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -1,12 +1,47 @@ +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}, + ) + account_limit = models.PositiveIntegerField(default=0) + hosting_plans = models.ManyToManyField("HostingPlan", blank=True) + + def may_create_accounts(self): + return self.account_limit > 0 and self.hosting_plans.exists() + + 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) + + class Meta: + ordering = ("name", "pk") + + def __str__(self): + return self.name + + class HostingAccount(models.Model): name = models.CharField(max_length=120) primary_domain = DomainNameField(max_length=253, unique=True) + 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/reseller_accounts.py b/src/coulobre/hosting/reseller_accounts.py new file mode 100644 index 0000000..f685312 --- /dev/null +++ b/src/coulobre/hosting/reseller_accounts.py @@ -0,0 +1,27 @@ +from django.core.exceptions import NON_FIELD_ERRORS, ValidationError +from django.db import connection, transaction +from django.db.models import F + +from .models import Reseller + + +@transaction.atomic +def create_reseller_account(access, account): + candidates = Reseller.objects.filter(pk=access.pk, user_id=access.user_id) + if connection.vendor == "sqlite": + # SQLite needs a write before the quota read to serialize writers. + candidates.update(account_limit=F("account_limit")) + reseller = candidates.select_for_update().first() + if reseller is None: + raise ValidationError({NON_FIELD_ERRORS: "Your reseller access has changed."}) + if reseller.hostingaccount_set.count() >= reseller.account_limit: + raise ValidationError( + {NON_FIELD_ERRORS: "Your hosting account limit has been reached."} + ) + if not reseller.hosting_plans.filter(pk=account.hosting_plan_id).exists(): + raise ValidationError( + {"hosting_plan": "This hosting plan is no longer available to you."} + ) + + account.reseller = reseller + account.save(force_insert=True) diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 4985ffe..986d254 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -29,7 +29,7 @@ a { text-underline-offset: 0.2em; } -button, input { +button, input, select { font: inherit; } @@ -155,6 +155,10 @@ button, input { linear-gradient(currentColor, currentColor) center 65% / 55% 1px no-repeat; } +.nav-icon-packages { + background: linear-gradient(currentColor, currentColor) center 35% / 40% 1px no-repeat; +} + .sidebar-account { display: grid; gap: 0.3rem; @@ -288,7 +292,7 @@ h2 { font-size: 0.88rem; } -input:not([type="hidden"]) { +input:not([type="hidden"]), select { min-width: 0; height: 2.55rem; padding: 0.48rem 0.68rem; @@ -463,7 +467,7 @@ dd { font-weight: 680; } -.editor input:not([type="hidden"]) { +.editor input:not([type="hidden"]), .editor select { width: 100%; } diff --git a/src/coulobre/hosting/templates/hosting/account_detail.html b/src/coulobre/hosting/templates/hosting/account_detail.html index 2865053..248e494 100644 --- a/src/coulobre/hosting/templates/hosting/account_detail.html +++ b/src/coulobre/hosting/templates/hosting/account_detail.html @@ -7,19 +7,25 @@
| Account | Primary domain | Added | |
|---|---|---|---|
| Account | +Primary domain | + {% if is_reseller or perms.hosting.view_hostingplan %}Hosting plan | {% endif %} +Added | +
| {{ account.name }} | {{ account.primary_domain }} | + {% 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" }} |