1
0

AppConfigController.php 4.5 KB

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