2015-09-14 15:57:49 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-09-14 15:57:49 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-09-14 15:57:49 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files_External\Controller;
|
|
|
|
|
2016-01-05 18:56:09 +03:00
|
|
|
use OCA\Files_External\Lib\Auth\AuthMechanism;
|
2016-01-19 18:57:20 +03:00
|
|
|
use OCA\Files_External\Lib\Auth\IUserProvided;
|
2019-09-09 18:14:21 +03:00
|
|
|
use OCA\Files_External\Lib\Auth\Password\UserGlobalAuth;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\Files_External\Lib\Backend\Backend;
|
2016-01-19 14:18:09 +03:00
|
|
|
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
|
2016-05-13 12:56:47 +03:00
|
|
|
use OCA\Files_External\Lib\StorageConfig;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\Files_External\NotFoundException;
|
|
|
|
use OCA\Files_External\Service\UserGlobalStoragesService;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IRequest;
|
2016-01-05 18:56:09 +03:00
|
|
|
use OCP\IUserSession;
|
2015-09-14 15:57:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User global storages controller
|
|
|
|
*/
|
|
|
|
class UserGlobalStoragesController extends StoragesController {
|
2016-01-05 18:56:09 +03:00
|
|
|
/**
|
|
|
|
* @var IUserSession
|
|
|
|
*/
|
|
|
|
private $userSession;
|
|
|
|
|
2015-09-14 15:57:49 +03:00
|
|
|
/**
|
|
|
|
* Creates a new user global storages controller.
|
|
|
|
*
|
|
|
|
* @param string $AppName application name
|
|
|
|
* @param IRequest $request request object
|
|
|
|
* @param IL10N $l10n l10n service
|
|
|
|
* @param UserGlobalStoragesService $userGlobalStoragesService storage service
|
2016-01-05 18:56:09 +03:00
|
|
|
* @param IUserSession $userSession
|
2015-09-14 15:57:49 +03:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$AppName,
|
|
|
|
IRequest $request,
|
|
|
|
IL10N $l10n,
|
2016-01-05 18:56:09 +03:00
|
|
|
UserGlobalStoragesService $userGlobalStoragesService,
|
2016-01-28 15:07:19 +03:00
|
|
|
IUserSession $userSession,
|
|
|
|
ILogger $logger
|
2015-09-14 15:57:49 +03:00
|
|
|
) {
|
|
|
|
parent::__construct(
|
|
|
|
$AppName,
|
|
|
|
$request,
|
|
|
|
$l10n,
|
2016-01-28 15:07:19 +03:00
|
|
|
$userGlobalStoragesService,
|
|
|
|
$logger
|
2015-09-14 15:57:49 +03:00
|
|
|
);
|
2016-01-05 18:56:09 +03:00
|
|
|
$this->userSession = $userSession;
|
2015-09-14 15:57:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all storage entries
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
|
|
|
public function index() {
|
2020-03-12 21:45:23 +03:00
|
|
|
$storages = $this->formatStoragesForUI($this->service->getUniqueStorages());
|
2015-09-14 15:57:49 +03:00
|
|
|
|
|
|
|
// remove configuration data, this must be kept private
|
|
|
|
foreach ($storages as $storage) {
|
|
|
|
$this->sanitizeStorage($storage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DataResponse(
|
|
|
|
$storages,
|
|
|
|
Http::STATUS_OK
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-01-05 18:56:09 +03: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());
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:57:49 +03:00
|
|
|
/**
|
|
|
|
* Get an external storage entry.
|
|
|
|
*
|
|
|
|
* @param int $id storage id
|
2016-06-17 15:25:29 +03:00
|
|
|
* @param bool $testOnly whether to storage should only test the connection or do more things
|
2015-09-14 15:57:49 +03:00
|
|
|
* @return DataResponse
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2016-06-08 13:48:33 +03:00
|
|
|
public function show($id, $testOnly = true) {
|
2015-09-14 15:57:49 +03:00
|
|
|
try {
|
|
|
|
$storage = $this->service->getStorage($id);
|
|
|
|
|
2016-06-07 19:25:17 +03:00
|
|
|
$this->updateStorageStatus($storage, $testOnly);
|
2015-09-14 15:57:49 +03:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return new DataResponse(
|
|
|
|
[
|
2020-03-26 11:30:18 +03:00
|
|
|
'message' => (string)$this->l10n->t('Storage with ID "%d" not found', [$id])
|
2015-09-14 15:57:49 +03:00
|
|
|
],
|
|
|
|
Http::STATUS_NOT_FOUND
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->sanitizeStorage($storage);
|
|
|
|
|
|
|
|
return new DataResponse(
|
2020-03-12 21:45:23 +03:00
|
|
|
$this->formatStorageForUI($storage),
|
2015-09-14 15:57:49 +03:00
|
|
|
Http::STATUS_OK
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-01-19 18:57:20 +03:00
|
|
|
/**
|
|
|
|
* Update an external storage entry.
|
|
|
|
* Only allows setting user provided backend fields
|
|
|
|
*
|
|
|
|
* @param int $id storage id
|
|
|
|
* @param array $backendOptions backend-specific options
|
2016-06-17 15:25:29 +03:00
|
|
|
* @param bool $testOnly whether to storage should only test the connection or do more things
|
2016-01-19 18:57:20 +03:00
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
|
|
|
public function update(
|
|
|
|
$id,
|
2016-06-07 17:53:16 +03:00
|
|
|
$backendOptions,
|
2016-06-08 13:48:33 +03:00
|
|
|
$testOnly = true
|
2016-01-19 18:57:20 +03:00
|
|
|
) {
|
|
|
|
try {
|
|
|
|
$storage = $this->service->getStorage($id);
|
|
|
|
$authMechanism = $storage->getAuthMechanism();
|
2019-09-09 18:14:21 +03:00
|
|
|
if ($authMechanism instanceof IUserProvided || $authMechanism instanceof UserGlobalAuth) {
|
2016-01-19 18:57:20 +03:00
|
|
|
$authMechanism->saveBackendOptions($this->userSession->getUser(), $id, $backendOptions);
|
|
|
|
$authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
|
|
|
|
} else {
|
|
|
|
return new DataResponse(
|
|
|
|
[
|
2020-03-26 11:30:18 +03:00
|
|
|
'message' => (string)$this->l10n->t('Storage with ID "%d" is not user editable', [$id])
|
2016-01-19 18:57:20 +03:00
|
|
|
],
|
|
|
|
Http::STATUS_FORBIDDEN
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return new DataResponse(
|
|
|
|
[
|
2020-03-26 11:30:18 +03:00
|
|
|
'message' => (string)$this->l10n->t('Storage with ID "%d" not found', [$id])
|
2016-01-19 18:57:20 +03:00
|
|
|
],
|
|
|
|
Http::STATUS_NOT_FOUND
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-07 19:25:17 +03:00
|
|
|
$this->updateStorageStatus($storage, $testOnly);
|
2016-01-19 18:57:20 +03:00
|
|
|
$this->sanitizeStorage($storage);
|
|
|
|
|
|
|
|
return new DataResponse(
|
2020-03-12 21:45:23 +03:00
|
|
|
$this->formatStorageForUI($storage),
|
2016-01-19 18:57:20 +03:00
|
|
|
Http::STATUS_OK
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:57:49 +03:00
|
|
|
/**
|
|
|
|
* Remove sensitive data from a StorageConfig before returning it to the user
|
|
|
|
*
|
|
|
|
* @param StorageConfig $storage
|
|
|
|
*/
|
|
|
|
protected function sanitizeStorage(StorageConfig $storage) {
|
|
|
|
$storage->setBackendOptions([]);
|
|
|
|
$storage->setMountOptions([]);
|
2016-01-19 14:18:09 +03:00
|
|
|
|
2016-01-26 15:22:27 +03:00
|
|
|
if ($storage->getAuthMechanism() instanceof IUserProvided) {
|
2016-01-19 14:18:09 +03:00
|
|
|
try {
|
|
|
|
$storage->getAuthMechanism()->manipulateStorageConfig($storage, $this->userSession->getUser());
|
|
|
|
} catch (InsufficientDataForMeaningfulAnswerException $e) {
|
2016-01-26 15:22:27 +03:00
|
|
|
// not configured yet
|
2016-01-19 14:18:09 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-14 15:57:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|