|
4 | 4 |
|
5 | 5 | /** |
6 | 6 | * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
7 | | - * SPDX-License-Identifier: AGPL-3.0-only |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
8 | 8 | */ |
9 | 9 |
|
10 | 10 | namespace OCA\Mail\UserMigration; |
11 | 11 |
|
12 | | -use Exception; |
13 | | -use JsonException; |
14 | | -use OCA\Mail\Db\Alias; |
15 | | -use OCA\Mail\Db\MailAccount; |
16 | | -use OCA\Mail\Service\AccountService; |
17 | | -use OCA\Mail\Service\AliasesService; |
| 12 | +use OCA\Mail\AppInfo\Application; |
| 13 | +use OCA\Mail\UserMigration\Service\AccountMigrationService; |
18 | 14 | use OCP\IL10N; |
19 | 15 | use OCP\IUser; |
20 | 16 | use OCP\Security\ICrypto; |
|
23 | 19 | use OCP\UserMigration\IMigrator; |
24 | 20 | use OCP\UserMigration\UserMigrationException; |
25 | 21 | use Symfony\Component\Console\Output\OutputInterface; |
26 | | -use function array_map; |
27 | | -use function json_decode; |
28 | | -use function json_encode; |
29 | 22 |
|
30 | 23 | class MailAccountMigrator implements IMigrator { |
31 | | - |
32 | | - public function __construct( |
33 | | - private AccountService $accountService, |
34 | | - private AliasesService $aliasesService, |
35 | | - private IL10N $l10n, |
36 | | - private ICrypto $crypto, |
37 | | - ) { |
38 | | - } |
39 | | - |
40 | | - #[\Override] |
41 | | - public function export(IUser $user, |
42 | | - IExportDestination $exportDestination, |
43 | | - OutputInterface $output, |
44 | | - ): void { |
45 | | - $accounts = $this->accountService->findByUserId($user->getUID()); |
46 | | - $index = []; |
47 | | - foreach ($accounts as $account) { |
48 | | - if ($account->getMailAccount()->getProvisioningId() !== null) { |
49 | | - // These configuration of these accounts is owned by the admins |
50 | | - $output->writeln("Skipping provisioned account {$account->getId()}"); |
51 | | - continue; |
52 | | - } |
53 | | - |
54 | | - $accountFilePath = "mail/accounts/{$account->getId()}.json"; |
55 | | - $accountData = $account->jsonSerialize(); |
56 | | - |
57 | | - if ($account->getMailAccount()->getAuthMethod() === 'password') { |
58 | | - $encryptedInboundPassword = $account->getMailAccount()->getInboundPassword(); |
59 | | - $encryptedOutboundPassword = $account->getMailAccount()->getOutboundPassword(); |
60 | | - if ($encryptedInboundPassword !== null) { |
61 | | - try { |
62 | | - $accountData['inboundPassword'] = $this->crypto->decrypt($encryptedInboundPassword); |
63 | | - } catch (Exception $e) { |
64 | | - $output->writeln("Can not decrypt inbound password of account {$account->getId()}: " . $e->getMessage()); |
65 | | - } |
66 | | - } |
67 | | - if ($encryptedOutboundPassword !== null) { |
68 | | - try { |
69 | | - $accountData['outboundPassword'] = $this->crypto->decrypt($encryptedOutboundPassword); |
70 | | - } catch (Exception $e) { |
71 | | - $output->writeln("Can not decrypt outbound password of account {$account->getId()}: " . $e->getMessage()); |
72 | | - } |
73 | | - } |
74 | | - } elseif ($account->getMailAccount()->getAuthMethod() === 'xoauth2') { |
75 | | - $encryptedRefreshToken = $account->getMailAccount()->getOauthRefreshToken(); |
76 | | - $encryptedAccessToken = $account->getMailAccount()->getOauthAccessToken(); |
77 | | - if ($encryptedRefreshToken !== null) { |
78 | | - try { |
79 | | - $accountData['oauthRefreshToken'] = $this->crypto->decrypt($encryptedRefreshToken); |
80 | | - } catch (Exception $e) { |
81 | | - $output->writeln("Can not decrypt oauth refresh token of account {$account->getId()}: " . $e->getMessage()); |
82 | | - } |
83 | | - } |
84 | | - if ($encryptedAccessToken !== null) { |
85 | | - try { |
86 | | - $accountData['oauthAccessToken'] = $this->crypto->decrypt($encryptedAccessToken); |
87 | | - } catch (Exception $e) { |
88 | | - $output->writeln("Can not decrypt oauth access token of account {$account->getId()}: " . $e->getMessage()); |
89 | | - } |
90 | | - } |
91 | | - $accountData['oauthTokenTtl'] = $account->getMailAccount()->getOauthTokenTtl(); |
92 | | - } |
93 | | - |
94 | | - unset( |
95 | | - $accountData['draftsMailboxId'], |
96 | | - $accountData['sentMailboxId'], |
97 | | - $accountData['trashMailboxId'], |
98 | | - $accountData['archiveMailboxId'], |
99 | | - $accountData['snoozeMailboxId'], |
100 | | - $accountData['junkMailboxId'], |
101 | | - ); |
102 | | - |
103 | | - $aliases = $this->aliasesService->findAll( |
104 | | - $account->getId(), |
105 | | - $account->getUserId(), // perf: this adds overhead - add dedicated method to fetch by account id only |
106 | | - ); |
107 | | - $accountData['aliases'] = array_map(function (Alias $alias) { |
108 | | - $data = $alias->jsonSerialize(); |
109 | | - return $data; |
110 | | - }, $aliases); |
111 | | - |
112 | | - $exportDestination->addFileContents($accountFilePath, json_encode($accountData)); |
113 | | - $index[$account->getId()] = $accountFilePath; |
114 | | - } |
115 | | - |
116 | | - $exportDestination->addFileContents('mail/accounts/index.json', json_encode($index)); |
117 | | - } |
118 | | - |
119 | | - #[\Override] |
120 | | - public function import(IUser $user, IImportSource $importSource, OutputInterface $output): void { |
121 | | - try { |
122 | | - $index = json_decode($importSource->getFileContents('mail/accounts/index.json'), true, flags: JSON_THROW_ON_ERROR); |
123 | | - } catch (JsonException $e) { |
124 | | - throw new UserMigrationException("Invalid index content: {$e->getMessage()}", $e->getCode(), $e); |
125 | | - } |
126 | | - foreach ($index as $accountFilePath) { |
127 | | - try { |
128 | | - $accountData = json_decode($importSource->getFileContents($accountFilePath), true, flags: JSON_THROW_ON_ERROR); |
129 | | - } catch (JsonException $e) { |
130 | | - throw new UserMigrationException("Invalid account content: {$e->getMessage()}", $e->getCode(), $e); |
131 | | - } |
132 | | - |
133 | | - // Wipe the old ID(s) to prevent overwrites |
134 | | - unset( |
135 | | - $accountData['id'], |
136 | | - $accountData['accountId'], |
137 | | - ); |
138 | | - |
139 | | - $newAccount = new MailAccount($accountData); |
140 | | - |
141 | | - // Change UID to new owner |
142 | | - $newAccount->setUserId($user->getUID()); |
143 | | - // Map the rest of the properties that are not mapped via the constructor |
144 | | - $newAccount->setName($accountData['name']); |
145 | | - $newAccount->setAuthMethod($accountData['authMethod']); |
146 | | - $newAccount->setEditorMode($accountData['editorMode'] ?? 'plain'); |
147 | | - $newAccount->setSearchBody($accountData['searchBody'] ?? false); |
148 | | - $newAccount->setClassificationEnabled($accountData['classificationEnabled'] ?? false); |
149 | | - $newAccount->setSignatureAboveQuote($accountData['signatureAboveQuote'] ?? false); |
150 | | - $newAccount->setPersonalNamespace($accountData['personalNamespace'] ?? null); |
151 | | - if (isset($accountData['inboundPassword'])) { |
152 | | - $newAccount->setInboundPassword($this->crypto->encrypt($accountData['inboundPassword'])); |
153 | | - } |
154 | | - if (isset($accountData['outboundPassword'])) { |
155 | | - $newAccount->setOutboundPassword($this->crypto->encrypt($accountData['outboundPassword'])); |
156 | | - } |
157 | | - if (isset($accountData['oauthRefreshToken'])) { |
158 | | - $newAccount->setOauthRefreshToken($this->crypto->encrypt($accountData['oauthRefreshToken'])); |
159 | | - } |
160 | | - if (isset($accountData['oauthAccessToken'])) { |
161 | | - $newAccount->setOauthAccessToken($this->crypto->encrypt($accountData['oauthAccessToken'])); |
162 | | - } |
163 | | - $newAccount->setOauthTokenTtl($accountData['oauthTokenTtl'] ?? null); |
164 | | - |
165 | | - $mailAccount = $this->accountService->save( |
166 | | - $newAccount |
167 | | - ); |
168 | | - |
169 | | - // Import aliases |
170 | | - foreach ($accountData['aliases'] as $alias) { |
171 | | - $this->aliasesService->create( |
172 | | - $user->getUID(), |
173 | | - $mailAccount->getId(), |
174 | | - $alias['alias'], |
175 | | - $alias['name'], |
176 | | - ); |
177 | | - } |
178 | | - } |
179 | | - } |
180 | | - |
181 | | - #[\Override] |
182 | | - public function getId(): string { |
183 | | - return 'mail_account'; |
184 | | - } |
185 | | - |
186 | | - #[\Override] |
187 | | - public function getDisplayName(): string { |
188 | | - return $this->l10n->t('Mail'); |
189 | | - } |
190 | | - |
191 | | - #[\Override] |
192 | | - public function getDescription(): string { |
193 | | - return $this->l10n->t('Mail account parameters, aliases and preferences'); |
194 | | - } |
195 | | - |
196 | | - #[\Override] |
197 | | - public function getVersion(): int { |
198 | | - return 01_00_00; |
199 | | - } |
200 | | - |
201 | | - #[\Override] |
202 | | - public function canImport(IImportSource $importSource): bool { |
203 | | - try { |
204 | | - return $importSource->getMigratorVersion($this->getId()) <= $this->getVersion(); |
205 | | - } catch (UserMigrationException) { |
206 | | - return false; |
207 | | - } |
208 | | - } |
| 24 | + public const EXPORT_ROOT = Application::APP_ID; |
| 25 | + public const FILENAME_PLACEHOLDER = '{filename}'; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + private readonly IL10N $l10n, |
| 29 | + private readonly ICrypto $crypto, |
| 30 | + private readonly AccountMigrationService $accountMigrationService, |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + #[\Override] |
| 35 | + public function export(IUser $user, |
| 36 | + IExportDestination $exportDestination, |
| 37 | + OutputInterface $output, |
| 38 | + ): void { |
| 39 | + $output->writeln($this->l10n->t("Exporting mail accounts for user {$user->getUID()}"), OutputInterface::VERBOSITY_VERBOSE); |
| 40 | + } |
| 41 | + |
| 42 | + #[\Override] |
| 43 | + public function import(IUser $user, IImportSource $importSource, OutputInterface $output): void { |
| 44 | + $output->writeln($this->l10n->t("Importing mail accounts for user {$user->getUID()}"), OutputInterface::VERBOSITY_VERBOSE); |
| 45 | + |
| 46 | + $this->accountMigrationService->scheduleBackgroundJobs($user, $output); |
| 47 | + } |
| 48 | + |
| 49 | + #[\Override] |
| 50 | + public function getId(): string { |
| 51 | + return 'mail_account'; |
| 52 | + } |
| 53 | + |
| 54 | + #[\Override] |
| 55 | + public function getDisplayName(): string { |
| 56 | + return $this->l10n->t('Mail'); |
| 57 | + } |
| 58 | + |
| 59 | + #[\Override] |
| 60 | + public function getDescription(): string { |
| 61 | + return $this->l10n->t('Mail account parameters, aliases and preferences'); |
| 62 | + } |
| 63 | + |
| 64 | + #[\Override] |
| 65 | + public function getVersion(): int { |
| 66 | + return 02_00_00; |
| 67 | + } |
| 68 | + |
| 69 | + #[\Override] |
| 70 | + public function canImport(IImportSource $importSource): bool { |
| 71 | + try { |
| 72 | + return $importSource->getMigratorVersion($this->getId()) <= $this->getVersion(); |
| 73 | + } catch (UserMigrationException) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
209 | 77 |
|
210 | 78 | } |
0 commit comments