1
0

AppConfigController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Provisioning_API\Controller;
  8. use OC\AppConfig;
  9. use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\DataResponse;
  12. use OCP\AppFramework\OCSController;
  13. use OCP\IAppConfig;
  14. use OCP\IGroupManager;
  15. use OCP\IL10N;
  16. use OCP\IRequest;
  17. use OCP\IUser;
  18. use OCP\IUserSession;
  19. use OCP\Settings\IDelegatedSettings;
  20. use OCP\Settings\IManager;
  21. class AppConfigController extends OCSController {
  22. public function __construct(
  23. string $appName,
  24. IRequest $request,
  25. /** @var AppConfig */
  26. private IAppConfig $appConfig,
  27. private IUserSession $userSession,
  28. private IL10N $l10n,
  29. private IGroupManager $groupManager,
  30. private IManager $settingManager,
  31. ) {
  32. parent::__construct($appName, $request);
  33. }
  34. /**
  35. * Get a list of apps
  36. *
  37. * @return DataResponse<Http::STATUS_OK, array{data: string[]}, array{}>
  38. *
  39. * 200: Apps returned
  40. */
  41. public function getApps(): DataResponse {
  42. return new DataResponse([
  43. 'data' => $this->appConfig->getApps(),
  44. ]);
  45. }
  46. /**
  47. * Get the config keys of an app
  48. *
  49. * @param string $app ID of the app
  50. * @return DataResponse<Http::STATUS_OK, array{data: string[]}, array{}>|DataResponse<Http::STATUS_FORBIDDEN, array{data: array{message: string}}, array{}>
  51. *
  52. * 200: Keys returned
  53. * 403: App is not allowed
  54. */
  55. public function getKeys(string $app): DataResponse {
  56. try {
  57. $this->verifyAppId($app);
  58. } catch (\InvalidArgumentException $e) {
  59. return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
  60. }
  61. return new DataResponse([
  62. 'data' => $this->appConfig->getKeys($app),
  63. ]);
  64. }
  65. /**
  66. * Get a the config value of an app
  67. *
  68. * @param string $app ID of the app
  69. * @param string $key Key
  70. * @param string $defaultValue Default returned value if the value is empty
  71. * @return DataResponse<Http::STATUS_OK, array{data: string}, array{}>|DataResponse<Http::STATUS_FORBIDDEN, array{data: array{message: string}}, array{}>
  72. *
  73. * 200: Value returned
  74. * 403: App is not allowed
  75. */
  76. public function getValue(string $app, string $key, string $defaultValue = ''): DataResponse {
  77. try {
  78. $this->verifyAppId($app);
  79. } catch (\InvalidArgumentException $e) {
  80. return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
  81. }
  82. /** @psalm-suppress InternalMethod */
  83. $value = $this->appConfig->getValueMixed($app, $key, $defaultValue, null);
  84. return new DataResponse(['data' => $value]);
  85. }
  86. /**
  87. * @PasswordConfirmationRequired
  88. * @NoSubAdminRequired
  89. * @NoAdminRequired
  90. *
  91. * Update the config value of an app
  92. *
  93. * @param string $app ID of the app
  94. * @param string $key Key to update
  95. * @param string $value New value for the key
  96. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>|DataResponse<Http::STATUS_FORBIDDEN, array{data: array{message: string}}, array{}>
  97. *
  98. * 200: Value updated successfully
  99. * 403: App or key is not allowed
  100. */
  101. public function setValue(string $app, string $key, string $value): DataResponse {
  102. $user = $this->userSession->getUser();
  103. if ($user === null) {
  104. throw new \Exception("User is not logged in."); // Should not happen, since method is guarded by middleware
  105. }
  106. if (!$this->isAllowedToChangedKey($user, $app, $key)) {
  107. throw new NotAdminException($this->l10n->t('Logged in account must be an administrator or have authorization to edit this setting.'));
  108. }
  109. try {
  110. $this->verifyAppId($app);
  111. $this->verifyConfigKey($app, $key, $value);
  112. } catch (\InvalidArgumentException $e) {
  113. return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
  114. }
  115. /** @psalm-suppress InternalMethod */
  116. $this->appConfig->setValueMixed($app, $key, $value);
  117. return new DataResponse();
  118. }
  119. /**
  120. * @PasswordConfirmationRequired
  121. *
  122. * Delete a config key of an app
  123. *
  124. * @param string $app ID of the app
  125. * @param string $key Key to delete
  126. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>|DataResponse<Http::STATUS_FORBIDDEN, array{data: array{message: string}}, array{}>
  127. *
  128. * 200: Key deleted successfully
  129. * 403: App or key is not allowed
  130. */
  131. public function deleteKey(string $app, string $key): DataResponse {
  132. try {
  133. $this->verifyAppId($app);
  134. $this->verifyConfigKey($app, $key, '');
  135. } catch (\InvalidArgumentException $e) {
  136. return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
  137. }
  138. $this->appConfig->deleteKey($app, $key);
  139. return new DataResponse();
  140. }
  141. /**
  142. * @param string $app
  143. * @throws \InvalidArgumentException
  144. */
  145. protected function verifyAppId(string $app) {
  146. if (\OC_App::cleanAppId($app) !== $app) {
  147. throw new \InvalidArgumentException('Invalid app id given');
  148. }
  149. }
  150. /**
  151. * @param string $app
  152. * @param string $key
  153. * @param string $value
  154. * @throws \InvalidArgumentException
  155. */
  156. protected function verifyConfigKey(string $app, string $key, string $value) {
  157. if (in_array($key, ['installed_version', 'enabled', 'types'])) {
  158. throw new \InvalidArgumentException('The given key can not be set');
  159. }
  160. if ($app === 'core' && $key === 'encryption_enabled' && $value !== 'yes') {
  161. throw new \InvalidArgumentException('The given key can not be set');
  162. }
  163. if ($app === 'core' && (strpos($key, 'public_') === 0 || strpos($key, 'remote_') === 0)) {
  164. throw new \InvalidArgumentException('The given key can not be set');
  165. }
  166. if ($app === 'files'
  167. && $key === 'default_quota'
  168. && $value === 'none'
  169. && $this->appConfig->getValueInt('files', 'allow_unlimited_quota', 1) === 0) {
  170. throw new \InvalidArgumentException('The given key can not be set, unlimited quota is forbidden on this instance');
  171. }
  172. }
  173. private function isAllowedToChangedKey(IUser $user, string $app, string $key): bool {
  174. // Admin right verification
  175. $isAdmin = $this->groupManager->isAdmin($user->getUID());
  176. if ($isAdmin) {
  177. return true;
  178. }
  179. $settings = $this->settingManager->getAllAllowedAdminSettings($user);
  180. foreach ($settings as $setting) {
  181. if (!($setting instanceof IDelegatedSettings)) {
  182. continue;
  183. }
  184. $allowedKeys = $setting->getAuthorizedAppConfig();
  185. if (!array_key_exists($app, $allowedKeys)) {
  186. continue;
  187. }
  188. foreach ($allowedKeys[$app] as $regex) {
  189. if ($regex === $key
  190. || (str_starts_with($regex, '/') && preg_match($regex, $key) === 1)) {
  191. return true;
  192. }
  193. }
  194. }
  195. return false;
  196. }
  197. }