Skip to content

Commit f8862db

Browse files
artongebackportbot[bot]
authored andcommitted
feat(files): Mark homefolder as overwritten when an external storage mounted at / exists
Signed-off-by: Robin Appelman <robin@icewind.nl> Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent 61e5281 commit f8862db

11 files changed

Lines changed: 96 additions & 26 deletions

apps/files_external/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
'OCA\\Files_External\\Migration\\Version1011Date20200630192246' => $baseDir . '/../lib/Migration/Version1011Date20200630192246.php',
108108
'OCA\\Files_External\\Migration\\Version1015Date20211104103506' => $baseDir . '/../lib/Migration/Version1015Date20211104103506.php',
109109
'OCA\\Files_External\\Migration\\Version1016Date20220324154536' => $baseDir . '/../lib/Migration/Version1016Date20220324154536.php',
110+
'OCA\\Files_External\\Migration\\Version1025Date20250228162604' => $baseDir . '/../lib/Migration/Version1025Date20250228162604.php',
110111
'OCA\\Files_External\\Migration\\Version22000Date20210216084416' => $baseDir . '/../lib/Migration/Version22000Date20210216084416.php',
111112
'OCA\\Files_External\\MountConfig' => $baseDir . '/../lib/MountConfig.php',
112113
'OCA\\Files_External\\NotFoundException' => $baseDir . '/../lib/NotFoundException.php',

apps/files_external/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class ComposerStaticInitFiles_External
122122
'OCA\\Files_External\\Migration\\Version1011Date20200630192246' => __DIR__ . '/..' . '/../lib/Migration/Version1011Date20200630192246.php',
123123
'OCA\\Files_External\\Migration\\Version1015Date20211104103506' => __DIR__ . '/..' . '/../lib/Migration/Version1015Date20211104103506.php',
124124
'OCA\\Files_External\\Migration\\Version1016Date20220324154536' => __DIR__ . '/..' . '/../lib/Migration/Version1016Date20220324154536.php',
125+
'OCA\\Files_External\\Migration\\Version1025Date20250228162604' => __DIR__ . '/..' . '/../lib/Migration/Version1025Date20250228162604.php',
125126
'OCA\\Files_External\\Migration\\Version22000Date20210216084416' => __DIR__ . '/..' . '/../lib/Migration/Version22000Date20210216084416.php',
126127
'OCA\\Files_External\\MountConfig' => __DIR__ . '/..' . '/../lib/MountConfig.php',
127128
'OCA\\Files_External\\NotFoundException' => __DIR__ . '/..' . '/../lib/NotFoundException.php',
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Files_External\Migration;
11+
12+
use Closure;
13+
use OCA\Files_External\Service\GlobalStoragesService;
14+
use OCP\DB\ISchemaWrapper;
15+
use OCP\IAppConfig;
16+
use OCP\Migration\IOutput;
17+
use OCP\Migration\SimpleMigrationStep;
18+
19+
/**
20+
* Check for any external storage overwriting the home folder
21+
*/
22+
class Version1025Date20250228162604 extends SimpleMigrationStep {
23+
public function __construct(
24+
private GlobalStoragesService $globalStoragesServices,
25+
private IAppConfig $appConfig,
26+
) {
27+
}
28+
29+
/**
30+
* @param Closure(): ISchemaWrapper $schemaClosure
31+
*/
32+
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
33+
$this->globalStoragesServices->updateOverwriteHomeFolders();
34+
}
35+
}

apps/files_external/lib/Service/DBConfigService.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
55
* SPDX-License-Identifier: AGPL-3.0-only
66
*/
7+
78
namespace OCA\Files_External\Service;
89

910
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
@@ -63,16 +64,16 @@ public function getMountsForUser($userId, $groupIds) {
6364
->where($builder->expr()->orX(
6465
$builder->expr()->andX( // global mounts
6566
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GLOBAL, IQueryBuilder::PARAM_INT)),
66-
$builder->expr()->isNull('a.value')
67+
$builder->expr()->isNull('a.value'),
6768
),
6869
$builder->expr()->andX( // mounts for user
6970
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_USER, IQueryBuilder::PARAM_INT)),
70-
$builder->expr()->eq('a.value', $builder->createNamedParameter($userId))
71+
$builder->expr()->eq('a.value', $builder->createNamedParameter($userId)),
7172
),
7273
$builder->expr()->andX( // mounts for group
7374
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GROUP, IQueryBuilder::PARAM_INT)),
74-
$builder->expr()->in('a.value', $builder->createNamedParameter($groupIds, IQueryBuilder::PARAM_STR_ARRAY))
75-
)
75+
$builder->expr()->in('a.value', $builder->createNamedParameter($groupIds, IQueryBuilder::PARAM_STR_ARRAY)),
76+
),
7677
));
7778

