SettingsController.php 2.0 KB

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