Skip to content

Commit

Permalink
feat: add appconfig value to disable fixed userfolder permissions opt…
Browse files Browse the repository at this point in the history
…imization

Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Feb 28, 2025
1 parent 7bab703 commit c426b9c
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 39 deletions.
20 changes: 12 additions & 8 deletions lib/private/Files/Node/LazyUserFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ class LazyUserFolder extends LazyFolder {
private string $path;
private IMountManager $mountManager;

public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager $mountManager) {
public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager $mountManager, bool $useDefaultPermissions = true) {
$this->user = $user;
$this->mountManager = $mountManager;
$this->path = '/' . $user->getUID() . '/files';
$data = [
'path' => $this->path,
// Sharing user root folder is not allowed
'type' => FileInfo::TYPE_FOLDER,
'mimetype' => FileInfo::MIMETYPE_FOLDER,
];
if ($useDefaultPermissions) {
$data['permissions'] = Constants::PERMISSION_ALL ^ Constants::PERMISSION_SHARE;
}

parent::__construct($rootFolder, function () use ($user): Folder {
try {
$node = $this->getRootFolder()->get($this->path);
Expand All @@ -44,13 +54,7 @@ public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager
}
return $this->getRootFolder()->newFolder($this->path);
}
}, [
'path' => $this->path,
// Sharing user root folder is not allowed
'permissions' => Constants::PERMISSION_ALL ^ Constants::PERMISSION_SHARE,
'type' => FileInfo::TYPE_FOLDER,
'mimetype' => FileInfo::MIMETYPE_FOLDER,
]);
}, $data);
}

public function getMountPoint() {
Expand Down
6 changes: 5 additions & 1 deletion lib/private/Files/Node/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use OCP\Files\Node as INode;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IAppConfig;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IUser;
Expand Down Expand Up @@ -60,6 +61,7 @@ class Root extends Folder implements IRootFolder {
private IUserManager $userManager;
private IEventDispatcher $eventDispatcher;
private ICache $pathByIdCache;
private bool $homeFolderOverwritten = false;

/**
* @param Manager $manager
Expand All @@ -75,6 +77,7 @@ public function __construct(
IUserManager $userManager,
IEventDispatcher $eventDispatcher,
ICacheFactory $cacheFactory,
IAppConfig $appConfig,
) {
parent::__construct($this, $view, '');
$this->mountManager = $manager;
Expand All @@ -88,6 +91,7 @@ public function __construct(
$this->userFolderCache = new CappedMemoryCache();
});
$this->pathByIdCache = $cacheFactory->createLocal('path-by-id');
$this->homeFolderOverwritten = $appConfig->getValueBool('files', 'homeFolderOverwritten');
}

/**
Expand Down Expand Up @@ -367,7 +371,7 @@ public function getUserFolder($userId) {
$folder = $this->newFolder('/' . $userId . '/files');
}
} else {
$folder = new LazyUserFolder($this, $userObject, $this->mountManager);
$folder = new LazyUserFolder($this, $userObject, $this->mountManager, !$this->homeFolderOverwritten);
}

$this->userFolderCache->set($userId, $folder);
Expand Down
1 change: 1 addition & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ public function __construct($webRoot, \OC\Config $config) {
$this->get(IUserManager::class),
$this->get(IEventDispatcher::class),
$this->get(ICacheFactory::class),
$this->get(IAppConfig::class),
);

$previewConnector = new \OC\Preview\WatcherConnector(
Expand Down
15 changes: 10 additions & 5 deletions tests/lib/Files/Node/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function getViewDeleteMethod() {
public function testGetContent(): void {
/** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory])
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory, $this->appConfig])
->getMock();

$hook = function ($file) {
Expand Down Expand Up @@ -68,7 +68,7 @@ public function testGetContentNotPermitted(): void {

/** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory])
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory, $this->appConfig])
->getMock();

$root->expects($this->any())
Expand All @@ -87,7 +87,7 @@ public function testGetContentNotPermitted(): void {
public function testPutContent(): void {
/** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory])
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory, $this->appConfig])
->getMock();

$root->expects($this->any())
Expand All @@ -114,7 +114,7 @@ public function testPutContentNotPermitted(): void {

/** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory])
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory, $this->appConfig])
->getMock();

$this->view->expects($this->once())
Expand All @@ -129,7 +129,7 @@ public function testPutContentNotPermitted(): void {
public function testGetMimeType(): void {
/** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory])
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory, $this->appConfig])
->getMock();

$this->view->expects($this->once())
Expand All @@ -155,6 +155,7 @@ public function testFOpenRead(): void {
$this->userManager,
$this->eventDispatcher,
$this->cacheFactory,
$this->appConfig,
);

$hook = function ($file) {
Expand Down Expand Up @@ -192,6 +193,7 @@ public function testFOpenWrite(): void {
$this->userManager,
$this->eventDispatcher,
$this->cacheFactory,
$this->appConfig,
);
$hooksCalled = 0;
$hook = function ($file) use (&$hooksCalled) {
Expand Down Expand Up @@ -233,6 +235,7 @@ public function testFOpenReadNotPermitted(): void {
$this->userManager,
$this->eventDispatcher,
$this->cacheFactory,
$this->appConfig,
);
$hook = function ($file) {
throw new \Exception('Hooks are not supposed to be called');
Expand Down Expand Up @@ -260,6 +263,7 @@ public function testFOpenReadWriteNoReadPermissions(): void {
$this->userManager,
$this->eventDispatcher,
$this->cacheFactory,
$this->appConfig,
);
$hook = function () {
throw new \Exception('Hooks are not supposed to be called');
Expand Down Expand Up @@ -287,6 +291,7 @@ public function testFOpenReadWriteNoWritePermissions(): void {
$this->userManager,
$this->eventDispatcher,
$this->cacheFactory,
$this->appConfig,
);
$hook = function () {
throw new \Exception('Hooks are not supposed to be called');
Expand Down
Loading

0 comments on commit c426b9c

Please sign in to comment.