From c10016149bebcd897ea7ec325fb37636dd8c686e Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Thu, 28 May 2026 06:26:59 +0200 Subject: [PATCH] fix(sync): correct processContactPerson username resolution and error logging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit H2: fix empty-if that caused $username to always fall back to email even when an existing username was stored in the contact record. Fix: $username = (empty($existingUsername) === false) ? $existingUsername : $email H2/L4: wrap error-log in else-branch of the success check so it only fires on actual failure — previously fired unconditionally after every user creation, even on success. Comment clarification: 'User exists, update username' block condition (empty($existingUsername) === true) correctly fires when no username is stored yet; renamed comment to reflect actual intent. --- lib/Service/OrganizationSyncService.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) 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',