Skip to content

Commit 98bc98a

Browse files
committed
Add profile config migration
Signed-off-by: Christopher Ng <[email protected]>
1 parent 253c064 commit 98bc98a

File tree

4 files changed

+64
-14
lines changed

4 files changed

+64
-14
lines changed

apps/settings/lib/UserMigration/AccountMigrator.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828

2929
use InvalidArgumentException;
3030
use OC\Accounts\TAccountsHelper;
31+
use OC\Core\Db\ProfileConfigMapper;
3132
use OC\NotSquareException;
33+
use OC\Profile\ProfileManager;
3234
use OCA\Settings\AppInfo\Application;
3335
use OCP\Accounts\IAccountManager;
3436
use OCP\IAvatarManager;
@@ -51,6 +53,10 @@ class AccountMigrator implements IMigrator, ISizeEstimationMigrator {
5153

5254
private IAvatarManager $avatarManager;
5355

56+
private ProfileManager $profileManager;
57+
58+
private ProfileConfigMapper $configMapper;
59+
5460
private IL10N $l10n;
5561

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

6066
private const AVATAR_BASENAME = 'avatar';
6167

68+
private const PATH_CONFIG_FILE = AccountMigrator::PATH_ROOT . 'config.json';
69+
6270
public function __construct(
6371
IAccountManager $accountManager,
6472
IAvatarManager $avatarManager,
73+
ProfileManager $profileManager,
74+
ProfileConfigMapper $configMapper,
6575
IL10N $l10n
6676
) {
6777
$this->accountManager = $accountManager;
6878
$this->avatarManager = $avatarManager;
79+
$this->profileManager = $profileManager;
80+
$this->configMapper = $configMapper;
6981
$this->l10n = $l10n;
7082
}
7183

@@ -113,6 +125,14 @@ public function export(IUser $user, IExportDestination $exportDestination, Outpu
113125
} catch (Throwable $e) {
114126
throw new AccountMigratorException('Could not export avatar', 0, $e);
115127
}
128+
129+
try {
130+
$output->writeln('Exporting profile config in ' . AccountMigrator::PATH_CONFIG_FILE . '');
131+
$config = $this->profileManager->getProfileConfig($user, $user);
132+
$exportDestination->addFileContents(AccountMigrator::PATH_CONFIG_FILE, json_encode($config));
133+
} catch (Throwable $e) {
134+
throw new AccountMigratorException('Could not export profile config', 0, $e);
135+
}
116136
}
117137

118138
/**
@@ -165,6 +185,19 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface
165185
throw new AccountMigratorException('Failed to import avatar', 0, $e);
166186
}
167187
}
188+
189+
try {
190+
$output->writeln('Importing profile config from ' . AccountMigrator::PATH_CONFIG_FILE . '');
191+
/** @var array $configData */
192+
$configData = json_decode($importSource->getFileContents(AccountMigrator::PATH_CONFIG_FILE), true, 512, JSON_THROW_ON_ERROR);
193+
// Ensure that a profile config entry exists in the database
194+
$this->profileManager->getProfileConfig($user, $user);
195+
$config = $this->configMapper->get($user->getUID());
196+
$config->setConfigArray($configData);
197+
$this->configMapper->update($config);
198+
} catch (Throwable $e) {
199+
throw new AccountMigratorException('Failed to import profile config');
200+
}
168201
}
169202

