AllConfig.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC;
  8. use NCU\Config\Exceptions\TypeConflictException;
  9. use NCU\Config\IUserConfig;
  10. use NCU\Config\ValueType;
  11. use OC\Config\UserConfig;
  12. use OCP\Cache\CappedMemoryCache;
  13. use OCP\IConfig;
  14. use OCP\IDBConnection;
  15. use OCP\PreConditionNotMetException;
  16. /**
  17. * Class to combine all the configuration options ownCloud offers
  18. */
  19. class AllConfig implements IConfig {
  20. private ?IDBConnection $connection = null;
  21. /**
  22. * 3 dimensional array with the following structure:
  23. * [ $userId =>
  24. * [ $appId =>
  25. * [ $key => $value ]
  26. * ]
  27. * ]
  28. *
  29. * database table: preferences
  30. *
  31. * methods that use this:
  32. * - setUserValue
  33. * - getUserValue
  34. * - getUserKeys
  35. * - deleteUserValue
  36. * - deleteAllUserValues
  37. * - deleteAppFromAllUsers
  38. *
  39. * @var CappedMemoryCache $userCache
  40. */
  41. private CappedMemoryCache $userCache;
  42. public function __construct(
  43. private SystemConfig $systemConfig,
  44. ) {
  45. $this->userCache = new CappedMemoryCache();
  46. }
  47. /**
  48. * TODO - FIXME This fixes an issue with base.php that cause cyclic
  49. * dependencies, especially with autoconfig setup
  50. *
  51. * Replace this by properly injected database connection. Currently the
  52. * base.php triggers the getDatabaseConnection too early which causes in
  53. * autoconfig setup case a too early distributed database connection and
  54. * the autoconfig then needs to reinit all already initialized dependencies
  55. * that use the database connection.
  56. *
  57. * otherwise a SQLite database is created in the wrong directory
  58. * because the database connection was created with an uninitialized config
  59. */
  60. private function fixDIInit() {
  61. if ($this->connection === null) {
  62. $this->connection = \OC::$server->get(IDBConnection::class);
  63. }
  64. }
  65. /**
  66. * Sets and deletes system wide values
  67. *
  68. * @param array $configs Associative array with `key => value` pairs
  69. * If value is null, the config key will be deleted
  70. */
  71. public function setSystemValues(array $configs) {
  72. $this->systemConfig->setValues($configs);
  73. }
  74. /**
  75. * Sets a new system wide value
  76. *
  77. * @param string $key the key of the value, under which will be saved
  78. * @param mixed $value the value that should be stored
  79. */
  80. public function setSystemValue($key, $value) {
  81. $this->systemConfig->setValue($key, $value);
  82. }
  83. /**
  84. * Looks up a system wide defined value
  85. *
  86. * @param string $key the key of the value, under which it was saved
  87. * @param mixed $default the default value to be returned if the value isn't set
  88. * @return mixed the value or $default
  89. */
  90. public function getSystemValue($key, $default = '') {
  91. return $this->systemConfig->getValue($key, $default);
  92. }
  93. /**
  94. * Looks up a boolean system wide defined value
  95. *
  96. * @param string $key the key of the value, under which it was saved
  97. * @param bool $default the default value to be returned if the value isn't set
  98. *
  99. * @return bool
  100. *
  101. * @since 16.0.0
  102. */
  103. public function getSystemValueBool(string $key, bool $default = false): bool {
  104. return (bool)$this->getSystemValue($key, $default);
  105. }
  106. /**
  107. * Looks up an integer system wide defined value
  108. *
  109. * @param string $key the key of the value, under which it was saved
  110. * @param int $default the default value to be returned if the value isn't set
  111. *
  112. * @return int
  113. *
  114. * @since 16.0.0
  115. */
  116. public function getSystemValueInt(string $key, int $default = 0): int {
  117. return (int)$this->getSystemValue($key, $default);
  118. }
  119. /**
  120. * Looks up a string system wide defined value
  121. *
  122. * @param string $key the key of the value, under which it was saved
  123. * @param string $default the default value to be returned if the value isn't set
  124. *
  125. * @return string
  126. *
  127. * @since 16.0.0
  128. */
  129. public function getSystemValueString(string $key, string $default = ''): string {
  130. return (string)$this->getSystemValue($key, $default);
  131. }
  132. /**
  133. * Looks up a system wide defined value and filters out sensitive data
  134. *
  135. * @param string $key the key of the value, under which it was saved
  136. * @param mixed $default the default value to be returned if the value isn't set
  137. * @return mixed the value or $default
  138. */
  139. public function getFilteredSystemValue($key, $default = '') {
  140. return $this->systemConfig->getFilteredValue($key, $default);
  141. }
  142. /**
  143. * Delete a system wide defined value
  144. *
  145. * @param string $key the key of the value, under which it was saved
  146. */
  147. public function deleteSystemValue($key) {
  148. $this->systemConfig->deleteValue($key);
  149. }
  150. /**
  151. * Get all keys stored for an app
  152. *
  153. * @param string $appName the appName that we stored the value under
  154. * @return string[] the keys stored for the app
  155. * @deprecated 29.0.0 Use {@see IAppConfig} directly
  156. */
  157. public function getAppKeys($appName) {
  158. return \OC::$server->get(AppConfig::class)->getKeys($appName);
  159. }
  160. /**
  161. * Writes a new app wide value
  162. *
  163. * @param string $appName the appName that we want to store the value under
  164. * @param string $key the key of the value, under which will be saved
  165. * @param string|float|int $value the value that should be stored
  166. * @deprecated 29.0.0 Use {@see IAppConfig} directly
  167. */
  168. public function setAppValue($appName, $key, $value) {
  169. \OC::$server->get(AppConfig::class)->setValue($appName, $key, $value);
  170. }
  171. /**
  172. * Looks up an app wide defined value
  173. *
  174. * @param string $appName the appName that we stored the value under
  175. * @param string $key the key of the value, under which it was saved
  176. * @param string $default the default value to be returned if the value isn't set
  177. * @return string the saved value
  178. * @deprecated 29.0.0 Use {@see IAppConfig} directly
  179. */
  180. public function getAppValue($appName, $key, $default = '') {
  181. return \OC::$server->get(AppConfig::class)->getValue($appName, $key, $default);
  182. }
  183. /**
  184. * Delete an app wide defined value
  185. *
  186. * @param string $appName the appName that we stored the value under
  187. * @param string $key the key of the value, under which it was saved
  188. * @deprecated 29.0.0 Use {@see IAppConfig} directly
  189. */
  190. public function deleteAppValue($appName, $key) {
  191. \OC::$server->get(AppConfig::class)->deleteKey($appName, $key);
  192. }
  193. /**
  194. * Removes all keys in appconfig belonging to the app
  195. *
  196. * @param string $appName the appName the configs are stored under
  197. * @deprecated 29.0.0 Use {@see IAppConfig} directly
  198. */
  199. public function deleteAppValues($appName) {
  200. \OC::$server->get(AppConfig::class)->deleteApp($appName);
  201. }
  202. /**
  203. * Set a user defined value
  204. *
  205. * @param string $userId the userId of the user that we want to store the value under
  206. * @param string $appName the appName that we want to store the value under
  207. * @param string $key the key under which the value is being stored
  208. * @param string|float|int $value the value that you want to store
  209. * @param string $preCondition only update if the config value was previously the value passed as $preCondition
  210. *
  211. * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met
  212. * @throws \UnexpectedValueException when trying to store an unexpected value
  213. * @deprecated 31.0.0 - use {@see IUserConfig} directly
  214. * @see IUserConfig::getValueString
  215. * @see IUserConfig::getValueInt
  216. * @see IUserConfig::getValueFloat
  217. * @see IUserConfig::getValueArray
  218. * @see IUserConfig::getValueBool
  219. */
  220. public function setUserValue($userId, $appName, $key, $value, $preCondition = null) {
  221. if (!is_int($value) && !is_float($value) && !is_string($value)) {
  222. throw new \UnexpectedValueException('Only integers, floats and strings are allowed as value');
  223. }
  224. /** @var UserConfig $userPreferences */
  225. $userPreferences = \OCP\Server::get(IUserConfig::class);
  226. if ($preCondition !== null) {
  227. try {
  228. if ($userPreferences->hasKey($userId, $appName, $key) && $userPreferences->getValueMixed($userId, $appName, $key) !== (string)$preCondition) {
  229. throw new PreConditionNotMetException();
  230. }
  231. } catch (TypeConflictException) {
  232. }
  233. }
  234. $userPreferences->setValueMixed($userId, $appName, $key, (string)$value);
  235. }
  236. /**
  237. * Getting a user defined value
  238. *
  239. * @param ?string $userId the userId of the user that we want to store the value under
  240. * @param string $appName the appName that we stored the value under
  241. * @param string $key the key under which the value is being stored
  242. * @param mixed $default the default value to be returned if the value isn't set
  243. *
  244. * @return string
  245. * @deprecated 31.0.0 - use {@see IUserConfig} directly
  246. * @see IUserConfig::getValueString
  247. * @see IUserConfig::getValueInt
  248. * @see IUserConfig::getValueFloat
  249. * @see IUserConfig::getValueArray
  250. * @see IUserConfig::getValueBool
  251. */
  252. public function getUserValue($userId, $appName, $key, $default = '') {
  253. if ($userId === null || $userId === '') {
  254. return $default;
  255. }
  256. /** @var UserConfig $userPreferences */
  257. $userPreferences = \OCP\Server::get(IUserConfig::class);
  258. // because $default can be null ...
  259. if (!$userPreferences->hasKey($userId, $appName, $key)) {
  260. return $default;
  261. }
  262. return $userPreferences->getValueMixed($userId, $appName, $key, $default ?? '');
  263. }
  264. /**
  265. * Get the keys of all stored by an app for the user
  266. *
  267. * @param string $userId the userId of the user that we want to store the value under
  268. * @param string $appName the appName that we stored the value under
  269. *
  270. * @return string[]
  271. * @deprecated 31.0.0 - use {@see IUserConfig::getKeys} directly
  272. */
  273. public function getUserKeys($userId, $appName) {
  274. return \OCP\Server::get(IUserConfig::class)->getKeys($userId, $appName);
  275. }
  276. /**
  277. * Delete a user value
  278. *
  279. * @param string $userId the userId of the user that we want to store the value under
  280. * @param string $appName the appName that we stored the value under
  281. * @param string $key the key under which the value is being stored
  282. *
  283. * @deprecated 31.0.0 - use {@see IUserConfig::deleteUserConfig} directly
  284. */
  285. public function deleteUserValue($userId, $appName, $key) {
  286. \OCP\Server::get(IUserConfig::class)->deleteUserConfig($userId, $appName, $key);
  287. }
  288. /**
  289. * Delete all user values
  290. *
  291. * @param string $userId the userId of the user that we want to remove all values from
  292. *
  293. * @deprecated 31.0.0 - use {@see IUserConfig::deleteAllUserConfig} directly
  294. */
  295. public function deleteAllUserValues($userId) {
  296. if ($userId === null) {
  297. return;
  298. }
  299. \OCP\Server::get(IUserConfig::class)->deleteAllUserConfig($userId);
  300. }
  301. /**
  302. * Delete all user related values of one app
  303. *
  304. * @param string $appName the appName of the app that we want to remove all values from
  305. *
  306. * @deprecated 31.0.0 - use {@see IUserConfig::deleteApp} directly
  307. */
  308. public function deleteAppFromAllUsers($appName) {
  309. \OCP\Server::get(IUserConfig::class)->deleteApp($appName);
  310. }
  311. /**
  312. * Returns all user configs sorted by app of one user
  313. *
  314. * @param ?string $userId the user ID to get the app configs from
  315. *
  316. * @psalm-return array<string, array<string, string>>
  317. * @return array[] - 2 dimensional array with the following structure:
  318. * [ $appId =>
  319. * [ $key => $value ]
  320. * ]
  321. * @deprecated 31.0.0 - use {@see IUserConfig::getAllValues} directly
  322. */
  323. public function getAllUserValues(?string $userId): array {
  324. if ($userId === null || $userId === '') {
  325. return [];
  326. }
  327. $values = \OCP\Server::get(IUserConfig::class)->getAllValues($userId);
  328. $result = [];
  329. foreach ($values as $app => $list) {
  330. foreach ($list as $key => $value) {
  331. $result[$app][$key] = (string)$value;
  332. }
  333. }
  334. return $result;
  335. }
  336. /**
  337. * Fetches a mapped list of userId -> value, for a specified app and key and a list of user IDs.
  338. *
  339. * @param string $appName app to get the value for
  340. * @param string $key the key to get the value for
  341. * @param array $userIds the user IDs to fetch the values for
  342. *
  343. * @return array Mapped values: userId => value
  344. * @deprecated 31.0.0 - use {@see IUserConfig::getValuesByUsers} directly
  345. */
  346. public function getUserValueForUsers($appName, $key, $userIds) {
  347. return \OCP\Server::get(IUserConfig::class)->getValuesByUsers($appName, $key, ValueType::MIXED, $userIds);
  348. }
  349. /**
  350. * Determines the users that have the given value set for a specific app-key-pair
  351. *
  352. * @param string $appName the app to get the user for
  353. * @param string $key the key to get the user for
  354. * @param string $value the value to get the user for
  355. *
  356. * @return list<string> of user IDs
  357. * @deprecated 31.0.0 - use {@see IUserConfig::searchUsersByValueString} directly
  358. */
  359. public function getUsersForUserValue($appName, $key, $value) {
  360. /** @var list<string> $result */
  361. $result = iterator_to_array(\OCP\Server::get(IUserConfig::class)->searchUsersByValueString($appName, $key, $value));
  362. return $result;
  363. }
  364. /**
  365. * Determines the users that have the given value set for a specific app-key-pair
  366. *
  367. * @param string $appName the app to get the user for
  368. * @param string $key the key to get the user for
  369. * @param string $value the value to get the user for
  370. *
  371. * @return list<string> of user IDs
  372. * @deprecated 31.0.0 - use {@see IUserConfig::searchUsersByValueString} directly
  373. */
  374. public function getUsersForUserValueCaseInsensitive($appName, $key, $value) {
  375. if ($appName === 'settings' && $key === 'email') {
  376. return $this->getUsersForUserValue($appName, $key, strtolower($value));
  377. }
  378. /** @var list<string> $result */
  379. $result = iterator_to_array(\OCP\Server::get(IUserConfig::class)->searchUsersByValueString($appName, $key, $value, true));
  380. return $result;
  381. }
  382. public function getSystemConfig() {
  383. return $this->systemConfig;
  384. }
  385. }