diff --git a/lib/Service/FolderService.php b/lib/Service/FolderService.php index ce24873401..0e056221f4 100644 --- a/lib/Service/FolderService.php +++ b/lib/Service/FolderService.php @@ -34,6 +34,7 @@ use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; +use OCP\IGroupManager; use OCP\IL10N; use OCP\IUser; @@ -43,6 +44,7 @@ public function __construct( private IRootFolder $root, private IUserMountCache $userMountCache, protected IAppDataFactory $appDataFactory, + protected IGroupManager $groupManager, private IAppConfig $appConfig, private IL10N $l10n, private ?string $userId, @@ -91,14 +93,26 @@ public function getFolder(int $nodeId = null): Folder { */ private function getOrCreateFolder(): Folder { $path = $this->getLibreSignDefaultPath(); + $withoutPermission = false; if ($this->getUserId()) { $containerFolder = $this->root->getUserFolder($this->getUserId()); + // TODO: retrieve guest group name from app once exposed + if ($this->groupManager->isInGroup($this->getUserId(), 'guest_app')) { + $withoutPermission = true; + } elseif (!$containerFolder->isUpdateable()) { + $withoutPermission = true; + } } else { + $withoutPermission = true; + } + if ($withoutPermission) { $containerFolder = $this->appData->getFolder('/'); $reflection = new \ReflectionClass($containerFolder); $reflectionProperty = $reflection->getProperty('folder'); $reflectionProperty->setAccessible(true); $containerFolder = $reflectionProperty->getValue($containerFolder); + } else { + $containerFolder = $this->root->getUserFolder($this->getUserId()); } if ($containerFolder->nodeExists($path)) { $folder = $containerFolder->get($path); @@ -115,6 +129,10 @@ public function getLibreSignDefaultPath(): string { if (!$this->userId) { return 'unauthenticated'; } + // TODO: retrieve guest group name from app once exposed + if ($this->groupManager->isInGroup($this->getUserId(), 'guest_app')) { + return 'guest_app/' . $this->getUserId(); + } $path = $this->appConfig->getUserValue($this->userId, 'folder'); if (empty($path)) { diff --git a/tests/Unit/Service/FolderServiceTest.php b/tests/Unit/Service/FolderServiceTest.php index 69d918561f..4f3d7b429c 100644 --- a/tests/Unit/Service/FolderServiceTest.php +++ b/tests/Unit/Service/FolderServiceTest.php @@ -7,6 +7,7 @@ use OCP\Files\AppData\IAppDataFactory; use OCP\Files\Config\IUserMountCache; use OCP\Files\IRootFolder; +use OCP\IGroupManager; use OCP\IL10N; final class FolderServiceTest extends \OCA\Libresign\Tests\Unit\TestCase { @@ -21,6 +22,7 @@ public function testGetFolderWithInvalidNodeId() { ->method('getMountsForFileId') ->willreturn([]); $appDataFactory = $this->createMock(IAppDataFactory::class); + $groupManager = $this->createMock(IGroupManager::class); $appConfig = $this->createMock(IAppConfig::class); $l10n = $this->createMock(IL10N::class); @@ -28,6 +30,7 @@ public function testGetFolderWithInvalidNodeId() { $root, $userMountCache, $appDataFactory, + $groupManager, $appConfig, $l10n, 171 @@ -49,6 +52,7 @@ public function testGetFolderWithValidNodeId() { $root->method('getById') ->willReturn([$node]); $appDataFactory = $this->createMock(IAppDataFactory::class); + $groupManager = $this->createMock(IGroupManager::class); $appConfig = $this->createMock(IAppConfig::class); $l10n = $this->createMock(IL10N::class); @@ -56,6 +60,7 @@ public function testGetFolderWithValidNodeId() { $root, $userMountCache, $appDataFactory, + $groupManager, $appConfig, $l10n, 1