1
0

AllConfig.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 Doctrine\DBAL\Platforms\OraclePlatform;
  9. use OCP\Cache\CappedMemoryCache;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\IConfig;
  12. use OCP\IDBConnection;
  13. use OCP\PreConditionNotMetException;
  14. /**
  15. * Class to combine all the configuration options ownCloud offers
  16. */
  17. class AllConfig implements IConfig {
  18. private ?IDBConnection $connection = null;
  19. /**
  20. * 3 dimensional array with the following structure:
  21. * [ $userId =>
  22. * [ $appId =>
  23. * [ $key => $value ]
  24. * ]
  25. * ]
  26. *
  27. * database table: preferences
  28. *
  29. * methods that use this:
  30. * - setUserValue
  31. * - getUserValue
  32. * - getUserKeys
  33. * - deleteUserValue
  34. * - deleteAllUserValues
  35. * - deleteAppFromAllUsers
  36. *
  37. * @var CappedMemoryCache $userCache
  38. */
  39. private CappedMemoryCache $userCache;
  40. public function __construct(
  41. private SystemConfig $systemConfig
  42. ) {
  43. $this->userCache = new CappedMemoryCache();
  44. }
  45. /**
  46. * TODO - FIXME This fixes an issue with base.php that cause cyclic
  47. * dependencies, especially with autoconfig setup
  48. *
  49. * Replace this by properly injected database connection. Currently the
  50. * base.php triggers the getDatabaseConnection too early which causes in
  51. * autoconfig setup case a too early distributed database connection and
  52. * the autoconfig then needs to reinit all already initialized dependencies
  53. * that use the database connection.
  54. *
  55. * otherwise a SQLite database is created in the wrong directory
  56. * because the database connection was created with an uninitialized config
  57. */
  58. private function fixDIInit() {
  59. if ($this->connection === null) {
  60. $this->connection = \OC::$server->get(IDBConnection::class);
  61. }
  62. }
  63. /**
  64. * Sets and deletes system wide values
  65. *
  66. * @param array $configs Associative array with `key => value` pairs
  67. * If value is null, the config key will be deleted
  68. */
  69. public function setSystemValues(array $configs) {
  70. $this->systemConfig->setValues($configs);
  71. }
  72. /**
  73. * Sets a new system wide value
  74. *
  75. * @param string $key the key of the value, under which will be saved
  76. * @param mixed $value the value that should be stored
  77. */
  78. public function setSystemValue($key, $value) {
  79. $this->systemConfig->setValue($key, $value);
  80. }
  81. /**
  82. * Looks up a system wide defined value
  83. *
  84. * @param string $key the key of the value, under which it was saved
  85. * @param mixed $default the default value to be returned if the value isn't set
  86. * @return mixed the value or $default
  87. */
  88. public function getSystemValue($key, $default = '') {
  89. return $this->systemConfig->getValue($key, $default);
  90. }
  91. /**
  92. * Looks up a boolean system wide defined value
  93. *
  94. * @param string $key the key of the value, under which it was saved
  95. * @param bool $default the default value to be returned if the value isn't set
  96. *
  97. * @return bool
  98. *
  99. * @since 16.0.0
  100. */
  101. public function getSystemValueBool(string $key, bool $default = false): bool {
  102. return (bool) $this->getSystemValue($key, $default);
  103. }
  104. /**
  105. * Looks up an integer system wide defined value
  106. *
  107. * @param string $key the key of the value, under which it was saved
  108. * @param int $default the default value to be returned if the value isn't set
  109. *
  110. * @return int
  111. *
  112. * @since 16.0.0
  113. */
  114. public function getSystemValueInt(string $key, int $default = 0): int {
  115. return (int) $this->getSystemValue($key, $default);
  116. }
  117. /**
  118. * Looks up a string system wide defined value
  119. *
  120. * @param string $key the key of the value, under which it was saved
  121. * @param string $default the default value to be returned if the value isn't set
  122. *
  123. * @return string
  124. *
  125. * @since 16.0.0
  126. */
  127. public function getSystemValueString(string $key, string $default = ''): string {
  128. return (string) $this->getSystemValue($key, $default);
  129. }
  130. /**
  131. * Looks up a system wide defined value and filters out sensitive data
  132. *
  133. * @param string $key the key of the value, under which it was saved
  134. * @param mixed $default the default value to be returned if the value isn't set
  135. * @return mixed the value or $default
  136. */
  137. public function getFilteredSystemValue($key, $default = '') {
  138. return $this->systemConfig->getFilteredValue($key, $default);
  139. }
  140. /**
  141. * Delete a system wide defined value
  142. *
  143. * @param string $key the key of the value, under which it was saved
  144. */
  145. public function deleteSystemValue($key) {
  146. $this->systemConfig->deleteValue($key);
  147. }
  148. /**
  149. * Get all keys stored for an app
  150. *
  151. * @param string $appName the appName that we stored the value under
  152. * @return string[] the keys stored for the app
  153. * @deprecated 29.0.0 Use {@see IAppConfig} directly
  154. */
  155. public function getAppKeys($appName) {
  156. return \OC::$server->get(AppConfig::class)->getKeys($appName);
  157. }
  158. /**
  159. * Writes a new app wide value
  160. *
  161. * @param string $appName the appName that we want to store the value under
  162. * @param string $key the key of the value, under which will be saved
  163. * @param string|float|int $value the value that should be stored
  164. * @deprecated 29.0.0 Use {@see IAppConfig} directly
  165. */
  166. public function setAppValue($appName, $key, $value) {
  167. \OC::$server->get(AppConfig::class)->setValue($appName, $key, $value);
  168. }
  169. /**
  170. * Looks up an app wide defined value
  171. *
  172. * @param string $appName the appName that we stored the value under
  173. * @param string $key the key of the value, under which it was saved
  174. * @param string $default the default value to be returned if the value isn't set
  175. * @return string the saved value
  176. * @deprecated 29.0.0 Use {@see IAppConfig} directly
  177. */
  178. public function getAppValue($appName, $key, $default = '') {
  179. return \OC::$server->get(AppConfig::class)->getValue($appName, $key, $default);
  180. }
  181. /**
  182. * Delete an app wide defined value
  183. *
  184. * @param string $appName the appName that we stored the value under
  185. * @param string $key the key of the value, under which it was saved
  186. * @deprecated 29.0.0 Use {@see IAppConfig} directly
  187. */
  188. public function deleteAppValue($appName, $key) {
  189. \OC::$server->get(AppConfig::class)->deleteKey($appName, $key);
  190. }
  191. /**
  192. * Removes all keys in appconfig belonging to the app
  193. *
  194. * @param string $appName the appName the configs are stored under
  195. * @deprecated 29.0.0 Use {@see IAppConfig} directly
  196. */
  197. public function deleteAppValues($appName) {
  198. \OC::$server->get(AppConfig::class)->deleteApp($appName);
  199. }
  200. /**
  201. * Set a user defined value
  202. *
  203. * @param string $userId the userId of the user that we want to store the value under
  204. * @param string $appName the appName that we want to store the value under
  205. * @param string $key the key under which the value is being stored
  206. * @param string|float|int $value the value that you want to store
  207. * @param string $preCondition only update if the config value was previously the value passed as $preCondition
  208. * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met
  209. * @throws \UnexpectedValueException when trying to store an unexpected value
  210. */
  211. public function setUserValue($userId, $appName, $key, $value, $preCondition = null) {
  212. if (!is_int($value) && !is_float($value) && !is_string($value)) {
  213. throw new \UnexpectedValueException('Only integers, floats and strings are allowed as value');
  214. }
  215. // TODO - FIXME
  216. $this->fixDIInit();
  217. if ($appName === 'settings' && $key === 'email') {
  218. $value = strtolower((string) $value);
  219. }
  220. $prevValue = $this->getUserValue($userId, $appName, $key, null);
  221. if ($prevValue !== null) {
  222. if ($prevValue === (string)$value) {
  223. return;
  224. } elseif ($preCondition !== null && $prevValue !== (string)$preCondition) {
  225. throw new PreConditionNotMetException();
  226. } else {
  227. $qb = $this->connection->getQueryBuilder();
  228. $qb->update('preferences')
  229. ->set('configvalue', $qb->createNamedParameter($value))
  230. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId)))
  231. ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($appName)))
  232. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
  233. $qb->executeStatement();
  234. $this->userCache[$userId][$appName][$key] = (string)$value;
  235. return;
  236. }
  237. }
  238. $preconditionArray = [];
  239. if (isset($preCondition)) {
  240. $preconditionArray = [
  241. 'configvalue' => $preCondition,
  242. ];
  243. }
  244. $this->connection->setValues('preferences', [
  245. 'userid' => $userId,
  246. 'appid' => $appName,
  247. 'configkey' => $key,
  248. ], [
  249. 'configvalue' => $value,
  250. ], $preconditionArray);
  251. // only add to the cache if we already loaded data for the user
  252. if (isset($this->userCache[$userId])) {
  253. if (!isset($this->userCache[$userId][$appName])) {
  254. $this->userCache[$userId][$appName] = [];
  255. }
  256. $this->userCache[$userId][$appName][$key] = (string)$value;
  257. }
  258. }
  259. /**
  260. * Getting a user defined value
  261. *
  262. * @param ?string $userId the userId of the user that we want to store the value under
  263. * @param string $appName the appName that we stored the value under
  264. * @param string $key the key under which the value is being stored
  265. * @param mixed $default the default value to be returned if the value isn't set
  266. * @return string
  267. */
  268. public function getUserValue($userId, $appName, $key, $default = '') {
  269. $data = $this->getAllUserValues($userId);
  270. if (isset($data[$appName][$key])) {
  271. return $data[$appName][$key];
  272. } else {
  273. return $default;
  274. }
  275. }
  276. /**
  277. * Get the keys of all stored by an app for the user
  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. * @return string[]
  282. */
  283. public function getUserKeys($userId, $appName) {
  284. $data = $this->getAllUserValues($userId);
  285. if (isset($data[$appName])) {
  286. return array_map('strval', array_keys($data[$appName]));
  287. } else {
  288. return [];
  289. }
  290. }
  291. /**
  292. * Delete a user value
  293. *
  294. * @param string $userId the userId of the user that we want to store the value under
  295. * @param string $appName the appName that we stored the value under
  296. * @param string $key the key under which the value is being stored
  297. */
  298. public function deleteUserValue($userId, $appName, $key) {
  299. // TODO - FIXME
  300. $this->fixDIInit();
  301. $qb = $this->connection->getQueryBuilder();
  302. $qb->delete('preferences')
  303. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
  304. ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
  305. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
  306. ->executeStatement();
  307. if (isset($this->userCache[$userId][$appName])) {
  308. unset($this->userCache[$userId][$appName][$key]);
  309. }
  310. }
  311. /**
  312. * Delete all user values
  313. *
  314. * @param string $userId the userId of the user that we want to remove all values from
  315. */
  316. public function deleteAllUserValues($userId) {
  317. // TODO - FIXME
  318. $this->fixDIInit();
  319. $qb = $this->connection->getQueryBuilder();
  320. $qb->delete('preferences')
  321. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
  322. ->executeStatement();
  323. unset($this->userCache[$userId]);
  324. }
  325. /**
  326. * Delete all user related values of one app
  327. *
  328. * @param string $appName the appName of the app that we want to remove all values from
  329. */
  330. public function deleteAppFromAllUsers($appName) {
  331. // TODO - FIXME
  332. $this->fixDIInit();
  333. $qb = $this->connection->getQueryBuilder();
  334. $qb->delete('preferences')
  335. ->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
  336. ->executeStatement();
  337. foreach ($this->userCache as &$userCache) {
  338. unset($userCache[$appName]);
  339. }
  340. }
  341. /**
  342. * Returns all user configs sorted by app of one user
  343. *
  344. * @param ?string $userId the user ID to get the app configs from
  345. * @psalm-return array<string, array<string, string>>
  346. * @return array[] - 2 dimensional array with the following structure:
  347. * [ $appId =>
  348. * [ $key => $value ]
  349. * ]
  350. */
  351. public function getAllUserValues(?string $userId): array {
  352. if (isset($this->userCache[$userId])) {
  353. return $this->userCache[$userId];
  354. }
  355. if ($userId === null || $userId === '') {
  356. $this->userCache[''] = [];
  357. return $this->userCache[''];
  358. }
  359. // TODO - FIXME
  360. $this->fixDIInit();
  361. $data = [];
  362. $qb = $this->connection->getQueryBuilder();
  363. $result = $qb->select('appid', 'configkey', 'configvalue')
  364. ->from('preferences')
  365. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
  366. ->executeQuery();
  367. while ($row = $result->fetch()) {
  368. $appId = $row['appid'];
  369. if (!isset($data[$appId])) {
  370. $data[$appId] = [];
  371. }
  372. $data[$appId][$row['configkey']] = $row['configvalue'];
  373. }
  374. $this->userCache[$userId] = $data;
  375. return $data;
  376. }
  377. /**
  378. * Fetches a mapped list of userId -> value, for a specified app and key and a list of user IDs.
  379. *
  380. * @param string $appName app to get the value for
  381. * @param string $key the key to get the value for
  382. * @param array $userIds the user IDs to fetch the values for
  383. * @return array Mapped values: userId => value
  384. */
  385. public function getUserValueForUsers($appName, $key, $userIds) {
  386. // TODO - FIXME
  387. $this->fixDIInit();
  388. if (empty($userIds) || !is_array($userIds)) {
  389. return [];
  390. }
  391. $chunkedUsers = array_chunk($userIds, 50, true);
  392. $qb = $this->connection->getQueryBuilder();
  393. $qb->select('userid', 'configvalue')
  394. ->from('preferences')
  395. ->where($qb->expr()->eq('appid', $qb->createParameter('appName')))
  396. ->andWhere($qb->expr()->eq('configkey', $qb->createParameter('configKey')))
  397. ->andWhere($qb->expr()->in('userid', $qb->createParameter('userIds')));
  398. $userValues = [];
  399. foreach ($chunkedUsers as $chunk) {
  400. $qb->setParameter('appName', $appName, IQueryBuilder::PARAM_STR);
  401. $qb->setParameter('configKey', $key, IQueryBuilder::PARAM_STR);
  402. $qb->setParameter('userIds', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
  403. $result = $qb->executeQuery();
  404. while ($row = $result->fetch()) {
  405. $userValues[$row['userid']] = $row['configvalue'];
  406. }
  407. }
  408. return $userValues;
  409. }
  410. /**
  411. * Determines the users that have the given value set for a specific app-key-pair
  412. *
  413. * @param string $appName the app to get the user for
  414. * @param string $key the key to get the user for
  415. * @param string $value the value to get the user for
  416. * @return array of user IDs
  417. */
  418. public function getUsersForUserValue($appName, $key, $value) {
  419. // TODO - FIXME
  420. $this->fixDIInit();
  421. $qb = $this->connection->getQueryBuilder();
  422. $configValueColumn = ($this->connection->getDatabasePlatform() instanceof OraclePlatform)
  423. ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR)
  424. : 'configvalue';
  425. $result = $qb->select('userid')
  426. ->from('preferences')
  427. ->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
  428. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
  429. ->andWhere($qb->expr()->eq(
  430. $configValueColumn,
  431. $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR))
  432. )->orderBy('userid')
  433. ->executeQuery();
  434. $userIDs = [];
  435. while ($row = $result->fetch()) {
  436. $userIDs[] = $row['userid'];
  437. }
  438. return $userIDs;
  439. }
  440. /**
  441. * Determines the users that have the given value set for a specific app-key-pair
  442. *
  443. * @param string $appName the app to get the user for
  444. * @param string $key the key to get the user for
  445. * @param string $value the value to get the user for
  446. * @return array of user IDs
  447. */
  448. public function getUsersForUserValueCaseInsensitive($appName, $key, $value) {
  449. // TODO - FIXME
  450. $this->fixDIInit();
  451. if ($appName === 'settings' && $key === 'email') {
  452. // Email address is always stored lowercase in the database
  453. return $this->getUsersForUserValue($appName, $key, strtolower($value));
  454. }
  455. $qb = $this->connection->getQueryBuilder();
  456. $configValueColumn = ($this->connection->getDatabasePlatform() instanceof OraclePlatform)
  457. ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR)
  458. : 'configvalue';
  459. $result = $qb->select('userid')
  460. ->from('preferences')
  461. ->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
  462. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
  463. ->andWhere($qb->expr()->eq(
  464. $qb->func()->lower($configValueColumn),
  465. $qb->createNamedParameter(strtolower($value), IQueryBuilder::PARAM_STR))
  466. )->orderBy('userid')
  467. ->executeQuery();
  468. $userIDs = [];
  469. while ($row = $result->fetch()) {
  470. $userIDs[] = $row['userid'];
  471. }
  472. return $userIDs;
  473. }
  474. public function getSystemConfig() {
  475. return $this->systemConfig;
  476. }
  477. }