Scope reseller access to assigned hosting accounts

Allow operators to assign accounts to resellers and give each reseller
read-only access to their own accounts and Apache previews.
This commit is contained in:
parkel committed 2026-11-19 22:16:29 +01:00
1 parent bb95b86173
commit 39243a8ab3
13 files changed
+325 -33

No files matched your search

+42 -17
View File
@@ -7,6 +7,7 @@ from django.views import View
from django.views.generic import CreateView, DetailView, ListView, UpdateView
from django.views.generic.detail import SingleObjectMixin
from .access import OperatorOnlyMixin, ResellerScopeMixin, reseller_for_user
from .models import HostingAccount, HostingPlan, WebProvisioningPlan
from .web_node import build_web_smoke_bundle, serialize_deployment_manifest
@@ -14,10 +15,13 @@ VIEW_ACCOUNTS = "hosting.view_hostingaccount"
CHANGE_ACCOUNTS = "hosting.change_hostingaccount"
VIEW_PLANS = "hosting.view_webprovisioningplan"
VIEW_HOSTING_PLANS = "hosting.view_hostingplan"
VIEW_RESELLERS = "hosting.view_reseller"
class AccountList(PermissionRequiredMixin, ListView):
model = HostingAccount
class AccountList(ResellerScopeMixin, PermissionRequiredMixin, ListView):
queryset = HostingAccount.objects.select_related("hosting_plan").order_by(
"primary_domain"
)
permission_required = VIEW_ACCOUNTS
template_name = "hosting/account_list.html"
context_object_name = "accounts"
@@ -25,9 +29,7 @@ class AccountList(PermissionRequiredMixin, ListView):
def get_queryset(self):
self.search = self.request.GET.get("q", "").strip()[:253]
accounts = HostingAccount.objects.select_related("hosting_plan").order_by(
"primary_domain"
)
accounts = super().get_queryset()
if self.search:
accounts = accounts.filter(
Q(name__icontains=self.search)
@@ -47,21 +49,34 @@ class AccountEditor:
self.fields = ("name", "primary_domain")
if self.request.user.has_perm(VIEW_HOSTING_PLANS):
self.fields += ("hosting_plan",)
if self.request.user.has_perm(VIEW_RESELLERS):
self.fields += ("reseller",)
return super().get_form_class()
def get_form(self, form_class=None):
form = super().get_form(form_class)
if "hosting_plan" in form.fields:
form.fields["hosting_plan"].empty_label = "Unassigned"
if "reseller" in form.fields:
form.fields["reseller"].empty_label = "Unassigned"
form.fields["reseller"].queryset = (
form.fields["reseller"]
.queryset.select_related("user")
.order_by("user__username")
)
return form
class AccountCreate(PermissionRequiredMixin, AccountEditor, CreateView):
class AccountCreate(
OperatorOnlyMixin, PermissionRequiredMixin, AccountEditor, CreateView
):
permission_required = (VIEW_ACCOUNTS, "hosting.add_hostingaccount")
extra_context = {"page_title": "Add hosting account", "submit_label": "Add account"}
class AccountEdit(PermissionRequiredMixin, AccountEditor, UpdateView):
class AccountEdit(
OperatorOnlyMixin, PermissionRequiredMixin, AccountEditor, UpdateView
):
permission_required = (VIEW_ACCOUNTS, CHANGE_ACCOUNTS)
context_object_name = "account"
extra_context = {
@@ -75,22 +90,24 @@ class AccountEdit(PermissionRequiredMixin, AccountEditor, UpdateView):
return redirect(self.object)
class AccountDetail(PermissionRequiredMixin, DetailView):
queryset = HostingAccount.objects.select_related("hosting_plan")
class AccountDetail(ResellerScopeMixin, PermissionRequiredMixin, DetailView):
queryset = HostingAccount.objects.select_related("hosting_plan", "reseller__user")
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):
if reseller_for_user(
self.request.user
) is not None or self.request.user.has_perm(VIEW_PLANS):
context["plans"] = self.object.webprovisioningplan_set.order_by(
"-requested_at", "-pk"
)[:10]
return context
class HostingPlanList(PermissionRequiredMixin, ListView):
class HostingPlanList(OperatorOnlyMixin, PermissionRequiredMixin, ListView):
model = HostingPlan
permission_required = VIEW_HOSTING_PLANS
template_name = "hosting/hosting_plan_list.html"
@@ -105,17 +122,23 @@ class HostingPlanEditor:
success_url = reverse_lazy("panel:hosting_plans")
class HostingPlanCreate(PermissionRequiredMixin, HostingPlanEditor, CreateView):
class HostingPlanCreate(
OperatorOnlyMixin, PermissionRequiredMixin, HostingPlanEditor, CreateView
):
permission_required = (VIEW_HOSTING_PLANS, "hosting.add_hostingplan")
extra_context = {"page_title": "Add hosting plan", "submit_label": "Add plan"}
class HostingPlanEdit(PermissionRequiredMixin, HostingPlanEditor, UpdateView):
class HostingPlanEdit(
OperatorOnlyMixin, PermissionRequiredMixin, HostingPlanEditor, UpdateView
):
permission_required = (VIEW_HOSTING_PLANS, "hosting.change_hostingplan")
extra_context = {"page_title": "Edit hosting plan", "submit_label": "Save changes"}
class RequestWebPlan(PermissionRequiredMixin, SingleObjectMixin, View):
class RequestWebPlan(
OperatorOnlyMixin, PermissionRequiredMixin, SingleObjectMixin, View
):
model = HostingAccount
permission_required = (VIEW_ACCOUNTS, CHANGE_ACCOUNTS, VIEW_PLANS)
@@ -125,24 +148,26 @@ class RequestWebPlan(PermissionRequiredMixin, SingleObjectMixin, View):
return redirect("panel:plan", pk=plan.pk)
class PlanList(PermissionRequiredMixin, ListView):
class PlanList(ResellerScopeMixin, PermissionRequiredMixin, ListView):
queryset = WebProvisioningPlan.objects.select_related("account").order_by(
"-requested_at", "-pk"
)
permission_required = VIEW_PLANS
reseller_relation = "account__reseller"
template_name = "hosting/plan_list.html"
context_object_name = "plans"
paginate_by = 20
class PlanDetail(PermissionRequiredMixin, DetailView):
class PlanDetail(ResellerScopeMixin, PermissionRequiredMixin, DetailView):
queryset = WebProvisioningPlan.objects.select_related("account")
permission_required = VIEW_PLANS
reseller_relation = "account__reseller"
template_name = "hosting/plan_detail.html"
context_object_name = "plan"
class DownloadWebPlan(PlanDetail):
class DownloadWebPlan(OperatorOnlyMixin, PlanDetail):
manifest = False
def render_to_response(self, context, **response_kwargs):