1
0

AllConfig.php 17 KB

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