109 lines
3.8 KiB
PHP
109 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once dirname(__DIR__, 3) . '/src/App/bootstrap.php';
|
|
|
|
use App\ConfigLoader;
|
|
use App\KeycloakAuth;
|
|
use App\LdapProvisioner;
|
|
use App\UserSelfManagementService;
|
|
use Desktop\AppRegistry;
|
|
use Desktop\AppVisibility;
|
|
use Desktop\SkinResolver;
|
|
use Desktop\WidgetRegistry;
|
|
|
|
session_start();
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
$projectRoot = dirname(__DIR__, 3);
|
|
$method = strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET');
|
|
|
|
if ($method === 'OPTIONS') {
|
|
header('Allow: GET, PUT, OPTIONS');
|
|
exit;
|
|
}
|
|
|
|
$keycloakConfig = ConfigLoader::load($projectRoot, 'keycloak');
|
|
$auth = new KeycloakAuth($keycloakConfig);
|
|
$registry = new AppRegistry($projectRoot . '/config/apps.php');
|
|
$widgetRegistry = new WidgetRegistry($projectRoot . '/config/widgets.php');
|
|
$service = new UserSelfManagementService($projectRoot);
|
|
$registrationConfig = ConfigLoader::load($projectRoot, 'registration');
|
|
$ldap = new LdapProvisioner($registrationConfig);
|
|
$isAuthenticated = $auth->isAuthenticated();
|
|
$authUser = $isAuthenticated && is_array($_SESSION['desktop_auth']['user'] ?? null)
|
|
? $_SESSION['desktop_auth']['user']
|
|
: [];
|
|
$authGroups = array_values(array_map('strval', (array) ($authUser['groups'] ?? [])));
|
|
$visibleApps = AppVisibility::filterByGroups($registry->all(), $authGroups);
|
|
$widgets = $widgetRegistry->all();
|
|
$skins = SkinResolver::all();
|
|
|
|
if ($method === 'GET') {
|
|
respond($service->buildBootstrapPayload($authUser, $visibleApps, $widgets, $skins));
|
|
}
|
|
|
|
if ($method !== 'PUT') {
|
|
respond(['error' => 'Method not allowed.'], 405);
|
|
}
|
|
|
|
$rawBody = file_get_contents('php://input');
|
|
$decoded = json_decode($rawBody !== false ? $rawBody : '', true);
|
|
|
|
if (!is_array($decoded)) {
|
|
respond(['error' => 'Invalid JSON payload.'], 400);
|
|
}
|
|
|
|
$before = $service->load($authUser, $visibleApps, $widgets, $skins);
|
|
$after = $service->save($decoded, $authUser, $visibleApps, $widgets, $skins);
|
|
$payload = $service->buildBootstrapPayload($authUser, $visibleApps, $widgets, $skins);
|
|
$payload['preferences'] = $after;
|
|
$payload['meta']['requires_reload'] = requiresReload($before, $after);
|
|
$payload['meta']['ldap_profile_sync'] = [
|
|
'attempted' => false,
|
|
'success' => false,
|
|
'message' => 'LDAP-Sync nicht ausgefuehrt.',
|
|
];
|
|
|
|
$username = trim((string) ($authUser['username'] ?? ''));
|
|
if ($username !== '' && $ldap->isEnabled()) {
|
|
$payload['meta']['ldap_profile_sync']['attempted'] = true;
|
|
$ldapResult = $ldap->updateUserProfile($username, is_array($after['profile'] ?? null) ? $after['profile'] : []);
|
|
$payload['meta']['ldap_profile_sync']['success'] = (bool) ($ldapResult['success'] ?? false);
|
|
$payload['meta']['ldap_profile_sync']['message'] = (string) ($ldapResult['message'] ?? 'LDAP-Sync ohne Rueckmeldung.');
|
|
if (isset($ldapResult['updated_fields']) && is_array($ldapResult['updated_fields'])) {
|
|
$payload['meta']['ldap_profile_sync']['updated_fields'] = array_values(array_map('strval', $ldapResult['updated_fields']));
|
|
}
|
|
}
|
|
|
|
respond($payload);
|
|
|
|
/**
|
|
* @param array<string, mixed> $payload
|
|
*/
|
|
function respond(array $payload, int $statusCode = 200): never
|
|
{
|
|
http_response_code($statusCode);
|
|
echo json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $before
|
|
* @param array<string, mixed> $after
|
|
*/
|
|
function requiresReload(array $before, array $after): bool
|
|
{
|
|
$beforeSkin = (string) ($before['desktop']['active_skin'] ?? '');
|
|
$afterSkin = (string) ($after['desktop']['active_skin'] ?? '');
|
|
$beforeApps = array_values(array_map('strval', (array) ($before['apps']['enabled_ids'] ?? [])));
|
|
$afterApps = array_values(array_map('strval', (array) ($after['apps']['enabled_ids'] ?? [])));
|
|
|
|
sort($beforeApps);
|
|
sort($afterApps);
|
|
|
|
return $beforeSkin !== $afterSkin || $beforeApps !== $afterApps;
|
|
}
|