You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppConfig.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Morris Jobke <hey@morrisjobke.de>
  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 OC\AppFramework\Services;
  26. use InvalidArgumentException;
  27. use JsonException;
  28. use OCP\AppFramework\Services\IAppConfig;
  29. use OCP\Exceptions\AppConfigTypeConflictException;
  30. use OCP\Exceptions\AppConfigUnknownKeyException;
  31. use OCP\IConfig;
  32. class AppConfig implements IAppConfig {
  33. public function __construct(
  34. private IConfig $config,
  35. /** @var \OC\AppConfig */
  36. private \OCP\IAppConfig $appConfig,
  37. private string $appName,
  38. ) {
  39. }
  40. /**
  41. * @inheritDoc
  42. *
  43. * @return string[] list of stored config keys
  44. * @since 20.0.0
  45. */
  46. public function getAppKeys(): array {
  47. return $this->appConfig->getKeys($this->appName);
  48. }
  49. /**
  50. * @inheritDoc
  51. *
  52. * @param string $key config key
  53. * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config
  54. *
  55. * @return bool TRUE if key exists
  56. * @since 29.0.0
  57. */
  58. public function hasAppKey(string $key, ?bool $lazy = false): bool {
  59. return $this->appConfig->hasKey($this->appName, $key, $lazy);
  60. }
  61. /**
  62. * @param string $key config key
  63. * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config
  64. *
  65. * @return bool
  66. * @throws AppConfigUnknownKeyException if config key is not known
  67. * @since 29.0.0
  68. */
  69. public function isSensitive(string $key, ?bool $lazy = false): bool {
  70. return $this->appConfig->isSensitive($this->appName, $key, $lazy);
  71. }
  72. /**
  73. * @inheritDoc
  74. *
  75. * @param string $key config key
  76. *
  77. * @return bool TRUE if config is lazy loaded
  78. * @throws AppConfigUnknownKeyException if config key is not known
  79. * @see \OCP\IAppConfig for details about lazy loading
  80. * @since 29.0.0
  81. */
  82. public function isLazy(string $key): bool {
  83. return $this->appConfig->isLazy($this->appName, $key);
  84. }
  85. /**
  86. * @inheritDoc
  87. *
  88. * @param string $key config keys prefix to search
  89. * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE}
  90. *
  91. * @return array<string, string|int|float|bool|array> [configKey => configValue]
  92. * @since 29.0.0
  93. */
  94. public function getAllAppValues(string $key = '', bool $filtered = false): array {
  95. return $this->appConfig->getAllValues($this->appName, $key, $filtered);
  96. }
  97. /**
  98. * @inheritDoc
  99. *
  100. * @param string $key the key of the value, under which will be saved
  101. * @param string $value the value that should be stored
  102. * @since 20.0.0
  103. * @deprecated 29.0.0 use {@see setAppValueString()}
  104. */
  105. public function setAppValue(string $key, string $value): void {
  106. /** @psalm-suppress InternalMethod */
  107. $this->appConfig->setValueMixed($this->appName, $key, $value);
  108. }
  109. /**
  110. * @inheritDoc
  111. *
  112. * @param string $key config key
  113. * @param string $value config value
  114. * @param bool $lazy set config as lazy loaded
  115. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  116. *
  117. * @return bool TRUE if value was different, therefor updated in database
  118. * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  119. * @since 29.0.0
  120. * @see \OCP\IAppConfig for explanation about lazy loading
  121. */
  122. public function setAppValueString(
  123. string $key,
  124. string $value,
  125. bool $lazy = false,
  126. bool $sensitive = false
  127. ): bool {
  128. return $this->appConfig->setValueString($this->appName, $key, $value, $lazy, $sensitive);
  129. }
  130. /**
  131. * @inheritDoc
  132. *
  133. * @param string $key config key
  134. * @param int $value config value
  135. * @param bool $lazy set config as lazy loaded
  136. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  137. *
  138. * @return bool TRUE if value was different, therefor updated in database
  139. * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  140. * @since 29.0.0
  141. * @see \OCP\IAppConfig for explanation about lazy loading
  142. */
  143. public function setAppValueInt(
  144. string $key,
  145. int $value,
  146. bool $lazy = false,
  147. bool $sensitive = false
  148. ): bool {
  149. return $this->appConfig->setValueInt($this->appName, $key, $value, $lazy, $sensitive);
  150. }
  151. /**
  152. * @inheritDoc
  153. *
  154. * @param string $key config key
  155. * @param float $value config value
  156. * @param bool $lazy set config as lazy loaded
  157. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  158. *
  159. * @return bool TRUE if value was different, therefor updated in database
  160. * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  161. * @since 29.0.0
  162. * @see \OCP\IAppConfig for explanation about lazy loading
  163. */
  164. public function setAppValueFloat(
  165. string $key,
  166. float $value,
  167. bool $lazy = false,
  168. bool $sensitive = false
  169. ): bool {
  170. return $this->appConfig->setValueFloat($this->appName, $key, $value, $lazy, $sensitive);
  171. }
  172. /**
  173. * @inheritDoc
  174. *
  175. * @param string $key config key
  176. * @param bool $value config value
  177. * @param bool $lazy set config as lazy loaded
  178. *
  179. * @return bool TRUE if value was different, therefor updated in database
  180. * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  181. * @since 29.0.0
  182. * @see \OCP\IAppConfig for explanation about lazy loading
  183. */
  184. public function setAppValueBool(
  185. string $key,
  186. bool $value,
  187. bool $lazy = false
  188. ): bool {
  189. return $this->appConfig->setValueBool($this->appName, $key, $value, $lazy);
  190. }
  191. /**
  192. * @inheritDoc
  193. *
  194. * @param string $key config key
  195. * @param array $value config value
  196. * @param bool $lazy set config as lazy loaded
  197. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  198. *
  199. * @return bool TRUE if value was different, therefor updated in database
  200. * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  201. * @throws JsonException
  202. * @since 29.0.0
  203. * @see \OCP\IAppConfig for explanation about lazy loading
  204. */
  205. public function setAppValueArray(
  206. string $key,
  207. array $value,
  208. bool $lazy = false,
  209. bool $sensitive = false
  210. ): bool {
  211. return $this->appConfig->setValueArray($this->appName, $key, $value, $lazy, $sensitive);
  212. }
  213. /**
  214. * @param string $key
  215. * @param string $default
  216. *
  217. * @since 20.0.0
  218. * @deprecated 29.0.0 use {@see getAppValueString()}
  219. * @return string
  220. */
  221. public function getAppValue(string $key, string $default = ''): string {
  222. /** @psalm-suppress InternalMethod */
  223. /** @psalm-suppress UndefinedInterfaceMethod */
  224. return $this->appConfig->getValueMixed($this->appName, $key, $default);
  225. }
  226. /**
  227. * @inheritDoc
  228. *
  229. * @param string $key config key
  230. * @param string $default default value
  231. * @param bool $lazy search within lazy loaded config
  232. *
  233. * @return string stored config value or $default if not set in database
  234. * @throws InvalidArgumentException if one of the argument format is invalid
  235. * @throws AppConfigTypeConflictException in case of conflict with the value type set in database
  236. * @since 29.0.0
  237. * @see \OCP\IAppConfig for explanation about lazy loading
  238. */
  239. public function getAppValueString(string $key, string $default = '', bool $lazy = false): string {
  240. return $this->appConfig->getValueString($this->appName, $key, $default, $lazy);
  241. }
  242. /**
  243. * @inheritDoc
  244. *
  245. * @param string $key config key
  246. * @param int $default default value
  247. * @param bool $lazy search within lazy loaded config
  248. *
  249. * @return int stored config value or $default if not set in database
  250. * @throws InvalidArgumentException if one of the argument format is invalid
  251. * @throws AppConfigTypeConflictException in case of conflict with the value type set in database
  252. * @since 29.0.0
  253. * @see \OCP\IAppConfig for explanation about lazy loading
  254. */
  255. public function getAppValueInt(string $key, int $default = 0, bool $lazy = false): int {
  256. return $this->appConfig->getValueInt($this->appName, $key, $default, $lazy);
  257. }
  258. /**
  259. * @inheritDoc
  260. *
  261. * @param string $key config key
  262. * @param float $default default value
  263. * @param bool $lazy search within lazy loaded config
  264. *
  265. * @return float stored config value or $default if not set in database
  266. * @throws InvalidArgumentException if one of the argument format is invalid
  267. * @throws AppConfigTypeConflictException in case of conflict with the value type set in database
  268. * @since 29.0.0
  269. * @see \OCP\IAppConfig for explanation about lazy loading
  270. */
  271. public function getAppValueFloat(string $key, float $default = 0, bool $lazy = false): float {
  272. return $this->appConfig->getValueFloat($this->appName, $key, $default, $lazy);
  273. }
  274. /**
  275. * @inheritDoc
  276. *
  277. * @param string $key config key
  278. * @param bool $default default value
  279. * @param bool $lazy search within lazy loaded config
  280. *
  281. * @return bool stored config value or $default if not set in database
  282. * @throws InvalidArgumentException if one of the argument format is invalid
  283. * @throws AppConfigTypeConflictException in case of conflict with the value type set in database
  284. * @since 29.0.0
  285. * @see \OCP\IAppConfig for explanation about lazy loading
  286. */
  287. public function getAppValueBool(string $key, bool $default = false, bool $lazy = false): bool {
  288. return $this->appConfig->getValueBool($this->appName, $key, $default, $lazy);
  289. }
  290. /**
  291. * @inheritDoc
  292. *
  293. * @param string $key config key
  294. * @param array $default default value
  295. * @param bool $lazy search within lazy loaded config
  296. *
  297. * @return array stored config value or $default if not set in database
  298. * @throws InvalidArgumentException if one of the argument format is invalid
  299. * @throws AppConfigTypeConflictException in case of conflict with the value type set in database
  300. * @since 29.0.0
  301. * @see \OCP\IAppConfig for explanation about lazy loading
  302. */
  303. public function getAppValueArray(string $key, array $default = [], bool $lazy = false): array {
  304. return $this->appConfig->getValueArray($this->appName, $key, $default, $lazy);
  305. }
  306. /**
  307. * @inheritDoc
  308. *
  309. * @param string $key the key of the value, under which it was saved
  310. * @since 20.0.0
  311. */
  312. public function deleteAppValue(string $key): void {
  313. $this->appConfig->deleteKey($this->appName, $key);
  314. }
  315. /**
  316. * @inheritDoc
  317. *
  318. * @since 20.0.0
  319. */
  320. public function deleteAppValues(): void {
  321. $this->appConfig->deleteApp($this->appName);
  322. }
  323. public function setUserValue(string $userId, string $key, string $value, ?string $preCondition = null): void {
  324. $this->config->setUserValue($userId, $this->appName, $key, $value, $preCondition);
  325. }
  326. public function getUserValue(string $userId, string $key, string $default = ''): string {
  327. return $this->config->getUserValue($userId, $this->appName, $key, $default);
  328. }
  329. public function deleteUserValue(string $userId, string $key): void {
  330. $this->config->deleteUserValue($userId, $this->appName, $key);
  331. }
  332. }