Skip to content

Commit 99d16d9

Browse files
PVince81backportbot[bot]
authored andcommitted
Add option to disallow creation of local storages
Introduce a new config option to prevent web UI admins to create or edit external storages of type "local". Signed-off-by: Vincent Petry <vincent@nextcloud.com>
1 parent f8c7124 commit 99d16d9

File tree

10 files changed

+148
-15
lines changed

10 files changed

+148
-15
lines changed

apps/files_external/js/settings.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ MountConfigListView.prototype = _.extend({
659659
}
660660

661661
this._encryptionEnabled = options.encryptionEnabled;
662+
this._canCreateLocal = options.canCreateLocal;
662663

663664
// read the backend config that was carefully crammed
664665
// into the data-configurations attribute of the select
@@ -825,10 +826,13 @@ MountConfigListView.prototype = _.extend({
825826
$tr.addClass(backend.identifier);
826827
$tr.find('.backend').data('identifier', backend.identifier);
827828

828-
if (backend.invalid) {
829+
if (backend.invalid || (backend.identifier === 'local' && !this._canCreateLocal)) {
829830
$tr.find('[name=mountPoint]').prop('disabled', true);
830831
$tr.find('.applicable,.mountOptionsToggle').empty();
831-
this.updateStatus($tr, false, 'Unknown backend: ' + backend.name);
832+
$tr.find('.save').empty();
833+
if (backend.invalid) {
834+
this.updateStatus($tr, false, 'Unknown backend: ' + backend.name);
835+
}
832836
return $tr;
833837
}
834838

@@ -970,6 +974,7 @@ MountConfigListView.prototype = _.extend({
970974
var storageConfig = new self._storageConfigClass();
971975
_.extend(storageConfig, storageParams);
972976
var $tr = self.newStorage(storageConfig, onCompletion);
977+
973978
self.recheckStorageConfig($tr);
974979
});
975980
onCompletion.resolve();
@@ -1313,9 +1318,11 @@ MountConfigListView.prototype = _.extend({
13131318

13141319
window.addEventListener('DOMContentLoaded', function() {
13151320
var enabled = $('#files_external').attr('data-encryption-enabled');
1321+
var canCreateLocal = $('#files_external').attr('data-can-create-local');
13161322
var encryptionEnabled = (enabled ==='true')? true: false;
13171323
var mountConfigListView = new MountConfigListView($('#externalStorage'), {
1318-
encryptionEnabled: encryptionEnabled
1324+
encryptionEnabled: encryptionEnabled,
1325+
canCreateLocal: (canCreateLocal === 'true') ? true: false,
13191326
});
13201327
mountConfigListView.loadStorages();
13211328

apps/files_external/lib/Controller/GlobalStoragesController.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use OCA\Files_External\Service\GlobalStoragesService;
3232
use OCP\AppFramework\Http;
3333
use OCP\AppFramework\Http\DataResponse;
34+
use OCP\IConfig;
3435
use OCP\IGroupManager;
3536
use OCP\IL10N;
3637
use OCP\ILogger;
@@ -51,6 +52,7 @@ class GlobalStoragesController extends StoragesController {
5152
* @param ILogger $logger
5253
* @param IUserSession $userSession
5354
* @param IGroupManager $groupManager
55+
* @param IConfig $config
5456
*/
5557
public function __construct(
5658
$AppName,
@@ -59,7 +61,8 @@ public function __construct(
5961
GlobalStoragesService $globalStoragesService,
6062
ILogger $logger,
6163
IUserSession $userSession,
62-
IGroupManager $groupManager
64+
IGroupManager $groupManager,
65+
IConfig $config
6366
) {
6467
parent::__construct(
6568
$AppName,
@@ -68,7 +71,8 @@ public function __construct(
6871
$globalStoragesService,
6972
$logger,
7073
$userSession,
71-
$groupManager
74+
$groupManager,
75+
$config
7276
);
7377
}
7478

@@ -96,6 +100,16 @@ public function create(
96100
$applicableGroups,
97101
$priority
98102
) {
103+
$canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
104+
if (!$canCreateNewLocalStorage && $backend === 'local') {
105+
return new DataResponse(
106+
[
107+
'message' => $this->l10n->t('Forbidden to manage local mounts')
108+
],
109+
Http::STATUS_FORBIDDEN
110+
);
111+
}
112+
99113
$newStorage = $this->createStorage(
100114
$mountPoint,
101115
$backend,

apps/files_external/lib/Controller/StoragesController.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use OCP\AppFramework\Http;
4040
use OCP\AppFramework\Http\DataResponse;
4141
use OCP\Files\StorageNotAvailableException;
42+
use OCP\IConfig;
4243
use OCP\IGroupManager;
4344
use OCP\IL10N;
4445
use OCP\ILogger;
@@ -79,6 +80,11 @@ abstract class StoragesController extends Controller {
7980
*/
8081
protected $groupManager;
8182

83+
/**
84+
* @var IConfig
85+
*/
86+
protected $config;
87+
8288
/**
8389
* Creates a new storages controller.
8490
*
@@ -95,14 +101,16 @@ public function __construct(
95101
StoragesService $storagesService,
96102
ILogger $logger,
97103
IUserSession $userSession,
98-
IGroupManager $groupManager
104+
IGroupManager $groupManager,
105+
IConfig $config
99106
) {
100107
parent::__construct($AppName, $request);
101108
$this->l10n = $l10n;
102109
$this->service = $storagesService;
103110
$this->logger = $logger;
104111
$this->userSession = $userSession;
105112
$this->groupManager = $groupManager;
113+
$this->config = $config;
106114
}
107115

108116
/**
@@ -129,6 +137,16 @@ protected function createStorage(
129137
$applicableGroups = null,
130138
$priority = null
131139
) {
140+
$canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
141+
if (!$canCreateNewLocalStorage && $backend === 'local') {
142+
return new DataResponse(
143+
[
144+
'message' => $this->l10n->t('Forbidden to manage local mounts')
145+
],
146+
Http::STATUS_FORBIDDEN
147+
);
148+
}
149+
132150
try {
133151
return $this->service->createStorage(
134152
$mountPoint,

apps/files_external/lib/Controller/UserGlobalStoragesController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OCA\Files_External\Service\UserGlobalStoragesService;
3737
use OCP\AppFramework\Http;
3838
use OCP\AppFramework\Http\DataResponse;
39+
use OCP\IConfig;
3940
use OCP\IGroupManager;
4041
use OCP\IL10N;
4142
use OCP\ILogger;
@@ -64,7 +65,8 @@ public function __construct(
6465
UserGlobalStoragesService $userGlobalStoragesService,
6566
ILogger $logger,
6667
IUserSession $userSession,
67-
IGroupManager $groupManager
68+
IGroupManager $groupManager,
69+
IConfig $config
6870
) {
6971
parent::__construct(
7072
$AppName,
@@ -73,7 +75,8 @@ public function __construct(
7375
$userGlobalStoragesService,
7476
$logger,
7577
$userSession,
76-
$groupManager
78+
$groupManager,
79+
$config
7780
);
7881
}
7982

apps/files_external/lib/Controller/UserStoragesController.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use OCA\Files_External\Service\UserStoragesService;
3636
use OCP\AppFramework\Http;
3737
use OCP\AppFramework\Http\DataResponse;
38+
use OCP\IConfig;
3839
use OCP\IGroupManager;
3940
use OCP\IL10N;
4041
use OCP\ILogger;
@@ -63,7 +64,8 @@ public function __construct(
6364
UserStoragesService $userStoragesService,
6465
ILogger $logger,
6566
IUserSession $userSession,
66-
IGroupManager $groupManager
67+
IGroupManager $groupManager,
68+
IConfig $config
6769
) {
6870
parent::__construct(
6971
$AppName,
@@ -72,7 +74,8 @@ public function __construct(
7274
$userStoragesService,
7375
$logger,
7476
$userSession,
75-
$groupManager
77+
$groupManager,
78+
$config
7679
);
7780
}
7881

@@ -127,6 +130,15 @@ public function create(
127130
$backendOptions,
128131
$mountOptions
129132
) {
133+
$canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
134+
if (!$canCreateNewLocalStorage && $backend === 'local') {
135+
return new DataResponse(
136+
[
137+
'message' => $this->l10n->t('Forbidden to manage local mounts')
138+
],
139+
Http::STATUS_FORBIDDEN
140+
);
141+
}
130142
$newStorage = $this->createStorage(
131143
$mountPoint,
132144
$backend,

apps/files_external/templates/settings.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ function writeParameterInput($parameter, $options, $classes = []) {
100100
<h2><?php p($l->t('No external storage configured or you don\'t have the permission to configure them')); ?></h2>
101101
</div>
102102

103-
<form data-can-create="<?php echo $canCreateMounts?'true':'false' ?>" id="files_external" class="section" data-encryption-enabled="<?php echo $_['encryptionEnabled']?'true': 'false'; ?>">
103+
<?php
104+
$canCreateNewLocalStorage = \OC::$server->getConfig()->getSystemValue('files_external_allow_create_new_local', true);
105+
?>
106+
<form data-can-create="<?php echo $canCreateMounts?'true':'false' ?>" data-can-create-local="<?php echo $canCreateNewLocalStorage?'true':'false' ?>" id="files_external" class="section" data-encryption-enabled="<?php echo $_['encryptionEnabled']?'true': 'false'; ?>">
104107
<h2 class="inlineblock" data-anchor-name="external-storage"><?php p($l->t('External storage')); ?></h2>
105108
<a target="_blank" rel="noreferrer" class="icon-info" title="<?php p($l->t('Open documentation'));?>" href="<?php p(link_to_docs('admin-external-storage')); ?>"></a>
106109
<p class="settings-hint"><?php p($l->t('External storage enables you to mount external storage services and devices as secondary Nextcloud storage devices. You may also allow users to mount their own external storage services.')); ?></p>
@@ -150,7 +153,7 @@ function writeParameterInput($parameter, $options, $classes = []) {
150153
});
151154
?>
152155
<?php foreach ($sortedBackends as $backend): ?>
153-
<?php if ($backend->getDeprecateTo()) {
156+
<?php if ($backend->getDeprecateTo() || (!$canCreateNewLocalStorage && $backend->getIdentifier() == "local")) {
154157
continue;
155158
} // ignore deprecated backends?>
156159
<option value="<?php p($backend->getIdentifier()); ?>"><?php p($backend->getText()); ?></option>

apps/files_external/tests/Controller/GlobalStoragesControllerTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OC\User\User;
2929
use OCA\Files_External\Controller\GlobalStoragesController;
3030
use OCA\Files_External\Service\BackendService;
31+
use OCP\IConfig;
3132
use OCP\IGroupManager;
3233
use OCP\IL10N;
3334
use OCP\ILogger;
@@ -38,25 +39,41 @@
3839
class GlobalStoragesControllerTest extends StoragesControllerTest {
3940
protected function setUp(): void {
4041
parent::setUp();
42+
4143
$this->service = $this->getMockBuilder('\OCA\Files_External\Service\GlobalStoragesService')
4244
->disableOriginalConstructor()
4345
->getMock();
4446

4547
$this->service->method('getVisibilityType')
4648
->willReturn(BackendService::VISIBILITY_ADMIN);
4749

50+
$this->controller = $this->createController(true);
51+
}
52+
53+
private function createController($allowCreateLocal = true) {
4854
$session = $this->createMock(IUserSession::class);
4955
$session->method('getUser')
5056
->willReturn(new User('test', null, $this->createMock(EventDispatcherInterface::class)));
5157

52-
$this->controller = new GlobalStoragesController(
58+
$config = $this->createMock(IConfig::class);
59+
$config->method('getSystemValue')
60+
->with('files_external_allow_create_new_local', true)
61+
->willReturn($allowCreateLocal);
62+
63+
return new GlobalStoragesController(
5364
'files_external',
5465
$this->createMock(IRequest::class),
5566
$this->createMock(IL10N::class),
5667
$this->service,
5768
$this->createMock(ILogger::class),
5869
$session,
5970
$this->createMock(IGroupManager::class),
71+
$config
6072
);
6173
}
74+
75+
public function testAddLocalStorageWhenDisabled() {
76+
$this->controller = $this->createController(false);
77+
parent::testAddLocalStorageWhenDisabled();
78+
}
6279
}

apps/files_external/tests/Controller/StoragesControllerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,36 @@ public function testAddStorage() {
130130
$this->assertEquals($storageConfig, $data);
131131
}
132132

133+
public function testAddLocalStorageWhenDisabled() {
134+
$authMech = $this->getAuthMechMock();
135+
$backend = $this->getBackendMock();
136+
137+
$storageConfig = new StorageConfig(1);
138+
$storageConfig->setMountPoint('mount');
139+
$storageConfig->setBackend($backend);
140+
$storageConfig->setAuthMechanism($authMech);
141+
$storageConfig->setBackendOptions([]);
142+
143+
$this->service->expects($this->never())
144+
->method('createStorage');
145+
$this->service->expects($this->never())
146+
->method('addStorage');
147+
148+
$response = $this->controller->create(
149+
'mount',
150+
'local',
151+
'\OCA\Files_External\Lib\Auth\NullMechanism',
152+
[],
153+
[],
154+
[],
155+
[],
156+
null
157+
);
158+
159+
$data = $response->getData();
160+
$this->assertEquals(Http::STATUS_FORBIDDEN, $response->getStatus());
161+
}
162+
133163
public function testUpdateStorage() {
134164
$authMech = $this->getAuthMechMock();
135165
$authMech->method('validateStorage')

apps/files_external/tests/Controller/UserStoragesControllerTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use OCA\Files_External\Lib\StorageConfig;
3232
use OCA\Files_External\Service\BackendService;
3333
use OCP\AppFramework\Http;
34+
use OCP\IConfig;
3435
use OCP\IGroupManager;
3536
use OCP\IL10N;
3637
use OCP\ILogger;
@@ -54,21 +55,36 @@ protected function setUp(): void {
5455
$this->service->method('getVisibilityType')
5556
->willReturn(BackendService::VISIBILITY_PERSONAL);
5657

58+
$this->controller = $this->createController(true);
59+
}
60+
61+
private function createController($allowCreateLocal = true) {
5762
$session = $this->createMock(IUserSession::class);
5863
$session->method('getUser')
5964
->willReturn(new User('test', null, $this->createMock(EventDispatcherInterface::class)));
6065

61-
$this->controller = new UserStoragesController(
66+
$config = $this->createMock(IConfig::class);
67+
$config->method('getSystemValue')
68+
->with('files_external_allow_create_new_local', true)
69+
->willReturn($allowCreateLocal);
70+
71+
return new UserStoragesController(
6272
'files_external',
6373
$this->createMock(IRequest::class),
6474
$this->createMock(IL10N::class),
6575
$this->service,
6676
$this->createMock(ILogger::class),
6777
$session,
68-
$this->createMock(IGroupManager::class)
78+
$this->createMock(IGroupManager::class),
79+
$config
6980
);
7081
}
7182

83+
public function testAddLocalStorageWhenDisabled() {
84+
$this->controller = $this->createController(false);
85+
parent::testAddLocalStorageWhenDisabled();
86+
}
87+
7288
public function testAddOrUpdateStorageDisallowedBackend() {
7389
$backend = $this->getBackendMock();
7490
$backend->method('isVisibleFor')

0 commit comments

Comments
 (0)