Skip to content

Commit 30bc836

Browse files
authored
Merge pull request #62072 from nextcloud/backport/60563/stable27
[stable27] fix: improve check if external storage backend is local
2 parents 07f3565 + de60664 commit 30bc836

7 files changed

Lines changed: 46 additions & 29 deletions

apps/files_external/lib/Controller/GlobalStoragesController.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
namespace OCA\Files_External\Controller;
2929

3030
use OCA\Files_External\NotFoundException;
31+
use OCA\Files_External\Service\BackendService;
3132
use OCA\Files_External\Service\GlobalStoragesService;
3233
use OCP\AppFramework\Http;
3334
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
@@ -63,7 +64,8 @@ public function __construct(
6364
ILogger $logger,
6465
IUserSession $userSession,
6566
IGroupManager $groupManager,
66-
IConfig $config
67+
IConfig $config,
68+
BackendService $backendService
6769
) {
6870
parent::__construct(
6971
$AppName,
@@ -73,7 +75,8 @@ public function __construct(
7375
$logger,
7476
$userSession,
7577
$groupManager,
76-
$config
78+
$config,
79+
$backendService
7780
);
7881
}
7982

@@ -102,16 +105,6 @@ public function create(
102105
$applicableGroups,
103106
$priority
104107
) {
105-
$canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
106-
if (!$canCreateNewLocalStorage && $backend === 'local') {
107-
return new DataResponse(
108-
[
109-
'message' => $this->l10n->t('Forbidden to manage local mounts')
110-
],
111-
Http::STATUS_FORBIDDEN
112-
);
113-
}
114-
115108
$newStorage = $this->createStorage(
116109
$mountPoint,
117110
$backend,

apps/files_external/lib/Controller/StoragesController.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
use OCA\Files_External\Lib\Auth\AuthMechanism;
3232
use OCA\Files_External\Lib\Backend\Backend;
3333
use OCA\Files_External\Lib\DefinitionParameter;
34+
use OCA\Files_External\Lib\Backend\Local;
3435
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
3536
use OCA\Files_External\Lib\StorageConfig;
3637
use OCA\Files_External\NotFoundException;
38+
use OCA\Files_External\Service\BackendService;
3739
use OCA\Files_External\Service\StoragesService;
3840
use OCP\AppFramework\Controller;
3941
use OCP\AppFramework\Http;
@@ -86,6 +88,11 @@ abstract class StoragesController extends Controller {
8688
*/
8789
protected $config;
8890

91+
/**
92+
* @var BackendService
93+
*/
94+
protected $backendService;
95+
8996
/**
9097
* Creates a new storages controller.
9198
*
@@ -103,7 +110,8 @@ public function __construct(
103110
ILogger $logger,
104111
IUserSession $userSession,
105112
IGroupManager $groupManager,
106-
IConfig $config
113+
IConfig $config,
114+
BackendService $backendService
107115
) {
108116
parent::__construct($AppName, $request);
109117
$this->l10n = $l10n;
@@ -112,6 +120,7 @@ public function __construct(
112120
$this->userSession = $userSession;
113121
$this->groupManager = $groupManager;
114122
$this->config = $config;
123+
$this->backendService = $backendService;
115124
}
116125

117126
/**
@@ -139,7 +148,7 @@ protected function createStorage(
139148
$priority = null
140149
) {
141150
$canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
142-
if (!$canCreateNewLocalStorage && $backend === 'local') {
151+
if (!$canCreateNewLocalStorage && $this->backendService->getBackend($backend) instanceof Local) {
143152
return new DataResponse(
144153
[
145154
'message' => $this->l10n->t('Forbidden to manage local mounts')

apps/files_external/lib/Controller/UserGlobalStoragesController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
3434
use OCA\Files_External\Lib\StorageConfig;
3535
use OCA\Files_External\NotFoundException;
36+
use OCA\Files_External\Service\BackendService;
3637
use OCA\Files_External\Service\UserGlobalStoragesService;
3738
use OCP\AppFramework\Http;
3839
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
@@ -68,7 +69,8 @@ public function __construct(
6869
ILogger $logger,
6970
IUserSession $userSession,
7071
IGroupManager $groupManager,
71-
IConfig $config
72+
IConfig $config,
73+
BackendService $backendService
7274
) {
7375
parent::__construct(
7476
$AppName,
@@ -78,7 +80,8 @@ public function __construct(
7880
$logger,
7981
$userSession,
8082
$groupManager,
81-
$config
83+
$config,
84+
$backendService,
8285
);
8386
}
8487

apps/files_external/lib/Controller/UserStoragesController.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCA\Files_External\Lib\Backend\Backend;
3333
use OCA\Files_External\Lib\StorageConfig;
3434
use OCA\Files_External\NotFoundException;
35+
use OCA\Files_External\Service\BackendService;
3536
use OCA\Files_External\Service\UserStoragesService;
3637
use OCP\AppFramework\Http;
3738
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
@@ -67,7 +68,8 @@ public function __construct(
6768
ILogger $logger,
6869
IUserSession $userSession,
6970
IGroupManager $groupManager,
70-
IConfig $config
71+
IConfig $config,
72+
BackendService $backendService
7173
) {
7274
parent::__construct(
7375
$AppName,
@@ -77,7 +79,8 @@ public function __construct(
7779
$logger,
7880
$userSession,
7981
$groupManager,
80-
$config
82+
$config,
83+
$backendService,
8184
);
8285
}
8386

@@ -132,15 +135,6 @@ public function create(
132135
$backendOptions,
133136
$mountOptions
134137
) {
135-
$canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
136-
if (!$canCreateNewLocalStorage && $backend === 'local') {
137-
return new DataResponse(
138-
[
139-
'message' => $this->l10n->t('Forbidden to manage local mounts')
140-
],
141-
Http::STATUS_FORBIDDEN
142-
);
143-
}
144138
$newStorage = $this->createStorage(
145139
$mountPoint,
146140
$backend,

apps/files_external/tests/Controller/GlobalStoragesControllerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ private function createController($allowCreateLocal = true) {
6868
$this->createMock(ILogger::class),
6969
$session,
7070
$this->createMock(IGroupManager::class),
71-
$config
71+
$config,
72+
$this->backendService,
7273
);
7374
}
7475

apps/files_external/tests/Controller/StoragesControllerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
use OCA\Files_External\Lib\Auth\AuthMechanism;
3232
use OCA\Files_External\Lib\Backend\Backend;
3333

34+
use OCA\Files_External\Lib\Backend\Local;
3435
use OCA\Files_External\Lib\StorageConfig;
3536
use OCA\Files_External\NotFoundException;
37+
use OCA\Files_External\Service\BackendService;
3638
use OCA\Files_External\Service\GlobalStoragesService;
3739
use OCP\AppFramework\Http;
3840

@@ -47,9 +49,23 @@ abstract class StoragesControllerTest extends \Test\TestCase {
4749
* @var GlobalStoragesService
4850
*/
4951
protected $service;
52+
/**
53+
* @var BackendService|MockObject
54+
*/
55+
protected $backendService;
5056

5157
protected function setUp(): void {
5258
\OCA\Files_External\MountConfig::$skipTest = true;
59+
60+
$this->backendService = $this->createMock(BackendService::class);
61+
$this->backendService->method('getBackend')
62+
->willReturnCallback(function ($identifier) {
63+
if ($identifier === 'local') {
64+
return $this->createMock(Local::class);
65+
} else {
66+
return $this->createMock(Backend::class);
67+
}
68+
});
5369
}
5470

5571
protected function tearDown(): void {

apps/files_external/tests/Controller/UserStoragesControllerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ private function createController($allowCreateLocal = true) {
7676
$this->createMock(ILogger::class),
7777
$session,
7878
$this->createMock(IGroupManager::class),
79-
$config
79+
$config,
80+
$this->backendService,
8081
);
8182
}
8283

0 commit comments

Comments
 (0)