170203
/**

apps/settings/tests/UserMigration/AccountMigratorTest.php

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OCP\IUserManager;
3535
use OCP\UserMigration\IExportDestination;
3636
use OCP\UserMigration\IImportSource;
37+
use PHPUnit\Framework\Constraint\JsonMatches;
3738
use PHPUnit\Framework\MockObject\MockObject;
3839
use Sabre\VObject\UUIDUtil;
3940
use Symfony\Component\Console\Output\OutputInterface;
@@ -65,6 +66,8 @@ class AccountMigratorTest extends TestCase {
6566

6667
private const REGEX_AVATAR_FILE = '/^' . Application::APP_ID . '\/' . 'avatar\.(jpg|png)' . '$/';
6768

69+
private const REGEX_CONFIG_FILE = '/^' . Application::APP_ID . '\/' . '[a-z]+\.json' . '$/';
70+
6871
protected function setUp(): void {
6972
$app = new App(Application::APP_ID);
7073
$container = $app->getContainer();
@@ -81,30 +84,33 @@ protected function setUp(): void {
8184
public function dataImportExportAccount(): array {
8285
return array_map(
8386
function (string $filename) {
84-
$dataPath = self::ASSETS_DIR . $filename;
85-
// For each json file there is an avatar image with the same basename
86-
$avatarBasename = pathinfo($filename, PATHINFO_FILENAME);
87-
$avatarPath = self::ASSETS_DIR . (file_exists(self::ASSETS_DIR . "$avatarBasename.jpg") ? "$avatarBasename.jpg" : "$avatarBasename.png");
87+
$dataPath = static::ASSETS_DIR . $filename;
88+
// For each account json file there is an avatar image and a config json file with the same basename
89+
$basename = pathinfo($filename, PATHINFO_FILENAME);
90+
$avatarPath = static::ASSETS_DIR . (file_exists(static::ASSETS_DIR . "$basename.jpg") ? "$basename.jpg" : "$basename.png");
91+
$configPath = static::ASSETS_DIR . "$basename-config." . pathinfo($filename, PATHINFO_EXTENSION);
8892
return [
8993
UUIDUtil::getUUID(),
9094
json_decode(file_get_contents($dataPath), true, 512, JSON_THROW_ON_ERROR),
9195
$avatarPath,
96+
json_decode(file_get_contents($configPath), true, 512, JSON_THROW_ON_ERROR),
9297
];
9398
},
9499
array_filter(
95-
scandir(self::ASSETS_DIR),
96-
fn (string $filename) => pathinfo($filename, PATHINFO_EXTENSION) === 'json',
100+
scandir(static::ASSETS_DIR),
101+
fn (string $filename) => pathinfo($filename, PATHINFO_EXTENSION) === 'json' && mb_strpos(pathinfo($filename, PATHINFO_FILENAME), 'config') === false,
97102
),
98103
);
99104
}
100105

101106
/**
102107
* @dataProvider dataImportExportAccount
103108
*/
104-
public function testImportExportAccount(string $userId, array $importData, string $avatarPath): void {
109+
public function testImportExportAccount(string $userId, array $importData, string $avatarPath, array $importConfig): void {
105110
$user = $this->userManager->createUser($userId, 'topsecretpassword');
106111
$avatarExt = pathinfo($avatarPath, PATHINFO_EXTENSION);
107112
$exportData = $importData;
113+
$exportConfig = $importConfig;
108114
// Verification status of email will be set to in progress on import so we set the export data to reflect that
109115
$exportData[IAccountManager::PROPERTY_EMAIL]['verified'] = IAccountManager::VERIFICATION_IN_PROGRESS;
110116

@@ -115,10 +121,16 @@ public function testImportExportAccount(string $userId, array $importData, strin
115121
->willReturn(1);
116122

117123
$this->importSource
118-
->expects($this->once())
124+
->expects($this->exactly(2))
119125
->method('getFileContents')
120-
->with($this->matchesRegularExpression(self::REGEX_ACCOUNT_FILE))
121-
->willReturn(json_encode($importData));
126+
->withConsecutive(
127+
[$this->matchesRegularExpression(static::REGEX_ACCOUNT_FILE)],
128+
[$this->matchesRegularExpression(static::REGEX_CONFIG_FILE)],
129+
)
130+
->willReturnOnConsecutiveCalls(
131+
json_encode($importData),
132+
json_encode($importConfig),
133+
);
122134

123135
$this->importSource
124136
->expects($this->once())
@@ -129,7 +141,7 @@ public function testImportExportAccount(string $userId, array $importData, strin
129141
$this->importSource
130142
->expects($this->once())
131143
->method('getFileAsStream')
132-
->with($this->matchesRegularExpression(self::REGEX_AVATAR_FILE))
144+
->with($this->matchesRegularExpression(static::REGEX_AVATAR_FILE))
133145
->willReturn(fopen($avatarPath, 'r'));
134146

135147
$this->migrator->import($user, $this->importSource, $this->output);
@@ -150,14 +162,17 @@ public function testImportExportAccount(string $userId, array $importData, strin
150162
}
151163

152164
$this->exportDestination
153-
->expects($this->once())
165+
->expects($this->exactly(2))
154166
->method('addFileContents')
155-
->with($this->matchesRegularExpression(self::REGEX_ACCOUNT_FILE), json_encode($exportData));
167+
->withConsecutive(
168+
[$this->matchesRegularExpression(static::REGEX_ACCOUNT_FILE), new JsonMatches(json_encode($exportData))],
169+
[$this->matchesRegularExpression(static::REGEX_CONFIG_FILE), new JsonMatches(json_encode($exportConfig))],
170+
);
156171

157172
$this->exportDestination
158173
->expects($this->once())
159174
->method('addFileAsStream')
160-
->with($this->matchesRegularExpression(self::REGEX_AVATAR_FILE), $this->isType('resource'));
175+
->with($this->matchesRegularExpression(static::REGEX_AVATAR_FILE), $this->isType('resource'));
161176

162177
$this->migrator->export($user, $this->exportDestination, $this->output);
163178
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"address":{"visibility":"show_users_only"},"avatar":{"visibility":"show_users_only"},"biography":{"visibility":"show"},"displayname":{"visibility":"show"},"headline":{"visibility":"show"},"organisation":{"visibility":"show"},"role":{"visibility":"show"},"email":{"visibility":"hide"},"phone":{"visibility":"hide"},"twitter":{"visibility":"show_users_only"},"website":{"visibility":"show_users_only"},"talk":{"visibility":"show"}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"address":{"visibility":"show_users_only"},"avatar":{"visibility":"show"},"biography":{"visibility":"show"},"displayname":{"visibility":"show"},"headline":{"visibility":"show"},"organisation":{"visibility":"show"},"role":{"visibility":"show"},"email":{"visibility":"show_users_only"},"phone":{"visibility":"show_users_only"},"twitter":{"visibility":"show"},"website":{"visibility":"show"}}

0 commit comments

Comments
 (0)