-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathInternalAddressesMigrationService.php
More file actions
127 lines (106 loc) · 4 KB
/
InternalAddressesMigrationService.php
File metadata and controls
127 lines (106 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\UserMigration\Service;
use JsonException;
use OCA\Mail\Contracts\IInternalAddressService;
use OCA\Mail\UserMigration\MailAccountMigrator;
use OCP\IL10N;
use OCP\IUser;
use OCP\UserMigration\IExportDestination;
use OCP\UserMigration\IImportSource;
use OCP\UserMigration\UserMigrationException;
use Symfony\Component\Console\Output\OutputInterface;
class InternalAddressesMigrationService {
public const INTERNAL_ADDRESSES_FILE = MailAccountMigrator::EXPORT_ROOT . '/internal_addresses.json';
public function __construct(
private readonly IInternalAddressService $internalAddressService,
private readonly IL10N $l10n,
) {
}
/**
* Export all addresses the user defined as internal ones
* on export.
*
* @param IUser $user
* @param IExportDestination $exportDestination
* @param OutputInterface $output
* @throws UserMigrationException
*/
public function exportInternalAddresses(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
$output->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');
}
}
}
}