Skip to content
Closed
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
28 changes: 25 additions & 3 deletions lib/private/Files/Config/UserMountCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,33 @@ private function removeFromCache(ICachedMountInfo $mount) {
private function dbRowToMountInfo(array $row) {
$user = $this->userManager->get($row['user_id']);
if (is_null($user)) {
// user does not exist any more, delete all mounts of that user directly

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in case your user backend (like ldap) has a hick-up or is temporarily wrong configured, everything will be whiped?
Sounds a bit odd, not sure if we should do this. I agree with the rest of this PR, but I'm not a friend of this delete query.

$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'], $row['mount_id'], isset($row['path'])? $row['path']:'');
}

/**
* 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 @@ -213,7 +235,7 @@ public function getMountsForUser(IUser $user) {

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

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

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

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

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

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

return array_filter(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 @@ -47,6 +47,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->createMock(Log::class));
}
Expand Down Expand Up @@ -211,6 +212,7 @@ public function testChangeMountId() {
public function testGetMountsForUser() {
$user1 = $this->userManager->get('u1');
$user2 = $this->userManager->get('u2');
$user3 = $this->userManager->get('u3');

list($storage1, $id1) = $this->getStorage(1);
list($storage2, $id2) = $this->getStorage(2);
Expand All @@ -219,9 +221,12 @@ public function testGetMountsForUser() {

$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 @@ -234,6 +239,9 @@ public function testGetMountsForUser() {
$this->assertEquals($user1, $cachedMounts[1]->getUser());
$this->assertEquals($id2, $cachedMounts[1]->getRootId());
$this->assertEquals(2, $cachedMounts[1]->getStorageId());

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

public function testGetMountsByStorageId() {
Expand Down Expand Up @@ -431,4 +439,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);
}
}