7879
return $this->getMountsFromQuery($query);
@@ -93,8 +94,8 @@ protected function modifyMountsOnDelete(string $applicableId, int $applicableTyp
9394
->leftJoin('a', 'external_applicable', 'b', $builder->expr()->eq('a.mount_id', 'b.mount_id'))
9495
->where($builder->expr()->andX(
9596
$builder->expr()->eq('b.type', $builder->createNamedParameter($applicableType, IQueryBuilder::PARAM_INT)),
96-
$builder->expr()->eq('b.value', $builder->createNamedParameter($applicableId))
97-
)
97+
$builder->expr()->eq('b.value', $builder->createNamedParameter($applicableId)),
98+
),
9899
)
99100
->groupBy(['a.mount_id']);
100101
$stmt = $query->executeQuery();
@@ -226,7 +227,7 @@ public function addMount($mountPoint, $storageBackend, $authBackend, $priority,
226227
'storage_backend' => $builder->createNamedParameter($storageBackend, IQueryBuilder::PARAM_STR),
227228
'auth_backend' => $builder->createNamedParameter($authBackend, IQueryBuilder::PARAM_STR),
228229
'priority' => $builder->createNamedParameter($priority, IQueryBuilder::PARAM_INT),
229-
'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT)
230+
'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT),
230231
]);
231232
$query->executeStatement();
232233
return $query->getLastInsertId();
@@ -497,4 +498,17 @@ private function decryptValue($value) {
497498
return $value;
498499
}
499500
}
501+
502+
/**
503+
* Check if any mountpoint is configured that overwrite the home folder
504+
*/
505+
public function hasHomeFolderOverwriteMount(): bool {
506+
$builder = $this->connection->getQueryBuilder();
507+
$query = $builder->select('mount_id')
508+
->from('external_mounts')
509+
->where($builder->expr()->eq('mount_point', $builder->createNamedParameter('/')))
510+
->setMaxResults(1);
511+
$result = $query->executeQuery();
512+
return count($result->fetchAll()) > 0;
513+
}
500514
}

apps/files_external/lib/Service/StoragesService.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
use OC\Files\Cache\Storage;
1010
use OC\Files\Filesystem;
11+
use OCA\Files\AppInfo\Application as FilesApplication;
12+
use OCA\Files\ConfigLexicon;
13+
use OCA\Files_External\AppInfo\Application;
1114
use OCA\Files_External\Lib\Auth\AuthMechanism;
1215
use OCA\Files_External\Lib\Auth\InvalidAuth;
1316
use OCA\Files_External\Lib\Backend\Backend;
@@ -38,6 +41,7 @@ public function __construct(
3841
protected DBConfigService $dbConfig,
3942
protected IUserMountCache $userMountCache,
4043
protected IEventDispatcher $eventDispatcher,
44+
protected IAppConfig $appConfig,
4145
) {
4246
}
4347

@@ -240,6 +244,9 @@ public function addStorage(StorageConfig $newStorage) {
240244
$this->triggerHooks($newStorage, Filesystem::signal_create_mount);
241245

242246
$newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS);
247+
248+
$this->updateOverwriteHomeFolders();
249+
243250
return $newStorage;
244251
}
245252

@@ -423,6 +430,8 @@ public function updateStorage(StorageConfig $updatedStorage) {
423430
}
424431
}
425432

433+
$this->updateOverwriteHomeFolders();
434+
426435
return $this->getStorage($id);
427436
}
428437

@@ -447,6 +456,8 @@ public function removeStorage(int $id) {
447456

448457
// delete oc_storages entries and oc_filecache
449458
Storage::cleanByMountId($id);
459+
460+
$this->updateOverwriteHomeFolders();
450461
}
451462

