Skip to content

Commit

Permalink
Add profile config migration
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <[email protected]>
  • Loading branch information
Pytal committed Jul 8, 2022
1 parent b4031dc commit 50848c2
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions apps/settings/lib/UserMigration/AccountMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

use InvalidArgumentException;
use OC\Accounts\TAccountsHelper;
use OC\Core\Db\ProfileConfigMapper;
use OC\NotSquareException;
use OC\Profile\ProfileManager;
use OCA\Settings\AppInfo\Application;
use OCP\Accounts\IAccountManager;
use OCP\IAvatarManager;
Expand All @@ -51,6 +53,10 @@ class AccountMigrator implements IMigrator, ISizeEstimationMigrator {

private IAvatarManager $avatarManager;

private ProfileManager $profileManager;

private ProfileConfigMapper $configMapper;

private IL10N $l10n;

private const PATH_ROOT = Application::APP_ID . '/';
Expand All @@ -59,13 +65,19 @@ class AccountMigrator implements IMigrator, ISizeEstimationMigrator {

private const AVATAR_BASENAME = 'avatar';

private const PATH_CONFIG_FILE = AccountMigrator::PATH_ROOT . 'config.json';

public function __construct(
IAccountManager $accountManager,
IAvatarManager $avatarManager,
ProfileManager $profileManager,
ProfileConfigMapper $configMapper,
IL10N $l10n
) {
$this->accountManager = $accountManager;
$this->avatarManager = $avatarManager;
$this->profileManager = $profileManager;
$this->configMapper = $configMapper;
$this->l10n = $l10n;
}

Expand Down Expand Up @@ -113,6 +125,14 @@ public function export(IUser $user, IExportDestination $exportDestination, Outpu
} catch (Throwable $e) {
throw new AccountMigratorException('Could not export avatar', 0, $e);
}

try {
$output->writeln('Exporting profile config in ' . AccountMigrator::PATH_CONFIG_FILE . '');
$config = $this->profileManager->getProfileConfig($user, $user);
$exportDestination->addFileContents(AccountMigrator::PATH_CONFIG_FILE, json_encode($config));
} catch (Throwable $e) {
throw new AccountMigratorException('Could not export profile config', 0, $e);
}
}

/**
Expand Down Expand Up @@ -165,6 +185,19 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface
throw new AccountMigratorException('Failed to import avatar', 0, $e);
}
}

try {
$output->writeln('Importing profile config from ' . AccountMigrator::PATH_CONFIG_FILE . '');
/** @var array $configData */
$configData = json_decode($importSource->getFileContents(AccountMigrator::PATH_CONFIG_FILE), true, 512, JSON_THROW_ON_ERROR);
// Ensure that a default profile config exists in the database
$this->profileManager->getProfileConfig($user, $user);
$config = $this->configMapper->get($user->getUID());
$config->setConfigArray($configData);
$this->configMapper->update($config);
} catch (Throwable $e) {
throw new AccountMigratorException('Failed to import profile config');
}
}

/**
Expand Down

0 comments on commit 50848c2

Please sign in to comment.