Respect operator permissions in system summaries
This commit is contained in:
1 parent
5bddcd5512
commit
10e8a9d90d
4 files changed
+74
-20
No files matched your search
+2
-2
@@ -623,8 +623,8 @@ missing a dependency required by the target version. From 0.11 onward, the
|
|||||||
updater checks installed dependencies before stopping panel services.
|
updater checks installed dependencies before stopping panel services.
|
||||||
|
|
||||||
Markdown release notes require `markdown-it-py` in the panel's virtual
|
Markdown release notes require `markdown-it-py` in the panel's virtual
|
||||||
environment. To use the web updater for the first release with this feature,
|
environment. When upgrading from 0.11.1 to 0.12 through the panel, install it
|
||||||
install the dependency before starting the update. The in-place procedure
|
first if the existing environment does not have it. The in-place procedure
|
||||||
installs it with the panel package.
|
installs it with the panel package.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from .models import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def account_metrics(accounts, *, databases=True, mailboxes=True):
|
def account_metrics(accounts, *, databases=True, mailboxes=True, usage=True):
|
||||||
accounts = list(accounts)
|
accounts = list(accounts)
|
||||||
metrics = {
|
metrics = {
|
||||||
account.pk: {
|
account.pk: {
|
||||||
@@ -40,6 +40,8 @@ def account_metrics(accounts, *, databases=True, mailboxes=True):
|
|||||||
.order_by()
|
.order_by()
|
||||||
):
|
):
|
||||||
metrics[row[field]][key] = row["total"]
|
metrics[row[field]][key] = row["total"]
|
||||||
|
if not usage:
|
||||||
|
return metrics
|
||||||
month = timezone.now().strftime("%Y-%m")
|
month = timezone.now().strftime("%Y-%m")
|
||||||
for item in WebsiteUsage.objects.filter(
|
for item in WebsiteUsage.objects.filter(
|
||||||
account_id__in=metrics, observed_at__isnull=False
|
account_id__in=metrics, observed_at__isnull=False
|
||||||
@@ -76,6 +78,7 @@ def customer_metrics(customers, *, manager=None, operator=None):
|
|||||||
or operator.has_perm("hosting.view_hostingdatabase"),
|
or operator.has_perm("hosting.view_hostingdatabase"),
|
||||||
mailboxes=not operator
|
mailboxes=not operator
|
||||||
or operator.has_perm("hosting.view_hostingmailbox"),
|
or operator.has_perm("hosting.view_hostingmailbox"),
|
||||||
|
usage=not operator or operator.has_perm("hosting.view_websiteusage"),
|
||||||
)
|
)
|
||||||
rows = defaultdict(list)
|
rows = defaultdict(list)
|
||||||
for account in accounts:
|
for account in accounts:
|
||||||
|
|||||||
@@ -1,13 +1,64 @@
|
|||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.test import TestCase
|
from django.contrib.auth.models import Permission
|
||||||
|
from django.test import TestCase, override_settings
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
from .models import Customer, HostingAccount, HostingPlan, Reseller
|
from .models import (
|
||||||
|
Customer,
|
||||||
|
HostingAccount,
|
||||||
|
HostingPlan,
|
||||||
|
Reseller,
|
||||||
|
WebsiteScheduledJob,
|
||||||
|
WebsiteUsage,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class WorkspaceTests(TestCase):
|
class WorkspaceTests(TestCase):
|
||||||
|
@override_settings(WEBSITE_USAGE=True)
|
||||||
|
def test_operator_summary_respects_usage_and_task_permissions(self):
|
||||||
|
operator = get_user_model().objects.create_user(
|
||||||
|
"limited-operator", is_staff=True
|
||||||
|
)
|
||||||
|
operator.user_permissions.add(
|
||||||
|
Permission.objects.get(codename="view_hostingaccount")
|
||||||
|
)
|
||||||
|
account = HostingAccount.objects.create(
|
||||||
|
name="Hosting", primary_domain="example.test"
|
||||||
|
)
|
||||||
|
WebsiteUsage.objects.create(
|
||||||
|
account=account,
|
||||||
|
node_key="site",
|
||||||
|
domain=account.primary_domain,
|
||||||
|
report={"storage_bytes": 1024},
|
||||||
|
observed_at=timezone.now(),
|
||||||
|
)
|
||||||
|
WebsiteScheduledJob.objects.create(
|
||||||
|
account=account,
|
||||||
|
domain=account.primary_domain,
|
||||||
|
name="Application update",
|
||||||
|
command="php update.php",
|
||||||
|
expression="0 * * * *",
|
||||||
|
)
|
||||||
|
self.client.force_login(operator)
|
||||||
|
statistics = reverse("panel:system_workspace", args=["statistics"])
|
||||||
|
tasks = reverse("panel:system_workspace", args=["tasks"])
|
||||||
|
self.assertIsNone(
|
||||||
|
self.client.get(statistics).context["rows"][0][1]["storage"]
|
||||||
|
)
|
||||||
|
self.assertNotContains(self.client.get(tasks), "Application update")
|
||||||
|
operator.user_permissions.add(
|
||||||
|
*Permission.objects.filter(
|
||||||
|
codename__in=("view_websiteusage", "manage_website_jobs")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
self.client.get(statistics).context["rows"][0][1]["storage"], 1024
|
||||||
|
)
|
||||||
|
self.assertContains(self.client.get(tasks), "Application update")
|
||||||
|
|
||||||
def test_operator_has_its_own_manager_workspace(self):
|
def test_operator_has_its_own_manager_workspace(self):
|
||||||
operator = get_user_model().objects.create_superuser("operator")
|
operator = get_user_model().objects.create_superuser("operator")
|
||||||
self.client.force_login(operator)
|
self.client.force_login(operator)
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ from .access import customer_for_user, reseller_for_user
|
|||||||
from .account_metrics import account_metrics
|
from .account_metrics import account_metrics
|
||||||
from .installation_features import panel_feature_allowed
|
from .installation_features import panel_feature_allowed
|
||||||
from .models import HostingAccount, WebsiteScheduledJob
|
from .models import HostingAccount, WebsiteScheduledJob
|
||||||
|
from .website_job_views import can_manage_website_jobs
|
||||||
|
|
||||||
CUSTOMER_ACCOUNT = "coulobre_active_hosting_account"
|
CUSTOMER_ACCOUNT = "coulobre_active_hosting_account"
|
||||||
|
|
||||||
@@ -184,24 +185,22 @@ def system_workspace(request, page):
|
|||||||
raise Http404
|
raise Http404
|
||||||
title, target = SYSTEM_PAGES[page]
|
title, target = SYSTEM_PAGES[page]
|
||||||
accounts = workspace_accounts(request.user).order_by("primary_domain")
|
accounts = workspace_accounts(request.user).order_by("primary_domain")
|
||||||
if page == "tasks":
|
|
||||||
may_open = (
|
|
||||||
panel_feature_allowed("WEBSITE_JOBS")
|
|
||||||
and reseller_for_user(request.user) is None
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
may_open = settings.WEBSITE_USAGE and (
|
|
||||||
customer_for_user(request.user) is not None
|
|
||||||
or reseller_for_user(request.user) is not None
|
|
||||||
or request.user.has_perm("hosting.view_websiteusage")
|
|
||||||
)
|
|
||||||
pagination = Paginator(accounts, 25)
|
|
||||||
page_obj = pagination.get_page(request.GET.get("page"))
|
|
||||||
month = timezone.now().strftime("%Y-%m")
|
|
||||||
operator = (
|
operator = (
|
||||||
customer_for_user(request.user) is None
|
customer_for_user(request.user) is None
|
||||||
and reseller_for_user(request.user) is None
|
and reseller_for_user(request.user) is None
|
||||||
)
|
)
|
||||||
|
if page == "tasks":
|
||||||
|
may_open = can_manage_website_jobs(request)
|
||||||
|
may_view_tasks = not operator or request.user.has_perm(
|
||||||
|
"hosting.manage_website_jobs"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
may_open = settings.WEBSITE_USAGE and (
|
||||||
|
not operator or request.user.has_perm("hosting.view_websiteusage")
|
||||||
|
)
|
||||||
|
pagination = Paginator(accounts, 25)
|
||||||
|
page_obj = pagination.get_page(request.GET.get("page"))
|
||||||
|
month = timezone.now().strftime("%Y-%m")
|
||||||
measures = (
|
measures = (
|
||||||
account_metrics(
|
account_metrics(
|
||||||
page_obj.object_list,
|
page_obj.object_list,
|
||||||
@@ -209,6 +208,7 @@ def system_workspace(request, page):
|
|||||||
or request.user.has_perm("hosting.view_hostingdatabase"),
|
or request.user.has_perm("hosting.view_hostingdatabase"),
|
||||||
mailboxes=not operator
|
mailboxes=not operator
|
||||||
or request.user.has_perm("hosting.view_hostingmailbox"),
|
or request.user.has_perm("hosting.view_hostingmailbox"),
|
||||||
|
usage=may_open,
|
||||||
)
|
)
|
||||||
if page == "statistics"
|
if page == "statistics"
|
||||||
else {}
|
else {}
|
||||||
@@ -216,7 +216,7 @@ def system_workspace(request, page):
|
|||||||
rows = [(account, measures.get(account.pk, {})) for account in page_obj]
|
rows = [(account, measures.get(account.pk, {})) for account in page_obj]
|
||||||
tasks = ()
|
tasks = ()
|
||||||
task_accounts = {account.pk for account, _ in rows}
|
task_accounts = {account.pk for account, _ in rows}
|
||||||
if page == "tasks":
|
if page == "tasks" and may_view_tasks:
|
||||||
tasks = (
|
tasks = (
|
||||||
WebsiteScheduledJob.objects.filter(account_id__in=task_accounts)
|
WebsiteScheduledJob.objects.filter(account_id__in=task_accounts)
|
||||||
.select_related("account")
|
.select_related("account")
|
||||||
|
|||||||
Reference in new issue
Block a user