Skip to content

Commit

Permalink
Merge pull request #31265 from nextcloud/fs-limited-setup
Browse files Browse the repository at this point in the history
Fine grained filesystem setup
  • Loading branch information
PVince81 authored Mar 24, 2022
2 parents 00076c0 + 91ab4e1 commit a7e778b
Show file tree
Hide file tree
Showing 25 changed files with 561 additions and 74 deletions.
1 change: 0 additions & 1 deletion apps/dav/lib/Connector/Sabre/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ private function auth(RequestInterface $request, ResponseInterface $response) {
\OC_User::handleApacheAuth()
) {
$user = $this->userSession->getUser()->getUID();
\OC_Util::setupFS($user);
$this->currentUser = $user;
$this->session->close();
return [true, $this->principalPrefix . $user];
Expand Down
2 changes: 1 addition & 1 deletion apps/dav/lib/Connector/Sabre/ServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
namespace OCA\DAV\Connector\Sabre;

use OC\Files\Node\Folder;
use OCP\Files\Folder;
use OCA\DAV\AppInfo\PluginManager;
use OCA\DAV\Files\BrowserErrorPagePlugin;
use OCP\Files\Mount\IMountManager;
Expand Down
16 changes: 14 additions & 2 deletions apps/files_external/lib/Service/StoragesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
use OCA\Files_External\Lib\DefinitionParameter;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\NotFoundException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\Events\InvalidateMountCacheEvent;
use OCP\Files\StorageNotAvailableException;
use OCP\ILogger;

Expand All @@ -62,15 +64,24 @@ abstract class StoragesService {
*/
protected $userMountCache;

protected IEventDispatcher $eventDispatcher;

/**
* @param BackendService $backendService
* @param DBConfigService $dbConfigService
* @param IUserMountCache $userMountCache
* @param IEventDispatcher $eventDispatcher
*/
public function __construct(BackendService $backendService, DBConfigService $dbConfigService, IUserMountCache $userMountCache) {
public function __construct(
BackendService $backendService,
DBConfigService $dbConfigService,
IUserMountCache $userMountCache,
IEventDispatcher $eventDispatcher
) {
$this->backendService = $backendService;
$this->dbConfig = $dbConfigService;
$this->userMountCache = $userMountCache;
$this->eventDispatcher = $eventDispatcher;
}

protected function readDBConfig() {
Expand Down Expand Up @@ -338,7 +349,8 @@ public function createStorage(
* @param string $mountType hook mount type param
* @param array $applicableArray array of applicable users/groups for which to trigger the hook
*/
protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray) {
protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray): void {
$this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent(null));
foreach ($applicableArray as $applicable) {
\OCP\Util::emitHook(
Filesystem::CLASSNAME,
Expand Down
7 changes: 5 additions & 2 deletions apps/files_external/lib/Service/UserGlobalStoragesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace OCA\Files_External\Service;

use OCA\Files_External\Lib\StorageConfig;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Config\IUserMountCache;
use OCP\IGroupManager;
use OCP\IUser;
Expand All @@ -45,15 +46,17 @@ class UserGlobalStoragesService extends GlobalStoragesService {
* @param IUserSession $userSession
* @param IGroupManager $groupManager
* @param IUserMountCache $userMountCache
* @param IEventDispatcher $eventDispatcher
*/
public function __construct(
BackendService $backendService,
DBConfigService $dbConfig,
IUserSession $userSession,
IGroupManager $groupManager,
IUserMountCache $userMountCache
IUserMountCache $userMountCache,
IEventDispatcher $eventDispatcher
) {
parent::__construct($backendService, $dbConfig, $userMountCache);
parent::__construct($backendService, $dbConfig, $userMountCache, $eventDispatcher);
$this->userSession = $userSession;
$this->groupManager = $groupManager;
}
Expand Down
7 changes: 5 additions & 2 deletions apps/files_external/lib/Service/UserStoragesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\NotFoundException;

use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Config\IUserMountCache;
use OCP\IUserSession;

Expand All @@ -49,15 +50,17 @@ class UserStoragesService extends StoragesService {
* @param DBConfigService $dbConfig
* @param IUserSession $userSession user session
* @param IUserMountCache $userMountCache
* @param IEventDispatcher $eventDispatcher
*/
public function __construct(
BackendService $backendService,
DBConfigService $dbConfig,
IUserSession $userSession,
IUserMountCache $userMountCache
IUserMountCache $userMountCache,
IEventDispatcher $eventDispatcher
) {
$this->userSession = $userSession;
parent::__construct($backendService, $dbConfig, $userMountCache);
parent::__construct($backendService, $dbConfig, $userMountCache, $eventDispatcher);
}

protected function readDBConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
class GlobalStoragesServiceTest extends StoragesServiceTest {
protected function setUp(): void {
parent::setUp();
$this->service = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache);
$this->service = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache, $this->eventDispatcher);
}

protected function tearDown(): void {
Expand Down
7 changes: 7 additions & 0 deletions apps/files_external/tests/Service/StoragesServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use OCA\Files_External\Service\DBConfigService;
use OCA\Files_External\Service\StoragesService;
use OCP\AppFramework\IAppContainer;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\ICache;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\Mount\IMountPoint;
Expand Down Expand Up @@ -96,6 +97,11 @@ abstract class StoragesServiceTest extends \Test\TestCase {
*/
protected $mountCache;

/**
* @var \PHPUnit\Framework\MockObject\MockObject|IEventDispatcher
*/
protected IEventDispatcher $eventDispatcher;

protected function setUp(): void {
parent::setUp();
$this->dbConfig = new CleaningDBConfig(\OC::$server->getDatabaseConnection(), \OC::$server->getCrypto());
Expand All @@ -108,6 +114,7 @@ protected function setUp(): void {
\OCA\Files_External\MountConfig::$skipTest = true;

$this->mountCache = $this->createMock(IUserMountCache::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);

// prepare BackendService mock
$this->backendService =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ protected function setUp(): void {
$this->dbConfig,
$userSession,
$this->groupManager,
$this->mountCache
$this->mountCache,
$this->eventDispatcher,
);
}

Expand Down
4 changes: 2 additions & 2 deletions apps/files_external/tests/Service/UserStoragesServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class UserStoragesServiceTest extends StoragesServiceTest {
protected function setUp(): void {
parent::setUp();

$this->globalStoragesService = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache);
$this->globalStoragesService = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache, $this->eventDispatcher);

$this->userId = $this->getUniqueID('user_');
$this->createUser($this->userId, $this->userId);
Expand All @@ -67,7 +67,7 @@ protected function setUp(): void {
->method('getUser')
->willReturn($this->user);

$this->service = new UserStoragesService($this->backendService, $this->dbConfig, $userSession, $this->mountCache);
$this->service = new UserStoragesService($this->backendService, $this->dbConfig, $userSession, $this->mountCache, $this->eventDispatcher);
}

private function makeTestStorageData() {
Expand Down
4 changes: 3 additions & 1 deletion apps/files_sharing/lib/MountProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
],
$loader,
$view,
$foldersExistCache
$foldersExistCache,
$this->eventDispatcher,
$user
);

$event = new ShareMountedEvent($mount);
Expand Down
42 changes: 23 additions & 19 deletions apps/files_sharing/lib/SharedMount.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
use OC\Files\Mount\MoveableMount;
use OC\Files\View;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\InvalidateMountCacheEvent;
use OCP\Files\Storage\IStorageFactory;
use OCP\IUser;
use OCP\Share\Events\VerifyMountPointEvent;

/**
Expand All @@ -51,33 +53,35 @@ class SharedMount extends MountPoint implements MoveableMount {
*/
private $recipientView;

/**
* @var string
*/
private $user;
private IUser $user;

/** @var \OCP\Share\IShare */
private $superShare;

/** @var \OCP\Share\IShare[] */
private $groupedShares;

/**
* @param string $storage
* @param SharedMount[] $mountpoints
* @param array $arguments
* @param IStorageFactory $loader
* @param View $recipientView
*/
public function __construct($storage, array $mountpoints, $arguments, IStorageFactory $loader, View $recipientView, CappedMemoryCache $folderExistCache) {
$this->user = $arguments['user'];
private IEventDispatcher $eventDispatcher;

public function __construct(
$storage,
array $mountpoints,
$arguments,
IStorageFactory $loader,
View $recipientView,
CappedMemoryCache $folderExistCache,
IEventDispatcher $eventDispatcher,
IUser $user
) {
$this->user = $user;
$this->recipientView = $recipientView;
$this->eventDispatcher = $eventDispatcher;

$this->superShare = $arguments['superShare'];
$this->groupedShares = $arguments['groupedShares'];

$newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache);
$absMountPoint = '/' . $this->user . '/files' . $newMountPoint;
$absMountPoint = '/' . $user->getUID() . '/files' . $newMountPoint;
parent::__construct($storage, $absMountPoint, $arguments, $loader, null, null, MountProvider::class);
}

Expand All @@ -93,9 +97,7 @@ private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints,
$parent = dirname($share->getTarget());

$event = new VerifyMountPointEvent($share, $this->recipientView, $parent);
/** @var IEventDispatcher $dispatcher */
$dispatcher = \OC::$server->query(IEventDispatcher::class);
$dispatcher->dispatchTyped($event);
$this->eventDispatcher->dispatchTyped($event);
$parent = $event->getParent();

if ($folderExistCache->hasKey($parent)) {
Expand All @@ -105,7 +107,7 @@ private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints,
$folderExistCache->set($parent, $parentExists);
}
if (!$parentExists) {
$parent = Helper::getShareFolder($this->recipientView, $this->user);
$parent = Helper::getShareFolder($this->recipientView, $this->user->getUID());
}

$newMountPoint = $this->generateUniqueTarget(
Expand Down Expand Up @@ -133,8 +135,10 @@ private function updateFileTarget($newPath, &$share) {

foreach ($this->groupedShares as $tmpShare) {
$tmpShare->setTarget($newPath);
\OC::$server->getShareManager()->moveShare($tmpShare, $this->user);
\OC::$server->getShareManager()->moveShare($tmpShare, $this->user->getUID());
}

$this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent($this->user));
}


Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
'OCP\\Files\\Events\\FileCacheUpdated' => $baseDir . '/lib/public/Files/Events/FileCacheUpdated.php',
'OCP\\Files\\Events\\FileScannedEvent' => $baseDir . '/lib/public/Files/Events/FileScannedEvent.php',
'OCP\\Files\\Events\\FolderScannedEvent' => $baseDir . '/lib/public/Files/Events/FolderScannedEvent.php',
'OCP\\Files\\Events\\InvalidateMountCacheEvent' => $baseDir . '/lib/public/Files/Events/InvalidateMountCacheEvent.php',
'OCP\\Files\\Events\\NodeAddedToCache' => $baseDir . '/lib/public/Files/Events/NodeAddedToCache.php',
'OCP\\Files\\Events\\NodeRemovedFromCache' => $baseDir . '/lib/public/Files/Events/NodeRemovedFromCache.php',
'OCP\\Files\\Events\\Node\\AbstractNodeEvent' => $baseDir . '/lib/public/Files/Events/Node/AbstractNodeEvent.php',
Expand Down Expand Up @@ -1141,6 +1142,7 @@
'OC\\Files\\Node\\HookConnector' => $baseDir . '/lib/private/Files/Node/HookConnector.php',
'OC\\Files\\Node\\LazyFolder' => $baseDir . '/lib/private/Files/Node/LazyFolder.php',
'OC\\Files\\Node\\LazyRoot' => $baseDir . '/lib/private/Files/Node/LazyRoot.php',
'OC\\Files\\Node\\LazyUserFolder' => $baseDir . '/lib/private/Files/Node/LazyUserFolder.php',
'OC\\Files\\Node\\Node' => $baseDir . '/lib/private/Files/Node/Node.php',
'OC\\Files\\Node\\NonExistingFile' => $baseDir . '/lib/private/Files/Node/NonExistingFile.php',
'OC\\Files\\Node\\NonExistingFolder' => $baseDir . '/lib/private/Files/Node/NonExistingFolder.php',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Files\\Events\\FileCacheUpdated' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileCacheUpdated.php',
'OCP\\Files\\Events\\FileScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileScannedEvent.php',
'OCP\\Files\\Events\\FolderScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FolderScannedEvent.php',
'OCP\\Files\\Events\\InvalidateMountCacheEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/InvalidateMountCacheEvent.php',
'OCP\\Files\\Events\\NodeAddedToCache' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeAddedToCache.php',
'OCP\\Files\\Events\\NodeRemovedFromCache' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeRemovedFromCache.php',
'OCP\\Files\\Events\\Node\\AbstractNodeEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/AbstractNodeEvent.php',
Expand Down Expand Up @@ -1170,6 +1171,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Files\\Node\\HookConnector' => __DIR__ . '/../../..' . '/lib/private/Files/Node/HookConnector.php',
'OC\\Files\\Node\\LazyFolder' => __DIR__ . '/../../..' . '/lib/private/Files/Node/LazyFolder.php',
'OC\\Files\\Node\\LazyRoot' => __DIR__ . '/../../..' . '/lib/private/Files/Node/LazyRoot.php',
'OC\\Files\\Node\\LazyUserFolder' => __DIR__ . '/../../..' . '/lib/private/Files/Node/LazyUserFolder.php',
'OC\\Files\\Node\\Node' => __DIR__ . '/../../..' . '/lib/private/Files/Node/Node.php',
'OC\\Files\\Node\\NonExistingFile' => __DIR__ . '/../../..' . '/lib/private/Files/Node/NonExistingFile.php',
'OC\\Files\\Node\\NonExistingFolder' => __DIR__ . '/../../..' . '/lib/private/Files/Node/NonExistingFolder.php',
Expand Down
34 changes: 25 additions & 9 deletions lib/private/Files/Config/MountProviderCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,15 @@ public function __construct(IStorageFactory $loader, IUserMountCache $mountCache
}

/**
* Get all configured mount points for the user
*
* @param \OCP\IUser $user
* @return \OCP\Files\Mount\IMountPoint[]
* @param IUser $user
* @param IMountProvider[] $providers
* @return IMountPoint[]
*/
public function getMountsForUser(IUser $user) {
private function getUserMountsForProviders(IUser $user, array $providers): array {
$loader = $this->loader;
$mounts = array_map(function (IMountProvider $provider) use ($user, $loader) {
return $provider->getMountsForUser($user, $loader);
}, $this->providers);
}, $providers);
$mounts = array_filter($mounts, function ($result) {
return is_array($result);
});
Expand All @@ -94,14 +93,31 @@ public function getMountsForUser(IUser $user) {
return $this->filterMounts($user, $mounts);
}

public function addMountForUser(IUser $user, IMountManager $mountManager) {
public function getMountsForUser(IUser $user): array {
return $this->getUserMountsForProviders($user, $this->providers);
}

public function getUserMountsForProviderClass(IUser $user, string $mountProviderClass): array {
$providers = array_filter(
$this->providers,
fn (IMountProvider $mountProvider) => (get_class($mountProvider) === $mountProviderClass)
);
return $this->getUserMountsForProviders($user, $providers);
}

public function addMountForUser(IUser $user, IMountManager $mountManager, callable $providerFilter = null) {
// shared mount provider gets to go last since it needs to know existing files
// to check for name collisions
$firstMounts = [];
$firstProviders = array_filter($this->providers, function (IMountProvider $provider) {
if ($providerFilter) {
$providers = array_filter($this->providers, $providerFilter);
} else {
$providers = $this->providers;
}
$firstProviders = array_filter($providers, function (IMountProvider $provider) {
return (get_class($provider) !== 'OCA\Files_Sharing\MountProvider');
});
$lastProviders = array_filter($this->providers, function (IMountProvider $provider) {
$lastProviders = array_filter($providers, function (IMountProvider $provider) {
return (get_class($provider) === 'OCA\Files_Sharing\MountProvider');
});
foreach ($firstProviders as $provider) {
Expand Down
Loading

0 comments on commit a7e778b

Please sign in to comment.