diff --git a/README.md b/README.md index 835a4a8..9ef27ed 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ See [Local development](docs/development.md) to run it. - Self-hosted installation on a single Ubuntu server, with Debian support planned. - Hosting plans and self-service access for administrators, resellers, and customers. +- Customer impersonation for administrator support, with a dedicated permission, + a visible session indicator, and an audit trail. - Domains, TLS certificates, SFTP, and databases using Apache, PHP-FPM, and MariaDB. - Mail and DNS on the hosting server or on separate servers. - Account backups, restore, and migration from DirectAdmin. diff --git a/docs/development.md b/docs/development.md index 1da7d02..5cbddee 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. +Set a database limit and a mailbox limit on a hosting plan to cap how many +requests each assigned account can make through the panel. A blank limit allows +unlimited requests. Zero blocks new requests of that type. Accounts without a +plan have unlimited requests. The account page shows usage for configured +limits. Mailboxes count toward the same limit across all of an account's mail +domains, including domains retained after a primary domain change. + +Changing or reassigning a plan preserves existing databases and mailboxes. If +the new limit is at or below current usage, the panel blocks additional +requests of that type. + ## Give a reseller access to assigned accounts In `/admin/`, create a normal user and add a **Reseller** record for that user. @@ -45,6 +56,29 @@ 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 Coulobre Panel, open **Customers** and select **Add customer**. Enter a +username and initial password. Select the customer when adding or editing a +hosting account. Repeat the assignment for each hosting account the customer +should be able to access. To give an existing user customer access, add a +**Customer** record for that user in `/admin/`. + +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. Customers can request databases and mailboxes for their own accounts +within their plan limits. Operators deploy those requests on the hosting nodes. + +Use **Suspend sign-in** in the customer list to block panel access for that +customer across all their hosting accounts. Use **Restore sign-in** to give +them access again. Account assignments are preserved through suspension. + +Operators need the **Can view customer** permission to use the customer list +and select a customer on hosting accounts they can create or edit. **Can add +customer** enables customer creation. **Can change customer** enables +sign-in suspension and restoration. + ## 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..3145f11 100644 --- a/src/coulobre/hosting/access.py +++ b/src/coulobre/hosting/access.py @@ -1,30 +1,59 @@ -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_customer": customer, + "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..e85e797 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): @@ -36,9 +38,15 @@ class HostingAccountAdmin(admin.ModelAdmin): @admin.register(HostingPlan) class HostingPlanAdmin(admin.ModelAdmin): - list_display = ("name", "description") + list_display = ("name", "description", "database_limit", "mailbox_limit") search_fields = ("name",) + 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.register(Reseller) class ResellerAdmin(admin.ModelAdmin): @@ -54,6 +62,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/migrations/0012_hosting_plan_quotas.py b/src/coulobre/hosting/migrations/0012_hosting_plan_quotas.py new file mode 100644 index 0000000..f23b1c7 --- /dev/null +++ b/src/coulobre/hosting/migrations/0012_hosting_plan_quotas.py @@ -0,0 +1,30 @@ +# Generated by Django 5.2.19 on 2026-11-20 23:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0011_customer_access"), + ] + + operations = [ + migrations.AddField( + model_name="hostingplan", + name="database_limit", + field=models.PositiveIntegerField( + blank=True, + help_text="Leave blank for unlimited. Enter 0 to block new database requests.", + null=True, + ), + ), + migrations.AddField( + model_name="hostingplan", + name="mailbox_limit", + field=models.PositiveIntegerField( + blank=True, + help_text="Leave blank for unlimited. Enter 0 to block new mailbox requests.", + null=True, + ), + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 75add91..cd8270c 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,9 +33,35 @@ 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) + database_limit = models.PositiveIntegerField( + blank=True, + null=True, + help_text="Leave blank for unlimited. Enter 0 to block new database requests.", + ) + mailbox_limit = models.PositiveIntegerField( + blank=True, + null=True, + help_text="Leave blank for unlimited. Enter 0 to block new mailbox requests.", + ) class Meta: ordering = ("name", "pk") @@ -53,6 +79,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/resource_requests.py b/src/coulobre/hosting/resource_requests.py new file mode 100644 index 0000000..b29f83e --- /dev/null +++ b/src/coulobre/hosting/resource_requests.py @@ -0,0 +1,62 @@ +from django.core.exceptions import ValidationError +from django.db import connection, transaction +from django.db.models import F + +from coulobre.mail_node import MailboxSpec + +from .models import HostingAccount, HostingMailbox, HostingMailDomain + + +def lock_hosting_account(account_id, customer_id): + accounts = HostingAccount.objects.filter(pk=account_id) + if customer_id is not None: + accounts = accounts.filter(customer_id=customer_id) + if connection.vendor == "sqlite": + # Acquire the write lock before reading the account and resource counts. + accounts.update(hosting_plan=F("hosting_plan")) + return accounts.select_for_update().get() + + +@transaction.atomic +def create_database_request(account_id, suffix, *, customer_id=None): + account = lock_hosting_account(account_id, customer_id) + databases = account.databases + if databases.filter(suffix=suffix).exists(): + raise ValidationError( + {"suffix": "This account already has a database with that suffix."} + ) + + plan = account.hosting_plan + if ( + plan + and plan.database_limit is not None + and databases.count() >= plan.database_limit + ): + raise ValidationError("This hosting account has reached its database limit.") + return databases.create(suffix=suffix) + + +@transaction.atomic +def create_mailbox_request(account_id, local_part, *, customer_id=None): + account = lock_hosting_account(account_id, customer_id) + try: + spec = MailboxSpec(account.primary_domain, local_part) + except ValueError as error: + raise ValidationError({"local_part": str(error)}) from error + + mail_domain, _ = HostingMailDomain.objects.get_or_create( + domain=spec.domain, defaults={"account": account} + ) + if mail_domain.account_id != account.pk: + raise ValidationError("This mail domain belongs to another hosting account.") + if mail_domain.mailboxes.filter(local_part=spec.local_part).exists(): + raise ValidationError( + {"local_part": "This mailbox has already been requested."} + ) + + plan = account.hosting_plan + if plan and plan.mailbox_limit is not None: + mailboxes = HostingMailbox.objects.filter(mail_domain__account=account) + if mailboxes.count() >= plan.mailbox_limit: + raise ValidationError("This hosting account has reached its mailbox limit.") + return mail_domain.mailboxes.create(local_part=spec.local_part) diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 3328f18..bcc92ed 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -159,6 +159,25 @@ button, input, select, textarea { background: linear-gradient(currentColor, currentColor) center 35% / 40% 1px no-repeat; } +.nav-icon-customers { + position: relative; + height: 0.46rem; + margin-top: 0.47rem; + border-radius: 0.4rem 0.4rem 2px 2px; +} + +.nav-icon-customers::before { + position: absolute; + left: 50%; + bottom: calc(100% + 2px); + width: 0.39rem; + height: 0.39rem; + transform: translateX(-50%); + border: 1px solid currentColor; + border-radius: 50%; + content: ""; +} + .sidebar-account { display: grid; gap: 0.3rem; @@ -439,12 +458,12 @@ dd { font-weight: 680; } -.status.ready { +.status.ready, .status.active { background: #dcebdc; color: #205038; } -.status.pending { +.status.pending, .status.suspended { background: #f5ead6; color: #6b431b; } diff --git a/src/coulobre/hosting/templates/hosting/account_detail.html b/src/coulobre/hosting/templates/hosting/account_detail.html index 3ee9cf2..d858710 100644 --- a/src/coulobre/hosting/templates/hosting/account_detail.html +++ b/src/coulobre/hosting/templates/hosting/account_detail.html @@ -7,25 +7,28 @@
| Database | {% if not is_reseller %}Deployment | {% endif %}|||
|---|---|---|---|---|
| Database | {% if is_operator %}Deployment | {% endif %}|||
| {{ database }} | - {% if not is_reseller %} + {% if is_operator %}Download manifest | {% endif %}|||
| Domain | SOA serial | - {% if not is_reseller %}Deployment | {% endif %} + {% if is_operator %}Deployment | {% endif %}|
| - {% 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 }} | - {% if not is_reseller %} + {% if is_operator %}Download manifest | {% endif %}||
| Email address | - {% if not is_reseller %}Deployment | {% endif %} + {% if is_operator %}Deployment | {% endif %}||
| {{ mailbox }} | - {% if not is_reseller %} + {% if is_operator %}Download manifest | {% endif %}|||
| Account | Primary domain | - {% if is_reseller or perms.hosting.view_hostingplan %}Hosting plan | {% endif %} + {% if is_tenant or perms.hosting.view_hostingplan %}Hosting plan | {% endif %}Added |
| {{ account.name }} | {{ account.primary_domain }} | - {% if is_reseller or perms.hosting.view_hostingplan %} + {% if is_tenant 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 efde25d..42569d3 100644 --- a/src/coulobre/hosting/templates/hosting/base.html +++ b/src/coulobre/hosting/templates/hosting/base.html @@ -17,19 +17,25 @@