weitere updates für instanzierbarkeit und sauberes testing

This commit is contained in:
2026-02-24 08:49:40 +01:00
parent 7b53faca37
commit 16759a2732
119 changed files with 6371 additions and 745 deletions

View File

@@ -443,6 +443,104 @@ class UserAccountService
return ['ok' => true];
}
/**
* Self-service profile update (subset of fields, no admin-only changes).
*
* @param int $userId
* @param array $input
* @return array{ok: bool, errors?: list<string>, form?: array<string, mixed>}
*/
public function updateSelfProfile(int $userId, array $input): array
{
$existing = $this->userReadRepository->find($userId);
if (!$existing) {
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
}
$allowedFields = [
'first_name', 'last_name', 'profile_description', 'job_title',
'phone', 'mobile', 'short_dial',
'address', 'postal_code', 'city', 'country', 'region',
];
$form = [];
foreach ($allowedFields as $field) {
$form[$field] = array_key_exists($field, $input)
? trim((string) $input[$field])
: ($existing[$field] ?? '');
}
$errors = [];
if ($form['first_name'] === '') {
$errors[] = t('First name cannot be empty');
}
if ($form['last_name'] === '') {
$errors[] = t('Last name cannot be empty');
}
$locale = array_key_exists('locale', $input)
? $this->normalizeLocale($input['locale'])
: ($existing['locale'] ?? '');
$theme = array_key_exists('theme', $input)
? $this->normalizeTheme($input['theme'])
: ($existing['theme'] ?? 'light');
// Tenant switch
$currentTenantId = null;
if (array_key_exists('current_tenant_uuid', $input)) {
$tenantUuid = trim((string) ($input['current_tenant_uuid'] ?? ''));
if ($tenantUuid !== '') {
$tenantService = directoryServicesFactory()->createTenantService();
$tenant = $tenantService->findByUuid($tenantUuid);
if (!$tenant || !isset($tenant['id'])) {
$errors[] = t('Tenant not found');
} else {
$userTenantIds = $this->userAssignmentService->buildAssignmentsForUser($userId)['tenants'] ?? [];
$userTenantIds = array_map(static fn(array $t): int => (int) ($t['id'] ?? 0), $userTenantIds);
$tenantId = (int) $tenant['id'];
if (!in_array($tenantId, $userTenantIds, true)) {
$errors[] = t('No access to this tenant');
} else {
$currentTenantId = $tenantId;
}
}
}
}
if ($errors) {
return ['ok' => false, 'errors' => $errors, 'form' => $form];
}
$updateData = [
'first_name' => $form['first_name'],
'last_name' => $form['last_name'],
'profile_description' => $form['profile_description'] !== '' ? $form['profile_description'] : null,
'job_title' => $form['job_title'] !== '' ? $form['job_title'] : null,
'phone' => $form['phone'] !== '' ? $form['phone'] : null,
'mobile' => $form['mobile'] !== '' ? $form['mobile'] : null,
'short_dial' => $form['short_dial'] !== '' ? $form['short_dial'] : null,
'address' => $form['address'] !== '' ? $form['address'] : null,
'postal_code' => $form['postal_code'] !== '' ? $form['postal_code'] : null,
'city' => $form['city'] !== '' ? $form['city'] : null,
'country' => $form['country'] !== '' ? $form['country'] : null,
'region' => $form['region'] !== '' ? $form['region'] : null,
'locale' => $locale,
'theme' => $theme,
'modified_by' => $userId,
];
$updated = $this->userWriteRepository->update($userId, $updateData);
if (!$updated) {
return ['ok' => false, 'errors' => [t('Profile can not be updated')]];
}
if ($currentTenantId !== null) {
$this->userWriteRepository->setCurrentTenant($userId, $currentTenantId);
}
return ['ok' => true, 'form' => $form];
}
private function filterUuidsByTenantScope(array $uuids, int $currentUserId): array
{
$allowed = [];