-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUserMigrationService.php
More file actions
661 lines (592 loc) · 19.8 KB
/
UserMigrationService.php
File metadata and controls
661 lines (592 loc) · 19.8 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\UserMigration\Service;
use OC\AppFramework\Bootstrap\Coordinator;
use OCA\UserMigration\BackgroundJob\UserExportJob;
use OCA\UserMigration\BackgroundJob\UserImportJob;
use OCA\UserMigration\Db\UserExport;
use OCA\UserMigration\Db\UserExportMapper;
use OCA\UserMigration\Db\UserImport;
use OCA\UserMigration\Db\UserImportMapper;
use OCA\UserMigration\ExportDestination;
use OCA\UserMigration\NotExportableException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\BackgroundJob\IJobList;
use OCP\Cache\CappedMemoryCache;
use OCP\Config\Exceptions\UnknownKeyException;
use OCP\Config\IUserConfig;
use OCP\Config\ValueType;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Security\ISecureRandom;
use OCP\UserMigration\IExportDestination;
use OCP\UserMigration\IImportSource;
use OCP\UserMigration\IMigrator;
use OCP\UserMigration\ISizeEstimationMigrator;
use OCP\UserMigration\TMigratorBasicVersionHandling;
use OCP\UserMigration\UserMigrationException;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
class UserMigrationService {
use TMigratorBasicVersionHandling;
protected CappedMemoryCache $internalCache;
protected const ENTITY_JOB_MAP = [
UserExport::class => UserExportJob::class,
UserImport::class => UserImportJob::class,
];
// Allow use of the private Coordinator class here to get and run registered migrators
public function __construct(
private IRootFolder $rootFolder,
private IConfig $config,
private IUserManager $userManager,
private ContainerInterface $container,
private Coordinator $coordinator,
private UserExportMapper $exportMapper,
private UserImportMapper $importMapper,
private IJobList $jobList,
private IAppConfig $appConfig,
private ISecureRandom $secureRandom,
) {
$this->internalCache = new CappedMemoryCache();
$this->mandatory = true;
}
/**
* @param ?string[] $filteredMigratorList If not null, only these migrators will run. If empty only the main account data will be exported.
*
* @return int|float Estimated size in KiB
*
* @throws UserMigrationException
*/
public function estimateExportSize(IUser $user, ?array $filteredMigratorList = null): int|float {
// 1MiB for base user data
$size = 1024;
foreach ($this->getMigrators() as $migrator) {
if ($filteredMigratorList !== null && !in_array($migrator->getId(), $filteredMigratorList)) {
continue;
}
$cacheKey = $user->getUID() . '::' . $migrator->getId();
if ($this->internalCache->hasKey($cacheKey)) {
/** @var int|float $size */
$size += $this->internalCache->get($cacheKey);
continue;
}
if ($migrator instanceof ISizeEstimationMigrator) {
try {
$migratorSize = $migrator->getEstimatedExportSize($user);
} catch (Throwable $e) {
throw new UserMigrationException('Could not estimate export size for ' . $migrator->getDisplayName(), 0, $e);
}
$this->internalCache->set($cacheKey, $migratorSize);
/** @var int|float $size */
$size += $migratorSize;
}
}
return $size;
}
/**
* @param ?string[] $filteredMigratorList If not null, only these migrators will run. If empty only the main account data will be exported.
*
* @throws NotExportableException
*/
public function checkExportability(IUser $user, ?array $filteredMigratorList = null): void {
try {
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$freeSpace = ceil($userFolder->getFreeSpace() / 1024);
} catch (Throwable $e) {
throw new NotExportableException('Error calculating amount of free storage space available');
}
try {
$exportFile = $userFolder->get(ExportDestination::EXPORT_FILENAME);
if (!($exportFile instanceof File)) {
throw new \InvalidArgumentException('User export is not a file');
}
// Add previous export file size to free space as it will be overwritten if existing
$freeSpace += $exportFile->getSize() / 1024;
} catch (NotFoundException $e) {
// No size addition needed if export file doesn't exist
}
try {
$exportSize = $this->estimateExportSize($user, $filteredMigratorList);
} catch (UserMigrationException $e) {
throw new NotExportableException('Error estimating export size');
}
$freeSpaceAfterExport = ($freeSpace - $exportSize) / 1024;
if ($freeSpaceAfterExport < 0) {
throw new NotExportableException('Insufficient storage space available, please free up ' . abs($freeSpaceAfterExport) . ' MiB or more to be able to export your data without running out of space');
}
}
/**
* @param ?string[] $filteredMigratorList If not null, only these migrators will run. If empty only the main account data will be exported.
*
* @throws UserMigrationException
*/
public function export(IExportDestination $exportDestination, IUser $user, ?array $filteredMigratorList = null, ?OutputInterface $output = null): void {
$output = $output ?? new NullOutput();
$uid = $user->getUID();
$this->exportUserInformation(
$user,
$exportDestination,
$output
);
$this->exportAppsSettings(
$uid,
$exportDestination,
$output
);
$this->exportVersions(
$uid,
$exportDestination,
$output
);
// Run exports of registered migrators
$migratorVersions = [
$this->getId() => $this->getVersion(),
];
foreach ($this->getMigrators() as $migrator) {
if ($filteredMigratorList !== null && !in_array($migrator->getId(), $filteredMigratorList)) {
$output->writeln('Skip non-selected migrator: ' . $migrator->getId(), OutputInterface::VERBOSITY_VERBOSE);
continue;
}
$migrator->export($user, $exportDestination, $output);
$migratorVersions[$migrator->getId()] = $migrator->getVersion();
}
try {
$exportDestination->setMigratorVersions($migratorVersions);
} catch (Throwable $e) {
throw new UserMigrationException('Could not export user information.', 0, $e);
}
$exportDestination->close();
}
public function import(IImportSource $importSource, ?IUser $user = null, ?OutputInterface $output = null): IUser {
$output = $output ?? new NullOutput();
try {
if (!$this->canImport($importSource)) {
throw new UserMigrationException('Version ' . ($importSource->getMigratorVersion($this->getId()) ?? 'null') . ' for main class ' . static::class . ' is not compatible');
}
// Check versions
foreach ($this->getMigrators() as $migrator) {
if (!$migrator->canImport($importSource)) {
throw new UserMigrationException('Version ' . ($importSource->getMigratorVersion($migrator->getId()) ?? 'null') . ' for migrator ' . get_class($migrator) . ' is not supported');
}
}
$user = $this->importUser($user, $importSource, $output);
$this->importAppsSettings($user, $importSource, $output);
// Run imports of registered migrators
foreach ($this->getMigrators() as $migrator) {
$migrator->import($user, $importSource, $output);
}
$uid = $user->getUID();
$output->writeln("Successfully imported $uid");
return $user;
} finally {
$importSource->close();
}
}
/**
* @throws UserMigrationException
*/
protected function exportUserInformation(IUser $user,
IExportDestination $exportDestination,
OutputInterface $output): void {
$output->writeln('Exporting user information in ' . IImportSource::PATH_USER . '…');
// TODO store backend? email? cloud id? quota?
$userinfo = [
'uid' => $user->getUID(),
'displayName' => $user->getDisplayName(),
'lastLogin' => $user->getLastLogin(),
'enabled' => $user->isEnabled(),
];
try {
$exportDestination->addFileContents(IImportSource::PATH_USER, json_encode($userinfo));
} catch (Throwable $e) {
throw new UserMigrationException('Could not export user information.', 0, $e);
}
}
/**
* @throws UserMigrationException
*/
protected function importUser(?IUser $user,
IImportSource $importSource,
OutputInterface $output): IUser {
$output->writeln('Importing user information from ' . IImportSource::PATH_USER . '…');
$data = json_decode($importSource->getFileContents(IImportSource::PATH_USER), true, 512, JSON_THROW_ON_ERROR);
if ($user === null) {
$user = $this->userManager->createUser(
$data['uid'],
$this->secureRandom->generate(10, ISecureRandom::CHAR_ALPHANUMERIC)
);
}
if (!($user instanceof IUser)) {
throw new UserMigrationException('Failed to create user.');
}
$user->setEnabled($data['enabled']);
$user->setDisplayName($data['displayName']);
return $user;
}
/**
* @throws UserMigrationException
*/
protected function exportVersions(string $uid,
IExportDestination $exportDestination,
OutputInterface $output): void {
$output->writeln('Exporting versions in versions.json…');
$versions = array_merge(
['core' => $this->config->getSystemValue('version')],
// TODO: When dropping support for Nextcloud <32, switch to $this->appConfig->getAppInstalledVersions()
\OC_App::getAppVersions()
);
try {
$exportDestination->addFileContents('versions.json', json_encode($versions));
} catch (Throwable $e) {
throw new UserMigrationException('Could not export versions.', 0, $e);
}
}
/**
* @throws UserMigrationException
*/
protected function exportAppsSettings(string $uid,
IExportDestination $exportDestination,
OutputInterface $output): void {
$output->writeln('Exporting settings in settings.json…');
$data = $this->config->getAllUserValues($uid);
try {
$exportDestination->addFileContents('settings.json', json_encode($data));
} catch (Throwable $e) {
throw new UserMigrationException('Could not export settings.', 0, $e);
}
}
/**
* @throws UserMigrationException
*/
protected function importAppsSettings(IUser $user,
IImportSource $importSource,
OutputInterface $output): void {
$output->writeln('Importing settings from settings.json…');
$data = json_decode($importSource->getFileContents('settings.json'), true, 512, JSON_THROW_ON_ERROR);
if (isset($data['settings']['email']) && $user->canChangeEmail()) {
$value = mb_strtolower(trim($data['settings']['email']));
if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') {
$user->setSystemEMailAddress($value);
}
}
if (interface_exists(IUserConfig::class)) {
/*
* Starting with 32, we have to use the correct type
* When dropping support for <32, IUserConfig should be injected instead
*/
$userConfig = \OCP\Server::get(IUserConfig::class);
$userId = $user->getUID();
foreach ($data as $app => $values) {
foreach ($values as $key => $value) {
/* Avoid issues with numeric keys */
$key = (string)$key;
if (!$this->isAppSettingImportAllowed($app, $key)) {
continue;
}
try {
$type = $userConfig->getValueType($userId, $app, $key);
} catch (UnknownKeyException) {
/** If type is unknown, default to mixed */
/** @psalm-suppress UndefinedClass ValueType only exists in 32 and higher, but in this if branch we know it exists */
$type = ValueType::MIXED;
}
/** @psalm-suppress UndefinedClass ValueType only exists in 32 and higher, but in this if branch we know it exists */
switch ($type) {
default:
case ValueType::MIXED:
$this->config->setUserValue($userId, $app, $key, $value);
break;
case ValueType::STRING:
$userConfig->setValueString($userId, $app, $key, $value);
break;
case ValueType::INT:
$userConfig->setValueInt($userId, $app, $key, (int)$value);
break;
case ValueType::FLOAT:
$userConfig->setValueFloat($userId, $app, $key, (float)$value);
break;
case ValueType::BOOL:
if (is_string($value)) {
$value = in_array(strtolower($value), ['1', 'true', 'yes', 'on']);
} elseif (!is_bool($value)) {
$value = (bool)$value;
}
$userConfig->setValueBool($userId, $app, $key, $value);
break;
case ValueType::ARRAY:
$userConfig->setValueArray($userId, $app, $key, $value);
break;
}
}
}
} else {
foreach ($data as $app => $values) {
foreach ($values as $key => $value) {
/* Avoid issues with numeric keys */
$key = (string)$key;
if (!$this->isAppSettingImportAllowed($app, $key)) {
continue;
}
$this->config->setUserValue($user->getUID(), $app, $key, $value);
}
}
}
}
private function isAppSettingImportAllowed(string $appid, string $key): bool {
$allowedKeys = [
'calendar' => [
'currentView',
'defaultReminder',
'eventLimit',
'firstRun',
'showTasks',
'showWeekNr',
'showWeekends',
'skipPopover',
'slotDuration',
'tasksSidebar',
'timezone',
],
'collectives' => ['user_folder'],
'contacts' => ['enableSocialSync'],
'core' => [
'apporder',
'first_day_of_week',
'lang',
'locale',
'templateDirectory',
'timezone',
'whatsNewLastRead',
],
'dashboard' => ['firstRun','layout','statuses'],
'dav' => ['attachmentsFolder','generateBirthdayCalendar','user_status_automation'],
'end_to_end_encryption' => ['e2eeInBrowserEnabled'],
'files' => [
'crop_image_previews',
'default_view',
'file_sorting',
'file_sorting_direction',
'files_views_configs',
'folder_tree',
'grid_view',
'show_Quick_Access',
'show_dialog_deletion',
'show_dialog_file_extension',
'show_favorites',
'show_files_extensions',
'show_grid,show_hidden',
'show_mime_column',
'show_shareoverview',
'show_sharing_menu',
'sort_favorites_first',
'sort_folders_first',
],
'files_sharing' => [
'default_accept',
'share_folder',
],
'firstrunwizard' => ['apphint','show'],
'notifications' => ['sound_notification','sound_talk'],
'photos' => ['croppedLayout','photosLocation','photosSourceFolders'],
'recommendations' => ['enabled'],
'systemtags' => ['last_used'],
'text' => ['is_full_width_editor','workspace_enabled'],
'theming' => [
'background',
'backgroundVersion',
'background_color',
'background_image',
'enabled-themes',
'force_enable_blur_filter',
'primary_color',
'shortcuts_disabled',
],
'twofactor_nextcloud_notification' => ['enabled'],
'weather_status' => ['address','altitude','favorites','lat','lon','mode'],
'whiteboard' => ['recording_auto_upload_on_disconnect'],
];
if (isset($allowedKeys[$appid]) && in_array($key, $allowedKeys[$appid])) {
return true;
}
if ($appid === 'activity') {
if (str_starts_with($key, 'notify_')) {
return true;
}
if (in_array($key, ['configured'])) {
return true;
}
}
return false;
}
/**
* @param UserExport|UserImport $job
*
* @throws UserMigrationException
*/
protected function deleteMapperJob($job): void {
switch (true) {
case $job instanceof UserExport:
try {
$this->exportMapper->delete($job);
} catch (Throwable $e) {
throw new UserMigrationException('Error deleting export job', 0, $e);
}
break;
case $job instanceof UserImport:
try {
$this->importMapper->delete($job);
} catch (Throwable $e) {
throw new UserMigrationException('Error deleting import job', 0, $e);
}
break;
default:
throw new UserMigrationException('Error deleting user migration job');
}
}
/**
* @throws UserMigrationException
*/
public function queueExportJob(IUser $user, array $migrators): void {
try {
$userExport = new UserExport();
$userExport->setSourceUser($user->getUID());
$userExport->setMigratorsArray($migrators);
$userExport->setStatus(UserExport::STATUS_WAITING);
/** @var UserExport $userExport */
$userExport = $this->exportMapper->insert($userExport);
$this->jobList->add(UserExportJob::class, [
'id' => $userExport->getId(),
]);
} catch (Throwable $e) {
throw new UserMigrationException('Error queueing export job', 0, $e);
}
}
/**
* @throws UserMigrationException
*/
public function queueImportJob(IUser $author, IUser $targetUser, string $path): void {
/** @var string[] $availableMigrators */
$availableMigrators = array_map(
fn (IMigrator $migrator) => $migrator->getId(),
$this->getMigrators(),
);
try {
$userImport = new UserImport();
$userImport->setAuthor($author->getUID());
$userImport->setTargetUser($targetUser->getUID());
// Path is relative to the author folder
$userImport->setPath($path);
// All available migrators are added as migrator selection for import is not allowed for now
$userImport->setMigratorsArray($availableMigrators);
$userImport->setStatus(UserImport::STATUS_WAITING);
/** @var UserImport $userImport */
$userImport = $this->importMapper->insert($userImport);
$this->jobList->add(UserImportJob::class, [
'id' => $userImport->getId(),
]);
} catch (Throwable $e) {
throw new UserMigrationException('Error queueing import job', 0, $e);
}
}
/**
* @return IMigrator[]
*
* @throws UserMigrationException
*/
public function getMigrators(): array {
$context = $this->coordinator->getRegistrationContext();
if ($context === null) {
throw new UserMigrationException('Failed to get context');
}
/** @var IMigrator[] $migrators */
$migrators = [];
foreach ($context->getUserMigrators() as $migratorRegistration) {
/** @var IMigrator $migrator */
$migrator = $this->container->get($migratorRegistration->getService());
$migrators[] = $migrator;
}
return $migrators;
}
/**
* @return null|UserExport|UserImport
*
* @throws UserMigrationException
*/
public function getCurrentJob(IUser $user) {
// TODO merge export and import entities?
try {
$exportJob = $this->exportMapper->getBySourceUser($user->getUID());
} catch (DoesNotExistException $e) {
// Allow this exception as this just means the user has no export jobs queued currently
}
try {
$importJob = $this->importMapper->getByTargetUser($user->getUID());
} catch (DoesNotExistException $e) {
// Allow this exception as this just means the user has no import jobs queued currently
}
if (!empty($exportJob) && !empty($importJob)) {
throw new UserMigrationException('A user export and import job cannot be queued or run at the same time');
}
$job = $exportJob ?? $importJob ?? null;
if (empty($job)) {
return null;
}
if (!$this->jobList->has(static::ENTITY_JOB_MAP[get_class($job)], ['id' => $job->getId()])) {
$this->deleteMapperJob($job);
throw new UserMigrationException('Expected "' . get_class($job) . '" job with id argument "' . $job->getId() . '" in `oc_jobs` database table');
}
return $job;
}
/**
* @param UserExport|UserImport $job
*
* @throws UserMigrationException
*/
public function cancelJob($job): void {
if (!$this->jobList->has(static::ENTITY_JOB_MAP[get_class($job)], ['id' => $job->getId()])) {
$this->deleteMapperJob($job);
throw new UserMigrationException('Expected "' . get_class($job) . '" job with id argument "' . $job->getId() . '" in `oc_jobs` database table');
}
switch (true) {
case $job instanceof UserExport:
try {
$this->jobList->remove(UserExportJob::class, [
'id' => $job->getId(),
]);
$this->exportMapper->delete($job);
} catch (Throwable $e) {
throw new UserMigrationException('Error cancelling export job', 0, $e);
}
break;
case $job instanceof UserImport:
try {
$this->jobList->remove(UserImportJob::class, [
'id' => $job->getId(),
]);
$this->importMapper->delete($job);
} catch (Throwable $e) {
throw new UserMigrationException('Error cancelling import job', 0, $e);
}
break;
default:
throw new UserMigrationException('Error cancelling user migration job');
}
}
/**
* Returns the unique ID
*/
public function getId(): string {
return 'usermigrationservice';
}
}