diff --git a/lib/Folder/FolderManager.php b/lib/Folder/FolderManager.php index 3335b70c9..8c7bdda82 100644 --- a/lib/Folder/FolderManager.php +++ b/lib/Folder/FolderManager.php @@ -18,6 +18,7 @@ use OCA\GroupFolders\ACL\UserMapping\IUserMapping; use OCA\GroupFolders\ACL\UserMapping\IUserMappingManager; use OCA\GroupFolders\ACL\UserMapping\UserMapping; +use OCA\GroupFolders\AppInfo\Application; use OCA\GroupFolders\Mount\FolderStorageManager; use OCA\GroupFolders\Mount\GroupMountPoint; use OCA\GroupFolders\ResponseDefinitions; @@ -29,6 +30,7 @@ use OCP\Files\FileInfo; use OCP\Files\IMimeTypeLoader; use OCP\Files\IRootFolder; +use OCP\IAppConfig; use OCP\IConfig; use OCP\IDBConnection; use OCP\IGroup; @@ -64,6 +66,7 @@ public function __construct( private readonly IConfig $config, private readonly IUserMappingManager $userMappingManager, private readonly FolderStorageManager $folderStorageManager, + private readonly IAppConfig $appConfig, ) { } @@ -679,6 +682,8 @@ public function createFolder(string $mountPoint): int { $this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('A new groupfolder "%s" was created with id %d', [$mountPoint, $id])); + $this->updateOverwriteHomeFolders(); + return $id; } @@ -790,6 +795,8 @@ public function removeFolder(int $folderId): void { $query->executeStatement(); $this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('The groupfolder with id %d was removed', [$folderId])); + + $this->updateOverwriteHomeFolders(); } /** @@ -818,6 +825,8 @@ public function renameFolder(int $folderId, string $newMountPoint): void { $query->executeStatement(); $this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('The groupfolder with id %d was renamed to "%s"', [$folderId, $newMountPoint])); + + $this->updateOverwriteHomeFolders(); } /** @@ -983,4 +992,33 @@ private function getRealQuota(int $quota): int { return $quota; } + + /** + * Check if any mountpoint is configured that overwrite the home folder + */ + private function hasHomeFolderOverwriteMount(): bool { + $builder = $this->connection->getQueryBuilder(); + $query = $builder->select('folder_id') + ->from('group_folders') + ->where($builder->expr()->eq('mount_point', $builder->createNamedParameter('/'))) + ->setMaxResults(1); + $result = $query->executeQuery(); + return $result->rowCount() > 0; + } + + public function updateOverwriteHomeFolders(): void { + $appIdsList = $this->appConfig->getValueArray('files', 'overwrites_home_folders'); + + if ($this->hasHomeFolderOverwriteMount()) { + if (!in_array(Application::APP_ID, $appIdsList)) { + $appIdsList[] = Application::APP_ID; + $this->appConfig->setValueArray('files', 'overwrites_home_folders', $appIdsList); + } + } else { + if (in_array(Application::APP_ID, $appIdsList)) { + $appIdsList = array_values(array_filter($appIdsList, fn ($v) => $v !== Application::APP_ID)); + $this->appConfig->setValueArray('files', 'overwrites_home_folders', $appIdsList); + } + } + } } diff --git a/lib/Migration/Version21000Date20250925152053.php b/lib/Migration/Version21000Date20250925152053.php new file mode 100644 index 000000000..5c1a96d6f --- /dev/null +++ b/lib/Migration/Version21000Date20250925152053.php @@ -0,0 +1,37 @@ +folderManager->updateOverwriteHomeFolders(); + } +} diff --git a/tests/Folder/FolderManagerTest.php b/tests/Folder/FolderManagerTest.php index 6d8ba943d..e644facb8 100644 --- a/tests/Folder/FolderManagerTest.php +++ b/tests/Folder/FolderManagerTest.php @@ -19,6 +19,7 @@ use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\FileInfo; use OCP\Files\IMimeTypeLoader; +use OCP\IAppConfig; use OCP\IConfig; use OCP\IDBConnection; use OCP\IGroupManager; @@ -42,6 +43,7 @@ class FolderManagerTest extends TestCase { private IConfig&MockObject $config; private IUserMappingManager&MockObject $userMappingManager; private FolderStorageManager $folderStorageManager; + private IAppConfig $appConfig; protected function setUp(): void { parent::setUp(); @@ -52,7 +54,9 @@ protected function setUp(): void { $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->config = $this->createMock(IConfig::class); $this->userMappingManager = $this->createMock(IUserMappingManager::class); + $this->userMappingManager = $this->createMock(IUserMappingManager::class); $this->folderStorageManager = Server::get(FolderStorageManager::class); + $this->appConfig = Server::get(IAppConfig::class); $this->manager = new FolderManager( Server::get(IDBConnection::class), @@ -63,6 +67,7 @@ protected function setUp(): void { $this->config, $this->userMappingManager, $this->folderStorageManager, + $this->appConfig, ); $this->clean(); } @@ -363,7 +368,7 @@ public function testGetFoldersForUserEmpty(): void { public function testGetFoldersForUserSimple(): void { $db = $this->createMock(IDBConnection::class); $manager = $this->getMockBuilder(FolderManager::class) - ->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config, $this->userMappingManager, $this->folderStorageManager]) + ->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config, $this->userMappingManager, $this->folderStorageManager, $this->appConfig]) ->onlyMethods(['getFoldersForGroups']) ->getMock(); @@ -392,7 +397,7 @@ public function testGetFoldersForUserSimple(): void { public function testGetFoldersForUserMerge(): void { $db = $this->createMock(IDBConnection::class); $manager = $this->getMockBuilder(FolderManager::class) - ->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config, $this->userMappingManager, $this->folderStorageManager]) + ->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config, $this->userMappingManager, $this->folderStorageManager, $this->appConfig]) ->onlyMethods(['getFoldersForGroups']) ->getMock(); @@ -443,7 +448,7 @@ public function testGetFoldersForUserMerge(): void { public function testGetFolderPermissionsForUserMerge(): void { $db = $this->createMock(IDBConnection::class); $manager = $this->getMockBuilder(FolderManager::class) - ->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config, $this->userMappingManager, $this->folderStorageManager]) + ->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config, $this->userMappingManager, $this->folderStorageManager, $this->appConfig]) ->onlyMethods(['getFoldersForGroups']) ->getMock();