-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathExport.php
More file actions
194 lines (175 loc) · 5.51 KB
/
Export.php
File metadata and controls
194 lines (175 loc) · 5.51 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\UserMigration\Command;
use OC\Core\Command\Base;
use OCA\UserMigration\AppInfo\Application;
use OCA\UserMigration\ExportDestination;
use OCA\UserMigration\Service\UserMigrationService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use OCP\UserMigration\IMigrator;
use Symfony\Component\Console\Formatter\WrappableOutputFormatterInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class Export extends Base {
public function __construct(
private IUserManager $userManager,
private UserMigrationService $migrationService,
private IConfig $config,
private ITimeFactory $timeFactory,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('user:export')
->setDescription('Export a user.')
->addOption(
'list',
'l',
InputOption::VALUE_OPTIONAL,
'List the available data types, pass <comment>--list=full</comment> to show more information',
false,
)
->addOption(
'output',
null,
InputOption::VALUE_OPTIONAL,
'Output format (plain, json or json_pretty, default is plain)',
$this->defaultOutputFormat,
)
->addOption(
'types',
't',
InputOption::VALUE_REQUIRED,
'Comma-separated list of data type ids, pass <comment>--types=none</comment> to only export base user data',
false,
)
->addOption(
'disable',
mode: InputOption::VALUE_NONE,
description: 'Disable the user before starting the export.',
)
->addArgument(
'user',
InputArgument::REQUIRED,
'user to export',
)
->addArgument(
'folder',
InputArgument::OPTIONAL,
'local folder to export into',
getcwd()
);
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$io = new SymfonyStyle($input, $output);
/** @var WrappableOutputFormatterInterface $formatter */
$formatter = $io->getFormatter();
$migrators = $this->migrationService->getMigrators();
$list = $input->getOption('list');
$outputOption = $input->getOption('output');
if ($list !== false) {
switch (true) {
case $list === null:
if ($outputOption !== static::OUTPUT_FORMAT_PLAIN) {
$this->writeArrayInOutputFormat($input, $io, array_map(fn (IMigrator $migrator) => $migrator->getId(), $migrators), '');
return 0;
}
$io->writeln(array_map(fn (IMigrator $migrator) => $migrator->getId(), $migrators));
return 0;
case $list === 'full':
if ($outputOption !== static::OUTPUT_FORMAT_PLAIN) {
$migratorMap = [];
foreach ($migrators as $migrator) {
$migratorMap[$migrator->getId()] = [
'name' => $migrator->getDisplayName(),
'description' => $migrator->getDescription(),
];
}
$this->writeArrayInOutputFormat($input, $io, $migratorMap, '');
return 0;
}
$io->table(
['Name', 'Id', 'Description'],
array_map(
fn (IMigrator $migrator) => [
$migrator->getDisplayName(),
$migrator->getId(),
'<comment>' . $formatter->formatAndWrap($migrator->getDescription(), 80) . '</comment>',
],
$migrators,
)
);
return 0;
default:
$io->error("Invalid list argument: \"$list\"");
return 1;
}
}
$selectedMigrators = null;
$types = $input->getOption('types');
if ($types !== false) {
$types = explode(',', $types);
if (count($types) === 1 && reset($types) === 'none') {
$selectedMigrators = [];
} else {
foreach ($types as $id) {
if (!in_array($id, array_map(fn (IMigrator $migrator) => $migrator->getId(), $migrators), true)) {
$io->error("Invalid type: \"$id\"");
return 1;
}
}
$selectedMigrators = $types;
}
}
$uid = $input->getArgument('user');
$user = $this->userManager->get($uid);
if (!$user instanceof IUser) {
$io->error("Unknown user <$uid>");
return 1;
}
$folder = $input->getArgument('folder');
try {
if (!is_writable($folder)) {
$io->error('The target folder (' . $folder . ') must exist and be writable by the web server user');
return 1;
}
$folder = realpath($folder);
$exportName = $user->getUID() . '_' . date('Y-m-d_H-i-s');
$partSuffix = '.part';
$exportPath = "$folder/$exportName.zip$partSuffix";
if ($input->getOption('disable')) {
$user->setEnabled(false);
$io->writeln('User ' . $user->getUID() . ' was disabled');
}
$resource = fopen($exportPath, 'w');
$exportDestination = new ExportDestination($resource, $exportPath);
$this->migrationService->export($exportDestination, $user, $selectedMigrators, $io);
$path = $exportDestination->getPath();
$finalPath = substr($path, 0, -mb_strlen($partSuffix));
if (rename($path, $finalPath) === false) {
throw new \Exception('Failed to rename ' . basename($path) . ' to ' . basename($finalPath));
}
$this->config->setUserValue($user->getUID(), Application::APP_ID, 'lastExport', (string)$this->timeFactory->getTime());
$io->writeln("Export saved in $finalPath");
} catch (\Exception $e) {
if ($io->isDebug()) {
$io->error("$e");
} else {
$io->error($e->getMessage());
}
return $e->getCode() !== 0 ? (int)$e->getCode() : 1;
}
return 0;
}
}