This commit is contained in:
Robin Appelman 2026-02-03 19:57:38 -01:00 committed by GitHub
commit fbe19cbcf5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 7 deletions

View file

@ -121,25 +121,31 @@ class DBConfigService {
public function getMountsForUserAndPath(string $userId, array $groupIds, string $path, bool $forChildren): array {
$path = str_replace('/' . $userId . '/files', '', $path);
$path = rtrim($path, '/');
if ($path === '') {
$nonChildPath = '/';
} else {
$nonChildPath = $path;
}
$builder = $this->getSelectQueryBuilder();
$pathFilter = $forChildren
? $builder->expr()->like('m.mount_point', $builder->createNamedParameter($this->connection->escapeLikeParameter($path) . '/_%', IQueryBuilder::PARAM_STR))
: $builder->expr()->eq('m.mount_point', $builder->createNamedParameter($nonChildPath, IQueryBuilder::PARAM_STR));
$builder->where($builder->expr()->orX(
$builder->expr()->andX( // global mounts
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GLOBAL, IQueryBuilder::PARAM_INT)),
$builder->expr()->isNull('a.value'),
$forChildren ? $builder->expr()->like('m.mount_point', $builder->createNamedParameter($this->connection->escapeLikeParameter($path) . '_%', IQueryBuilder::PARAM_STR))
: $builder->expr()->eq('m.mount_point', $builder->createNamedParameter($path, IQueryBuilder::PARAM_STR)),
$pathFilter,
),
$builder->expr()->andX( // mounts for user
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_USER, IQueryBuilder::PARAM_INT)),
$builder->expr()->eq('a.value', $builder->createNamedParameter($userId)),
$forChildren ? $builder->expr()->like('m.mount_point', $builder->createNamedParameter($this->connection->escapeLikeParameter($path) . '_%', IQueryBuilder::PARAM_STR))
: $builder->expr()->eq('m.mount_point', $builder->createNamedParameter($path, IQueryBuilder::PARAM_STR)),
$pathFilter,
),
$builder->expr()->andX( // mounts for group
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GROUP, IQueryBuilder::PARAM_INT)),
$builder->expr()->in('a.value', $builder->createNamedParameter($groupIds, IQueryBuilder::PARAM_STR_ARRAY)),
$forChildren ? $builder->expr()->like('m.mount_point', $builder->createNamedParameter($this->connection->escapeLikeParameter($path) . '_%', IQueryBuilder::PARAM_STR))
: $builder->expr()->eq('m.mount_point', $builder->createNamedParameter($path, IQueryBuilder::PARAM_STR)),
$pathFilter,
),
));
@ -157,7 +163,7 @@ class DBConfigService {
->where($builder->expr()->andX( // global mounts
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GLOBAL, IQueryBuilder::PARAM_INT)),
$builder->expr()->isNull('a.value'),
), );
));
return $this->getMountsFromQuery($query);
}

View file

@ -12,6 +12,7 @@ use OCA\Files_External\Service\DBConfigService;
use OCP\IDBConnection;
use OCP\Security\ICrypto;
use OCP\Server;
use PHPUnit\Framework\Attributes\DataProvider;
use Test\TestCase;
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
@ -271,4 +272,32 @@ class DBConfigServiceTest extends TestCase {
$this->assertEquals($id1, $mounts[0]['mount_id']);
$this->assertEquals($id2, $mounts[1]['mount_id']);
}
public static function mountsForPathProvider(): array {
return [
['/test/files/test/', false, ['/test']],
['/test/files/test/', true, ['/test/more']],
['/test/files/', false, ['/']],
['/test/files/', true, ['/test', '/test/more', '/test2']],
];
}
#[DataProvider('mountsForPathProvider')]
public function testGetMountsForUserAndPath(string $path, bool $forChildren, array $expectedMountPoints): void {
sort($expectedMountPoints);
$id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
$this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
$id2 = $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAL);
$this->dbConfig->addApplicable($id2, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
$id3 = $this->addMount('/test/more', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
$this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
$id4 = $this->addMount('/', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
$this->dbConfig->addApplicable($id4, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
$mounts = $this->dbConfig->getMountsForUserAndPath('test', [], $path, $forChildren);
$mountPoints = array_map(fn (array $mountInfo) => $mountInfo['mount_point'], $mounts);
sort($mountPoints);
$this->assertEquals($expectedMountPoints, $mountPoints);
}
}