AppConfigController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  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
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Provisioning_API\Controller;
  25. use OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http\DataResponse;
  27. use OCP\AppFramework\OCSController;
  28. use OCP\IAppConfig;
  29. use OCP\IConfig;
  30. use OCP\IRequest;
  31. class AppConfigController extends OCSController {
  32. /** @var IConfig */
  33. protected $config;
  34. /** @var IAppConfig */
  35. protected $appConfig;
  36. /**
  37. * @param string $appName
  38. * @param IRequest $request
  39. * @param IConfig $config
  40. * @param IAppConfig $appConfig
  41. */
  42. public function __construct(string $appName,
  43. IRequest $request,
  44. IConfig $config,
  45. IAppConfig $appConfig) {
  46. parent::__construct($appName, $request);
  47. $this->config = $config;
  48. $this->appConfig = $appConfig;
  49. }
  50. /**
  51. * @return DataResponse
  52. */
  53. public function getApps(): DataResponse {
  54. return new DataResponse([
  55. 'data' => $this->appConfig->getApps(),
  56. ]);
  57. }
  58. /**
  59. * @param string $app
  60. * @return DataResponse
  61. */
  62. public function getKeys(string $app): DataResponse {
  63. try {
  64. $this->verifyAppId($app);
  65. } catch (\InvalidArgumentException $e) {
  66. return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
  67. }
  68. return new DataResponse([
  69. 'data' => $this->config->getAppKeys($app),
  70. ]);
  71. }
  72. /**
  73. * @param string $app
  74. * @param string $key
  75. * @param string $defaultValue
  76. * @return DataResponse
  77. */
  78. public function getValue(string $app, string $key, string $defaultValue = ''): DataResponse {
  79. try {
  80. $this->verifyAppId($app);
  81. } catch (\InvalidArgumentException $e) {
  82. return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
  83. }
  84. return new DataResponse([
  85. 'data' => $this->config->getAppValue($app, $key, $defaultValue),
  86. ]);
  87. }
  88. /**
  89. * @PasswordConfirmationRequired
  90. * @param string $app
  91. * @param string $key
  92. * @param string $value
  93. * @return DataResponse
  94. */
  95. public function setValue(string $app, string $key, string $value): DataResponse {
  96. try {
  97. $this->verifyAppId($app);
  98. $this->verifyConfigKey($app, $key, $value);
  99. } catch (\InvalidArgumentException $e) {
  100. return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
  101. }
  102. $this->config->setAppValue($app, $key, $value);
  103. return new DataResponse();
  104. }
  105. /**
  106. * @PasswordConfirmationRequired
  107. * @param string $app
  108. * @param string $key
  109. * @return DataResponse
  110. */
  111. public function deleteKey(string $app, string $key): DataResponse {
  112. try {
  113. $this->verifyAppId($app);
  114. $this->verifyConfigKey($app, $key, '');
  115. } catch (\InvalidArgumentException $e) {
  116. return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
  117. }
  118. $this->config->deleteAppValue($app, $key);
  119. return new DataResponse();
  120. }
  121. /**
  122. * @param string $app
  123. * @throws \InvalidArgumentException
  124. */
  125. protected function verifyAppId(string $app) {
  126. if (\OC_App::cleanAppId($app) !== $app) {
  127. throw new \InvalidArgumentException('Invalid app id given');
  128. }
  129. }
  130. /**
  131. * @param string $app
  132. * @param string $key
  133. * @param string $value
  134. * @throws \InvalidArgumentException
  135. */
  136. protected function verifyConfigKey(string $app, string $key, string $value) {
  137. if (in_array($key, ['installed_version', 'enabled', 'types'])) {
  138. throw new \InvalidArgumentException('The given key can not be set');
  139. }
  140. if ($app === 'core' && $key === 'encryption_enabled' && $value !== 'yes') {
  141. throw new \InvalidArgumentException('The given key can not be set');
  142. }
  143. if ($app === 'core' && (strpos($key, 'public_') === 0 || strpos($key, 'remote_') === 0)) {
  144. throw new \InvalidArgumentException('The given key can not be set');
  145. }
  146. }
  147. }