Skip to content

Commit

Permalink
Merge pull request #27488 from owncloud/stable9.1-usermountcache-dele…
Browse files Browse the repository at this point in the history
…tedusers

[stable9.1] Properly handle deleted users in UserMountCache
  • Loading branch information
Vincent Petry committed Apr 20, 2017
2 parents 9c4b618 + 9fd7c9b commit 4fc5a1a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
31 changes: 28 additions & 3 deletions lib/private/Files/Config/UserMountCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,34 @@ private function removeFromCache(ICachedMountInfo $mount) {

private function dbRowToMountInfo(array $row) {
$user = $this->userManager->get($row['user_id']);
if ($user === null) {
// user does not exist any more, delete all mounts of that user directly
$builder = $this->connection->getQueryBuilder();
$query = $builder->delete('mounts')
->where($builder->expr()->eq('user_id', $builder->createNamedParameter($row['user_id'])));
$query->execute();
return null;
}
return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point']);
}

/**
* Convert DB rows to CachedMountInfo
*
* @param array $rows DB rows
* @return CachedMountInfo[]
*/
private function convertRows($rows) {
$mountInfos = [];
foreach ($rows as $row) {
$mountInfo = $this->dbRowToMountInfo($row);
if (!is_null($mountInfo)) {
$mountInfos[] = $mountInfo;
}
}
return $mountInfos;
}

/**
* @param IUser $user
* @return ICachedMountInfo[]
Expand All @@ -186,7 +211,7 @@ public function getMountsForUser(IUser $user) {

$rows = $query->execute()->fetchAll();

$this->mountsForUsers[$user->getUID()] = array_map([$this, 'dbRowToMountInfo'], $rows);
$this->mountsForUsers[$user->getUID()] = $this->convertRows($rows);
}
return $this->mountsForUsers[$user->getUID()];
}
Expand All @@ -203,7 +228,7 @@ public function getMountsForStorageId($numericStorageId) {

$rows = $query->execute()->fetchAll();

return array_map([$this, 'dbRowToMountInfo'], $rows);
return $this->convertRows($rows);
}

/**
Expand All @@ -218,7 +243,7 @@ public function getMountsForRootId($rootFileId) {

$rows = $query->execute()->fetchAll();

return array_map([$this, 'dbRowToMountInfo'], $rows);
return $this->convertRows($rows);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/Files/Config/UserMountCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function setUp() {
$userBackend = new Dummy();
$userBackend->createUser('u1', '');
$userBackend->createUser('u2', '');
$userBackend->createUser('u3', '');
$this->userManager->registerBackend($userBackend);
$this->cache = new \OC\Files\Config\UserMountCache($this->connection, $this->userManager, $this->getMock('\OC\Log'));
}
Expand Down Expand Up @@ -183,15 +184,19 @@ public function testChangeMounts() {
public function testGetMountsForUser() {
$user1 = $this->userManager->get('u1');
$user2 = $this->userManager->get('u2');
$user3 = $this->userManager->get('u3');

$mount1 = new MountPoint($this->getStorage(1, 2), '/foo/');
$mount2 = new MountPoint($this->getStorage(3, 4), '/bar/');

$this->cache->registerMounts($user1, [$mount1, $mount2]);
$this->cache->registerMounts($user2, [$mount2]);
$this->cache->registerMounts($user3, [$mount2]);

$this->clearCache();

$user3->delete();

$cachedMounts = $this->cache->getMountsForUser($user1);

$this->assertCount(2, $cachedMounts);
Expand All @@ -204,6 +209,9 @@ public function testGetMountsForUser() {
$this->assertEquals($user1, $cachedMounts[1]->getUser());
$this->assertEquals(4, $cachedMounts[1]->getRootId());
$this->assertEquals(3, $cachedMounts[1]->getStorageId());

$cachedMounts = $this->cache->getMountsForUser($user3);
$this->assertEmpty($cachedMounts);
}

public function testGetMountsByStorageId() {
Expand Down Expand Up @@ -372,4 +380,22 @@ public function testGetMountsForFileIdSubFolderMountOutside() {

$this->assertCount(0, $cachedMounts);
}

public function testGetMountsForFileIdDeletedUser() {
$user1 = $this->userManager->get('u1');

$rootId = $this->createCacheEntry('', 2);

$mount1 = new MountPoint($this->getStorage(2, $rootId), '/foo/');

$this->cache->registerMounts($user1, [$mount1]);

$user1->delete();

$this->clearCache();

$cachedMounts = $this->cache->getMountsForFileId($rootId);

$this->assertEmpty($cachedMounts);
}
}

0 comments on commit 4fc5a1a

Please sign in to comment.