SettingsController.php 3.1 KB

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