diff options
Diffstat (limited to 'apps/federation/lib')
-rw-r--r-- | apps/federation/lib/AppInfo/Application.php | 3 | ||||
-rw-r--r-- | apps/federation/lib/Controller/SettingsController.php | 91 | ||||
-rw-r--r-- | apps/federation/lib/Middleware/AddServerMiddleware.php | 55 | ||||
-rw-r--r-- | apps/federation/lib/TrustedServers.php | 23 |
4 files changed, 54 insertions, 118 deletions
diff --git a/apps/federation/lib/AppInfo/Application.php b/apps/federation/lib/AppInfo/Application.php index c7503921164..358e3f68d50 100644 --- a/apps/federation/lib/AppInfo/Application.php +++ b/apps/federation/lib/AppInfo/Application.php @@ -9,7 +9,6 @@ namespace OCA\Federation\AppInfo; use OCA\DAV\Events\SabrePluginAuthInitEvent; use OCA\Federation\Listener\SabrePluginAuthInitListener; -use OCA\Federation\Middleware\AddServerMiddleware; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; @@ -25,8 +24,6 @@ class Application extends App implements IBootstrap { } public function register(IRegistrationContext $context): void { - $context->registerMiddleware(AddServerMiddleware::class); - $context->registerEventListener(SabrePluginAuthInitEvent::class, SabrePluginAuthInitListener::class); } diff --git a/apps/federation/lib/Controller/SettingsController.php b/apps/federation/lib/Controller/SettingsController.php index 5b2ee257e14..27341eba815 100644 --- a/apps/federation/lib/Controller/SettingsController.php +++ b/apps/federation/lib/Controller/SettingsController.php @@ -10,11 +10,15 @@ namespace OCA\Federation\Controller; use OCA\Federation\Settings\Admin; use OCA\Federation\TrustedServers; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\Attribute\ApiRoute; use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting; -use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSException; +use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\AppFramework\OCSController; use OCP\IL10N; use OCP\IRequest; +use Psr\Log\LoggerInterface; class SettingsController extends OCSController { public function __construct( @@ -22,6 +26,7 @@ class SettingsController extends OCSController { IRequest $request, private IL10N $l, private TrustedServers $trustedServers, + private LoggerInterface $logger, ) { parent::__construct($AppName, $request); } @@ -31,28 +36,23 @@ class SettingsController extends OCSController { * Add server to the list of trusted Nextcloud servers * * @param string $url The URL of the server to add - * @return JSONResponse<Http::STATUS_OK, array{data: array{id: int, message: string, url: string}, status: 'ok'}, array{}>|JSONResponse<Http::STATUS_NOT_FOUND|Http::STATUS_CONFLICT, array{data: array{hint: string, message: string}, status: 'error'}, array{}> + * @return DataResponse<Http::STATUS_OK, array{id: int, message: string, url: string}, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_CONFLICT, array{message: string}, array{}> * * 200: Server added successfully * 404: Server not found at the given URL * 409: Server is already in the list of trusted servers */ #[AuthorizedAdminSetting(settings: Admin::class)] - public function addServer(string $url): JSONResponse { - $check = $this->checkServer(trim($url)); - if ($check instanceof JSONResponse) { - return $check; - } + #[ApiRoute(verb: 'POST', url: '/trusted-servers')] + public function addServer(string $url): DataResponse { + $this->checkServer(trim($url)); // Add the server to the list of trusted servers, all is well $id = $this->trustedServers->addServer(trim($url)); - return new JSONResponse([ - 'status' => 'ok', - 'data' => [ - 'url' => $url, - 'id' => $id, - 'message' => $this->l->t('Added to the list of trusted servers') - ], + return new DataResponse([ + 'url' => $url, + 'id' => $id, + 'message' => $this->l->t('Added to the list of trusted servers') ]); } @@ -60,38 +60,39 @@ class SettingsController extends OCSController { * Add server to the list of trusted Nextcloud servers * * @param int $id The ID of the trusted server to remove - * @return JSONResponse<Http::STATUS_OK, array{data: array{id: int}, status: 'ok'}, array{}>|JSONResponse<Http::STATUS_NOT_FOUND, array{data: array{message: string}, status: 'error'}, array{}> + * @return DataResponse<Http::STATUS_OK, array{id: int}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{message: string}, array{}> * * 200: Server removed successfully * 404: Server not found at the given ID */ #[AuthorizedAdminSetting(settings: Admin::class)] - public function removeServer(int $id): JSONResponse { + #[ApiRoute(verb: 'DELETE', url: '/trusted-servers/{id}', requirements: ['id' => '\d+'])] + public function removeServer(int $id): DataResponse { + try { + $this->trustedServers->getServer($id); + } catch (\Exception $e) { + throw new OCSNotFoundException($this->l->t('No server found with ID: %s', [$id])); + } + try { $this->trustedServers->removeServer($id); - return new JSONResponse([ - 'status' => 'ok', - 'data' => ['id' => $id], - ]); + return new DataResponse(['id' => $id]); } catch (\Exception $e) { - return new JSONResponse([ - 'status' => 'error', - 'data' => [ - 'message' => $e->getMessage(), - ], - ], Http::STATUS_NOT_FOUND); + $this->logger->error($e->getMessage(), ['e' => $e]); + throw new OCSException($this->l->t('Could not remove server'), Http::STATUS_INTERNAL_SERVER_ERROR); } } /** * List all trusted servers * - * @return JSONResponse<Http::STATUS_OK, array{data: list<array{id: int, status: int, url: string}>, status: 'ok'}, array{}> + * @return DataResponse<Http::STATUS_OK, list<array{id: int, status: int, url: string}>, array{}> * * 200: List of trusted servers */ #[AuthorizedAdminSetting(settings: Admin::class)] - public function getServers(): JSONResponse { + #[ApiRoute(verb: 'GET', url: '/trusted-servers')] + public function getServers(): DataResponse { $servers = $this->trustedServers->getServers(); // obfuscate the shared secret @@ -104,47 +105,21 @@ class SettingsController extends OCSController { }, $servers); // return the list of trusted servers - return new JSONResponse([ - 'status' => 'ok', - 'data' => $servers, - ]); + return new DataResponse($servers); } /** * Check if the server should be added to the list of trusted servers or not. - * - * @return JSONResponse<Http::STATUS_NOT_FOUND|Http::STATUS_CONFLICT, array{data: array{hint: string, message: string}, status: 'error'}, array{}>|null - * - * 404: Server not found at the given URL - * 409: Server is already in the list of trusted servers */ #[AuthorizedAdminSetting(settings: Admin::class)] - protected function checkServer(string $url): ?JSONResponse { + protected function checkServer(string $url): void { if ($this->trustedServers->isTrustedServer($url) === true) { - $message = 'Server is already in the list of trusted servers.'; - $hint = $this->l->t('Server is already in the list of trusted servers.'); - return new JSONResponse([ - 'status' => 'error', - 'data' => [ - 'message' => $message, - 'hint' => $hint, - ], - ], Http::STATUS_CONFLICT); + throw new OCSException($this->l->t('Server is already in the list of trusted servers.'), Http::STATUS_CONFLICT); } if ($this->trustedServers->isNextcloudServer($url) === false) { - $message = 'No server to federate with found'; - $hint = $this->l->t('No server to federate with found'); - return new JSONResponse([ - 'status' => 'error', - 'data' => [ - 'message' => $message, - 'hint' => $hint, - ], - ], Http::STATUS_NOT_FOUND); + throw new OCSNotFoundException($this->l->t('No server to federate with found')); } - - return null; } } diff --git a/apps/federation/lib/Middleware/AddServerMiddleware.php b/apps/federation/lib/Middleware/AddServerMiddleware.php deleted file mode 100644 index 0e38dd8e700..00000000000 --- a/apps/federation/lib/Middleware/AddServerMiddleware.php +++ /dev/null @@ -1,55 +0,0 @@ -<?php - -/** - * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2016 ownCloud, Inc. - * SPDX-License-Identifier: AGPL-3.0-only - */ -namespace OCA\Federation\Middleware; - -use OCA\Federation\Controller\SettingsController; -use OCP\AppFramework\Controller; -use OCP\AppFramework\Http; -use OCP\AppFramework\Http\JSONResponse; -use OCP\AppFramework\Middleware; -use OCP\HintException; -use OCP\IL10N; -use Psr\Log\LoggerInterface; - -class AddServerMiddleware extends Middleware { - public function __construct( - protected string $appName, - protected IL10N $l, - protected LoggerInterface $logger, - ) { - } - - /** - * Log error message and return a response which can be displayed to the user - * - * @param Controller $controller - * @param string $methodName - * @param \Exception $exception - * @return JSONResponse - * @throws \Exception - */ - public function afterException($controller, $methodName, \Exception $exception) { - if (($controller instanceof SettingsController) === false) { - throw $exception; - } - $this->logger->error($exception->getMessage(), [ - 'app' => $this->appName, - 'exception' => $exception, - ]); - if ($exception instanceof HintException) { - $message = $exception->getHint(); - } else { - $message = $exception->getMessage(); - } - - return new JSONResponse( - ['message' => $message], - Http::STATUS_BAD_REQUEST - ); - } -} diff --git a/apps/federation/lib/TrustedServers.php b/apps/federation/lib/TrustedServers.php index 231b892fc3e..a62ddfa1275 100644 --- a/apps/federation/lib/TrustedServers.php +++ b/apps/federation/lib/TrustedServers.php @@ -96,9 +96,9 @@ class TrustedServers { * Get all trusted servers * * @return list<array{id: int, url: string, url_hash: string, shared_secret: ?string, status: int, sync_token: ?string}> - * @throws Exception + * @throws \Exception */ - public function getServers() { + public function getServers(): ?array { if ($this->trustedServersCache === null) { $this->trustedServersCache = $this->dbHandler->getAllServer(); } @@ -106,6 +106,25 @@ class TrustedServers { } /** + * Get a trusted server + * + * @return array{id: int, url: string, url_hash: string, shared_secret: ?string, status: int, sync_token: ?string} + * @throws Exception + */ + public function getServer(int $id): ?array { + if ($this->trustedServersCache === null) { + $this->trustedServersCache = $this->dbHandler->getAllServer(); + } + + $server = array_filter($this->trustedServersCache, fn ($server) => $server['id'] === $id); + if (empty($server)) { + throw new \Exception('No server found with ID: ' . $id); + } + + return $server[0]; + } + + /** * Check if given server is a trusted Nextcloud server */ public function isTrustedServer(string $url): bool { |