From 1c6baa0e2bbfa8aea2420c50e7909fe8b7613988 Mon Sep 17 00:00:00 2001 From: David Dreschner Date: Fri, 24 Apr 2026 16:47:15 +0200 Subject: [PATCH 1/2] feat(UserMigration): Overwork migration to include all settings (internal addresses) Signed-off-by: David Dreschner --- lib/UserMigration/MailAccountMigrator.php | 5 + .../InternalAddressesMigrationService.php | 127 ++++++++++++++++++ .../InternalAddressesMigrationServiceTest.php | 106 +++++++++++++++ 3 files changed, 238 insertions(+) create mode 100644 lib/UserMigration/Service/InternalAddressesMigrationService.php create mode 100644 tests/Unit/UserMigration/Service/InternalAddressesMigrationServiceTest.php diff --git a/lib/UserMigration/MailAccountMigrator.php b/lib/UserMigration/MailAccountMigrator.php index ce90b375d9..f4b3866373 100644 --- a/lib/UserMigration/MailAccountMigrator.php +++ b/lib/UserMigration/MailAccountMigrator.php @@ -14,6 +14,7 @@ use OCA\Mail\Exception\ServiceException; 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; @@ -37,6 +38,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, $output->writeln($this->l10n->t("Exporting mail accounts for user {$user->getUID()}"), OutputInterface::VERBOSITY_VERBOSE); $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); @@ -65,6 +68,7 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface $this->deleteExistingData($user, $output); $this->appConfigMigrationService->importAppConfiguration($user, $importSource, $output); + $this->internalAddressesMigrationService->importInternalAddresses($user, $importSource, $output); $this->trustedSendersMigrationService->importTrustedSenders($user, $importSource, $output); $this->textBlocksMigrationService->importTextBlocks($user, $importSource, $output); $newTagIds = $this->tagsMigrationService->importTags($user, $importSource, $output); @@ -87,6 +91,7 @@ private function deleteExistingData(IUser $user, OutputInterface $output): void $output->writeln($this->l10n->t("Deleting existing mail data for user {$user->getUID()}"), OutputInterface::VERBOSITY_VERBOSE); $this->appConfigMigrationService->deleteAppConfiguration($user, $output); + $this->internalAddressesMigrationService->removeInternalAddresses($user, $output); $this->trustedSendersMigrationService->removeAllTrustedSenders($user, $output); $this->textBlocksMigrationService->deleteAllTextBlocks($user, $output); $this->tagsMigrationService->deleteAllTags($user, $output); diff --git a/lib/UserMigration/Service/InternalAddressesMigrationService.php b/lib/UserMigration/Service/InternalAddressesMigrationService.php new file mode 100644 index 0000000000..cfc5cf1431 --- /dev/null +++ b/lib/UserMigration/Service/InternalAddressesMigrationService.php @@ -0,0 +1,127 @@ +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. + * + * @throws UserMigrationException + */ + 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 + ); + + $internalAddresses = json_decode($importSource->getFileContents(self::INTERNAL_ADDRESSES_FILE), true); + $this->validateInternalAddresses($internalAddresses); + + foreach ($internalAddresses as $internalAddress) { + $this->internalAddressService->add($user->getUID(), $internalAddress['address'], $internalAddress['type']); + } + } + + public function removeInternalAddresses(IUser $user, OutputInterface $output): void { + $output->writeln( + $this->l10n->t('Deleting all internal addresses for user %s', [$user->getUID()]), + OutputInterface::VERBOSITY_VERBOSE + ); + + $this->internalAddressService->removeInternalAddresses($user->getUID()); + } + + /** + * 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..71ec0d1222 --- /dev/null +++ b/tests/Unit/UserMigration/Service/InternalAddressesMigrationServiceTest.php @@ -0,0 +1,106 @@ +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)); + + $this->serviceMock->getParameter('internalAddressService')->expects(self::exactly(2))->method('add')->with(self::USER_ID, self::callback(function ($email) use ($trustedIndividual, $trustedDomain) { + return $email === $trustedIndividual->getAddress() || $email === $trustedDomain->getAddress(); + }), self::callback(function ($type) use ($trustedIndividual, $trustedDomain) { + return $type === $trustedIndividual->getType() || $type === $trustedDomain->getType(); + })); + + $this->migrationService->importInternalAddresses($this->user, $this->importSource, $this->output); + } + + public function testImportNoneInternalAddress(): void { + $trustedSendersList = []; + $this->importSource->expects(self::once())->method('getFileContents')->with(InternalAddressesMigrationService::INTERNAL_ADDRESSES_FILE)->willReturn(json_encode($trustedSendersList)); + $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; + } +} From ee936960049f9d48cb6c93d536947eddfade35a8 Mon Sep 17 00:00:00 2001 From: David Dreschner Date: Fri, 24 Apr 2026 17:59:23 +0200 Subject: [PATCH 2/2] feat(UserMigration): Overwork migration to include all settings (internal addresses) Signed-off-by: David Dreschner --- lib/AppInfo/Application.php | 3 +++ lib/Contracts/IInternalAddressService.php | 2 ++ lib/Db/InternalAddressMapper.php | 11 +++++++++++ lib/Service/InternalAddressService.php | 5 +++++ 4 files changed, 21 insertions(+) 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/Contracts/IInternalAddressService.php b/lib/Contracts/IInternalAddressService.php index 9dc69747cb..d1ffb7b33f 100644 --- a/lib/Contracts/IInternalAddressService.php +++ b/lib/Contracts/IInternalAddressService.php @@ -16,6 +16,8 @@ public function isInternal(string $uid, string $address): bool; public function add(string $uid, string $address, string $type, ?bool $trust = true); + public function removeInternalAddresses(string $uid): void; + /** * @param string $uid * @return InternalAddress[] diff --git a/lib/Db/InternalAddressMapper.php b/lib/Db/InternalAddressMapper.php index eda65fc77c..b3bb3b60cc 100644 --- a/lib/Db/InternalAddressMapper.php +++ b/lib/Db/InternalAddressMapper.php @@ -11,6 +11,7 @@ use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\QBMapper; +use OCP\DB\Exception; use OCP\IDBConnection; /** @@ -100,4 +101,14 @@ public function find(string $uid, string $address): ?InternalAddress { return null; } } + + /** + * @throws Exception + */ + public function removeAll(string $uid) : void { + $qb = $this->db->getQueryBuilder(); + $delete = $qb->delete($this->getTableName()) + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($uid))); + $delete->executeStatement(); + } } diff --git a/lib/Service/InternalAddressService.php b/lib/Service/InternalAddressService.php index 34ace7a608..5289b3ecc0 100644 --- a/lib/Service/InternalAddressService.php +++ b/lib/Service/InternalAddressService.php @@ -52,6 +52,11 @@ public function add(string $uid, string $address, string $type, ?bool $trust = t return null; } + #[\Override] + public function removeInternalAddresses(string $uid): void { + $this->mapper->removeAll($uid); + } + #[\Override] public function getInternalAddresses(string $uid): array { return $this->mapper->findAll($uid);