ldap update
All checks were successful
Deploy / deploy-staging (push) Successful in 24s
Deploy / deploy-production (push) Has been skipped

This commit is contained in:
2026-06-17 23:43:32 +02:00
parent 26b4355898
commit b3e8085085
4 changed files with 109 additions and 2 deletions

View File

@@ -202,6 +202,63 @@ final class LdapProvisioner
}
}
/**
* @param array<string, string> $profile
* @return array{success: bool, message: string, dn?: string, updated_fields?: array<int, string>, skipped_fields?: array<int, string>}
*/
public function updateUserProfile(string $username, array $profile): array
{
try {
$link = $this->connectAsService();
$userDn = $this->userDnForUsername($username);
$fullName = trim((string) ($profile['name'] ?? ''));
[$givenName, $surname] = $this->splitDisplayName($fullName, $username);
$attributeMap = [
'cn' => $fullName,
'displayName' => $fullName,
'givenName' => $givenName,
'sn' => $surname,
'mail' => trim((string) ($profile['email'] ?? '')),
'telephoneNumber' => trim((string) ($profile['phone'] ?? '')),
'title' => trim((string) ($profile['title'] ?? '')),
'l' => trim((string) ($profile['city'] ?? '')),
];
$updatedFields = [];
$skippedFields = [];
foreach ($attributeMap as $attribute => $value) {
if ($value === '' && in_array($attribute, ['cn', 'displayName', 'givenName', 'sn'], true)) {
$skippedFields[] = $attribute;
continue;
}
if ($value === '') {
@ldap_mod_del($link, $userDn, [$attribute => []]);
continue;
}
if (!@ldap_mod_replace($link, $userDn, [$attribute => [$value]])) {
throw new RuntimeException('LDAP-Profilaktualisierung fehlgeschlagen fuer ' . $attribute . ': ' . $this->lastError($link));
}
$updatedFields[] = $attribute;
}
return [
'success' => true,
'message' => 'LDAP-Profil wurde aktualisiert.',
'dn' => $userDn,
'updated_fields' => $updatedFields,
'skipped_fields' => $skippedFields,
];
} catch (\Throwable $exception) {
return [
'success' => false,
'message' => $exception->getMessage(),
];
}
}
/**
* @param array<string, mixed> $request
* @param array<string, mixed> $provisioning
@@ -452,6 +509,32 @@ final class LdapProvisioner
return array_values(array_map('strval', (array) ($this->config['ldap']['default_groups'] ?? [])));
}
/**
* @return array{0: string, 1: string}
*/
private function splitDisplayName(string $fullName, string $username): array
{
$normalized = preg_replace('/\s+/', ' ', trim($fullName)) ?? '';
if ($normalized === '') {
return [$username, $username];
}
$parts = explode(' ', $normalized);
if (count($parts) === 1) {
return [$parts[0], $parts[0]];
}
$surname = (string) array_pop($parts);
$givenName = trim(implode(' ', $parts));
return [
$givenName !== '' ? $givenName : $surname,
$surname !== '' ? $surname : $username,
];
}
/**
* @return resource|\LDAP\Connection
*/