Skip to content
Next Next commit
Run exports and imports of registered migrators
Signed-off-by: Christopher Ng <[email protected]>
  • Loading branch information
Pytal committed Feb 15, 2022
commit 7d2d8aa87c7c99889d66877608d0c07aa3a457aa
1 change: 1 addition & 0 deletions lib/ExportDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\ITempManager;
use OCP\UserMigration\IExportDestination;
use ZipStreamer\COMPR;
use ZipStreamer\ZipStreamer;

Expand Down
54 changes: 0 additions & 54 deletions lib/IExportDestination.php

This file was deleted.

60 changes: 0 additions & 60 deletions lib/IImportSource.php

This file was deleted.

1 change: 1 addition & 0 deletions lib/ImportSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OC\Archive\Archive;
use OC\Archive\ZIP;
use OCP\Files\Folder;
use OCP\UserMigration\IImportSource;

class ImportSource implements IImportSource {
private Archive $archive;
Expand Down
43 changes: 39 additions & 4 deletions lib/Service/UserMigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/**
* @copyright Copyright (c) 2022 Côme Chilliet <[email protected]>
*
* @author Christopher Ng <[email protected]>
* @author Côme Chilliet <[email protected]>
*
* @license GNU AGPL version 3 or any later version
Expand All @@ -26,19 +27,22 @@

namespace OCA\UserMigration\Service;

use OC\AppFramework\Bootstrap\Coordinator;
use OC\Files\Filesystem;
use OCA\UserMigration\Exception\UserMigrationException;
use OCA\UserMigration\ExportDestination;
use OCA\UserMigration\IExportDestination;
use OCA\UserMigration\IImportSource;
use OCA\UserMigration\ImportSource;
use OC\Files\Filesystem;
use OCP\Accounts\IAccountManager;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\ITempManager;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Security\ISecureRandom;
use OCP\UserMigration\IExportDestination;
use OCP\UserMigration\IImportSource;
use OCP\UserMigration\IMigrator;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -53,18 +57,26 @@ class UserMigrationService {

protected IUserManager $userManager;

protected ContainerInterface $container;

protected Coordinator $coordinator;

public function __construct(
IRootFolder $rootFolder,
IConfig $config,
IAccountManager $accountManager,
ITempManager $tempManager,
IUserManager $userManager
IUserManager $userManager,
ContainerInterface $container,
Coordinator $coordinator
) {
$this->root = $rootFolder;
$this->config = $config;
$this->accountManager = $accountManager;
$this->tempManager = $tempManager;
$this->userManager = $userManager;
$this->container = $container;
$this->coordinator = $coordinator;
}

/**
Expand Down Expand Up @@ -113,6 +125,17 @@ public function export(IUser $user, ?OutputInterface $output = null): string {
$output
);

// Run exports of registered migrators
$context = $this->coordinator->getRegistrationContext();

if ($context !== null) {
foreach ($context->getUserMigrators() as $migratorRegistration) {
/** @var IMigrator $migrator */
$migrator = $this->container->get($migratorRegistration->getService());
$migrator->export($user, $exportDestination, $output);
}
}

$exportDestination->close();
$output->writeln("Export saved in ".$exportDestination->getPath());
return $exportDestination->getPath();
Expand All @@ -131,6 +154,18 @@ public function import(string $path, ?OutputInterface $output = null): void {
$this->importAccountInformation($user, $importSource, $output);
$this->importAppsSettings($user, $importSource, $output);
$this->importFiles($user, $importSource, $output);

// Run imports of registered migrators
$context = $this->coordinator->getRegistrationContext();

if ($context !== null) {
foreach ($context->getUserMigrators() as $migratorRegistration) {
/** @var IMigrator $migrator */
$migrator = $this->container->get($migratorRegistration->getService());
$migrator->import($user, $importSource, $output);
}
}

$uid = $user->getUID();
$output->writeln("Successfully imported $uid from $path");
} finally {
Expand Down