diff --git a/docs/deployment.md b/docs/deployment.md index 14daf48..bb70586 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -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. 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, -install the dependency before starting the update. The in-place procedure +environment. When upgrading from 0.11.1 to 0.12 through the panel, install it +first if the existing environment does not have it. The in-place procedure installs it with the panel package. ```sh diff --git a/src/coulobre/hosting/account_metrics.py b/src/coulobre/hosting/account_metrics.py index 1f6ebab..7ee0582 100644 --- a/src/coulobre/hosting/account_metrics.py +++ b/src/coulobre/hosting/account_metrics.py @@ -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) metrics = { account.pk: { @@ -40,6 +40,8 @@ def account_metrics(accounts, *, databases=True, mailboxes=True): .order_by() ): metrics[row[field]][key] = row["total"] + if not usage: + return metrics month = timezone.now().strftime("%Y-%m") for item in WebsiteUsage.objects.filter( 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"), mailboxes=not operator or operator.has_perm("hosting.view_hostingmailbox"), + usage=not operator or operator.has_perm("hosting.view_websiteusage"), ) rows = defaultdict(list) for account in accounts: diff --git a/src/coulobre/hosting/test_workspaces.py b/src/coulobre/hosting/test_workspaces.py index a8b3e92..209a59e 100644 --- a/src/coulobre/hosting/test_workspaces.py +++ b/src/coulobre/hosting/test_workspaces.py @@ -1,13 +1,64 @@ from unittest.mock import patch 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.utils import timezone -from .models import Customer, HostingAccount, HostingPlan, Reseller +from .models import ( + Customer, + HostingAccount, + HostingPlan, + Reseller, + WebsiteScheduledJob, + WebsiteUsage, +) 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): operator = get_user_model().objects.create_superuser("operator") self.client.force_login(operator) diff --git a/src/coulobre/hosting/workspaces.py b/src/coulobre/hosting/workspaces.py index 36186a8..58479a9 100644 --- a/src/coulobre/hosting/workspaces.py +++ b/src/coulobre/hosting/workspaces.py @@ -12,6 +12,7 @@ from .access import customer_for_user, reseller_for_user from .account_metrics import account_metrics from .installation_features import panel_feature_allowed from .models import HostingAccount, WebsiteScheduledJob +from .website_job_views import can_manage_website_jobs CUSTOMER_ACCOUNT = "coulobre_active_hosting_account" @@ -184,24 +185,22 @@ def system_workspace(request, page): raise Http404 title, target = SYSTEM_PAGES[page] 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 = ( customer_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 = ( account_metrics( page_obj.object_list, @@ -209,6 +208,7 @@ def system_workspace(request, page): or request.user.has_perm("hosting.view_hostingdatabase"), mailboxes=not operator or request.user.has_perm("hosting.view_hostingmailbox"), + usage=may_open, ) if page == "statistics" else {} @@ -216,7 +216,7 @@ def system_workspace(request, page): rows = [(account, measures.get(account.pk, {})) for account in page_obj] tasks = () task_accounts = {account.pk for account, _ in rows} - if page == "tasks": + if page == "tasks" and may_view_tasks: tasks = ( WebsiteScheduledJob.objects.filter(account_id__in=task_accounts) .select_related("account")