From ad58aac1075e1604ed84a71919c8e8bb9392ce99 Mon Sep 17 00:00:00 2001 From: Olav Seyfarth Date: Thu, 3 Dec 2026 01:35:18 +0100 Subject: [PATCH 1/2] fix(core): route user:profile email and displayname through IUser occ user:profile only wrote the account property, so the system email address (IUser::getEMailAddress()) and the user backend's display name kept their old values and no UserChangedEvent was fired. Mails kept going to a deleted address and apps listening for the change never noticed. Setting both keys through the IUser setters, as user:setting does, lets the existing listener sync the account. Fixes #61831 Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Olav Seyfarth --- core/Command/User/Profile.php | 18 +++++ tests/Core/Command/User/ProfileTest.php | 92 +++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/core/Command/User/Profile.php b/core/Command/User/Profile.php index 1b3b6ac7797d4..738ac9bc426cc 100644 --- a/core/Command/User/Profile.php +++ b/core/Command/User/Profile.php @@ -179,6 +179,24 @@ private function deleteProfileProperty(OutputInterface $output, IAccount $userAc } private function editProfileProperty(OutputInterface $output, IAccount $userAccount, string $key, string $value): int { + $user = $userAccount->getUser(); + // Through IUser, so system values and events follow; a listener syncs the account. + if ($key === IAccountManager::PROPERTY_EMAIL) { + $user->setSystemEMailAddress($value); + return self::SUCCESS; + } + if ($key === IAccountManager::PROPERTY_DISPLAYNAME) { + if ($user->setDisplayName($value) || $user->getDisplayName() === $value) { + return self::SUCCESS; + } + if ($value === '') { + $output->writeln('New display name can\'t be empty'); + } else { + $output->writeln('Could not set display name'); + } + return self::FAILURE; + } + try { $userAccount->getProperty($key)->setValue($value); } catch (PropertyDoesNotExistException $exception) { diff --git a/tests/Core/Command/User/ProfileTest.php b/tests/Core/Command/User/ProfileTest.php index d967fc0b1281e..e0e8e69c2f6b9 100644 --- a/tests/Core/Command/User/ProfileTest.php +++ b/tests/Core/Command/User/ProfileTest.php @@ -318,6 +318,98 @@ public function testExecuteSetProfileProperty(): void { $this->assertEquals(0, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); } + /** + * Prepares a set (value given) or delete (value null) call for a key that must go through IUser. + * + * @return array{0: Profile&MockObject, 1: IUser&MockObject} + */ + private function prepareUserBackedProperty(string $key, ?string $inputValue, string $storedValue): array { + $command = $this->getCommand(['checkInput']); + + $this->consoleInput->method('getArgument') + ->willReturnMap([ + ['uid', 'username'], + ['key', $key], + ['value', $inputValue], + ]); + $this->consoleInput->method('hasParameterOption') + ->willReturnCallback(fn (string $option): bool => $inputValue === null && $option === '--delete'); + + $mocks = $this->setupProfilePropertiesMock([$key => $storedValue]); + $command->method('checkInput')->willReturn($mocks['userMock']); + $mocks['accountMock']->method('getUser')->willReturn($mocks['userMock']); + + $mocks['profilePropertiesMocks'][0]->expects($this->never()) + ->method('setValue'); + $this->accountManager->expects($this->never()) + ->method('updateAccount'); + + return [$command, $mocks['userMock']]; + } + + public static function dataExecuteEmail(): array { + return [ + 'Setting email should update system email address' => ['new@example.com'], + 'Deleting email should clear system email address' => [null], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteEmail')] + public function testExecuteEmail(?string $inputValue): void { + [$command, $user] = $this->prepareUserBackedProperty('email', $inputValue, 'old@example.com'); + + $user->expects($this->once()) + ->method('setSystemEMailAddress') + ->with($inputValue ?? ''); + + $this->assertEquals(Command::SUCCESS, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + + public static function dataExecuteSetDisplayName(): array { + return [ + 'Setting new display name should succeed' => ['New Name', true], + 'Setting unchanged display name should succeed' => ['Old Name', false], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteSetDisplayName')] + public function testExecuteSetDisplayName(string $inputValue, bool $changed): void { + [$command, $user] = $this->prepareUserBackedProperty('displayname', $inputValue, 'Old Name'); + + $user->expects($this->once()) + ->method('setDisplayName') + ->with($inputValue) + ->willReturn($changed); + $user->method('getDisplayName')->willReturn('Old Name'); + $this->consoleOutput->expects($this->never()) + ->method('writeln'); + + $this->assertEquals(Command::SUCCESS, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + + public static function dataExecuteDisplayNameFailure(): array { + return [ + 'Setting display name rejected by backend should fail' => ['New Name', 'Could not set display name'], + 'Deleting display name should fail' => [null, 'New display name can\'t be empty'], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteDisplayNameFailure')] + public function testExecuteDisplayNameFailure(?string $inputValue, string $expectedLine): void { + [$command, $user] = $this->prepareUserBackedProperty('displayname', $inputValue, 'Old Name'); + + $user->expects($this->once()) + ->method('setDisplayName') + ->with($inputValue ?? '') + ->willReturn(false); + $user->method('getDisplayName')->willReturn('Old Name'); + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($expectedLine); + + $this->assertEquals(Command::FAILURE, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + public static function dataExecuteGet(): array { return [ 'Get property with set value should pass' => ['configkey', 'value', null, 'value', Command::SUCCESS], From 2da8bd86ecc775f2ae99a34e945ab92af5b0471b Mon Sep 17 00:00:00 2001 From: Olav Seyfarth Date: Thu, 3 Dec 2026 12:57:24 +0100 Subject: [PATCH 2/2] fix(core): sync account and validate input in user:profile email/displayname If IUser already held the requested value, no UserChangedEvent fired and a drifted account property stayed wrong while the command reported success - the very state #61831 leaves behind. Invalid addresses reached the system email that mails are sent to, display names were compared untrimmed, a rejected account update crashed the command, and reads, --update-only and --error-if-not-exists still looked at the account property instead of the value the command now writes. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Olav Seyfarth --- core/Command/User/Profile.php | 67 ++++++---- tests/Core/Command/User/ProfileTest.php | 166 +++++++++++++++++++----- 2 files changed, 176 insertions(+), 57 deletions(-) diff --git a/core/Command/User/Profile.php b/core/Command/User/Profile.php index 738ac9bc426cc..d435b1f8b2d9d 100644 --- a/core/Command/User/Profile.php +++ b/core/Command/User/Profile.php @@ -15,6 +15,7 @@ use OCP\Accounts\PropertyDoesNotExistException; use OCP\IUser; use OCP\IUserManager; +use OCP\Mail\IEmailValidator; use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -25,6 +26,7 @@ class Profile extends Base { public function __construct( protected IUserManager $userManager, protected IAccountManager $accountManager, + protected IEmailValidator $emailValidator, ) { parent::__construct(); } @@ -180,49 +182,66 @@ private function deleteProfileProperty(OutputInterface $output, IAccount $userAc private function editProfileProperty(OutputInterface $output, IAccount $userAccount, string $key, string $value): int { $user = $userAccount->getUser(); - // Through IUser, so system values and events follow; a listener syncs the account. - if ($key === IAccountManager::PROPERTY_EMAIL) { - $user->setSystemEMailAddress($value); - return self::SUCCESS; - } - if ($key === IAccountManager::PROPERTY_DISPLAYNAME) { - if ($user->setDisplayName($value) || $user->getDisplayName() === $value) { - return self::SUCCESS; - } - if ($value === '') { - $output->writeln('New display name can\'t be empty'); - } else { - $output->writeln('Could not set display name'); + try { + // Through IUser, so system values and events follow; the account is written too, as it may have drifted. + if ($key === IAccountManager::PROPERTY_EMAIL) { + if ($value !== '' && !$this->emailValidator->isValid(trim($value))) { + $output->writeln('Invalid email address'); + return self::FAILURE; + } + $user->setSystemEMailAddress($value); + $value = $user->getSystemEMailAddress() ?? ''; + } elseif ($key === IAccountManager::PROPERTY_DISPLAYNAME) { + $value = trim($value); + if ($value === '') { + $output->writeln('New display name can\'t be empty'); + return self::FAILURE; + } + if (!$user->setDisplayName($value) && $user->getDisplayName() !== $value) { + $output->writeln('Could not set display name'); + return self::FAILURE; + } + $value = $user->getDisplayName(); } - return self::FAILURE; - } - try { $userAccount->getProperty($key)->setValue($value); + $this->accountManager->updateAccount($userAccount); } catch (PropertyDoesNotExistException $exception) { $output->writeln('' . $exception->getMessage() . ''); return self::FAILURE; + } catch (\InvalidArgumentException $exception) { + $output->writeln('Could not save: ' . $exception->getMessage() . ''); + return self::FAILURE; } - $this->accountManager->updateAccount($userAccount); return self::SUCCESS; } private function getStoredValue(IAccount $userAccount, string $key): ?string { - try { - $property = $userAccount->getProperty($key); - } catch (PropertyDoesNotExistException) { - return null; + if ($key === IAccountManager::PROPERTY_EMAIL) { + $value = $userAccount->getUser()->getSystemEMailAddress() ?? ''; + } elseif ($key === IAccountManager::PROPERTY_DISPLAYNAME) { + $value = $userAccount->getUser()->getDisplayName(); + } else { + try { + $value = $userAccount->getProperty($key)->getValue(); + } catch (PropertyDoesNotExistException) { + return null; + } } - return $property->getValue() === '' ? null : $property->getValue(); + return $value === '' ? null : $value; } private function getAllProfileProperties(IAccount $userAccount): array { $properties = []; foreach ($userAccount->getAllProperties() as $property) { - if ($property->getValue() !== '') { - $properties[$property->getName()] = $property->getValue(); + $name = $property->getName(); + $value = in_array($name, [IAccountManager::PROPERTY_EMAIL, IAccountManager::PROPERTY_DISPLAYNAME], true) + ? $this->getStoredValue($userAccount, $name) ?? '' + : $property->getValue(); + if ($value !== '') { + $properties[$name] = $value; } } diff --git a/tests/Core/Command/User/ProfileTest.php b/tests/Core/Command/User/ProfileTest.php index e0e8e69c2f6b9..edfbee3551c39 100644 --- a/tests/Core/Command/User/ProfileTest.php +++ b/tests/Core/Command/User/ProfileTest.php @@ -16,6 +16,7 @@ use OCP\IDBConnection; use OCP\IUser; use OCP\IUserManager; +use OCP\Mail\IEmailValidator; use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; @@ -26,6 +27,7 @@ class ProfileTest extends TestCase { protected IAccountManager&MockObject $accountManager; protected IUserManager&MockObject $userManager; + protected IEmailValidator&MockObject $emailValidator; protected IDBConnection&MockObject $connection; protected InputInterface&MockObject $consoleInput; protected OutputInterface&MockObject $consoleOutput; @@ -36,6 +38,7 @@ protected function setUp(): void { $this->accountManager = $this->createMock(IAccountManager::class); $this->userManager = $this->createMock(IUserManager::class); + $this->emailValidator = $this->createMock(IEmailValidator::class); $this->connection = $this->createMock(IDBConnection::class); $this->consoleInput = $this->createMock(InputInterface::class); $this->consoleOutput = $this->createMock(OutputInterface::class); @@ -43,12 +46,13 @@ protected function setUp(): void { public function getCommand(array $methods = []): Profile|MockObject { if (empty($methods)) { - return new Profile($this->userManager, $this->accountManager); + return new Profile($this->userManager, $this->accountManager, $this->emailValidator); } else { return $this->getMockBuilder(Profile::class) ->setConstructorArgs([ $this->userManager, $this->accountManager, + $this->emailValidator, ]) ->onlyMethods($methods) ->getMock(); @@ -320,10 +324,11 @@ public function testExecuteSetProfileProperty(): void { /** * Prepares a set (value given) or delete (value null) call for a key that must go through IUser. + * The account property is expected to receive $synced, or to stay untouched when $synced is null. * * @return array{0: Profile&MockObject, 1: IUser&MockObject} */ - private function prepareUserBackedProperty(string $key, ?string $inputValue, string $storedValue): array { + private function prepareUserBackedProperty(string $key, ?string $inputValue, ?string $synced, bool $errorIfNotExists = false, ?\Throwable $updateFails = null): array { $command = $this->getCommand(['checkInput']); $this->consoleInput->method('getArgument') @@ -333,54 +338,146 @@ private function prepareUserBackedProperty(string $key, ?string $inputValue, str ['value', $inputValue], ]); $this->consoleInput->method('hasParameterOption') - ->willReturnCallback(fn (string $option): bool => $inputValue === null && $option === '--delete'); - - $mocks = $this->setupProfilePropertiesMock([$key => $storedValue]); - $command->method('checkInput')->willReturn($mocks['userMock']); - $mocks['accountMock']->method('getUser')->willReturn($mocks['userMock']); - - $mocks['profilePropertiesMocks'][0]->expects($this->never()) - ->method('setValue'); - $this->accountManager->expects($this->never()) - ->method('updateAccount'); + ->willReturnCallback(fn (string $option): bool => $inputValue === null + && ($option === '--delete' || ($errorIfNotExists && $option === '--error-if-not-exists'))); + + $user = $this->createMock(IUser::class); + $property = $this->getAccountPropertyMock($key, 'drifted'); + $account = $this->createMock(IAccount::class); + $account->method('getUser')->willReturn($user); + $account->method('getProperty')->with($key)->willReturn($property); + $this->accountManager->method('getAccount')->with($user)->willReturn($account); + $command->method('checkInput')->willReturn($user); + + if ($synced === null) { + $property->expects($this->never())->method('setValue'); + $this->accountManager->expects($this->never())->method('updateAccount'); + } else { + $property->expects($this->once())->method('setValue')->with($synced); + $update = $this->accountManager->expects($this->once())->method('updateAccount')->with($account); + if ($updateFails !== null) { + $update->willThrowException($updateFails); + } + } - return [$command, $mocks['userMock']]; + return [$command, $user]; } public static function dataExecuteEmail(): array { return [ - 'Setting email should update system email address' => ['new@example.com'], - 'Deleting email should clear system email address' => [null], + 'Setting email should update system address and account' => ['New@Example.com', 'old@example.com', 'new@example.com'], + 'Setting unchanged email should still sync a drifted account' => ['old@example.com', 'old@example.com', 'old@example.com'], + 'Deleting email should clear system address and account' => [null, 'old@example.com', ''], ]; } #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteEmail')] - public function testExecuteEmail(?string $inputValue): void { - [$command, $user] = $this->prepareUserBackedProperty('email', $inputValue, 'old@example.com'); + public function testExecuteEmail(?string $inputValue, string $systemValue, string $synced): void { + [$command, $user] = $this->prepareUserBackedProperty('email', $inputValue, $synced); + $this->emailValidator->method('isValid')->willReturn(true); $user->expects($this->once()) ->method('setSystemEMailAddress') ->with($inputValue ?? ''); + $user->method('getSystemEMailAddress')->willReturnOnConsecutiveCalls($systemValue, $synced === '' ? null : $synced); $this->assertEquals(Command::SUCCESS, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); } + public function testExecuteEmailAccountUpdateFails(): void { + [$command, $user] = $this->prepareUserBackedProperty('email', 'new@example.com', 'new@example.com', false, new \InvalidArgumentException('phone')); + $this->emailValidator->method('isValid')->willReturn(true); + $user->method('getSystemEMailAddress')->willReturnOnConsecutiveCalls('old@example.com', 'new@example.com'); + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with('Could not save: phone'); + + $this->assertEquals(Command::FAILURE, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + + public function testExecuteInvalidEmail(): void { + [$command, $user] = $this->prepareUserBackedProperty('email', 'no mail', null); + $this->emailValidator->method('isValid')->with('no mail')->willReturn(false); + + $user->expects($this->never())->method('setSystemEMailAddress'); + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with('Invalid email address'); + + $this->assertEquals(Command::FAILURE, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + + public static function dataExecuteDeleteEmailErrorIfNotExists(): array { + return [ + 'System address set should delete' => ['old@example.com', Command::SUCCESS], + 'System address empty should fail' => [null, Command::FAILURE], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteDeleteEmailErrorIfNotExists')] + public function testExecuteDeleteEmailErrorIfNotExists(?string $systemValue, int $expectedReturn): void { + $success = $expectedReturn === Command::SUCCESS; + [$command, $user] = $this->prepareUserBackedProperty('email', null, $success ? '' : null, true); + + $user->method('getSystemEMailAddress')->willReturnOnConsecutiveCalls($systemValue, null); + $user->expects($success ? $this->once() : $this->never())->method('setSystemEMailAddress')->with(''); + + $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + + public static function dataExecuteGetEmail(): array { + return [ + 'Getting email should print the system address' => [null, 'new@example.com', 'new@example.com', Command::SUCCESS], + 'Getting unset email should fail' => [null, null, 'The property does not exist for user "username".', Command::FAILURE], + 'Updating unset email with update-only should fail' => ['a@example.com', null, 'The property does not exist for user "username".', Command::FAILURE], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteGetEmail')] + public function testExecuteGetEmail(?string $inputValue, ?string $systemValue, string $expectedLine, int $expectedReturn): void { + $command = $this->getCommand(['checkInput']); + $this->consoleInput->method('getArgument') + ->willReturnMap([ + ['uid', 'username'], + ['key', 'email'], + ['value', $inputValue], + ]); + $this->consoleInput->method('hasParameterOption') + ->willReturnCallback(fn (string $option): bool => $option === '--update-only'); + + $user = $this->createMock(IUser::class); + $user->method('getSystemEMailAddress')->willReturn($systemValue); + $user->expects($this->never())->method('setSystemEMailAddress'); + $account = $this->createMock(IAccount::class); + $account->method('getUser')->willReturn($user); + $account->expects($this->never())->method('getProperty'); + $this->accountManager->method('getAccount')->willReturn($account); + $command->method('checkInput')->willReturn($user); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($expectedLine); + + $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + public static function dataExecuteSetDisplayName(): array { return [ - 'Setting new display name should succeed' => ['New Name', true], - 'Setting unchanged display name should succeed' => ['Old Name', false], + 'Setting new display name should succeed' => ['New Name', true, 'New Name'], + 'Setting unchanged display name should still sync a drifted account' => ['Old Name', false, 'Old Name'], + 'Surrounding whitespace should be ignored' => [' Old Name ', false, 'Old Name'], ]; } #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteSetDisplayName')] - public function testExecuteSetDisplayName(string $inputValue, bool $changed): void { - [$command, $user] = $this->prepareUserBackedProperty('displayname', $inputValue, 'Old Name'); + public function testExecuteSetDisplayName(string $inputValue, bool $changed, string $synced): void { + [$command, $user] = $this->prepareUserBackedProperty('displayname', $inputValue, $synced); $user->expects($this->once()) ->method('setDisplayName') - ->with($inputValue) + ->with($synced) ->willReturn($changed); - $user->method('getDisplayName')->willReturn('Old Name'); + $user->method('getDisplayName')->willReturn($synced); $this->consoleOutput->expects($this->never()) ->method('writeln'); @@ -389,19 +486,19 @@ public function testExecuteSetDisplayName(string $inputValue, bool $changed): vo public static function dataExecuteDisplayNameFailure(): array { return [ - 'Setting display name rejected by backend should fail' => ['New Name', 'Could not set display name'], - 'Deleting display name should fail' => [null, 'New display name can\'t be empty'], + 'Setting display name rejected by backend should fail' => ['New Name', false, 'Could not set display name'], + 'Setting display name the backend throws on should fail' => ['New Name', true, 'Could not save: Invalid displayname'], + 'Deleting display name should fail' => [null, false, 'New display name can\'t be empty'], + 'Whitespace-only display name should fail' => [' ', false, 'New display name can\'t be empty'], ]; } #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteDisplayNameFailure')] - public function testExecuteDisplayNameFailure(?string $inputValue, string $expectedLine): void { - [$command, $user] = $this->prepareUserBackedProperty('displayname', $inputValue, 'Old Name'); + public function testExecuteDisplayNameFailure(?string $inputValue, bool $throws, string $expectedLine): void { + [$command, $user] = $this->prepareUserBackedProperty('displayname', $inputValue, null); - $user->expects($this->once()) - ->method('setDisplayName') - ->with($inputValue ?? '') - ->willReturn(false); + $setter = $user->method('setDisplayName'); + $throws ? $setter->willThrowException(new \InvalidArgumentException('Invalid displayname')) : $setter->willReturn(false); $user->method('getDisplayName')->willReturn('Old Name'); $this->consoleOutput->expects($this->once()) ->method('writeln') @@ -482,7 +579,10 @@ public function testExecuteList(): void { ['key', ''], ]); - $mocks = $this->setupProfilePropertiesMock(['address' => $profileData['address'], 'pronouns' => $profileData['pronouns']]); + $mocks = $this->setupProfilePropertiesMock(['address' => $profileData['address'], 'pronouns' => $profileData['pronouns'], 'email' => 'drifted@example.com', 'displayname' => 'Drifted']); + $mocks['accountMock']->method('getUser')->willReturn($mocks['userMock']); + $mocks['userMock']->method('getSystemEMailAddress')->willReturn(null); + $mocks['userMock']->method('getDisplayName')->willReturn('System Name'); $command->expects($this->once()) ->method('checkInput') @@ -490,7 +590,7 @@ public function testExecuteList(): void { $command->expects($this->once()) ->method('writeArrayInOutputFormat') - ->with($this->consoleInput, $this->consoleOutput, $profileData); + ->with($this->consoleInput, $this->consoleOutput, $profileData + ['displayname' => 'System Name']); $this->assertEquals(0, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); }