2014-10-31 10:41:07 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2024-06-06 07:55:47 +00:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-10-31 10:41:07 +00:00
|
|
|
*/
|
|
|
|
namespace OCA\Files_External\Controller;
|
|
|
|
|
2016-01-05 15:56:09 +00:00
|
|
|
use OCA\Files_External\Lib\Auth\AuthMechanism;
|
2019-11-22 19:52:10 +00:00
|
|
|
use OCA\Files_External\Lib\Backend\Backend;
|
2016-05-13 09:56:47 +00:00
|
|
|
use OCA\Files_External\Lib\StorageConfig;
|
2019-11-22 19:52:10 +00:00
|
|
|
use OCA\Files_External\NotFoundException;
|
|
|
|
use OCA\Files_External\Service\UserStoragesService;
|
|
|
|
use OCP\AppFramework\Http;
|
2024-07-25 11:14:46 +00:00
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
2024-09-26 09:35:03 +00:00
|
|
|
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
|
2019-11-22 19:52:10 +00:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2021-12-06 10:18:59 +00:00
|
|
|
use OCP\IConfig;
|
2020-12-11 15:40:29 +00:00
|
|
|
use OCP\IGroupManager;
|
2019-11-22 19:52:10 +00:00
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserSession;
|
2023-07-20 06:42:15 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2014-10-31 10:41:07 +00:00
|
|
|
|
2015-03-17 10:42:52 +00:00
|
|
|
/**
|
|
|
|
* User storages controller
|
|
|
|
*/
|
2014-10-31 10:41:07 +00:00
|
|
|
class UserStoragesController extends StoragesController {
|
|
|
|
/**
|
2015-03-16 11:18:01 +00:00
|
|
|
* Creates a new user storages controller.
|
|
|
|
*
|
|
|
|
* @param string $AppName application name
|
|
|
|
* @param IRequest $request request object
|
|
|
|
* @param IL10N $l10n l10n service
|
|
|
|
* @param UserStoragesService $userStoragesService storage service
|
2023-07-20 06:42:15 +00:00
|
|
|
* @param LoggerInterface $logger
|
2020-12-11 15:40:29 +00:00
|
|
|
* @param IUserSession $userSession
|
|
|
|
* @param IGroupManager $groupManager
|
2014-10-31 10:41:07 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$AppName,
|
|
|
|
IRequest $request,
|
2015-03-16 11:18:01 +00:00
|
|
|
IL10N $l10n,
|
2016-01-05 15:56:09 +00:00
|
|
|
UserStoragesService $userStoragesService,
|
2023-07-20 06:42:15 +00:00
|
|
|
LoggerInterface $logger,
|
2016-01-28 12:07:19 +00:00
|
|
|
IUserSession $userSession,
|
2021-12-06 10:18:59 +00:00
|
|
|
IGroupManager $groupManager,
|
|
|
|
IConfig $config,
|
2015-03-17 10:42:52 +00:00
|
|
|
) {
|
2014-10-31 10:41:07 +00:00
|
|
|
parent::__construct(
|
|
|
|
$AppName,
|
|
|
|
$request,
|
|
|
|
$l10n,
|
2016-01-28 12:07:19 +00:00
|
|
|
$userStoragesService,
|
2020-12-11 15:40:29 +00:00
|
|
|
$logger,
|
|
|
|
$userSession,
|
2021-12-06 10:18:59 +00:00
|
|
|
$groupManager,
|
|
|
|
$config
|
2014-10-31 10:41:07 +00:00
|
|
|
);
|
2016-01-05 15:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function manipulateStorageConfig(StorageConfig $storage) {
|
|
|
|
/** @var AuthMechanism */
|
|
|
|
$authMechanism = $storage->getAuthMechanism();
|
|
|
|
$authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
|
|
|
|
/** @var Backend */
|
|
|
|
$backend = $storage->getBackend();
|
|
|
|
$backend->manipulateStorageConfig($storage, $this->userSession->getUser());
|
2014-10-31 10:41:07 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 16:46:53 +00:00
|
|
|
/**
|
|
|
|
* Get all storage entries
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
2024-07-25 11:14:46 +00:00
|
|
|
#[NoAdminRequired]
|
2016-01-12 16:46:53 +00:00
|
|
|
public function index() {
|
|
|
|
return parent::index();
|
|
|
|
}
|
|
|
|
|
2014-10-31 10:41:07 +00:00
|
|
|
/**
|
2015-03-17 10:42:52 +00:00
|
|
|
* Return storage
|
|
|
|
*
|
|
|
|
* {@inheritdoc}
|
2014-10-31 10:41:07 +00:00
|
|
|
*/
|
2024-07-25 11:14:46 +00:00
|
|
|
#[NoAdminRequired]
|
2024-11-25 08:42:53 +00:00
|
|
|
public function show(int $id, $testOnly = true) {
|
2016-06-07 16:25:17 +00:00
|
|
|
return parent::show($id, $testOnly);
|
2014-10-31 10:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an external storage entry.
|
|
|
|
*
|
|
|
|
* @param string $mountPoint storage mount point
|
2015-08-12 19:03:11 +00:00
|
|
|
* @param string $backend backend identifier
|
|
|
|
* @param string $authMechanism authentication mechanism identifier
|
2014-10-31 10:41:07 +00:00
|
|
|
* @param array $backendOptions backend-specific options
|
2015-03-13 11:49:11 +00:00
|
|
|
* @param array $mountOptions backend-specific mount options
|
2014-10-31 10:41:07 +00:00
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
2024-07-25 11:14:46 +00:00
|
|
|
#[NoAdminRequired]
|
2024-11-27 16:01:25 +00:00
|
|
|
#[PasswordConfirmationRequired(strict: true)]
|
2014-10-31 10:41:07 +00:00
|
|
|
public function create(
|
|
|
|
$mountPoint,
|
2015-08-12 19:03:11 +00:00
|
|
|
$backend,
|
|
|
|
$authMechanism,
|
2015-03-13 11:49:11 +00:00
|
|
|
$backendOptions,
|
|
|
|
$mountOptions,
|
2014-10-31 10:41:07 +00:00
|
|
|
) {
|
2021-12-06 10:18:59 +00:00
|
|
|
$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
|
|
|
|
);
|
|
|
|
}
|
2015-08-11 17:45:07 +00:00
|
|
|
$newStorage = $this->createStorage(
|
|
|
|
$mountPoint,
|
2015-08-12 19:03:11 +00:00
|
|
|
$backend,
|
|
|
|
$authMechanism,
|
2015-08-11 17:45:07 +00:00
|
|
|
$backendOptions,
|
|
|
|
$mountOptions
|
|
|
|
);
|
2020-04-09 12:04:56 +00:00
|
|
|
if ($newStorage instanceof DataResponse) {
|
2015-08-11 17:45:07 +00:00
|
|
|
return $newStorage;
|
|
|
|
}
|
2014-10-31 10:41:07 +00:00
|
|
|
|
2015-09-17 09:24:19 +00:00
|
|
|
$response = $this->validate($newStorage);
|
2014-10-31 10:41:07 +00:00
|
|
|
if (!empty($response)) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
$newStorage = $this->service->addStorage($newStorage);
|
|
|
|
$this->updateStorageStatus($newStorage);
|
|
|
|
|
|
|
|
return new DataResponse(
|
2023-07-13 07:58:24 +00:00
|
|
|
$newStorage->jsonSerialize(true),
|
2014-10-31 10:41:07 +00:00
|
|
|
Http::STATUS_CREATED
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an external storage entry.
|
|
|
|
*
|
|
|
|
* @param int $id storage id
|
|
|
|
* @param string $mountPoint storage mount point
|
2015-08-12 19:03:11 +00:00
|
|
|
* @param string $backend backend identifier
|
|
|
|
* @param string $authMechanism authentication mechanism identifier
|
2014-10-31 10:41:07 +00:00
|
|
|
* @param array $backendOptions backend-specific options
|
2015-03-13 11:49:11 +00:00
|
|
|
* @param array $mountOptions backend-specific mount options
|
2016-06-17 12:25:29 +00:00
|
|
|
* @param bool $testOnly whether to storage should only test the connection or do more things
|
2014-10-31 10:41:07 +00:00
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
2024-07-25 11:14:46 +00:00
|
|
|
#[NoAdminRequired]
|
2024-11-27 16:01:25 +00:00
|
|
|
#[PasswordConfirmationRequired(strict: true)]
|
2014-10-31 10:41:07 +00:00
|
|
|
public function update(
|
|
|
|
$id,
|
|
|
|
$mountPoint,
|
2015-08-12 19:03:11 +00:00
|
|
|
$backend,
|
|
|
|
$authMechanism,
|
2015-03-13 11:49:11 +00:00
|
|
|
$backendOptions,
|
2016-06-07 14:53:16 +00:00
|
|
|
$mountOptions,
|
2016-06-08 10:48:33 +00:00
|
|
|
$testOnly = true,
|
2014-10-31 10:41:07 +00:00
|
|
|
) {
|
2015-08-11 17:45:07 +00:00
|
|
|
$storage = $this->createStorage(
|
|
|
|
$mountPoint,
|
2015-08-12 19:03:11 +00:00
|
|
|
$backend,
|
|
|
|
$authMechanism,
|
2015-08-11 17:45:07 +00:00
|
|
|
$backendOptions,
|
|
|
|
$mountOptions
|
|
|
|
);
|
2020-04-09 12:04:56 +00:00
|
|
|
if ($storage instanceof DataResponse) {
|
2015-08-11 17:45:07 +00:00
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
$storage->setId($id);
|
2014-10-31 10:41:07 +00:00
|
|
|
|
2015-09-17 09:24:19 +00:00
|
|
|
$response = $this->validate($storage);
|
2014-10-31 10:41:07 +00:00
|
|
|
if (!empty($response)) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$storage = $this->service->updateStorage($storage);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return new DataResponse(
|
|
|
|
[
|
2021-01-11 11:57:03 +00:00
|
|
|
'message' => $this->l10n->t('Storage with ID "%d" not found', [$id])
|
2014-10-31 10:41:07 +00:00
|
|
|
],
|
|
|
|
Http::STATUS_NOT_FOUND
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-07 16:25:17 +00:00
|
|
|
$this->updateStorageStatus($storage, $testOnly);
|
2014-10-31 10:41:07 +00:00
|
|
|
|
|
|
|
return new DataResponse(
|
2023-07-13 07:58:24 +00:00
|
|
|
$storage->jsonSerialize(true),
|
2014-10-31 10:41:07 +00:00
|
|
|
Http::STATUS_OK
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-17 10:42:52 +00:00
|
|
|
* Delete storage
|
|
|
|
*
|
|
|
|
* {@inheritdoc}
|
2014-10-31 10:41:07 +00:00
|
|
|
*/
|
2024-07-25 11:14:46 +00:00
|
|
|
#[NoAdminRequired]
|
2024-11-27 16:01:25 +00:00
|
|
|
#[PasswordConfirmationRequired(strict: true)]
|
2024-11-25 08:42:53 +00:00
|
|
|
public function destroy(int $id) {
|
2014-10-31 10:41:07 +00:00
|
|
|
return parent::destroy($id);
|
|
|
|
}
|
|
|
|
}
|