129 lines
5.5 KiB
PHP
129 lines
5.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App;
|
|
|
|
use Desktop\AppRegistry;
|
|
use Desktop\AppIconResolver;
|
|
use Desktop\AppVisibility;
|
|
use Desktop\DesktopState;
|
|
use Desktop\SkinResolver;
|
|
use Desktop\WidgetRegistry;
|
|
use Desktop\WindowManager;
|
|
use ModulesCore\ModuleRegistry;
|
|
|
|
final class App
|
|
{
|
|
public function __construct(
|
|
private readonly string $projectRoot,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function desktopPayload(): array
|
|
{
|
|
$keycloakConfig = ConfigLoader::load($this->projectRoot, 'keycloak');
|
|
$auth = new KeycloakAuth($keycloakConfig);
|
|
$moduleRegistry = new ModuleRegistry(AppPaths::customAppsRoot($this->projectRoot));
|
|
$registry = new AppRegistry($this->projectRoot . '/config/apps.php', $moduleRegistry->desktopApps());
|
|
$widgetRegistry = new WidgetRegistry($this->projectRoot . '/config/widgets.php', $moduleRegistry->widgets());
|
|
$skins = SkinResolver::all();
|
|
$isAuthenticated = isset($_SESSION['desktop_auth']) && is_array($_SESSION['desktop_auth']);
|
|
$authUser = $isAuthenticated && is_array($_SESSION['desktop_auth']['user'] ?? null)
|
|
? $_SESSION['desktop_auth']['user']
|
|
: [];
|
|
$authGroups = array_values(array_map('strval', (array) ($authUser['groups'] ?? [])));
|
|
$isAdmin = in_array('administrators', $authGroups, true);
|
|
$visibleApps = AppVisibility::filterByGroups($registry->all(), $authGroups);
|
|
$userSelfManagement = new UserSelfManagementService($this->projectRoot);
|
|
$resolvedSkin = $userSelfManagement->resolveActiveSkin(
|
|
isset($_GET['skin']) ? (string) $_GET['skin'] : null,
|
|
$authUser,
|
|
$visibleApps,
|
|
$widgetRegistry->all(),
|
|
$skins
|
|
);
|
|
$userSettings = $resolvedSkin['settings'];
|
|
$activeSkin = (string) $resolvedSkin['skin'];
|
|
$enabledApps = AppVisibility::filterByEnabledIds(
|
|
$visibleApps,
|
|
array_values(array_map('strval', (array) ($userSettings['apps']['enabled_ids'] ?? []))),
|
|
[UserSelfManagementService::APP_ID]
|
|
);
|
|
$apps = AppIconResolver::decorate($enabledApps, $activeSkin);
|
|
$windows = (new WindowManager($registry))->defaultWindows();
|
|
$enabledAppIds = array_values(array_filter(array_map(
|
|
static fn (array $app): string => (string) ($app['app_id'] ?? ''),
|
|
$enabledApps
|
|
)));
|
|
$availableWidgets = $widgetRegistry->availableForApps($enabledAppIds);
|
|
$activeWidgetIds = array_values(array_map('strval', (array) ($userSettings['widgets']['active_ids'] ?? DesktopState::defaultWidgetIds())));
|
|
$trayItems = DesktopState::trayItems();
|
|
foreach ($enabledApps as $app) {
|
|
if (empty($app['supports_tray']) || (string) ($app['app_id'] ?? '') !== 'fx-rates') {
|
|
continue;
|
|
}
|
|
|
|
$trayItems[] = [
|
|
'id' => 'app-' . (string) $app['app_id'],
|
|
'label' => (string) ($app['icon'] ?? 'FX'),
|
|
'app_id' => (string) ($app['app_id'] ?? ''),
|
|
'title' => (string) ($app['title'] ?? ''),
|
|
];
|
|
}
|
|
$displayName = (string) ($authUser['name'] ?? $authUser['username'] ?? 'Gast');
|
|
$storageScope = $isAuthenticated
|
|
? strtolower((string) ($authUser['username'] ?? $authUser['sub'] ?? 'user'))
|
|
: 'guest';
|
|
|
|
return [
|
|
'meta' => [
|
|
'title' => 'Kusche.Berlin Desktop UI',
|
|
'active_skin' => $activeSkin,
|
|
'available_skins' => $skins,
|
|
'base_skin_profile' => SkinResolver::profile('base'),
|
|
'skin_profile' => SkinResolver::profile($activeSkin),
|
|
],
|
|
'desktop' => [
|
|
'greeting' => 'Neue Desktop-Shell fuer Kusche.Berlin',
|
|
'wallpaper' => SkinResolver::wallpaper($activeSkin),
|
|
'storage_scope' => preg_replace('/[^a-z0-9._-]+/i', '-', $storageScope) ?: 'guest',
|
|
'workspace' => DesktopState::defaultWorkspace(),
|
|
'settings_api' => '/api/user-self-management/index.php',
|
|
'user_preferences' => $userSettings,
|
|
'login' => [
|
|
'authenticated' => $isAuthenticated,
|
|
'mode' => $isAuthenticated ? 'authenticated' : 'login-required',
|
|
'keycloak_theme_handoff' => '/docs/keycloak-theme-handoff.md',
|
|
'user' => [
|
|
'name' => (string) ($authUser['name'] ?? ''),
|
|
'username' => (string) ($authUser['username'] ?? ''),
|
|
'email' => (string) ($authUser['email'] ?? ''),
|
|
],
|
|
],
|
|
],
|
|
'apps' => $apps,
|
|
'windows' => $windows,
|
|
'widgets' => [
|
|
'active' => $widgetRegistry->active($activeWidgetIds, $enabledAppIds),
|
|
'registry' => $availableWidgets,
|
|
'active_ids' => $activeWidgetIds,
|
|
],
|
|
'tray' => $trayItems,
|
|
'session' => [
|
|
'display_name' => $displayName,
|
|
'authenticated' => $isAuthenticated,
|
|
'is_admin' => $isAdmin,
|
|
'groups' => $authGroups,
|
|
'logout_url' => $isAuthenticated && $auth->isConfigured() ? '/auth/logout' : null,
|
|
],
|
|
'menu' => [
|
|
'quick_actions' => DesktopState::quickActions(),
|
|
],
|
|
];
|
|
}
|
|
}
|