Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable user unmount using 'allow_user_unmounting' #39134

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 5 additions & 14 deletions apps/files_external/lib/Controller/GlobalStoragesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
namespace OCA\Files_External\Controller;

use OCA\Files_External\NotFoundException;
use OCA\Files_External\Service\BackendService;
use OCA\Files_External\Service\GlobalStoragesService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
Expand All @@ -42,36 +43,26 @@
* Global storages controller
*/
class GlobalStoragesController extends StoragesController {
/**
* Creates a new global storages controller.
*
* @param string $AppName application name
* @param IRequest $request request object
* @param IL10N $l10n l10n service
* @param GlobalStoragesService $globalStoragesService storage service
* @param LoggerInterface $logger
* @param IUserSession $userSession
* @param IGroupManager $groupManager
* @param IConfig $config
*/
public function __construct(
$AppName,
string $appName,
IRequest $request,
IL10N $l10n,
GlobalStoragesService $globalStoragesService,
LoggerInterface $logger,
IUserSession $userSession,
IGroupManager $groupManager,
BackendService $backendService,
IConfig $config
) {
parent::__construct(
$AppName,
$appName,
$request,
$l10n,
$globalStoragesService,
$logger,
$userSession,
$groupManager,
$backendService,
$config
);
}
Expand Down
17 changes: 5 additions & 12 deletions apps/files_external/lib/Controller/StoragesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\NotFoundException;
use OCA\Files_External\Service\BackendService;
use OCA\Files_External\Service\StoragesService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
Expand All @@ -50,26 +51,18 @@
* Base class for storages controllers
*/
abstract class StoragesController extends Controller {
/**
* Creates a new storages controller.
*
* @param string $AppName application name
* @param IRequest $request request object
* @param IL10N $l10n l10n service
* @param StoragesService $storagesService storage service
* @param LoggerInterface $logger
*/
public function __construct(
$AppName,
string $appName,
IRequest $request,
protected IL10N $l10n,
protected StoragesService $service,
protected LoggerInterface $logger,
protected IUserSession $userSession,
protected IGroupManager $groupManager,
protected BackendService $backendService,
protected IConfig $config
) {
parent::__construct($AppName, $request);
parent::__construct($appName, $request);
}

/**
Expand Down Expand Up @@ -118,7 +111,7 @@ protected function createStorage(
$priority
);
} catch (\InvalidArgumentException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
$this->logger->warning('Invalid backend or authentication mechanism class', ['exception' => $e]);
return new DataResponse(
[
'message' => $this->l10n->t('Invalid backend or authentication mechanism class')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\NotFoundException;
use OCA\Files_External\Service\BackendService;
use OCA\Files_External\Service\UserGlobalStoragesService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
Expand All @@ -59,23 +60,25 @@ class UserGlobalStoragesController extends StoragesController {
* @param IGroupManager $groupManager
*/
public function __construct(
$AppName,
string $appName,
IRequest $request,
IL10N $l10n,
UserGlobalStoragesService $userGlobalStoragesService,
LoggerInterface $logger,
IUserSession $userSession,
IGroupManager $groupManager,
BackendService $backendService,
IConfig $config
) {
parent::__construct(
$AppName,
$appName,
$request,
$l10n,
$userGlobalStoragesService,
$logger,
$userSession,
$groupManager,
$backendService,
$config
);
}
Expand Down
18 changes: 16 additions & 2 deletions apps/files_external/lib/Controller/UserStoragesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCA\Files_External\Lib\Backend\Backend;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\NotFoundException;
use OCA\Files_External\Service\BackendService;
use OCA\Files_External\Service\UserStoragesService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
Expand All @@ -58,23 +59,25 @@ class UserStoragesController extends StoragesController {
* @param IGroupManager $groupManager
*/
public function __construct(
$AppName,
string $appName,
IRequest $request,
IL10N $l10n,
UserStoragesService $userStoragesService,
LoggerInterface $logger,
IUserSession $userSession,
IGroupManager $groupManager,
BackendService $backendService,
IConfig $config
) {
parent::__construct(
$AppName,
$appName,
$request,
$l10n,
$userStoragesService,
$logger,
$userSession,
$groupManager,
$backendService,
$config
);
}
Expand Down Expand Up @@ -232,6 +235,17 @@ public function update(
* {@inheritdoc}
*/
public function destroy($id) {
if (!$this->backendService->isUserUnmountingAllowed()) {
return new DataResponse(
[
'message' => $this->l10n->t(
'Insufficient right to disconnect this storage'
)
],
Http::STATUS_NOT_FOUND
);
}

return parent::destroy($id);
}
}
36 changes: 19 additions & 17 deletions apps/files_external/lib/Service/BackendService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Files_External\Service;

use OCA\Files_External\Config\IConfigHandler;
use OCA\Files_External\Lib\Auth\AuthMechanism;

use OCA\Files_External\Lib\Backend\Backend;
use OCA\Files_External\Lib\Config\IAuthMechanismProvider;
use OCA\Files_External\Lib\Config\IBackendProvider;
Expand All @@ -52,40 +52,34 @@ class BackendService {
/** Priority constants for PriorityTrait */
public const PRIORITY_DEFAULT = 100;

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

/** @var bool */
private $userMountingAllowed = true;
private bool $userMountingAllowed = true;

/** @var string[] */
private $userMountingBackends = [];
private array $userMountingBackends;

/** @var Backend[] */
private $backends = [];
private array $backends = [];

/** @var IBackendProvider[] */
private $backendProviders = [];
private array $backendProviders = [];

/** @var AuthMechanism[] */
private $authMechanisms = [];
private array $authMechanisms = [];

/** @var IAuthMechanismProvider[] */
private $authMechanismProviders = [];
private array $authMechanismProviders = [];

/** @var callable[] */
private $configHandlerLoaders = [];
private array $configHandlerLoaders = [];

private $configHandlers = [];
private array $configHandlers = [];

/**
* @param IConfig $config
*/
public function __construct(
IConfig $config
protected IConfig $config
) {
$this->config = $config;

// Load config values
if ($this->config->getAppValue('files_external', 'allow_user_mounting', 'yes') !== 'yes') {
$this->userMountingAllowed = false;
Expand Down Expand Up @@ -276,10 +270,18 @@ public function getAuthMechanism($identifier) {
/**
* @return bool
*/
public function isUserMountingAllowed() {
public function isUserMountingAllowed(): bool {
return $this->userMountingAllowed;
}

public function isUserUnmountingAllowed(): bool {
return ('yes' === $this->config->getAppValue(
'files_external',
'allow_user_unmounting',
'yes'
));
}

/**
* Check a backend if a user is allowed to mount it
*
Expand Down
1 change: 1 addition & 0 deletions apps/files_external/lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function getForm() {
'authMechanisms' => $this->backendService->getAuthMechanisms(),
'dependencies' => \OCA\Files_External\MountConfig::dependencyMessage($this->backendService->getBackends()),
'allowUserMounting' => $this->backendService->isUserMountingAllowed(),
'allowUserUnmounting' => $this->backendService->isUserUnmountingAllowed(),
'globalCredentials' => $this->globalAuth->getAuth(''),
'globalCredentialsUid' => '',
];
Expand Down
1 change: 1 addition & 0 deletions apps/files_external/lib/Settings/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function getForm() {
'authMechanisms' => $this->backendService->getAuthMechanisms(),
'dependencies' => \OCA\Files_External\MountConfig::dependencyMessage($this->backendService->getBackends()),
'allowUserMounting' => $this->backendService->isUserMountingAllowed(),
'allowUserUnmounting' => $this->backendService->isUserUnmountingAllowed(),
'globalCredentials' => $this->globalAuth->getAuth($uid),
'globalCredentialsUid' => $uid,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private function createController($allowCreateLocal = true) {
$this->createMock(LoggerInterface::class),
$session,
$this->createMock(IGroupManager::class),
$this->createMock(BackendService::class),
$config
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ private function createController($allowCreateLocal = true) {
$this->createMock(LoggerInterface::class),
$session,
$this->createMock(IGroupManager::class),
$this->createMock(BackendService::class),
$config
);
}
Expand Down
1 change: 1 addition & 0 deletions apps/files_external/tests/Settings/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function testGetForm() {
'authMechanisms' => ['g', 'h', 'i'],
'dependencies' => \OCA\Files_External\MountConfig::dependencyMessage($this->backendService->getBackends()),
'allowUserMounting' => true,
'allowUserUnmounting' => true,
'globalCredentials' => 'asdf:asdf',
'globalCredentialsUid' => '',
];
Expand Down
Loading