AllConfig.php 18 KB

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