Distinguish workspaces in the role switcher
This commit is contained in:
1 parent
d9a85fd8d7
commit
006c85d327
6 files changed
+152
-34
No files matched your search
@@ -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)
|
||||
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -51,16 +51,25 @@
|
||||
</form>
|
||||
</div>
|
||||
{% if not request.impersonation and request.panel_role_options|length > 1 %}
|
||||
<form method="post" class="panel-role-switch" action="{% url 'panel:switch_panel_role' %}" data-role-switch>
|
||||
{% csrf_token %}
|
||||
<label class="visually-hidden" for="panel-role">Panel role</label>
|
||||
<select name="role" id="panel-role">
|
||||
{% for value, label in request.panel_role_options %}
|
||||
<option value="{{ value }}"{% if request.active_panel_role == value %} selected{% endif %}>{{ label }}</option>
|
||||
<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>
|
||||
{{ request.panel_role_label }}
|
||||
</summary>
|
||||
<form method="post" class="panel-role-options" action="{% url 'panel:switch_panel_role' %}">
|
||||
{% csrf_token %}
|
||||
{% for value, label, icon, description in request.panel_role_options %}
|
||||
<button class="panel-role-option" type="submit" name="role" value="{{ value }}"{% if request.active_panel_role == value %} aria-current="true" disabled{% endif %}>
|
||||
<svg class="nav-icon" aria-hidden="true" focusable="false"><use href="{{ panel_icons }}#{{ icon }}"></use></svg>
|
||||
<span>
|
||||
<strong>{{ label }}</strong>
|
||||
<small>{{ description }}</small>
|
||||
</span>
|
||||
{% if request.active_panel_role == value %}<span class="panel-role-current">Current</span>{% endif %}
|
||||
</button>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<button class="sidebar-action" type="submit">Switch role</button>
|
||||
</form>
|
||||
</form>
|
||||
</details>
|
||||
{% endif %}
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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, '<option value="manager">Manager</option>', 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"
|
||||
|
||||
Reference in new issue
Block a user