diff --git a/lib/Service/OrganizationSyncService.php b/lib/Service/OrganizationSyncService.php index 1bc913af..e56280bb 100644 --- a/lib/Service/OrganizationSyncService.php +++ b/lib/Service/OrganizationSyncService.php @@ -1240,10 +1240,9 @@ private function processContactPerson(object $contactPerson, array &$stats): ?st } // Check if user already exists. - $userManager = $this->container->get('OCP\IUserManager'); - $username = $email; - if (empty($existingUsername) === false) { - } + $userManager = $this->container->get('OCP\IUserManager'); + // Use the stored username when available, otherwise fall back to email as username. + $username = (empty($existingUsername) === false) ? $existingUsername : $email; $user = $userManager->get($username); @@ -1268,8 +1267,7 @@ private function processContactPerson(object $contactPerson, array &$stats): ?st 'username' => $username, ] ); - } - + } else { $this->logger->error( 'OrganizationSyncService: Failed to create user account', [ @@ -1277,9 +1275,10 @@ private function processContactPerson(object $contactPerson, array &$stats): ?st 'username' => $username, ] ); + } }//end if - // User exists, update username in contact if needed. + // Contact exists without a stored username — record the resolved username for future syncs. if (empty($existingUsername) === true) { $this->logger->debug( 'OrganizationSyncService: Updating contact person with username', @@ -1291,7 +1290,7 @@ private function processContactPerson(object $contactPerson, array &$stats): ?st $stats['usersUpdated']++; } - return $username; + return $username; } catch (\Exception $e) { $this->logger->error( 'OrganizationSyncService: Failed to process contact person',