2014-10-31 13:41:07 +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>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2014-10-31 13:41:07 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +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/>
|
2014-10-31 13:41:07 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files_External\Controller;
|
|
|
|
|
2016-01-05 18:56:09 +03:00
|
|
|
use OCA\Files_External\Lib\Auth\AuthMechanism;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\Files_External\Lib\Backend\Backend;
|
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\UserStoragesService;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserSession;
|
2014-10-31 13:41:07 +03:00
|
|
|
|
2015-03-17 13:42:52 +03:00
|
|
|
/**
|
|
|
|
* User storages controller
|
|
|
|
*/
|
2014-10-31 13:41:07 +03:00
|
|
|
class UserStoragesController extends StoragesController {
|
2016-01-05 18:56:09 +03:00
|
|
|
/**
|
|
|
|
* @var IUserSession
|
|
|
|
*/
|
|
|
|
private $userSession;
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
/**
|
2015-03-16 14:18:01 +03: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
|
2016-01-15 17:18:55 +03:00
|
|
|
* @param IUserSession $userSession
|
2016-01-28 15:07:19 +03:00
|
|
|
* @param ILogger $logger
|
2014-10-31 13:41:07 +03:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$AppName,
|
|
|
|
IRequest $request,
|
2015-03-16 14:18:01 +03:00
|
|
|
IL10N $l10n,
|
2016-01-05 18:56:09 +03:00
|
|
|
UserStoragesService $userStoragesService,
|
2016-01-28 15:07:19 +03:00
|
|
|
IUserSession $userSession,
|
|
|
|
ILogger $logger
|
2015-03-17 13:42:52 +03:00
|
|
|
) {
|
2014-10-31 13:41:07 +03:00
|
|
|
parent::__construct(
|
|
|
|
$AppName,
|
|
|
|
$request,
|
|
|
|
$l10n,
|
2016-01-28 15:07:19 +03:00
|
|
|
$userStoragesService,
|
|
|
|
$logger
|
2014-10-31 13:41:07 +03:00
|
|
|
);
|
2016-01-05 18:56:09 +03:00
|
|
|
$this->userSession = $userSession;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 13:41:07 +03:00
|
|
|
}
|
|
|
|
|
2016-01-12 19:46:53 +03:00
|
|
|
/**
|
|
|
|
* Get all storage entries
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function index() {
|
|
|
|
return parent::index();
|
|
|
|
}
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
/**
|
2015-03-17 13:42:52 +03:00
|
|
|
* Return storage
|
|
|
|
*
|
2014-10-31 13:41:07 +03:00
|
|
|
* @NoAdminRequired
|
2015-03-17 13:42:52 +03:00
|
|
|
*
|
|
|
|
* {@inheritdoc}
|
2014-10-31 13:41:07 +03:00
|
|
|
*/
|
2016-06-08 13:48:33 +03:00
|
|
|
public function show($id, $testOnly = true) {
|
2016-06-07 19:25:17 +03:00
|
|
|
return parent::show($id, $testOnly);
|
2014-10-31 13:41:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an external storage entry.
|
|
|
|
*
|
|
|
|
* @param string $mountPoint storage mount point
|
2015-08-12 22:03:11 +03:00
|
|
|
* @param string $backend backend identifier
|
|
|
|
* @param string $authMechanism authentication mechanism identifier
|
2014-10-31 13:41:07 +03:00
|
|
|
* @param array $backendOptions backend-specific options
|
2015-03-13 14:49:11 +03:00
|
|
|
* @param array $mountOptions backend-specific mount options
|
2014-10-31 13:41:07 +03:00
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
|
|
|
public function create(
|
|
|
|
$mountPoint,
|
2015-08-12 22:03:11 +03:00
|
|
|
$backend,
|
|
|
|
$authMechanism,
|
2015-03-13 14:49:11 +03:00
|
|
|
$backendOptions,
|
|
|
|
$mountOptions
|
2014-10-31 13:41:07 +03:00
|
|
|
) {
|
2015-08-11 20:45:07 +03:00
|
|
|
$newStorage = $this->createStorage(
|
|
|
|
$mountPoint,
|
2015-08-12 22:03:11 +03:00
|
|
|
$backend,
|
|
|
|
$authMechanism,
|
2015-08-11 20:45:07 +03:00
|
|
|
$backendOptions,
|
|
|
|
$mountOptions
|
|
|
|
);
|
2020-04-09 15:04:56 +03:00
|
|
|
if ($newStorage instanceof DataResponse) {
|
2015-08-11 20:45:07 +03:00
|
|
|
return $newStorage;
|
|
|
|
}
|
2014-10-31 13:41:07 +03:00
|
|
|
|
2015-09-17 12:24:19 +03:00
|
|
|
$response = $this->validate($newStorage);
|
2014-10-31 13:41:07 +03:00
|
|
|
if (!empty($response)) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
$newStorage = $this->service->addStorage($newStorage);
|
|
|
|
$this->updateStorageStatus($newStorage);
|
|
|
|
|
|
|
|
return new DataResponse(
|
2020-03-12 21:45:23 +03:00
|
|
|
$this->formatStorageForUI($newStorage),
|
2014-10-31 13:41:07 +03:00
|
|
|
Http::STATUS_CREATED
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an external storage entry.
|
|
|
|
*
|
|
|
|
* @param int $id storage id
|
|
|
|
* @param string $mountPoint storage mount point
|
2015-08-12 22:03:11 +03:00
|
|
|
* @param string $backend backend identifier
|
|
|
|
* @param string $authMechanism authentication mechanism identifier
|
2014-10-31 13:41:07 +03:00
|
|
|
* @param array $backendOptions backend-specific options
|
2015-03-13 14:49:11 +03:00
|
|
|
* @param array $mountOptions backend-specific mount options
|
2016-06-17 15:25:29 +03:00
|
|
|
* @param bool $testOnly whether to storage should only test the connection or do more things
|
2014-10-31 13:41:07 +03:00
|
|
|
*
|
|
|
|
* @return DataResponse
|
2015-03-17 13:42:52 +03:00
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
2014-10-31 13:41:07 +03:00
|
|
|
*/
|
|
|
|
public function update(
|
|
|
|
$id,
|
|
|
|
$mountPoint,
|
2015-08-12 22:03:11 +03:00
|
|
|
$backend,
|
|
|
|
$authMechanism,
|
2015-03-13 14:49:11 +03:00
|
|
|
$backendOptions,
|
2016-06-07 17:53:16 +03:00
|
|
|
$mountOptions,
|
2016-06-08 13:48:33 +03:00
|
|
|
$testOnly = true
|
2014-10-31 13:41:07 +03:00
|
|
|
) {
|
2015-08-11 20:45:07 +03:00
|
|
|
$storage = $this->createStorage(
|
|
|
|
$mountPoint,
|
2015-08-12 22:03:11 +03:00
|
|
|
$backend,
|
|
|
|
$authMechanism,
|
2015-08-11 20:45:07 +03:00
|
|
|
$backendOptions,
|
|
|
|
$mountOptions
|
|
|
|
);
|
2020-04-09 15:04:56 +03:00
|
|
|
if ($storage instanceof DataResponse) {
|
2015-08-11 20:45:07 +03:00
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
$storage->setId($id);
|
2014-10-31 13:41:07 +03:00
|
|
|
|
2015-09-17 12:24:19 +03:00
|
|
|
$response = $this->validate($storage);
|
2014-10-31 13:41:07 +03:00
|
|
|
if (!empty($response)) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$storage = $this->service->updateStorage($storage);
|
|
|
|
} 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])
|
2014-10-31 13:41:07 +03:00
|
|
|
],
|
|
|
|
Http::STATUS_NOT_FOUND
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-07 19:25:17 +03:00
|
|
|
$this->updateStorageStatus($storage, $testOnly);
|
2014-10-31 13:41:07 +03:00
|
|
|
|
|
|
|
return new DataResponse(
|
2020-03-12 21:45:23 +03:00
|
|
|
$this->formatStorageForUI($storage),
|
2014-10-31 13:41:07 +03:00
|
|
|
Http::STATUS_OK
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-17 13:42:52 +03:00
|
|
|
* Delete storage
|
|
|
|
*
|
2014-10-31 13:41:07 +03:00
|
|
|
* @NoAdminRequired
|
2015-03-17 13:42:52 +03:00
|
|
|
*
|
|
|
|
* {@inheritdoc}
|
2014-10-31 13:41:07 +03:00
|
|
|
*/
|
|
|
|
public function destroy($id) {
|
|
|
|
return parent::destroy($id);
|
|
|
|
}
|
|
|
|
}
|