Skip to content

Commit

Permalink
disable user unmount
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Nov 6, 2023
1 parent f412c8d commit 1882c4c
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 47 deletions.
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

0 comments on commit 1882c4c

Please sign in to comment.