2015-10-29 12:27:14 -04:00
|
|
|
<?php
|
2024-05-28 10:42:42 -04:00
|
|
|
|
2015-10-29 12:27:14 -04:00
|
|
|
/**
|
2024-05-28 10:42:42 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-10-29 12:27:14 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Federation\Controller;
|
|
|
|
|
|
|
|
|
|
use OCA\Federation\TrustedServers;
|
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2021-06-29 19:20:33 -04:00
|
|
|
use OCP\HintException;
|
2015-10-29 12:27:14 -04:00
|
|
|
use OCP\IL10N;
|
|
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
|
|
|
|
class SettingsController extends Controller {
|
2022-06-24 09:24:16 -04:00
|
|
|
private IL10N $l;
|
|
|
|
|
private TrustedServers $trustedServers;
|
2015-10-29 12:27:14 -04:00
|
|
|
|
2022-06-24 09:24:16 -04:00
|
|
|
public function __construct(string $AppName,
|
2023-11-23 04:22:34 -05:00
|
|
|
IRequest $request,
|
|
|
|
|
IL10N $l10n,
|
|
|
|
|
TrustedServers $trustedServers
|
2015-10-29 12:27:14 -04:00
|
|
|
) {
|
|
|
|
|
parent::__construct($AppName, $request);
|
|
|
|
|
$this->l = $l10n;
|
|
|
|
|
$this->trustedServers = $trustedServers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-14 09:07:14 -04:00
|
|
|
* Add server to the list of trusted Nextclouds.
|
2015-10-29 12:27:14 -04:00
|
|
|
*
|
2021-10-14 09:07:14 -04:00
|
|
|
* @AuthorizedAdminSetting(settings=OCA\Federation\Settings\Admin)
|
2015-10-29 12:27:14 -04:00
|
|
|
* @throws HintException
|
|
|
|
|
*/
|
2022-06-24 09:24:16 -04:00
|
|
|
public function addServer(string $url): DataResponse {
|
2015-10-29 12:27:14 -04:00
|
|
|
$this->checkServer($url);
|
|
|
|
|
$id = $this->trustedServers->addServer($url);
|
|
|
|
|
|
2022-06-24 09:24:16 -04:00
|
|
|
return new DataResponse([
|
|
|
|
|
'url' => $url,
|
|
|
|
|
'id' => $id,
|
|
|
|
|
'message' => $this->l->t('Added to the list of trusted servers')
|
|
|
|
|
]);
|
2015-10-29 12:27:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-14 09:07:14 -04:00
|
|
|
* Add server to the list of trusted Nextclouds.
|
2015-10-29 12:27:14 -04:00
|
|
|
*
|
2021-10-14 09:07:14 -04:00
|
|
|
* @AuthorizedAdminSetting(settings=OCA\Federation\Settings\Admin)
|
2015-10-29 12:27:14 -04:00
|
|
|
*/
|
2022-06-24 09:24:16 -04:00
|
|
|
public function removeServer(int $id): DataResponse {
|
2015-10-29 12:27:14 -04:00
|
|
|
$this->trustedServers->removeServer($id);
|
|
|
|
|
return new DataResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-14 09:07:14 -04:00
|
|
|
* Check if the server should be added to the list of trusted servers or not.
|
2015-10-29 12:27:14 -04:00
|
|
|
*
|
2021-10-14 09:07:14 -04:00
|
|
|
* @AuthorizedAdminSetting(settings=OCA\Federation\Settings\Admin)
|
2015-10-29 12:27:14 -04:00
|
|
|
* @throws HintException
|
|
|
|
|
*/
|
2022-06-28 05:26:30 -04:00
|
|
|
protected function checkServer(string $url): bool {
|
2015-10-29 12:27:14 -04:00
|
|
|
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.');
|
|
|
|
|
throw new HintException($message, $hint);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 09:24:16 -04:00
|
|
|
if ($this->trustedServers->isNextcloudServer($url) === false) {
|
2016-08-08 08:56:24 -04:00
|
|
|
$message = 'No server to federate with found';
|
|
|
|
|
$hint = $this->l->t('No server to federate with found');
|
2015-10-29 12:27:14 -04:00
|
|
|
throw new HintException($message, $hint);
|
|
|
|
|
}
|
2022-06-28 05:26:30 -04:00
|
|
|
|
|
|
|
|
return true;
|
2015-10-29 12:27:14 -04:00
|
|
|
}
|
|
|
|
|
}
|