SettingsController.php 2.1 KB

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