Expose customer impersonation in the operator workspace
Coulobre Panel CI / Checks (Python 3.12) (pull_request) Successful in 1m57s
Coulobre Panel CI / Checks (Python 3.14) (pull_request) Successful in 2m12s

This commit is contained in:
parkel committed 2026-12-04 14:57:42 +01:00
1 parent 4119909b3d
commit 3131821afc
11 files changed
+173 -42

No files matched your search

+16 -10
View File
@@ -109,12 +109,13 @@ requires password confirmation for each reveal.
## View the panel as a customer
In **Customers**, select **View as customer** next to an active customer.
The panel shows the customer's assigned hosting accounts and resource pages.
Request forms are visible with their inputs disabled. The banner identifies
the customer and offers **End customer view**. Ending the view returns you
to your administrator account. Signing out ends the view and signs you out
of the panel.
In the operator workspace, open **Show All Users** and select **Sign in as
customer** next to an eligible customer. The panel shows the customer's
assigned hosting accounts and supported resource pages. Request forms are
visible with their inputs disabled. The banner identifies the customer and
the session mode. Select **Leave impersonation** below the sidebar account
name, in place of the workspace switcher, to return to **All users** without
signing out. Signing out ends both sessions.
A support session lasts up to 30 minutes and applies to the current browser
session. The panel checks that both users remain eligible on each request.
@@ -123,16 +124,21 @@ permission ends the view on the next request.
A staff user needs both **Can view customer** and **Can view the panel as a
customer**. The second permission is listed under **Customer impersonation
sessions**. In `/admin/`, users with **Can view customer impersonation
sessions**. Manager and Customer workspaces cannot start impersonation.
Managers can use **Manage hosting** and **Contact and invitation** in
**My users** for their assigned customers without changing identity.
In `/admin/`, users with **Can view customer impersonation
session** can review the administrator, customer, timestamps, and outcome
for each session. The audit records are read-only in Django admin.
### Make changes during a support session
Grant **Can start a support session with write access** in addition to the
two customer view permissions. Select **Support with write access** next
to the customer. The banner identifies the active customer, administrator,
and mode.
two customer view permissions. In the operator workspace, open the legacy
customer list at `/customers/` and select **Support with write access**
next to the customer. The banner identifies the active customer,
administrator, and mode. **Sign in as customer** in **All users** still
starts a read-only session.
In a writable session, the administrator can request a database or mailbox
and manage SSH keys on the customer's hosting accounts. Each action also
+6
View File
@@ -103,6 +103,12 @@ heading, help, fields, and actions should share a left edge.
action that opens a form, confirmation page, or download is still an
`<a href>` styled as a button. An action that submits the change uses a
native `<button type="submit">` in its existing POST form.
- Show only actions available in the active workspace. For example, give
operators a **Sign in as customer** row action and give managers direct
actions for assigned hosting and invitations without impersonation.
- During impersonation, identify the customer and session mode in a
persistent banner. Replace the sidebar workspace switcher with **Leave
impersonation**, and keep it available when mobile navigation is closed.
### Checkboxes and radio choices
@@ -25,6 +25,7 @@ from .access import OperatorOnlyMixin, reseller_for_user
from .account_metrics import customer_metrics
from .account_setup import setup_new_account
from .dns_defaults import zone_defaults
from .impersonation import can_impersonate_customer
from .installation_features import (
default_dns_zones_allowed,
panel_feature_allowed,
@@ -366,11 +367,15 @@ class AllUsersView(OperatorOnlyMixin, PermissionRequiredMixin, ListView):
operator=self.request.user,
)
actor = self.request.user
may_impersonate = can_impersonate_customer(actor)
context["login_rows"] = [
(
login,
measurements.get(profile.pk) if profile else None,
deletable_profile(actor, login) is not None,
may_impersonate
and profile is not None
and profile.may_be_impersonated,
)
for login, profile in zip(context["logins"], profiles, strict=True)
]
@@ -378,7 +383,8 @@ class AllUsersView(OperatorOnlyMixin, PermissionRequiredMixin, ListView):
"hosting.change_customer"
) or actor.has_perm("hosting.change_reseller")
context["has_user_actions"] = context["may_manage_invitations"] or any(
row[2] for row in context["login_rows"]
may_delete or may_view
for _, _, may_delete, may_view in context["login_rows"]
)
return context
+2 -1
View File
@@ -9,7 +9,7 @@ from django.shortcuts import render
from django.utils import timezone
from django.utils.cache import add_never_cache_headers
from .access import is_tenant
from .access import PanelRole, is_tenant
from .models import CustomerImpersonation
from .sftp import MANAGE_SFTP_KEYS
@@ -48,6 +48,7 @@ def can_impersonate_customer(user):
user.is_authenticated
and user.is_active
and user.is_staff
and getattr(user, "panel_role", None) in (None, PanelRole.ADMIN)
and not is_tenant(user)
and user.has_perms(IMPERSONATE_CUSTOMER)
)
@@ -478,6 +478,12 @@ a.sidebar-identity:hover {
color: var(--sidebar-text);
}
.sidebar-impersonation-exit .sidebar-action {
width: 100%;
text-align: start;
white-space: normal;
}
.sidebar-icon-action {
display: grid;
width: 2.2rem;
@@ -921,7 +927,7 @@ dd {
background: var(--notice-bg);
}
.customer-preview, .table-actions {
.table-actions {
display: flex;
align-items: center;
flex-wrap: wrap;
@@ -929,7 +935,6 @@ dd {
}
.customer-preview {
justify-content: space-between;
border-color: var(--focus);
background: var(--warning-bg);
color: var(--warning-text);
@@ -22,7 +22,7 @@
<table>
<thead><tr><th scope="col">User</th><th scope="col">Workspace</th><th scope="col">Manager</th><th scope="col">Hosting accounts</th><th scope="col">Disk</th><th scope="col">Monthly traffic</th><th scope="col">Domains</th><th scope="col">Databases</th><th scope="col">Mailboxes</th><th scope="col">Added</th>{% if has_user_actions %}<th scope="col">Actions</th>{% endif %}</tr></thead>
<tbody>
{% for login, usage, may_delete in login_rows %}
{% for login, usage, may_delete, may_impersonate in login_rows %}
<tr>
<th scope="row">{{ login.get_username }}</th>
<td>{% if login.is_superuser %}Operator{% elif login.hosting_reseller %}Manager{% else %}Customer{% endif %}</td>
@@ -32,7 +32,13 @@
<td>{{ login.date_joined|date:"j M Y" }}</td>
{% if has_user_actions %}
<td>
<div class="form-actions">
<div class="table-actions">
{% if may_impersonate %}
<form method="post" action="{% url 'panel:impersonation_start' login.hosting_customer.pk %}">
{% csrf_token %}
<button class="button secondary compact" type="submit" aria-label="Sign in as customer {{ login.get_username }}">Sign in as customer</button>
</form>
{% endif %}
{% if not login.is_staff and not login.is_superuser %}
{% if login.hosting_reseller and perms.hosting.change_reseller %}
<a class="button secondary compact" href="{% url 'panel:manage_invitation' 'manager' login.hosting_reseller.pk %}" aria-label="Contact and invitation for {{ login.get_username }}">Contact and invitation</a>
@@ -50,7 +50,12 @@
</button>
</form>
</div>
{% if not request.impersonation and request.panel_role_options|length > 1 %}
{% if request.impersonation %}
<form class="sidebar-impersonation-exit" method="post" action="{% url 'panel:impersonation_stop' %}">
{% csrf_token %}
<button class="sidebar-action" type="submit">Leave impersonation</button>
</form>
{% elif request.panel_role_options|length > 1 %}
<details class="panel-role-switch" data-role-switch>
<summary aria-label="Switch role from {{ request.panel_role_label }}">
<svg class="nav-icon" aria-hidden="true" focusable="false"><use href="{{ panel_icons }}#{{ request.panel_role_icon }}"></use></svg>
@@ -77,18 +82,12 @@
{% block account_selector %}{% include "hosting/workspace_account_selector.html" %}{% endblock %}
{% if request.impersonation %}
<section class="notice customer-preview" aria-label="Customer view">
<div>
<strong>Viewing as {{ request.impersonation.customer }}</strong>
{% if request.impersonation.write_enabled %}
<p>Support session with write access · {{ request.impersonation.operator_username }}</p>
{% else %}
<p>Read-only support session</p>
{% endif %}
</div>
<form method="post" action="{% url 'panel:impersonation_stop' %}">
{% csrf_token %}
<button class="button secondary" type="submit">End customer view</button>
</form>
<strong>Viewing as {{ request.impersonation.customer }}</strong>
{% if request.impersonation.write_enabled %}
<p>Support session with write access · {{ request.impersonation.operator_username }}</p>
{% else %}
<p>Read-only support session</p>
{% endif %}
</section>
{% endif %}
{% for announcement in panel_announcements %}<section class="notice"><strong>{{ announcement.subject }}</strong>{{ announcement.body|linebreaks }}</section>{% endfor %}
@@ -4,7 +4,7 @@
<header class="page-heading">
<div>
<h1>{% if is_reseller %}My users{% else %}Customers{% endif %}</h1>
<p class="heading-meta">Manage the logins people use to access their assigned hosting.</p>
<p class="heading-meta">{% if is_reseller %}Manage assigned hosting and invitations for your customers.{% else %}Manage the logins people use to access their assigned hosting.{% endif %}</p>
</div>
{% if is_operator and perms.hosting.add_customer %}
<div class="form-actions">
@@ -42,7 +42,7 @@
<tr>
<td>
<strong>{{ customer }}</strong>
{% if is_reseller or perms.hosting.view_hostingaccount %}
{% if is_operator and perms.hosting.view_hostingaccount %}
<div><a href="{% url 'panel:accounts' %}?customer={{ customer.pk }}">View assigned hosting</a></div>
{% endif %}
</td>
@@ -56,13 +56,16 @@
{% if is_operator and perms.hosting.change_customer or can_impersonate or is_reseller %}
<td>
<div class="table-actions">
{% if is_reseller %}
<a class="button secondary compact" href="{% url 'panel:accounts' %}?customer={{ customer.pk }}" aria-label="Manage hosting for {{ customer }}">Manage hosting</a>
{% endif %}
{% if is_reseller or is_operator and perms.hosting.change_customer %}
<a class="button secondary compact" href="{% url 'panel:manage_invitation' 'customer' customer.pk %}">Contact and invitation</a>
<a class="button secondary compact" href="{% url 'panel:manage_invitation' 'customer' customer.pk %}" aria-label="Contact and invitation for {{ customer }}">Contact and invitation</a>
{% endif %}
{% if is_operator and perms.hosting.add_hostingaccount and perms.hosting.view_hostingaccount %}
<a class="button secondary compact" href="{% url 'panel:account_create' %}?customer={{ customer.pk }}">Add hosting</a>
{% endif %}
{% if perms.hosting.change_customer and not customer.user.is_staff and not customer.user.is_superuser and not customer.user.hosting_reseller %}
{% if is_operator and perms.hosting.change_customer and not customer.user.is_staff and not customer.user.is_superuser and not customer.user.hosting_reseller %}
<form method="post" action="{% url 'panel:customer_access' customer.pk %}{% querystring %}">
{% csrf_token %}
{% if customer.user.is_active %}
@@ -72,7 +75,7 @@
{% endif %}
</form>
{% endif %}
{% if can_impersonate and customer.may_be_impersonated %}
{% if is_operator and can_impersonate and customer.may_be_impersonated %}
<form method="post" action="{% url 'panel:impersonation_start' customer.pk %}">
{% csrf_token %}
<button class="button secondary compact" type="submit" aria-label="View as customer {{ customer }}">View as customer</button>
@@ -5,7 +5,7 @@
<div>
{% if request.impersonation %}
<h1>Unavailable in customer view</h1>
<p class="heading-meta">End the customer view to use your administrator account.</p>
<p class="heading-meta">Choose Leave impersonation in the account controls to return to your operator workspace.</p>
{% else %}
<h1>Customer view ended</h1>
<p class="heading-meta">The session expired or access changed.</p>
@@ -14,9 +14,11 @@
</header>
{% endblock %}
{% block page_content %}
{% if not request.impersonation and perms.hosting.view_customer %}
<a class="button secondary" href="{% url 'panel:customers' %}">Back to customers</a>
{% else %}
<a class="button secondary" href="{% url 'panel:accounts' %}">Back to hosting accounts</a>
{% if not request.impersonation %}
{% if is_operator and perms.hosting.view_customer %}
<a class="button secondary" href="{% url 'panel:all_users' %}">Back to all users</a>
{% else %}
<a class="button secondary" href="{% url 'panel:accounts' %}">Back to hosting accounts</a>
{% endif %}
{% endif %}
{% endblock %}
+99 -2
View File
@@ -7,6 +7,7 @@ from django.contrib.auth.models import Permission
from django.test import Client, TestCase
from django.urls import reverse
from django.utils import timezone
from django_otp.plugins.otp_totp.models import TOTPDevice
from .impersonation import SESSION_DURATION, SESSION_KEY
from .models import (
@@ -39,11 +40,15 @@ class CustomerImpersonationTests(TestCase):
def setUp(self):
self.client.force_login(self.operator)
self.accounts = reverse("panel:accounts")
self.all_users = reverse("panel:all_users")
self.customers = reverse("panel:customers")
self.start_url = reverse(
"panel:impersonation_start", args=[self.customer.pk]
)
self.stop_url = reverse("panel:impersonation_stop")
self.write_start_url = reverse(
"panel:impersonation_write_start", args=[self.customer.pk]
)
def grant_support_permissions(self, user, *codenames):
user.user_permissions.add(
@@ -62,6 +67,94 @@ class CustomerImpersonationTests(TestCase):
pk=self.client.session[SESSION_KEY]
)
def test_operator_starts_from_all_users_and_leaves_in_sidebar(self):
response = self.client.get(self.all_users)
self.assertContains(response, f'action="{self.start_url}"', count=1)
self.assertContains(response, "Sign in as customer alice")
self.assertNotContains(response, self.write_start_url)
self.assertContains(response, "data-role-switch")
preview = self.start_preview()
self.assertFalse(preview.write_enabled)
response = self.client.get(self.accounts)
self.assertContains(
response,
f'<form class="sidebar-impersonation-exit" method="post" '
f'action="{self.stop_url}">',
count=1,
)
self.assertContains(response, "Leave impersonation", count=1)
self.assertNotContains(response, "data-role-switch")
self.assertRedirects(self.client.post(self.stop_url), self.all_users)
self.assertEqual(
self.client.session[AUTH_SESSION_KEY], str(self.operator.pk)
)
self.assertNotIn(SESSION_KEY, self.client.session)
preview.refresh_from_db()
self.assertEqual(preview.operator, self.operator)
self.assertEqual(preview.end_reason, preview.EndReason.STOPPED)
def test_manager_actions_remain_scoped_without_impersonation(self):
manager = Reseller.objects.create(
user=get_user_model().objects.create_user("manager")
)
self.customer.reseller = manager
self.customer.save(update_fields=["reseller"])
self.account.reseller = manager
self.account.save(update_fields=["reseller"])
other = Customer.objects.create(
user=get_user_model().objects.create_user("unassigned")
)
invite = reverse(
"panel:manage_invitation", args=["customer", self.customer.pk]
)
other_invite = reverse(
"panel:manage_invitation", args=["customer", other.pk]
)
self.client.force_login(manager.user)
response = self.client.get(self.customers)
self.assertContains(response, "Manage hosting for alice")
self.assertContains(
response, f"{self.accounts}?customer={self.customer.pk}"
)
self.assertContains(response, invite)
self.assertNotContains(response, "unassigned")
self.assertNotContains(response, self.start_url)
self.assertNotContains(response, self.write_start_url)
self.assertNotContains(response, "Suspend sign-in")
self.assertEqual(self.client.get(invite).status_code, 200)
self.assertEqual(self.client.get(other_invite).status_code, 404)
self.assertContains(
self.client.get(self.accounts, {"customer": self.customer.pk}),
self.account.name,
)
for route in (self.start_url, self.write_start_url):
self.assertEqual(self.client.post(route).status_code, 403)
def test_operator_cannot_impersonate_in_another_workspace(self):
self.client.get(self.accounts)
Customer.objects.create(
user=self.operator,
reseller=Reseller.objects.get(user=self.operator),
)
device = TOTPDevice.objects.create(user=self.operator, name="Panel")
session = self.client.session
session["otp_device_id"] = device.persistent_id
session.save()
self.assertContains(self.client.get(self.all_users), self.start_url)
switch = reverse("panel:switch_panel_role")
for role in ("manager", "customer"):
with self.subTest(role=role):
self.assertRedirects(
self.client.post(switch, {"role": role}), self.accounts
)
self.assertEqual(
self.client.get(self.all_users).status_code, 403
)
for route in (self.start_url, self.write_start_url):
self.assertEqual(self.client.post(route).status_code, 403)
self.assertFalse(CustomerImpersonation.objects.exists())
def test_reads_only_customer_accounts_and_keeps_operator_login(self):
preview = self.start_preview()
@@ -124,7 +217,7 @@ class CustomerImpersonationTests(TestCase):
method = "post" if url == self.start_url else "get"
self.assertContains(
getattr(self.client, method)(url),
"End customer view",
"Leave impersonation",
status_code=403,
)
self.assertEqual(CustomerImpersonation.objects.count(), 1)
@@ -135,14 +228,16 @@ class CustomerImpersonationTests(TestCase):
self.assertNotContains(
self.client.get(self.customers), "View as customer"
)
self.assertNotContains(self.client.get(self.all_users), self.start_url)
self.assertEqual(self.client.post(self.start_url).status_code, 403)
self.grant_support_permissions(self.support, "impersonate_customer")
self.assertContains(
self.client.get(self.customers), "View as customer alice"
)
self.assertContains(self.client.get(self.all_users), self.start_url)
self.start_preview()
self.assertRedirects(self.client.post(self.stop_url), self.customers)
self.assertRedirects(self.client.post(self.stop_url), self.all_users)
Reseller.objects.create(user=self.support)
self.assertEqual(self.client.post(self.start_url).status_code, 403)
@@ -163,9 +258,11 @@ class CustomerImpersonationTests(TestCase):
)
customer = Customer.objects.create(user=user)
url = reverse("panel:impersonation_start", args=[customer.pk])
self.assertNotContains(self.client.get(self.all_users), url)
self.assertEqual(self.client.post(url).status_code, 404)
Reseller.objects.create(user=self.alice)
self.assertNotContains(self.client.get(self.all_users), self.start_url)
self.assertEqual(self.client.post(self.start_url).status_code, 404)
self.assertFalse(CustomerImpersonation.objects.exists())
+1 -1
View File
@@ -2118,7 +2118,7 @@ class CustomerImpersonationStop(View):
session = getattr(request, "impersonation", None)
if session is not None:
end_impersonation(request, session, EndReason.STOPPED)
return redirect("panel:customers")
return redirect("panel:all_users")
return redirect("panel:accounts")