Add account-scoped access for hosting customers
This commit is contained in:
1 parent
4bbe265915
commit
8de6eb8d88
11 files changed
+390
-51
No files matched your search
@@ -13,7 +13,12 @@ from django.views.generic.detail import SingleObjectMixin
|
||||
from coulobre.dns_node import MAX_SOA_SERIAL
|
||||
from coulobre.mail_node import INVALID_LOCAL_PART, MailboxSpec
|
||||
|
||||
from .access import OperatorOnlyMixin, ResellerScopeMixin, reseller_for_user
|
||||
from .access import (
|
||||
OperatorOnlyMixin,
|
||||
TenantScopeMixin,
|
||||
customer_for_user,
|
||||
reseller_for_user,
|
||||
)
|
||||
from .dns_forms import DnsZoneEditForm, DnsZoneForm, format_txt_record
|
||||
from .models import (
|
||||
HostingAccount,
|
||||
@@ -32,12 +37,13 @@ CHANGE_ACCOUNTS = "hosting.change_hostingaccount"
|
||||
VIEW_PLANS = "hosting.view_webprovisioningplan"
|
||||
VIEW_HOSTING_PLANS = "hosting.view_hostingplan"
|
||||
VIEW_RESELLERS = "hosting.view_reseller"
|
||||
VIEW_CUSTOMERS = "hosting.view_customer"
|
||||
VIEW_DATABASES = "hosting.view_hostingdatabase"
|
||||
VIEW_DNS_ZONES = "hosting.view_hostingdnszone"
|
||||
VIEW_MAILBOXES = "hosting.view_hostingmailbox"
|
||||
|
||||
|
||||
class AccountList(ResellerScopeMixin, PermissionRequiredMixin, ListView):
|
||||
class AccountList(TenantScopeMixin, PermissionRequiredMixin, ListView):
|
||||
queryset = HostingAccount.objects.select_related("hosting_plan").order_by(
|
||||
"primary_domain"
|
||||
)
|
||||
@@ -45,6 +51,7 @@ class AccountList(ResellerScopeMixin, PermissionRequiredMixin, ListView):
|
||||
template_name = "hosting/account_list.html"
|
||||
context_object_name = "accounts"
|
||||
paginate_by = 20
|
||||
customer_relation = "customer"
|
||||
|
||||
def get_queryset(self):
|
||||
self.search = self.request.GET.get("q", "").strip()[:253]
|
||||
@@ -78,6 +85,8 @@ class AccountEditor:
|
||||
self.fields += ("hosting_plan",)
|
||||
if reseller is None and self.request.user.has_perm(VIEW_RESELLERS):
|
||||
self.fields += ("reseller",)
|
||||
if reseller is None and self.request.user.has_perm(VIEW_CUSTOMERS):
|
||||
self.fields += ("customer",)
|
||||
return super().get_form_class()
|
||||
|
||||
def get_form(self, form_class=None):
|
||||
@@ -91,13 +100,13 @@ class AccountEditor:
|
||||
field.queryset = reseller.hosting_plans.all()
|
||||
field.required = True
|
||||
field.empty_label = "Select a hosting plan"
|
||||
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")
|
||||
)
|
||||
for name in ("reseller", "customer"):
|
||||
if name in form.fields:
|
||||
field = form.fields[name]
|
||||
field.empty_label = "Unassigned"
|
||||
field.queryset = field.queryset.select_related("user").order_by(
|
||||
"user__username"
|
||||
)
|
||||
return form
|
||||
|
||||
|
||||
@@ -106,6 +115,8 @@ class AccountCreate(PermissionRequiredMixin, AccountEditor, CreateView):
|
||||
extra_context = {"page_title": "Add hosting account", "submit_label": "Add account"}
|
||||
|
||||
def has_permission(self):
|
||||
if customer_for_user(self.request.user) is not None:
|
||||
return False
|
||||
reseller = reseller_for_user(self.request.user)
|
||||
if reseller is not None:
|
||||
return reseller.may_create_accounts()
|
||||
@@ -148,29 +159,34 @@ class AccountEdit(
|
||||
return redirect(self.object)
|
||||
|
||||
|
||||
class AccountDetail(ResellerScopeMixin, PermissionRequiredMixin, DetailView):
|
||||
queryset = HostingAccount.objects.select_related("hosting_plan", "reseller__user")
|
||||
class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView):
|
||||
queryset = HostingAccount.objects.select_related(
|
||||
"hosting_plan", "reseller__user", "customer__user"
|
||||
)
|
||||
permission_required = VIEW_ACCOUNTS
|
||||
template_name = "hosting/account_detail.html"
|
||||
context_object_name = "account"
|
||||
customer_relation = "customer"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
user = self.request.user
|
||||
reseller = reseller_for_user(user)
|
||||
if reseller is not None or user.has_perm(VIEW_PLANS):
|
||||
customer = customer_for_user(user)
|
||||
tenant = reseller is not None or customer is not None
|
||||
if reseller is not None or (customer is None and user.has_perm(VIEW_PLANS)):
|
||||
context["plans"] = self.object.webprovisioningplan_set.order_by(
|
||||
"-requested_at", "-pk"
|
||||
)[:10]
|
||||
if reseller is not None or user.has_perm(VIEW_DATABASES):
|
||||
if tenant or user.has_perm(VIEW_DATABASES):
|
||||
context["databases"] = self.object.databases.all()
|
||||
if reseller is not None or user.has_perm(VIEW_DNS_ZONES):
|
||||
if tenant or user.has_perm(VIEW_DNS_ZONES):
|
||||
zones = list(self.object.dns_zones.all())
|
||||
context["dns_zones"] = zones
|
||||
context["has_primary_zone"] = any(
|
||||
zone.domain == self.object.primary_domain for zone in zones
|
||||
)
|
||||
if reseller is not None or user.has_perm(VIEW_MAILBOXES):
|
||||
if tenant or user.has_perm(VIEW_MAILBOXES):
|
||||
context["mailboxes"] = HostingMailbox.objects.filter(
|
||||
mail_domain__account=self.object
|
||||
).select_related("mail_domain")
|
||||
@@ -441,7 +457,7 @@ class RequestWebPlan(
|
||||
return redirect("panel:plan", pk=plan.pk)
|
||||
|
||||
|
||||
class PlanList(ResellerScopeMixin, PermissionRequiredMixin, ListView):
|
||||
class PlanList(TenantScopeMixin, PermissionRequiredMixin, ListView):
|
||||
queryset = WebProvisioningPlan.objects.select_related("account").order_by(
|
||||
"-requested_at", "-pk"
|
||||
)
|
||||
@@ -452,7 +468,7 @@ class PlanList(ResellerScopeMixin, PermissionRequiredMixin, ListView):
|
||||
paginate_by = 20
|
||||
|
||||
|
||||
class PlanDetail(ResellerScopeMixin, PermissionRequiredMixin, DetailView):
|
||||
class PlanDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView):
|
||||
queryset = WebProvisioningPlan.objects.select_related("account")
|
||||
permission_required = VIEW_PLANS
|
||||
reseller_relation = "account__reseller"
|
||||
|
||||
Reference in new issue
Block a user