CORSSettingsController.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Ferdinand Thiessen <opensource@fthiessend.de>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License
  10. * as published by the Free Software Foundation,
  11. * either version 3 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. */
  21. namespace OCA\Settings\Controller;
  22. use OCP\AppFramework\Controller;
  23. use OCP\AppFramework\Http;
  24. use OCP\AppFramework\Http\DataResponse;
  25. use OCP\IConfig;
  26. use OCP\IRequest;
  27. use OCP\Util;
  28. class CORSSettingsController extends Controller {
  29. /**
  30. * @param string $appName
  31. * @param IRequest $request
  32. * @param IConfig $config
  33. */
  34. public function __construct(
  35. $appName,
  36. IRequest $request,
  37. private IConfig $config,
  38. ) {
  39. parent::__construct($appName, $request);
  40. }
  41. /**
  42. * Set whether users can configure their own list of allowed CORS domains
  43. *
  44. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Security)
  45. *
  46. * @param bool $value
  47. * @return DataResponse
  48. */
  49. public function updateUserEnabled($value) {
  50. if (!is_bool($value)) {
  51. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  52. }
  53. $this->config->setSystemValue('cors.allow-user-domains', $value);
  54. return new DataResponse();
  55. }
  56. /**
  57. * Set list of globally allowed CORS domains
  58. *
  59. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Security)
  60. *
  61. * @param array $value
  62. * @return DataResponse
  63. */
  64. public function allowedDomains(array $value) {
  65. try {
  66. foreach ($value as $entry) {
  67. if (!is_string($entry) || $entry === '' || Util::getFullDomain($entry) === '') {
  68. return new DataResponse([], HTTP::STATUS_BAD_REQUEST);
  69. }
  70. }
  71. } catch (\InvalidArgumentException $e) {
  72. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  73. }
  74. $this->config->setSystemValue('cors.allowed-domains', $value);
  75. return new DataResponse();
  76. }
  77. }