Skip to content

Commit b6dd478

Browse files
committed
fix: move backend checks to a different place
1 parent 5080fc6 commit b6dd478

6 files changed

Lines changed: 79 additions & 53 deletions

File tree

apps/files_external/lib/Controller/GlobalStoragesController.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ public function create(
8686
$applicableGroups,
8787
$priority
8888
) {
89-
$canCreateNewLocalStorage = \OC::$server->getConfig()->getSystemValue('files_external_allow_create_new_local', false);
90-
91-
if (($backend === 'local' || $backend === '\OC\Files\Storage\Local') && $canCreateNewLocalStorage === false) {
92-
return new DataResponse(
93-
null,
94-
Http::STATUS_FORBIDDEN
95-
);
96-
}
97-
9889
$newStorage = $this->createStorage(
9990
$mountPoint,
10091
$backend,

apps/files_external/lib/Controller/StoragesController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ protected function validate(IStorageConfig $storage) {
183183
$backend->getIdentifier()
184184
])
185185
],
186-
Http::STATUS_UNPROCESSABLE_ENTITY
186+
Http::STATUS_FORBIDDEN
187187
);
188188
}
189189
if (!$authMechanism->isVisibleFor($this->service->getVisibilityType())) {
@@ -194,7 +194,7 @@ protected function validate(IStorageConfig $storage) {
194194
$authMechanism->getIdentifier()
195195
])
196196
],
197-
Http::STATUS_UNPROCESSABLE_ENTITY
197+
Http::STATUS_FORBIDDEN
198198
);
199199
}
200200

