Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly sync user settings during migration #27635

Merged
merged 1 commit into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/private/DB/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ public function migrate($to = 'latest') {

/**
* @param string $version
* @return mixed
* @throws \Exception
*/
protected function createInstance($version) {
$class = $this->getClass($version);
Expand Down
11 changes: 7 additions & 4 deletions lib/private/User/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,15 @@ public function run(\Closure $callback) {
}
$a = $this->setupAccount($a, $uid);
$this->mapper->update($a);
// clean the user's preferences
$this->cleanPreferences($uid);
} catch(DoesNotExistException $ex) {
$a = $this->createNewAccount($uid);
$this->setupAccount($a, $uid);
$this->mapper->insert($a);
}
// clean the user's preferences
$this->cleanPreferences($uid);

// call the callback
$callback($uid);
}
$offset += $limit;
Expand All @@ -122,7 +125,7 @@ public function run(\Closure $callback) {
* @param string $uid
* @return Account
*/
private function setupAccount(Account $a, $uid) {
public function setupAccount(Account $a, $uid) {
list($hasKey, $value) = $this->readUserConfig($uid, 'core', 'enabled');
if ($hasKey) {
$a->setState(($value === 'true') ? Account::STATE_ENABLED : Account::STATE_DISABLED);
Expand Down Expand Up @@ -171,7 +174,7 @@ private function createNewAccount($uid) {
*/
private function readUserConfig($uid, $app, $key) {
$keys = $this->config->getUserKeys($uid, $app);
if (isset($keys[$key])) {
if (in_array($key, $keys)) {
$enabled = $this->config->getUserValue($uid, $app, $key);
return [true, $enabled];
}
Expand Down
44 changes: 44 additions & 0 deletions tests/lib/User/SyncServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Created by PhpStorm.
* User: deepdiver
* Date: 12.04.17
* Time: 14:56
*/

namespace Test\User;


use OC\User\Account;
use OC\User\AccountMapper;
use OC\User\SyncService;
use OCP\IConfig;
use OCP\ILogger;
use OCP\UserInterface;
use Test\TestCase;

class SyncServiceTest extends TestCase {

public function testSetupAccount() {
$mapper = $this->createMock(AccountMapper::class);
$backend = $this->createMock(UserInterface::class);
$config = $this->createMock(IConfig::class);
$logger = $this->createMock(ILogger::class);

$config->expects($this->any())->method('getUserKeys')->willReturnMap([
['user1', 'core', []],
['user1', 'login', []],
['user1', 'settings', ['email']],
['user1', 'files', []],
]);
$config->expects($this->any())->method('getUserValue')->willReturnMap([
['user1', 'settings', 'email', '', 'foo@bar.net'],
]);

$s = new SyncService($mapper, $backend, $config, $logger);
$a = new Account();
$s->setupAccount($a, 'user1');

$this->assertEquals('foo@bar.net', $a->getEmail());
}
}