fix(topbar): resolve theme + tenant-switch endpoints via lurl() to avoid relative URL breakage

Both endpoints were declared as bare relative paths ("admin/users/theme",
"admin/users/switch-tenant") and handed to postForm(), which normalizes
through `new URL(value, window.location.href)`. On a deep route like
/de/admin/permissions that resolves to /de/admin/admin/users/theme —
404 → 302 to error page → HTML response → JSON.parse throws → the theme
toggle reverts visually. The tenant switcher had the same latent bug.

- Topbar renders data-theme-url and data-switch-tenant-url via lurl(), so
  both carry the locale-aware root-anchored path.
- Tenant switcher reads data-switch-tenant-url from the switcher root
  (matching the theme-menu convention) and passes it through to
  switchTenant(); fail-fast warning if the attribute is missing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-22 20:39:20 +02:00
parent 4cf4ccc595
commit 3c6ce0cbdb
2 changed files with 16 additions and 7 deletions

View File

@@ -85,7 +85,7 @@ $moduleTopbarSlots = is_array($moduleSlots['topbar.right_item'] ?? null) ? $modu
?> ?>
<?php endforeach; ?> <?php endforeach; ?>
<?php if ($canSwitchTenant): ?> <?php if ($canSwitchTenant): ?>
<li data-app-component="tenant-switcher" data-tenant-switcher data-tooltip="<?php e(t('Tenant')); ?>" data-tooltip-pos="bottom"> <li data-app-component="tenant-switcher" data-tenant-switcher data-switch-tenant-url="<?php e(lurl('admin/users/switch-tenant')); ?>" data-tooltip="<?php e(t('Tenant')); ?>" data-tooltip-pos="bottom">
<details class="dropdown app-topbar-tenant-menu" data-summary-chevron="none"> <details class="dropdown app-topbar-tenant-menu" data-summary-chevron="none">
<summary aria-label="<?php e(t('Switch tenant')); ?>" title="<?php e($tenantLabel); ?>"> <summary aria-label="<?php e(t('Switch tenant')); ?>" title="<?php e($tenantLabel); ?>">
<i class="bi bi-building" aria-hidden="true"></i> <i class="bi bi-building" aria-hidden="true"></i>
@@ -127,7 +127,7 @@ $moduleTopbarSlots = is_array($moduleSlots['topbar.right_item'] ?? null) ? $modu
<details class="dropdown app-topbar-user-menu" data-summary-chevron="none" data-app-component="theme-controls" <details class="dropdown app-topbar-user-menu" data-summary-chevron="none" data-app-component="theme-controls"
<?php if ($allowUserTheme): ?> <?php if ($allowUserTheme): ?>
data-theme-menu data-theme-menu
data-theme-url="admin/users/theme" data-theme-url="<?php e(lurl('admin/users/theme')); ?>"
data-csrf-key="<?php e($csrfKey); ?>" data-csrf-key="<?php e($csrfKey); ?>"
data-csrf-token="<?php e($csrfToken); ?>" data-csrf-token="<?php e($csrfToken); ?>"
<?php endif; ?>> <?php endif; ?>>

View File

@@ -18,14 +18,14 @@ const setPending = (link, pending) => {
} }
}; };
const switchTenant = async (link) => { const switchTenant = async (link, switchUrl) => {
const tenantId = link.dataset.switchTenant; const tenantId = link.dataset.switchTenant;
const csrfKey = link.dataset.csrfKey; const csrfKey = link.dataset.csrfKey;
const csrfToken = link.dataset.csrfToken; const csrfToken = link.dataset.csrfToken;
const errorMessage = link.dataset.errorMessage || 'Failed to switch tenant'; const errorMessage = link.dataset.errorMessage || 'Failed to switch tenant';
if (!tenantId || !csrfKey || !csrfToken) { if (!tenantId || !csrfKey || !csrfToken || !switchUrl) {
warnOnce('UI_DATA_MISSING', 'Missing tenant ID or CSRF token', { module: 'tenant-switcher' }); warnOnce('UI_DATA_MISSING', 'Missing tenant ID, CSRF token, or switch URL', { module: 'tenant-switcher' });
return; return;
} }
@@ -33,7 +33,7 @@ const switchTenant = async (link) => {
setPending(link, true); setPending(link, true);
try { try {
const data = await postForm('admin/users/switch-tenant', { const data = await postForm(switchUrl, {
tenant_id: tenantId, tenant_id: tenantId,
[csrfKey]: csrfToken [csrfKey]: csrfToken
}); });
@@ -71,11 +71,20 @@ export const initTenantSwitcher = (root = document, options = {}) => {
return { destroy: () => {} }; return { destroy: () => {} };
} }
// Endpoint is rendered server-side via lurl() so it carries the locale-aware
// absolute path. Passing a relative path to postForm would resolve against
// window.location.href and break on deep routes like /de/admin/tenants/edit/...
const switchUrl = (switcherRoot instanceof HTMLElement ? switcherRoot.dataset.switchTenantUrl : '') || '';
if (!switchUrl) {
warnOnce('UI_DATA_MISSING', 'Missing data-switch-tenant-url on tenant switcher root', { module: 'tenant-switcher' });
return { destroy: () => {} };
}
const cleanupFns = []; const cleanupFns = [];
tenantLinks.forEach(link => { tenantLinks.forEach(link => {
const onClick = (e) => { const onClick = (e) => {
e.preventDefault(); e.preventDefault();
switchTenant(link); switchTenant(link, switchUrl);
}; };
link.addEventListener('click', onClick); link.addEventListener('click', onClick);
cleanupFns.push(() => link.removeEventListener('click', onClick)); cleanupFns.push(() => link.removeEventListener('click', onClick));