SettingsController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Federation\Controller;
  25. use OCA\Federation\TrustedServers;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\HintException;
  29. use OCP\IL10N;
  30. use OCP\IRequest;
  31. class SettingsController extends Controller {
  32. private IL10N $l;
  33. private TrustedServers $trustedServers;
  34. public function __construct(string $AppName,
  35. IRequest $request,
  36. IL10N $l10n,
  37. TrustedServers $trustedServers
  38. ) {
  39. parent::__construct($AppName, $request);
  40. $this->l = $l10n;
  41. $this->trustedServers = $trustedServers;
  42. }
  43. /**
  44. * Add server to the list of trusted Nextclouds.
  45. *
  46. * @AuthorizedAdminSetting(settings=OCA\Federation\Settings\Admin)
  47. * @throws HintException
  48. */
  49. public function addServer(string $url): DataResponse {
  50. $this->checkServer($url);
  51. $id = $this->trustedServers->addServer($url);
  52. return new DataResponse([
  53. 'url' => $url,
  54. 'id' => $id,
  55. 'message' => $this->l->t('Added to the list of trusted servers')
  56. ]);
  57. }
  58. /**
  59. * Add server to the list of trusted Nextclouds.
  60. *
  61. * @AuthorizedAdminSetting(settings=OCA\Federation\Settings\Admin)
  62. */
  63. public function removeServer(int $id): DataResponse {
  64. $this->trustedServers->removeServer($id);
  65. return new DataResponse();
  66. }
  67. /**
  68. * Check if the server should be added to the list of trusted servers or not.
  69. *
  70. * @AuthorizedAdminSetting(settings=OCA\Federation\Settings\Admin)
  71. * @throws HintException
  72. */
  73. protected function checkServer(string $url): bool {
  74. if ($this->trustedServers->isTrustedServer($url) === true) {
  75. $message = 'Server is already in the list of trusted servers.';
  76. $hint = $this->l->t('Server is already in the list of trusted servers.');
  77. throw new HintException($message, $hint);
  78. }
  79. if ($this->trustedServers->isNextcloudServer($url) === false) {
  80. $message = 'No server to federate with found';
  81. $hint = $this->l->t('No server to federate with found');
  82. throw new HintException($message, $hint);
  83. }
  84. return true;
  85. }
  86. }