apps/files_external/lib/Controller/UserStoragesController.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,6 @@ public function create(
128128
Http::STATUS_FORBIDDEN
129129
);
130130
}
131-
$canCreateNewLocalStorage = \OC::$server->getConfig()->getSystemValue('files_external_allow_create_new_local', false);
132-
if (($backend === 'local' || $backend === '\OC\Files\Storage\Local') && $canCreateNewLocalStorage === false) {
133-
return new DataResponse(
134-
null,
135-
Http::STATUS_FORBIDDEN
136-
);
137-
}
138-
139131
$newStorage = $this->createStorage(
140132
$mountPoint,
141133
$backend,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace OC\Files\External;
4+
5+
use OCP\IConfig;
6+
use OCP\Files\External\Backend\Backend;
7+
8+
class StoragesBackendChecker {
9+
/** @var IConfig */
10+
private IConfig $config;
11+
/** @var bool */
12+
private $allowUserMounting = null;
13+
/** @var array */
14+
private $userMountingBackends = null;
15+
16+
/**
17+
* @param IConfig $config
18+
*/
19+
public function __construct(IConfig $config) {
20+
$this->config = $config;
21+
}
22+
23+
/**
24+
* Checks if the regular users are allowed to mount external storages
25+
* @return bool
26+
*/
27+
public function isUserMountingAllowed() {
28+
if ($this->allowUserMounting === null) {
29+
$this->allowUserMounting = $this->config->getAppValue('files_external', 'allow_user_mounting', 'no') === 'yes';
30+
// if no backend is in the list an empty string is in the array and user mounting is disabled
31+
if ($this->allowedBackendsForUsers() === ['']) {
32+
$this->allowUserMounting = false;
33+
}
34+
}
35+
return $this->allowUserMounting;
36+
}
37+
38+
private function allowedBackendsForUsers() {
39+
if ($this->userMountingBackends === null) {
40+
$user_mounting_backends = $this->config->getAppValue('files_external', 'user_mounting_backends', '');
41+
$this->userMountingBackends = \explode(
42+
',',
43+
$user_mounting_backends
44+
);
45+
}
46+
return $this->userMountingBackends;
47+
}
48+
49+
/**
50+
* Checks if the regular users are allowed to mount the specified backend.
51+
* Note that the admin might still mount the backend.
52+
* @return bool
53+
*/
54+
public function isAllowedUserBackend(Backend $backend) {
55+
$blacklistedBackendsForUsers = ['\OC\Files\Storage\Local'];
56+
if (in_array($backend->getStorageClass(), $blacklistedBackendsForUsers, true)) {
57+
return false;
58+
}
59+
60+
if ($this->isUserMountingAllowed() &&
61+
\array_intersect($backend->getIdentifierAliases(), $this->allowedBackendsForUsers()))
62+
{
63+
return true;
64+
}
65+
return false;
66+
}
67+
}

lib/private/Files/External/StoragesBackendService.php

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use OCP\IConfig;
2727

28+
use OC\Files\External\StoragesBackendChecker;
2829
use OCP\Files\External\Backend\Backend;
2930
use OCP\Files\External\Auth\AuthMechanism;
3031
use OCP\Files\External\Config\IBackendProvider;
@@ -35,14 +36,8 @@
3536
* Service class to manage backend definitions
3637
*/
3738
class StoragesBackendService implements IStoragesBackendService {
38-
/** @var IConfig */
39-
protected $config;
40-
41-
/** @var bool */
42-
private $userMountingAllowed = true;
43-
44-
/** @var string[] */
45-
private $userMountingBackends = [];
39+
/** @var StoragesBackendChecker */
40+
protected $storagesBackendChecker;
4641

4742
/** @var Backend[] */
4843
private $backends = [];
@@ -57,27 +52,12 @@ class StoragesBackendService implements IStoragesBackendService {
5752
private $authMechanismProviders = [];
5853

5954
/**
60-
* @param IConfig $config
55+
* @param StoragesBackendChecker $storagesBackendChecker
6156
*/
6257
public function __construct(
63-
IConfig $config
58+
StoragesBackendChecker $storagesBackendChecker
6459
) {
65-
$this->config = $config;
66-
67-
// Load config values
68-
if ($this->config->getAppValue('files_external', 'allow_user_mounting', 'no') !== 'yes') {
69-
$this->userMountingAllowed = false;
70-
}
71-
$user_mounting_backends = $this->config->getAppValue('files_external', 'user_mounting_backends', '');
72-
$this->userMountingBackends = \explode(
73-
',',
74-
$user_mounting_backends
75-
);
76-
77-
// if no backend is in the list an empty string is in the array and user mounting is disabled
78-
if ($this->userMountingBackends === ['']) {
79-
$this->userMountingAllowed = false;
80-
}
60+
$this->storagesBackendChecker = $storagesBackendChecker;
8161
}
8262

8363
/**
@@ -244,7 +224,7 @@ public function getAuthMechanism($identifier) {
244224
* @return bool
245225
*/
246226
public function isUserMountingAllowed() {
247-
return $this->userMountingAllowed;
227+
return $this->storagesBackendChecker->isUserMountingAllowed();
248228
}
249229

250230
/**
@@ -254,12 +234,7 @@ public function isUserMountingAllowed() {
254234
* @return bool
255235
*/
256236
protected function isAllowedUserBackend(Backend $backend) {
257-
if ($this->userMountingAllowed &&
258-
\array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends)
259-
) {
260-
return true;
261-
}
262-
return false;
237+
return $this->storagesBackendChecker->isAllowedUserBackend($backend);
263238
}
264239

265240
/**

lib/private/Server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
use Symfony\Component\EventDispatcher\EventDispatcher;
115115
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
116116
use OC\Files\External\StoragesBackendService;
117+
use OC\Files\External\StoragesBackendChecker;
117118
use OC\Files\External\Service\UserStoragesService;
118119
use OC\Files\External\Service\UserGlobalStoragesService;
119120
use OC\Files\External\Service\GlobalStoragesService;
@@ -844,7 +845,7 @@ public function __construct($webRoot, \OC\Config $config) {
844845
);
845846
});
846847
$this->registerService('StoragesBackendService', function (Server $c) {
847-
$service = new StoragesBackendService($c->query('AllConfig'));
848+
$service = new StoragesBackendService($c->query(StoragesBackendChecker::class));
848849

849850
// register auth mechanisms provided by core
850851
$provider = new \OC\Files\External\Auth\CoreAuthMechanismProvider($c, [

0 commit comments

Comments
 (0)