mirror of
https://github.com/nextcloud/server.git
synced 2026-02-17 18:00:44 -05:00
Fix search results filtering
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
This commit is contained in:
parent
c8ca2da5a6
commit
0c19c5c65a
4 changed files with 58 additions and 17 deletions
|
|
@ -173,14 +173,10 @@ class Cache implements ICache {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create a CacheEntry from database row
|
||||
*
|
||||
* @param array $data
|
||||
* @param IMimeTypeLoader $mimetypeLoader
|
||||
* @return CacheEntry
|
||||
* Helper method that creates a CacheEntry from a database row.
|
||||
*/
|
||||
public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) {
|
||||
//fix types
|
||||
public static function cacheEntryFromData(array $data, IMimeTypeLoader $mimetypeLoader): ICacheEntry {
|
||||
// Fix types
|
||||
$data['fileid'] = (int)$data['fileid'];
|
||||
$data['parent'] = (int)$data['parent'];
|
||||
$data['size'] = 0 + $data['size'];
|
||||
|
|
|
|||
|
|
@ -107,8 +107,8 @@ class CacheJail extends CacheWrapper {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param ICacheEntry|array $entry
|
||||
* @return array
|
||||
* @param ICacheEntry $entry
|
||||
* @return ICacheEntry
|
||||
*/
|
||||
protected function formatCacheEntry($entry) {
|
||||
if (isset($entry['path'])) {
|
||||
|
|
@ -118,7 +118,7 @@ class CacheJail extends CacheWrapper {
|
|||
}
|
||||
|
||||
/**
|
||||
* get the stored metadata of a file or folder
|
||||
* Get the stored metadata of a file or folder
|
||||
*
|
||||
* @param string /int $file
|
||||
* @return ICacheEntry|false
|
||||
|
|
@ -229,10 +229,25 @@ class CacheJail extends CacheWrapper {
|
|||
}
|
||||
|
||||
private function formatSearchResults($results) {
|
||||
return array_map(function ($entry) {
|
||||
$finalResult = [];
|
||||
foreach ($results as $entry) {
|
||||
// Filter not accessible entries (e.g. some groupfolder entries when ACLs are enabled)
|
||||
$cacheWrapper = $this;
|
||||
while (($cacheWrapper instanceof CacheWrapper) && $entry !== false) {
|
||||
if (!($cacheWrapper instanceof CacheJail)) { // We apply the jail at the end
|
||||
$entry = $cacheWrapper->formatCacheEntry($entry);
|
||||
}
|
||||
$cacheWrapper = $cacheWrapper->getCache();
|
||||
}
|
||||
if ($entry === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Unjailed the path (remove __groupfolder/<id> prefix)
|
||||
$entry['path'] = $this->getJailedPath($entry['path'], $this->getGetUnjailedRoot());
|
||||
return $entry;
|
||||
}, $results);
|
||||
$finalResult[] = $entry;
|
||||
}
|
||||
return $finalResult;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ class CacheWrapper extends Cache {
|
|||
}
|
||||
|
||||
/**
|
||||
* Make it easy for wrappers to modify every returned cache entry
|
||||
* Makes it easy for wrappers to modify every returned cache entry
|
||||
*
|
||||
* @param ICacheEntry $entry
|
||||
* @return ICacheEntry
|
||||
|
|
@ -224,7 +224,9 @@ class CacheWrapper extends Cache {
|
|||
*/
|
||||
public function search($pattern) {
|
||||
$results = $this->getCache()->search($pattern);
|
||||
return array_map([$this, 'formatCacheEntry'], $results);
|
||||
return array_filter(array_map([$this, 'formatCacheEntry'], $results), function ($entry) {
|
||||
return $entry !== false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -235,12 +237,16 @@ class CacheWrapper extends Cache {
|
|||
*/
|
||||
public function searchByMime($mimetype) {
|
||||
$results = $this->getCache()->searchByMime($mimetype);
|
||||
return array_map([$this, 'formatCacheEntry'], $results);
|
||||
return array_filter(array_map([$this, 'formatCacheEntry'], $results), function ($entry) {
|
||||
return $entry !== false;
|
||||
});
|
||||
}
|
||||
|
||||
public function searchQuery(ISearchQuery $query) {
|
||||
$results = $this->getCache()->searchQuery($query);
|
||||
return array_map([$this, 'formatCacheEntry'], $results);
|
||||
return array_filter(array_map([$this, 'formatCacheEntry'], $results), function ($entry) {
|
||||
return $entry !== false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
namespace Test\Files\Cache\Wrapper;
|
||||
|
||||
use OC\Files\Cache\Wrapper\CacheJail;
|
||||
use OC\Files\Cache\Wrapper\CacheWrapper;
|
||||
use OC\Files\Search\SearchComparison;
|
||||
use OC\Files\Search\SearchQuery;
|
||||
use OC\User\User;
|
||||
|
|
@ -201,6 +202,29 @@ class CacheJailTest extends CacheTest {
|
|||
$this->assertEquals('asd', $result[0]['path']);
|
||||
}
|
||||
|
||||
public function testNotAuthorized() {
|
||||
$this->storage->getScanner()->scan('');
|
||||
$file1 = 'foo';
|
||||
$file2 = 'foo/bar';
|
||||
$file3 = 'foo/bar/asd';
|
||||
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
|
||||
|
||||
$this->sourceCache->put($file1, $data1);
|
||||
$this->sourceCache->put($file2, $data1);
|
||||
$this->sourceCache->put($file3, $data1);
|
||||
|
||||
$nested = new class($this->cache) extends CacheWrapper {
|
||||
protected function formatCacheEntry($entry) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
$nested = new \OC\Files\Cache\Wrapper\CacheJail($nested, 'bar');
|
||||
|
||||
$result = $nested->search('%asd%');
|
||||
$this->assertCount(0, $result);
|
||||
}
|
||||
|
||||
public function testRootJail() {
|
||||
$this->storage->getScanner()->scan('');
|
||||
$file1 = 'foo';
|
||||
|
|
|
|||
Loading…
Reference in a new issue