Skip to content
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
1 change: 1 addition & 0 deletions lib/Fs/UserFolderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function get(string $userId): Folder {
/** @var Folder $userCollectivesFolder */
$userCollectivesFolder = $userHomeFolder->get($userCollectivesPath);
} catch (NotFoundException) {
// TODO: Replace with ISetupManager::setupForUser() once Nextcloud <= 33 is no longer supported
OC_Util::setupFS($userId);
/** @var Folder $userCollectivesFolder */
$userCollectivesFolder = $userHomeFolder->get($userCollectivesPath);
Expand Down
30 changes: 15 additions & 15 deletions lib/Mount/CollectivesUserStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

use Icewind\Streams\IteratorDirectory;
use OC\Files\Storage\Common;
use OC\ForbiddenException;
use OCP\Constants;
use OCP\Files\ForbiddenException;
use OCP\Files\Storage\IStorage;

/**
Expand All @@ -33,11 +33,11 @@ public function getId(): string {
}

public function mkdir(string $path): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function rmdir(string $path): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function opendir(string $path): IteratorDirectory {
Expand All @@ -53,15 +53,15 @@ public function is_file(string $path): bool {
}

public function stat(string $path): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function filetype(string $path): string|false {
return self::pathIsRoot($path) ? 'dir' : false;
}

public function filesize(string $path): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function isCreatable(string $path): bool {
Expand Down Expand Up @@ -101,27 +101,27 @@ public function filemtime(string $path): int|false {
}

public function file_get_contents(string $path): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function file_put_contents(string $path, mixed $data): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function unlink(string $path): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function rename(string $source, string $target): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function copy(string $source, string $target): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function fopen(string $path, string $mode): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function getMimeType(string $path): string|false {
Expand All @@ -132,15 +132,15 @@ public function getMimeType(string $path): string|false {
}

public function hash(string $type, string $path, bool $raw = false): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function free_space(string $path): int {
return 0;
}

public function touch(string $path, ?int $mtime = null): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function getLocalFile(string $path): string|false {
Expand All @@ -164,11 +164,11 @@ public function getDirectDownloadById(string $fileId): array|false {
}

public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath, bool $preserveMtime = false): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): never {
throw new ForbiddenException('This request is not allowed to access the filesystem');
throw new ForbiddenException('This request is not allowed to access the filesystem', false);
}

public function test(): bool {
Expand Down
16 changes: 5 additions & 11 deletions lib/Versions/CollectiveVersionsExpireManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@
namespace OCA\Collectives\Versions;

use OC\Files\View;
use OC\Hooks\BasicEmitter;
use OC\User\User;
use OCA\Collectives\Db\CollectiveMapper;
use OCA\Collectives\Mount\CollectiveFolderManager;
use OCA\Collectives\Service\MissingDependencyException;
use OCA\Collectives\Service\NotFoundException;
use OCA\Collectives\Service\NotPermittedException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\FileInfo;
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException as FilesNotFoundException;
use OCP\Files\NotPermittedException as FilesNotPermittedException;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserManager;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;

class CollectiveVersionsExpireManager extends BasicEmitter {
class CollectiveVersionsExpireManager {
private ?VersionsBackend $versionsBackend = null;
private string $dependencyInjectionError = '';

Expand All @@ -39,7 +36,7 @@ public function __construct(
private readonly IDBConnection $connection,
private readonly CollectiveMapper $collectiveMapper,
private readonly ITimeFactory $timeFactory,
private readonly IEventDispatcher $dispatcher,
private readonly IUserManager $userManager,
) {
try {
$this->versionsBackend = $appContainer->get(VersionsBackend::class);
Expand Down Expand Up @@ -76,7 +73,6 @@ public function getAllFolders(): array {
public function expireAll(): void {
$folders = $this->getAllFolders();
foreach ($folders as $folder) {
$this->emit(self::class, 'enterFolder', [$folder]);
$this->expireFolder($folder);
}
}
Expand All @@ -100,8 +96,8 @@ public function expireFolder(array $folder): void {
} catch (FilesNotPermittedException $e) {
throw new NotPermittedException($e->getMessage(), 0, $e);
}
/** @var IUser $dummyUser */
$dummyUser = new User('', null, $this->dispatcher);
// Versions are not user-specific; the user object is never resolved against a backend.
$dummyUser = $this->userManager->getExistingUser('');
foreach ($files as $fileId => $file) {
if ($file instanceof FileInfo) {
// Some versions could have been lost during move operations across storage.
Expand All @@ -119,12 +115,10 @@ public function expireFolder(array $folder): void {
$expireVersions = $this->expireManager->getExpiredVersion($versions, $this->timeFactory->getTime(), false);
foreach ($expireVersions as $version) {
/** @var CollectiveVersion $version */
$this->emit(self::class, 'deleteVersion', [$version]);
$view->unlink('/' . $fileId . '/' . $version->getVersionFile()->getName());
}
} else {
// source file no longer exists
$this->emit(self::class, 'deleteFile', [$fileId]);
try {
$this->versionsBackend->deleteAllVersionsForFile($folder['id'], $fileId);
} catch (FilesNotPermittedException|InvalidPathException $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@

namespace BackgroundJob;

use OC\User\User;
use OCA\Collectives\Db\CollectiveMapper;
use OCA\Collectives\Mount\CollectiveFolderManager;
use OCA\Collectives\Versions\CollectiveVersionsExpireManager;
use OCA\Collectives\Versions\ExpireManager;
use OCA\Collectives\Versions\VersionsBackend;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\FileInfo;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Container\ContainerInterface;
use Test\TestCase;
Expand All @@ -31,24 +31,28 @@ public function setUp(): void {
$this->versionsBackend = $this->createMock(VersionsBackend::class);
}

public function testEventDispatcherFallback() {
public function testExpireFolderUsesDummyUser(): void {
$appContainer = $this->createMock(ContainerInterface::class);
$appContainer->method('get')
->with(VersionsBackend::class)
->willReturn($this->versionsBackend);

$dummyUser = $this->createMock(IUser::class);
$userManager = $this->createMock(IUserManager::class);
$userManager->expects($this->once())
->method('getExistingUser')
->with('')
->willReturn($dummyUser);

$file = $this->createMock(FileInfo::class);
$file->method('getPath')->willReturn('/path/to/file.txt');
$file->method('getName')->willReturn('file.txt');
$this->versionsBackend->method('getAllVersionedFiles')
->willReturn([$file]);
$this->versionsBackend->expects($this->once())
->method('getVersionsForFile')
->willReturnCallback(function ($user, $passedFile) use ($file) {
self::assertEquals($file, $passedFile);
self::assertInstanceOf(User::class, $user);
return [];
});
->with($dummyUser, $file)
->willReturn([]);

$manager = new CollectiveVersionsExpireManager(
$appContainer,
Expand All @@ -57,7 +61,7 @@ public function testEventDispatcherFallback() {
$this->createMock(IDBConnection::class),
$this->createMock(CollectiveMapper::class),
$this->createMock(ITimeFactory::class),
$this->createMock(IEventDispatcher::class),
$userManager,
);
$manager->expireFolder(['id' => 123]);
}
Expand Down
82 changes: 82 additions & 0 deletions tests/Unit/Mount/CollectivesUserStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Unit\Mount;

use OCA\Collectives\Mount\CollectivesUserStorage;
use OCP\Constants;
use OCP\Files\ForbiddenException;
use OCP\Files\Storage\IStorage;
use Test\TestCase;

class CollectivesUserStorageTest extends TestCase {
private CollectivesUserStorage $storage;

protected function setUp(): void {
parent::setUp();
$this->storage = new CollectivesUserStorage([]);
}

public static function forbiddenOperationsProvider(): array {
return [
'mkdir' => ['mkdir', ['/foo']],
'rmdir' => ['rmdir', ['/foo']],
'stat' => ['stat', ['/foo']],
'filesize' => ['filesize', ['/foo']],
'file_get_contents' => ['file_get_contents', ['/foo']],
'file_put_contents' => ['file_put_contents', ['/foo', 'bar']],
'unlink' => ['unlink', ['/foo']],
'rename' => ['rename', ['/foo', '/bar']],
'copy' => ['copy', ['/foo', '/bar']],
'fopen' => ['fopen', ['/foo', 'r']],
'hash' => ['hash', ['md5', '/foo']],
'touch' => ['touch', ['/foo']],
];
}

/**
* @dataProvider forbiddenOperationsProvider
*/
public function testForbiddenOperationThrows(string $method, array $arguments): void {
try {
$this->storage->$method(...$arguments);
self::fail("$method did not throw");
} catch (ForbiddenException $e) {
self::assertFalse($e->getRetry());
}
}

public function testCrossStorageOperationsThrow(): void {
$sourceStorage = $this->createMock(IStorage::class);
foreach (['copyFromStorage', 'moveFromStorage'] as $method) {
try {
$this->storage->$method($sourceStorage, 'foo', 'bar');
self::fail("$method did not throw");
} catch (ForbiddenException $e) {
self::assertFalse($e->getRetry());
}
}
}

public function testRootIsReadOnlyDirectory(): void {
self::assertTrue($this->storage->is_dir('/'));
self::assertTrue($this->storage->file_exists(''));
self::assertSame('dir', $this->storage->filetype('/'));
self::assertSame(Constants::PERMISSION_READ, $this->storage->getPermissions('/'));
self::assertSame('httpd/unix-directory', $this->storage->getMimeType('/'));
}

public function testNonRootPathDoesNotExist(): void {
self::assertFalse($this->storage->is_dir('/foo'));
self::assertFalse($this->storage->file_exists('/foo'));
self::assertFalse($this->storage->filetype('/foo'));
self::assertSame(0, $this->storage->getPermissions('/foo'));
self::assertFalse($this->storage->getMimeType('/foo'));
}
}
2 changes: 0 additions & 2 deletions tests/stub.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ namespace OC\Hooks {
/** Closure $closure */
public function listen(string $class, string $value, $closure) {}
}
class BasicEmitter extends Emitter {
}
}

namespace OC\Cache {
Expand Down
Loading