From aa811d2df746f1f36c9c2e51d4d29f331cb20ff2 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Tue, 11 Feb 2025 13:18:50 +0100 Subject: [PATCH 1/2] make the user backend a ISetDisplayNameBackend one, this fixes incorrect photo in system addressbook vcard and simplifies the provisioning service Signed-off-by: Julien Veyssier --- lib/Service/ProvisioningService.php | 28 ++++------------------------ lib/User/Backend.php | 14 +++++++++++++- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/lib/Service/ProvisioningService.php b/lib/Service/ProvisioningService.php index a853d95d..fb92e00d 100644 --- a/lib/Service/ProvisioningService.php +++ b/lib/Service/ProvisioningService.php @@ -9,7 +9,6 @@ use InvalidArgumentException; use OC\Accounts\AccountManager; -use OCA\UserOIDC\AppInfo\Application; use OCA\UserOIDC\Db\UserMapper; use OCA\UserOIDC\Event\AttributeMappedEvent; use OCP\Accounts\IAccountManager; @@ -176,29 +175,10 @@ public function provisionUser(string $tokenUserId, int $providerId, object $idTo if ($event->hasValue() && $event->getValue() !== null && $event->getValue() !== '') { $oidcGssUserData[$displaynameAttribute] = $event->getValue(); $newDisplayName = $event->getValue(); - if ($existingLocalUser === null) { - $oldDisplayName = $backendUser->getDisplayName(); - if ($newDisplayName !== $oldDisplayName) { - $backendUser->setDisplayName($newDisplayName); - $this->userMapper->update($backendUser); - } - // 2 reasons why we should update the display name: It does not match the one - // - of our backend - // - returned by the user manager (outdated one before the fix in https://github.com/nextcloud/user_oidc/pull/530) - if ($newDisplayName !== $oldDisplayName || $newDisplayName !== $user->getDisplayName()) { - $this->eventDispatcher->dispatchTyped(new UserChangedEvent($user, 'displayName', $newDisplayName, $oldDisplayName)); - } - } else { - $oldDisplayName = $user->getDisplayName(); - if ($newDisplayName !== $oldDisplayName) { - $user->setDisplayName($newDisplayName); - if ($user->getBackendClassName() === Application::APP_ID) { - $backendUser = $this->userMapper->getOrCreate($providerId, $user->getUID()); - $backendUser->setDisplayName($newDisplayName); - $this->userMapper->update($backendUser); - } - $this->eventDispatcher->dispatchTyped(new UserChangedEvent($user, 'displayName', $newDisplayName, $oldDisplayName)); - } + $oldDisplayName = $user->getDisplayName(); + if ($newDisplayName !== $oldDisplayName) { + $user->setDisplayName($newDisplayName); + $this->eventDispatcher->dispatchTyped(new UserChangedEvent($user, 'displayName', $newDisplayName, $oldDisplayName)); } } diff --git a/lib/User/Backend.php b/lib/User/Backend.php index 260560be..4ebe314d 100644 --- a/lib/User/Backend.php +++ b/lib/User/Backend.php @@ -37,9 +37,10 @@ use OCP\User\Backend\ICustomLogout; use OCP\User\Backend\IGetDisplayNameBackend; use OCP\User\Backend\IPasswordConfirmationBackend; +use OCP\User\Backend\ISetDisplayNameBackend; use Psr\Log\LoggerInterface; -class Backend extends ABackend implements IPasswordConfirmationBackend, IGetDisplayNameBackend, IApacheBackend, ICustomLogout, ICountUsersBackend { +class Backend extends ABackend implements IPasswordConfirmationBackend, IGetDisplayNameBackend, ISetDisplayNameBackend, IApacheBackend, ICustomLogout, ICountUsersBackend { private $tokenValidators = [ SelfEncodedValidator::class, UserInfoValidator::class, @@ -91,6 +92,17 @@ public function userExists($uid): bool { return $this->userMapper->userExists($uid); } + public function setDisplayName(string $uid, string $displayName): bool { + try { + $user = $this->userMapper->getUser($uid); + $user->setDisplayName($displayName); + $this->userMapper->update($user); + return true; + } catch (DoesNotExistException $e) { + return false; + } + } + public function getDisplayName($uid): string { try { $user = $this->userMapper->getUser($uid); From 1008f6150e7ae52196f5dd8c0c905f4eecfad862 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Tue, 11 Feb 2025 13:39:41 +0100 Subject: [PATCH 2/2] fix tests Signed-off-by: Julien Veyssier --- tests/unit/Service/ProvisioningServiceTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/Service/ProvisioningServiceTest.php b/tests/unit/Service/ProvisioningServiceTest.php index bf3369c8..a6ef2294 100644 --- a/tests/unit/Service/ProvisioningServiceTest.php +++ b/tests/unit/Service/ProvisioningServiceTest.php @@ -154,7 +154,7 @@ public function testProvisionUserAutoProvisioning(): void { ->method('get') ->willReturn($user); - $backendUser->expects(self::once()) + $user->expects(self::once()) ->method('setDisplayName') ->with($name); $user->expects(self::once()) @@ -225,7 +225,7 @@ public function testProvisionUserInvalidProperties(): void { ->method('get') ->willReturn($user); - $backendUser->expects(self::once()) + $user->expects(self::once()) ->method('setDisplayName') ->with($name); $user->expects(self::once()) @@ -257,7 +257,7 @@ public function testProvisionUserInvalidProperties(): void { ->method('getProperty') ->with('twitter') ->willReturn($property); - + $this->accountManager->expects(self::once()) ->method('getAccount')