SettingsController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Federation\Controller;
  8. use OCA\Federation\Settings\Admin;
  9. use OCA\Federation\TrustedServers;
  10. use OCP\AppFramework\Controller;
  11. use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\HintException;
  14. use OCP\IL10N;
  15. use OCP\IRequest;
  16. class SettingsController extends Controller {
  17. private IL10N $l;
  18. private TrustedServers $trustedServers;
  19. public function __construct(string $AppName,
  20. IRequest $request,
  21. IL10N $l10n,
  22. TrustedServers $trustedServers
  23. ) {
  24. parent::__construct($AppName, $request);
  25. $this->l = $l10n;
  26. $this->trustedServers = $trustedServers;
  27. }
  28. /**
  29. * Add server to the list of trusted Nextclouds.
  30. *
  31. * @throws HintException
  32. */
  33. #[AuthorizedAdminSetting(settings: Admin::class)]
  34. public function addServer(string $url): DataResponse {
  35. $this->checkServer($url);
  36. $id = $this->trustedServers->addServer($url);
  37. return new DataResponse([
  38. 'url' => $url,
  39. 'id' => $id,
  40. 'message' => $this->l->t('Added to the list of trusted servers')
  41. ]);
  42. }
  43. /**
  44. * Add server to the list of trusted Nextclouds.
  45. */
  46. #[AuthorizedAdminSetting(settings: Admin::class)]
  47. public function removeServer(int $id): DataResponse {
  48. $this->trustedServers->removeServer($id);
  49. return new DataResponse();
  50. }
  51. /**
  52. * Check if the server should be added to the list of trusted servers or not.
  53. *
  54. * @throws HintException
  55. */
  56. #[AuthorizedAdminSetting(settings: Admin::class)]
  57. protected function checkServer(string $url): bool {
  58. if ($this->trustedServers->isTrustedServer($url) === true) {
  59. $message = 'Server is already in the list of trusted servers.';
  60. $hint = $this->l->t('Server is already in the list of trusted servers.');
  61. throw new HintException($message, $hint);
  62. }
  63. if ($this->trustedServers->isNextcloudServer($url) === false) {
  64. $message = 'No server to federate with found';
  65. $hint = $this->l->t('No server to federate with found');
  66. throw new HintException($message, $hint);
  67. }
  68. return true;
  69. }
  70. }