452463
/**
@@ -471,4 +482,20 @@ private function getStorageId(StorageConfig $storageConfig) {
471482
return -1;
472483
}
473484
}
485+
486+
public function updateOverwriteHomeFolders(): void {
487+
$appIdsList = $this->appConfig->getValueArray(FilesApplication::APP_ID, ConfigLexicon::OVERWRITES_HOME_FOLDERS);
488+
489+
if ($this->dbConfig->hasHomeFolderOverwriteMount()) {
490+
if (!in_array(Application::APP_ID, $appIdsList)) {
491+
$appIdsList[] = Application::APP_ID;
492+
$this->appConfig->setValueArray(FilesApplication::APP_ID, ConfigLexicon::OVERWRITES_HOME_FOLDERS, $appIdsList);
493+
}
494+
} else {
495+
if (in_array(Application::APP_ID, $appIdsList)) {
496+
$appIdsList = array_values(array_filter($appIdsList, fn ($v) => $v !== Application::APP_ID));
497+
$this->appConfig->setValueArray(FilesApplication::APP_ID, ConfigLexicon::OVERWRITES_HOME_FOLDERS, $appIdsList);
498+
}
499+
}
500+
}
474501
}

apps/files_external/lib/Service/UserGlobalStoragesService.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use OCA\Files_External\Lib\StorageConfig;
1010
use OCP\EventDispatcher\IEventDispatcher;
1111
use OCP\Files\Config\IUserMountCache;
12+
use OCP\IAppConfig;
1213
use OCP\IGroupManager;
1314
use OCP\IUser;
1415
use OCP\IUserSession;
@@ -20,23 +21,16 @@
2021
class UserGlobalStoragesService extends GlobalStoragesService {
2122
use UserTrait;
2223

23-
/**
24-
* @param BackendService $backendService
25-
* @param DBConfigService $dbConfig
26-
* @param IUserSession $userSession
27-
* @param IGroupManager $groupManager
28-
* @param IUserMountCache $userMountCache
29-
* @param IEventDispatcher $eventDispatcher
30-
*/
3124
public function __construct(
3225
BackendService $backendService,
3326
DBConfigService $dbConfig,
3427
IUserSession $userSession,
3528
protected IGroupManager $groupManager,
3629
IUserMountCache $userMountCache,
3730
IEventDispatcher $eventDispatcher,
31+
IAppConfig $appConfig,
3832
) {
39-
parent::__construct($backendService, $dbConfig, $userMountCache, $eventDispatcher);
33+
parent::__construct($backendService, $dbConfig, $userMountCache, $eventDispatcher, $appConfig);
4034
$this->userSession = $userSession;
4135
}
4236

apps/files_external/lib/Service/UserStoragesService.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCA\Files_External\NotFoundException;
1313
use OCP\EventDispatcher\IEventDispatcher;
1414
use OCP\Files\Config\IUserMountCache;
15+
use OCP\IAppConfig;
1516
use OCP\IUserSession;
1617

1718
/**
@@ -23,22 +24,17 @@ class UserStoragesService extends StoragesService {
2324

2425
/**
2526
* Create a user storages service
26-
*
27-
* @param BackendService $backendService
28-
* @param DBConfigService $dbConfig
29-
* @param IUserSession $userSession user session
30-
* @param IUserMountCache $userMountCache
31-
* @param IEventDispatcher $eventDispatcher
3227
*/
3328
public function __construct(
3429
BackendService $backendService,
3530
DBConfigService $dbConfig,
3631
IUserSession $userSession,
3732
IUserMountCache $userMountCache,
3833
IEventDispatcher $eventDispatcher,
34+
IAppConfig $appConfig,
3935
) {
4036
$this->userSession = $userSession;
41-
parent::__construct($backendService, $dbConfig, $userMountCache, $eventDispatcher);
37+
parent::__construct($backendService, $dbConfig, $userMountCache, $eventDispatcher, $appConfig);
4238
}
4339

4440
protected function readDBConfig() {

apps/files_external/tests/Service/GlobalStoragesServiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class GlobalStoragesServiceTest extends StoragesServiceTest {
1818
protected function setUp(): void {
1919
parent::setUp();
20-
$this->service = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache, $this->eventDispatcher);
20+
$this->service = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache, $this->eventDispatcher, $this->appConfig);
2121
}
2222

2323
protected function tearDown(): void {

apps/files_external/tests/Service/StoragesServiceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ protected function setUp(): void {
9898

9999
$this->mountCache = $this->createMock(IUserMountCache::class);
100100
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
101+
$this->appConfig = $this->createMock(IAppConfig::class);
101102

102103
// prepare BackendService mock
103104
$this->backendService =

apps/files_external/tests/Service/UserGlobalStoragesServiceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ protected function setUp(): void {
8383
$this->groupManager,
8484
$this->mountCache,
8585
$this->eventDispatcher,
86+
$this->appConfig,
8687
);
8788
}
8889

0 commit comments

Comments
 (0)