diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 71b50051c4..c6a614d9b8 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -16,6 +16,7 @@ use OCA\Mail\Contracts\IAvatarService; use OCA\Mail\Contracts\IDkimService; use OCA\Mail\Contracts\IDkimValidator; +use OCA\Mail\Contracts\IInternalAddressService; use OCA\Mail\Contracts\IMailManager; use OCA\Mail\Contracts\IMailSearch; use OCA\Mail\Contracts\IMailTransmission; @@ -62,6 +63,7 @@ use OCA\Mail\Service\AvatarService; use OCA\Mail\Service\DkimService; use OCA\Mail\Service\DkimValidator; +use OCA\Mail\Service\InternalAddressService; use OCA\Mail\Service\MailManager; use OCA\Mail\Service\MailTransmission; use OCA\Mail\Service\Search\MailSearch; @@ -123,6 +125,7 @@ public function register(IRegistrationContext $context): void { $context->registerServiceAlias(IMailManager::class, MailManager::class); $context->registerServiceAlias(IMailSearch::class, MailSearch::class); $context->registerServiceAlias(IMailTransmission::class, MailTransmission::class); + $context->registerServiceAlias(IInternalAddressService::class, InternalAddressService::class); $context->registerServiceAlias(ITrustedSenderService::class, TrustedSenderService::class); $context->registerServiceAlias(IUserPreferences::class, UserPreferenceService::class); $context->registerServiceAlias(IDkimService::class, DkimService::class); diff --git a/lib/UserMigration/MailAccountMigrator.php b/lib/UserMigration/MailAccountMigrator.php index 418acacf4c..5bf2ef7eaa 100644 --- a/lib/UserMigration/MailAccountMigrator.php +++ b/lib/UserMigration/MailAccountMigrator.php @@ -12,6 +12,7 @@ use OCA\Mail\AppInfo\Application; use OCA\Mail\UserMigration\Service\AccountMigrationService; use OCA\Mail\UserMigration\Service\AppConfigMigrationService; +use OCA\Mail\UserMigration\Service\InternalAddressesMigrationService; use OCA\Mail\UserMigration\Service\SMIMEMigrationService; use OCA\Mail\UserMigration\Service\TagsMigrationService; use OCA\Mail\UserMigration\Service\TextBlocksMigrationService; @@ -34,6 +35,7 @@ public function __construct( private readonly ICrypto $crypto, private readonly AccountMigrationService $accountMigrationService, private readonly AppConfigMigrationService $appConfigMigrationService, + private readonly InternalAddressesMigrationService $internalAddressesMigrationService, private readonly TrustedSendersMigrationService $trustedSendersMigrationService, private readonly TextBlocksMigrationService $textBlocksMigrationService, private readonly TagsMigrationService $tagsMigrationService, @@ -52,6 +54,7 @@ public function export(IUser $user, ); $this->appConfigMigrationService->exportAppConfiguration($user, $exportDestination, $output); + $this->internalAddressesMigrationService->exportInternalAddresses($user, $exportDestination, $output); $this->trustedSendersMigrationService->exportTrustedSenders($user, $exportDestination, $output); $this->textBlocksMigrationService->exportTextBlocks($user, $exportDestination, $output); $this->tagsMigrationService->exportTags($user, $exportDestination, $output); @@ -69,6 +72,7 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface ); $this->appConfigMigrationService->importAppConfiguration($user, $importSource, $output); + $this->internalAddressesMigrationService->importInternalAddresses($user, $importSource, $output); $this->trustedSendersMigrationService->importTrustedSenders($user, $importSource, $output); $this->textBlocksMigrationService->importTextBlocks($user, $importSource, $output); $migratedTags = $this->tagsMigrationService->importTags($user, $importSource, $output); diff --git a/lib/UserMigration/Service/InternalAddressesMigrationService.php b/lib/UserMigration/Service/InternalAddressesMigrationService.php new file mode 100644 index 0000000000..d3c5f05911 --- /dev/null +++ b/lib/UserMigration/Service/InternalAddressesMigrationService.php @@ -0,0 +1,140 @@ +writeln( + $this->l10n->t('Exporting internal addresses for user %s', [$user->getUID()]), + OutputInterface::VERBOSITY_VERBOSE + ); + + $internalAddresses = $this->internalAddressService->getInternalAddresses($user->getUID()); + + try { + $exportDestination->addFileContents(self::INTERNAL_ADDRESSES_FILE, + json_encode($internalAddresses, JSON_THROW_ON_ERROR)); + } catch (JsonException|UserMigrationException $exception) { + throw new UserMigrationException( + "Failed to export internal addresses for user {$user->getUID()}", + previous: $exception + ); + } + } + + /** + * Import all addresses the user defined as internal ones. + */ + public function importInternalAddresses(IUser $user, IImportSource $importSource, OutputInterface $output): void { + $output->writeln( + $this->l10n->t('Importing internal addresses for user %s', [$user->getUID()]), + OutputInterface::VERBOSITY_VERBOSE + ); + + try { + $internalAddressesFileContent = $importSource->getFileContents(self::INTERNAL_ADDRESSES_FILE); + } catch (UserMigrationException) { + $output->writeln( + $this->l10n->t('Internal addresses for user %s not found. Continue...', [$user->getUID()]), + OutputInterface::VERBOSITY_VERBOSE + ); + + return; + } + + try { + $internalAddresses = json_decode($internalAddressesFileContent, true, flags: JSON_THROW_ON_ERROR); + $this->validateInternalAddresses($internalAddresses); + } catch (JsonException|UserMigrationException) { + $output->writeln( + $this->l10n->t('Internal addresses configuration for user %s is invalid and will be skipped. Continue...', + [$user->getUID()]), + OutputInterface::VERBOSITY_VERBOSE + ); + + return; + } + + foreach ($internalAddresses as $internalAddress) { + $this->internalAddressService->add($user->getUID(), $internalAddress['address'], $internalAddress['type']); + } + } + + /** + * Validate the parsed internal addresses to ensure they + * have the expected structure and types. + * + * @throws UserMigrationException + */ + private function validateInternalAddresses(mixed $internalAddresses): void { + $internalAddressesArrayIsValid = is_array($internalAddresses) && array_is_list($internalAddresses); + if (!$internalAddressesArrayIsValid) { + throw new UserMigrationException('Invalid internal addresses export structure'); + } + + foreach ($internalAddresses as $internalAddress) { + $internalAddressArrayIsValid = is_array($internalAddress); + + $idIsValid = $internalAddressArrayIsValid + && array_key_exists('id', $internalAddress) + && is_int($internalAddress['id']); + + $addressIsValid = $internalAddressArrayIsValid + && array_key_exists('address', $internalAddress) + && is_string($internalAddress['address']); + + $uidIsValid = $internalAddressArrayIsValid + && array_key_exists('uid', $internalAddress) + && is_string($internalAddress['uid']); + + $typeIsValid = $internalAddressArrayIsValid + && array_key_exists('type', $internalAddress) + && is_string($internalAddress['type']); + + if ( + !$idIsValid + || !$addressIsValid + || !$uidIsValid + || !$typeIsValid + ) { + throw new UserMigrationException('Invalid internal address entry'); + } + } + } +} diff --git a/tests/Unit/UserMigration/Service/InternalAddressesMigrationServiceTest.php b/tests/Unit/UserMigration/Service/InternalAddressesMigrationServiceTest.php new file mode 100644 index 0000000000..186c762e5f --- /dev/null +++ b/tests/Unit/UserMigration/Service/InternalAddressesMigrationServiceTest.php @@ -0,0 +1,152 @@ +serviceMock = $this->createServiceMock(InternalAddressesMigrationService::class); + $this->migrationService = $this->serviceMock->getService(); + + $this->user = $this->createMock(IUser::class); + $this->user->method('getUID')->willReturn(self::USER_ID); + + $this->output = $this->createMock(OutputInterface::class); + $this->exportDestination = $this->createMock(IExportDestination::class); + $this->importSource = $this->createMock(IImportSource::class); + } + + public function testExportsMultipleInternalAddresses(): void { + $trustedSendersList = [$this->getTrustedIndividual(), $this->getTrustedDomain()]; + $this->exportDestination->expects(self::once()) + ->method('addFileContents') + ->with(InternalAddressesMigrationService::INTERNAL_ADDRESSES_FILE, json_encode($trustedSendersList)); + + $this->serviceMock->getParameter('internalAddressService') + ->method('getInternalAddresses') + ->with(self::USER_ID) + ->willReturn($trustedSendersList); + + $this->migrationService->exportInternalAddresses($this->user, $this->exportDestination, $this->output); + } + + public function testExportsNoneInternalAddress(): void { + $trustedSendersList = []; + $this->exportDestination->expects(self::once()) + ->method('addFileContents') + ->with(InternalAddressesMigrationService::INTERNAL_ADDRESSES_FILE, json_encode($trustedSendersList)); + + $this->serviceMock->getParameter('internalAddressService') + ->method('getInternalAddresses') + ->with(self::USER_ID) + ->willReturn($trustedSendersList); + + $this->migrationService->exportInternalAddresses($this->user, $this->exportDestination, $this->output); + } + + public function testImportMultipleInternalAddresses(): void { + $trustedIndividual = $this->getTrustedIndividual(); + $trustedDomain = $this->getTrustedDomain(); + $trustedSendersList = [$trustedIndividual, $trustedDomain]; + $this->importSource->expects(self::once()) + ->method('getFileContents') + ->with(InternalAddressesMigrationService::INTERNAL_ADDRESSES_FILE) + ->willReturn(json_encode($trustedSendersList)); + + $callCount = 0; + $expectedAddresses = [$trustedIndividual, $trustedDomain]; + $this->serviceMock + ->getParameter('internalAddressService')->expects(self::exactly(2))->method('add') + ->willReturnCallback(function (string $uid, string $address, string $type) use ( + &$callCount, + $expectedAddresses + ): void { + $expected = $expectedAddresses[$callCount]; + self::assertSame(self::USER_ID, $uid); + self::assertSame($expected->getAddress(), $address); + self::assertSame($expected->getType(), $type); + $callCount++; + }); + + $this->migrationService->importInternalAddresses($this->user, $this->importSource, $this->output); + } + + public static function provideFileContentsWithNoInternalAddressesImported(): array { + return [ + 'empty list' => [json_encode([])], + 'invalid JSON' => ['this is not valid json {{{'], + 'JSON object instead of list' => [json_encode(['unexpected' => 'object'])], + ]; + } + + /** + * @dataProvider provideFileContentsWithNoInternalAddressesImported + */ + public function testImportEmptyOrInvalidInternalAddresses(string $fileContents): void { + $this->importSource + ->expects(self::once())->method('getFileContents') + ->with(InternalAddressesMigrationService::INTERNAL_ADDRESSES_FILE) + ->willReturn($fileContents); + $this->serviceMock->getParameter('internalAddressService')->expects(self::never())->method('add'); + $this->migrationService->importInternalAddresses($this->user, $this->importSource, $this->output); + } + + public function testImportNoFileIsBeingIgnored(): void { + $this->importSource + ->expects(self::once()) + ->method('getFileContents') + ->with(InternalAddressesMigrationService::INTERNAL_ADDRESSES_FILE) + ->willThrowException(new UserMigrationException()); + $this->serviceMock->getParameter('internalAddressService')->expects(self::never())->method('add'); + + $this->migrationService->importInternalAddresses($this->user, $this->importSource, $this->output); + } + + private function getTrustedIndividual(): InternalAddress { + $individualSender = new InternalAddress; + + $individualSender->setId(1); + $individualSender->setUserId(self::USER_ID); + $individualSender->setAddress('max@mustermann.com'); + $individualSender->setType('individual'); + + return $individualSender; + } + + private function getTrustedDomain(): InternalAddress { + $domainSender = new InternalAddress(); + + $domainSender->setId(2); + $domainSender->setUserId(self::USER_ID); + $domainSender->setAddress('nextcloud.com'); + $domainSender->setType('domain'); + + return $domainSender; + } +}