diff --git a/src/coulobre/hosting/panel_roles.py b/src/coulobre/hosting/panel_roles.py index c2acc7e..fc9ae7a 100644 --- a/src/coulobre/hosting/panel_roles.py +++ b/src/coulobre/hosting/panel_roles.py @@ -22,8 +22,13 @@ from .models import Customer, Reseller, ResellerProfile SESSION_ROLE = "coulobre_panel_role" ROLE_LABELS = { PanelRole.ADMIN: "Server administrator", - PanelRole.CUSTOMER: "Customer", PanelRole.MANAGER: "Manager", + PanelRole.CUSTOMER: "Customer", +} +ROLE_SWITCH_DETAILS = { + PanelRole.ADMIN: ("shield", "Server and all accounts"), + PanelRole.MANAGER: ("resellers", "Customers you manage"), + PanelRole.CUSTOMER: ("customers", "Your own hosting"), } @@ -126,8 +131,12 @@ class PanelRoleMiddleware: return redirect("panel:login") request.active_panel_role = role + request.panel_role_label = ROLE_LABELS[role] + request.panel_role_icon = ROLE_SWITCH_DETAILS[role][0] request.panel_role_options = [ - (option, ROLE_LABELS[option]) for option in roles + (option, ROLE_LABELS[option], *ROLE_SWITCH_DETAILS[option]) + for option in PanelRole + if option in roles ] request.user = scoped_user(user, role) diff --git a/src/coulobre/hosting/static/hosting/panel-navigation.js b/src/coulobre/hosting/static/hosting/panel-navigation.js index 18b05fd..8336033 100644 --- a/src/coulobre/hosting/static/hosting/panel-navigation.js +++ b/src/coulobre/hosting/static/hosting/panel-navigation.js @@ -33,18 +33,19 @@ updateMenuLayout(); const roleSwitch = document.querySelector("[data-role-switch]"); if (roleSwitch) { - const picker = roleSwitch.querySelector("select"); - const currentRole = picker.value; - roleSwitch.classList.add("role-switch-on-change"); - roleSwitch.querySelector("button").hidden = true; - picker.setAttribute("aria-label", "Panel role (switches when selected)"); - picker.addEventListener("change", () => { - if (picker.value !== currentRole) { - roleSwitch.requestSubmit(); + roleSwitch.addEventListener("keydown", event => { + if (event.key === "Escape" && roleSwitch.open) { + roleSwitch.open = false; + roleSwitch.querySelector("summary").focus(); + } + }); + document.addEventListener("click", event => { + if (roleSwitch.open && !roleSwitch.contains(event.target)) { + roleSwitch.open = false; } }); window.addEventListener("pageshow", () => { - picker.value = currentRole; + roleSwitch.open = false; }); } diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 6a261c5..a69ed39 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -329,25 +329,102 @@ a.sidebar-identity:hover { } .panel-role-switch { - display: grid; - grid-template-columns: minmax(0, 1fr) auto; - gap: 0.35rem; + position: relative; font-size: 0.82rem; } -.panel-role-switch.role-switch-on-change { - display: block; -} - -.panel-role-switch select { - width: 100%; - min-width: 0; +.panel-role-switch summary { + display: flex; + align-items: center; + gap: 0.45rem; min-height: 2.2rem; background: var(--sidebar-active); color: var(--sidebar-text); border: 1px solid var(--sidebar-control-line); border-radius: 6px; - padding: 0.35rem 0.45rem; + padding: 0.35rem 0.55rem; + cursor: pointer; + font-weight: 650; + list-style: none; +} + +.panel-role-switch summary::-webkit-details-marker { + display: none; +} + +.panel-role-switch summary::after { + width: 0.45rem; + height: 0.45rem; + flex: 0 0 auto; + margin-inline-start: auto; + margin-inline-end: 0.15rem; + border-inline-end: 1.5px solid currentColor; + border-block-end: 1.5px solid currentColor; + content: ""; + transform: translateY(-0.1rem) rotate(45deg); +} + +.panel-role-switch[open] summary::after { + transform: translateY(0.1rem) rotate(225deg); +} + +.panel-role-options { + position: absolute; + z-index: 3; + inset-block-end: calc(100% + 0.4rem); + inset-inline-start: 0; + display: grid; + width: max(100%, 16rem); + max-height: 55dvh; + overflow-y: auto; + padding: 0.25rem; + border: 1px solid var(--sidebar-control-line); + border-radius: 8px; + background: var(--sidebar); + box-shadow: 0 4px 14px #0004; +} + +.panel-role-option { + display: grid; + grid-template-columns: auto minmax(0, 1fr) auto; + align-items: center; + gap: 0.5rem; + padding: 0.5rem; + border: 1px solid transparent; + border-radius: 5px; + background: transparent; + color: var(--sidebar-text); + text-align: start; +} + +.panel-role-option:enabled { + cursor: pointer; +} + +.panel-role-option:hover:enabled, +.panel-role-option[aria-current] { + border-color: var(--sidebar-line); + background: var(--sidebar-active); +} + +.sidebar .panel-role-option:focus-visible { + outline-offset: -2px; +} + +.panel-role-option strong, +.panel-role-option small { + display: block; +} + +.panel-role-option small { + color: var(--sidebar-account); + font-size: 0.75rem; +} + +.panel-role-current { + color: var(--sidebar-highlight); + font-size: 0.75rem; + font-weight: 650; } .sidebar-action { @@ -1069,6 +1146,14 @@ dd { margin: 0; padding-top: 0.6rem; } + + .panel-role-options { + inset-block-start: calc(100% + 0.4rem); + inset-block-end: auto; + inset-inline-start: auto; + inset-inline-end: 0; + max-width: calc(100vw - 1.5rem); + } } @media (min-width: 520px) and (max-width: 780px) { diff --git a/src/coulobre/hosting/templates/hosting/base.html b/src/coulobre/hosting/templates/hosting/base.html index eb73888..be757ba 100644 --- a/src/coulobre/hosting/templates/hosting/base.html +++ b/src/coulobre/hosting/templates/hosting/base.html @@ -51,16 +51,25 @@ {% if not request.impersonation and request.panel_role_options|length > 1 %} -
- {% csrf_token %} - - - -
+ + {% endif %} diff --git a/src/coulobre/hosting/test_panel_roles.py b/src/coulobre/hosting/test_panel_roles.py index 279136c..11e2369 100644 --- a/src/coulobre/hosting/test_panel_roles.py +++ b/src/coulobre/hosting/test_panel_roles.py @@ -92,6 +92,22 @@ class PanelRoleTests(TestCase): self.assertEqual(self.switch("administrator").status_code, 403) self.assertEqual(self.switch("forged-role").status_code, 403) + def test_switcher_orders_workspaces_and_identifies_the_current_role(self): + self.sign_in() + self.assertEqual(self.switch("customer").status_code, 302) + response = self.client.get(reverse("panel:accounts")) + self.assertEqual( + [role for role, *_ in response.wsgi_request.panel_role_options], + [PanelRole.ADMIN, PanelRole.MANAGER, PanelRole.CUSTOMER], + ) + self.assertContains(response, 'aria-label="Switch role from Customer"') + self.assertContains(response, "Server and all accounts") + self.assertContains(response, "Customers you manage") + self.assertContains(response, "Your own hosting") + self.assertContains( + response, 'value="customer" aria-current="true" disabled' + ) + def test_removed_assignment_signs_out_before_processing_request(self): self.sign_in() self.assertEqual(self.switch("manager").status_code, 302) diff --git a/src/coulobre/hosting/test_workspaces.py b/src/coulobre/hosting/test_workspaces.py index 209a59e..d39b384 100644 --- a/src/coulobre/hosting/test_workspaces.py +++ b/src/coulobre/hosting/test_workspaces.py @@ -63,9 +63,7 @@ class WorkspaceTests(TestCase): operator = get_user_model().objects.create_superuser("operator") self.client.force_login(operator) landing = self.client.get(reverse("panel:accounts")) - self.assertContains( - landing, '', html=True - ) + self.assertContains(landing, 'name="role" value="manager"') profile = Reseller.objects.get(user=operator) unrelated = HostingAccount.objects.create( name="Other hosting", primary_domain="other.test"