Files
breadcrumb-the-shire/pages/admin/users/switch-tenant().php
fs c7b8fd516a feat: extend module platform with UI slots, runtime components, CLI tooling and {{userId}} search support
Completes the generic module platform that enables modules to contribute
UI elements, runtime JS components, and search resources without any
core hardcoding.

New generic UI slot types:
- topbar.right_item: module-contributed topbar buttons
- layout.body_end_template: module-contributed dialog/overlay templates
- layout.head_style: module-contributed global CSS
- runtime.component: declarative JS component registration with phase ordering

New infrastructure:
- ModuleAutoloader: PSR-4 autoloading for module-local PHP classes
- ModuleRuntimePageBuilder: symlinks module pages into runtime directory
- ModuleRuntimeAssetPublisher: publishes module CSS/JS to web/modules/
- ModulePermissionSynchronizer: syncs module permissions to DB
- CLI scripts: module-runtime-sync, module-build, module-migrate,
  module-permissions-sync, module-assets-sync
- {{userId}} placeholder in SearchDataService for user-scoped search queries
- Component runtime with phased initialization (early/default/late)
- AppContainer.protectExistingBindings() to prevent module→core overwrites
- Architecture tests: ModuleStructureContractTest, CoreTemplateIsolationTest,
  FrontendComponentRuntimeContractTest, AppContainerIsolationContractTest

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 22:19:56 +01:00

94 lines
2.5 KiB
PHP

<?php
use MintyPHP\Http\Request;
use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Router;
use MintyPHP\Session;
use MintyPHP\Support\Guard;
$sessionStore = app(SessionStoreInterface::class);
$session = $sessionStore->all();
Guard::requireLogin();
$userTenantContextService = app(\MintyPHP\Service\User\UserTenantContextService::class);
$errorBag = formErrors();
if ((requestInput()->method()) !== 'POST') {
Router::redirect('admin');
return;
}
if (!Session::checkCsrfToken()) {
if (Request::wantsJson()) {
http_response_code(400);
Router::json(['ok' => false, 'error' => 'csrf']);
return;
}
$errorBag->addGlobal(t('Form expired, please try again'));
flashFormErrors($errorBag, 'admin', 'switch_tenant');
Router::redirect('admin');
return;
}
$userId = (int) ($session['user']['id'] ?? 0);
if ($userId <= 0) {
http_response_code(401);
if (Request::wantsJson()) {
Router::json(['ok' => false, 'error' => 'unauthorized']);
return;
}
Router::redirect('login');
return;
}
// Get tenant_id from POST
$tenantId = (int) (requestInput()->bodyAll()['tenant_id'] ?? 0);
if ($tenantId <= 0) {
http_response_code(400);
if (Request::wantsJson()) {
Router::json(['ok' => false, 'error' => 'invalid_tenant_id']);
return;
}
$errorBag->addGlobal(t('Failed to switch tenant'));
flashFormErrors($errorBag, 'admin', 'switch_tenant');
Router::redirect('admin');
return;
}
$result = $userTenantContextService->setCurrentTenant($userId, $tenantId);
if (!($result['ok'] ?? false)) {
$error = $result['error'] ?? 'update_failed';
http_response_code(400);
if (Request::wantsJson()) {
Router::json(['ok' => false, 'error' => $error]);
return;
}
$errorBag->addGlobal(t('Failed to switch tenant'));
flashFormErrors($errorBag, 'admin', 'switch_tenant');
Router::redirect('admin');
return;
}
// Update session with new current tenant
if (isset($session['user']) && is_array($session['user'])) {
$userSession = $session['user'];
$userSession['current_tenant_id'] = $tenantId;
$sessionStore->set('user', $userSession);
}
// Reload full tenant + module session context in one place.
app(\MintyPHP\Service\Auth\AuthService::class)->loadTenantDataIntoSession($userId);
if (Request::wantsJson()) {
Router::json([
'ok' => true,
'tenant_id' => $tenantId,
'tenant' => $result['tenant'] ?? null,
'theme' => currentTheme(),
]);
return;
}
Router::redirect('admin');