Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions apps/files_external/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ MountConfigListView.prototype = _.extend({
}

this._encryptionEnabled = options.encryptionEnabled;
this._canCreateLocal = options.canCreateLocal;

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

if (backend.invalid) {
if (backend.invalid || (backend.identifier === 'local' && !this._canCreateLocal)) {
$tr.find('[name=mountPoint]').prop('disabled', true);
$tr.find('.applicable,.mountOptionsToggle').empty();
this.updateStatus($tr, false, 'Unknown backend: ' + backend.name);
$tr.find('.save').empty();
if (backend.invalid) {
this.updateStatus($tr, false, 'Unknown backend: ' + backend.name);
}
return $tr;
}

Expand Down Expand Up @@ -981,6 +985,7 @@ MountConfigListView.prototype = _.extend({
var storageConfig = new self._storageConfigClass();
_.extend(storageConfig, storageParams);
var $tr = self.newStorage(storageConfig, onCompletion);

self.recheckStorageConfig($tr);
});
onCompletion.resolve();
Expand Down Expand Up @@ -1325,9 +1330,11 @@ MountConfigListView.prototype = _.extend({

window.addEventListener('DOMContentLoaded', function() {
var enabled = $('#files_external').attr('data-encryption-enabled');
var canCreateLocal = $('#files_external').attr('data-can-create-local');
var encryptionEnabled = (enabled ==='true')? true: false;
var mountConfigListView = new MountConfigListView($('#externalStorage'), {
encryptionEnabled: encryptionEnabled
encryptionEnabled: encryptionEnabled,
canCreateLocal: (canCreateLocal === 'true') ? true: false,
});
mountConfigListView.loadStorages();

Expand Down
18 changes: 16 additions & 2 deletions apps/files_external/lib/Controller/GlobalStoragesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCA\Files_External\Service\GlobalStoragesService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
Expand All @@ -51,6 +52,7 @@ class GlobalStoragesController extends StoragesController {
* @param ILogger $logger
* @param IUserSession $userSession
* @param IGroupManager $groupManager
* @param IConfig $config
*/
public function __construct(
$AppName,
Expand All @@ -59,7 +61,8 @@ public function __construct(
GlobalStoragesService $globalStoragesService,
ILogger $logger,
IUserSession $userSession,
IGroupManager $groupManager
IGroupManager $groupManager,
IConfig $config
) {
parent::__construct(
$AppName,
Expand All @@ -68,7 +71,8 @@ public function __construct(
$globalStoragesService,
$logger,
$userSession,
$groupManager
$groupManager,
$config
);
}

Expand Down Expand Up @@ -96,6 +100,16 @@ public function create(
$applicableGroups,
$priority
) {
$canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
if (!$canCreateNewLocalStorage && $backend === 'local') {
return new DataResponse(
[
'message' => $this->l10n->t('Forbidden to manage local mounts')
],
Http::STATUS_FORBIDDEN
);
}

$newStorage = $this->createStorage(
$mountPoint,
$backend,
Expand Down
20 changes: 19 additions & 1 deletion apps/files_external/lib/Controller/StoragesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\StorageNotAvailableException;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
Expand Down Expand Up @@ -79,6 +80,11 @@ abstract class StoragesController extends Controller {
*/
protected $groupManager;

/**
* @var IConfig
*/
protected $config;

/**
* Creates a new storages controller.
*
Expand All @@ -95,14 +101,16 @@ public function __construct(
StoragesService $storagesService,
ILogger $logger,
IUserSession $userSession,
IGroupManager $groupManager
IGroupManager $groupManager,
IConfig $config
) {
parent::__construct($AppName, $request);
$this->l10n = $l10n;
$this->service = $storagesService;
$this->logger = $logger;
$this->userSession = $userSession;
$this->groupManager = $groupManager;
$this->config = $config;
}

/**
Expand All @@ -129,6 +137,16 @@ protected function createStorage(
$applicableGroups = null,
$priority = null
) {
$canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
if (!$canCreateNewLocalStorage && $backend === 'local') {
return new DataResponse(
[
'message' => $this->l10n->t('Forbidden to manage local mounts')
],
Http::STATUS_FORBIDDEN
);
}

try {
return $this->service->createStorage(
$mountPoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCA\Files_External\Service\UserGlobalStoragesService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
Expand Down Expand Up @@ -64,7 +65,8 @@ public function __construct(
UserGlobalStoragesService $userGlobalStoragesService,
ILogger $logger,
IUserSession $userSession,
IGroupManager $groupManager
IGroupManager $groupManager,
IConfig $config
) {
parent::__construct(
$AppName,
Expand All @@ -73,7 +75,8 @@ public function __construct(
$userGlobalStoragesService,
$logger,
$userSession,
$groupManager
$groupManager,
$config
);
}

Expand Down
16 changes: 14 additions & 2 deletions apps/files_external/lib/Controller/UserStoragesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCA\Files_External\Service\UserStoragesService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
Expand Down Expand Up @@ -63,7 +64,8 @@ public function __construct(
UserStoragesService $userStoragesService,
ILogger $logger,
IUserSession $userSession,
IGroupManager $groupManager
IGroupManager $groupManager,
IConfig $config
) {
parent::__construct(
$AppName,
Expand All @@ -72,7 +74,8 @@ public function __construct(
$userStoragesService,
$logger,
$userSession,
$groupManager
$groupManager,
$config
);
}

Expand Down Expand Up @@ -127,6 +130,15 @@ public function create(
$backendOptions,
$mountOptions
) {
$canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
if (!$canCreateNewLocalStorage && $backend === 'local') {
return new DataResponse(
[
'message' => $this->l10n->t('Forbidden to manage local mounts')
],
Http::STATUS_FORBIDDEN
);
}
$newStorage = $this->createStorage(
$mountPoint,
$backend,
Expand Down
7 changes: 5 additions & 2 deletions apps/files_external/templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ function writeParameterInput($parameter, $options, $classes = []) {
<h2><?php p($l->t('No external storage configured or you don\'t have the permission to configure them')); ?></h2>
</div>

<form data-can-create="<?php echo $canCreateMounts?'true':'false' ?>" id="files_external" class="section" data-encryption-enabled="<?php echo $_['encryptionEnabled']?'true': 'false'; ?>">
<?php
$canCreateNewLocalStorage = \OC::$server->getConfig()->getSystemValue('files_external_allow_create_new_local', true);
?>
<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'; ?>">
<h2 class="inlineblock" data-anchor-name="external-storage"><?php p($l->t('External storage')); ?></h2>
<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>
<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>
Expand Down Expand Up @@ -155,7 +158,7 @@ function writeParameterInput($parameter, $options, $classes = []) {
});
?>
<?php foreach ($sortedBackends as $backend): ?>
<?php if ($backend->getDeprecateTo()) {
<?php if ($backend->getDeprecateTo() || (!$canCreateNewLocalStorage && $backend->getIdentifier() == "local")) {
continue;
} // ignore deprecated backends?>
<option value="<?php p($backend->getIdentifier()); ?>"><?php p($backend->getText()); ?></option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OC\User\User;
use OCA\Files_External\Controller\GlobalStoragesController;
use OCA\Files_External\Service\BackendService;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
Expand All @@ -38,25 +39,41 @@
class GlobalStoragesControllerTest extends StoragesControllerTest {
protected function setUp(): void {
parent::setUp();

$this->service = $this->getMockBuilder('\OCA\Files_External\Service\GlobalStoragesService')
->disableOriginalConstructor()
->getMock();

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

$this->controller = $this->createController(true);
}

private function createController($allowCreateLocal = true) {
$session = $this->createMock(IUserSession::class);
$session->method('getUser')
->willReturn(new User('test', null, $this->createMock(EventDispatcherInterface::class)));

$this->controller = new GlobalStoragesController(
$config = $this->createMock(IConfig::class);
$config->method('getSystemValue')
->with('files_external_allow_create_new_local', true)
->willReturn($allowCreateLocal);

return new GlobalStoragesController(
'files_external',
$this->createMock(IRequest::class),
$this->createMock(IL10N::class),
$this->service,
$this->createMock(ILogger::class),
$session,
$this->createMock(IGroupManager::class),
$config
);
}

public function testAddLocalStorageWhenDisabled() {
$this->controller = $this->createController(false);
parent::testAddLocalStorageWhenDisabled();
}
}
30 changes: 30 additions & 0 deletions apps/files_external/tests/Controller/StoragesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,36 @@ public function testAddStorage() {
$this->assertEquals($storageConfig, $data);
}

public function testAddLocalStorageWhenDisabled() {
$authMech = $this->getAuthMechMock();
$backend = $this->getBackendMock();

$storageConfig = new StorageConfig(1);
$storageConfig->setMountPoint('mount');
$storageConfig->setBackend($backend);
$storageConfig->setAuthMechanism($authMech);
$storageConfig->setBackendOptions([]);

$this->service->expects($this->never())
->method('createStorage');
$this->service->expects($this->never())
->method('addStorage');

$response = $this->controller->create(
'mount',
'local',
'\OCA\Files_External\Lib\Auth\NullMechanism',
[],
[],
[],
[],
null
);

$data = $response->getData();
$this->assertEquals(Http::STATUS_FORBIDDEN, $response->getStatus());
}

public function testUpdateStorage() {
$authMech = $this->getAuthMechMock();
$authMech->method('validateStorage')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Service\BackendService;
use OCP\AppFramework\Http;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
Expand All @@ -54,21 +55,36 @@ protected function setUp(): void {
$this->service->method('getVisibilityType')
->willReturn(BackendService::VISIBILITY_PERSONAL);

$this->controller = $this->createController(true);
}

private function createController($allowCreateLocal = true) {
$session = $this->createMock(IUserSession::class);
$session->method('getUser')
->willReturn(new User('test', null, $this->createMock(EventDispatcherInterface::class)));

$this->controller = new UserStoragesController(
$config = $this->createMock(IConfig::class);
$config->method('getSystemValue')
->with('files_external_allow_create_new_local', true)
->willReturn($allowCreateLocal);

return new UserStoragesController(
'files_external',
$this->createMock(IRequest::class),
$this->createMock(IL10N::class),
$this->service,
$this->createMock(ILogger::class),
$session,
$this->createMock(IGroupManager::class)
$this->createMock(IGroupManager::class),
$config
);
}

public function testAddLocalStorageWhenDisabled() {
$this->controller = $this->createController(false);
parent::testAddLocalStorageWhenDisabled();
}

public function testAddOrUpdateStorageDisallowedBackend() {
$backend = $this->getBackendMock();
$backend->method('isVisibleFor')
Expand Down
Loading