AllConfig.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 SystemConfig $systemConfig;
  45. private ?IDBConnection $connection = null;
  46. /**
  47. * 3 dimensional array with the following structure:
  48. * [ $userId =>
  49. * [ $appId =>
  50. * [ $key => $value ]
  51. * ]
  52. * ]
  53. *
  54. * database table: preferences
  55. *
  56. * methods that use this:
  57. * - setUserValue
  58. * - getUserValue
  59. * - getUserKeys
  60. * - deleteUserValue
  61. * - deleteAllUserValues
  62. * - deleteAppFromAllUsers
  63. *
  64. * @var CappedMemoryCache $userCache
  65. */
  66. private CappedMemoryCache $userCache;
  67. public function __construct(SystemConfig $systemConfig) {
  68. $this->userCache = new CappedMemoryCache();
  69. $this->systemConfig = $systemConfig;
  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. */
  180. public function getAppKeys($appName) {
  181. return \OC::$server->get(AppConfig::class)->getKeys($appName);
  182. }
  183. /**
  184. * Writes a new app wide value
  185. *
  186. * @param string $appName the appName that we want to store the value under
  187. * @param string $key the key of the value, under which will be saved
  188. * @param string|float|int $value the value that should be stored
  189. */
  190. public function setAppValue($appName, $key, $value) {
  191. \OC::$server->get(AppConfig::class)->setValue($appName, $key, $value);
  192. }
  193. /**
  194. * Looks up an app wide defined value
  195. *
  196. * @param string $appName the appName that we stored the value under
  197. * @param string $key the key of the value, under which it was saved
  198. * @param string $default the default value to be returned if the value isn't set
  199. * @return string the saved value
  200. */
  201. public function getAppValue($appName, $key, $default = '') {
  202. return \OC::$server->get(AppConfig::class)->getValue($appName, $key, $default);
  203. }
  204. /**
  205. * Delete an app wide defined value
  206. *
  207. * @param string $appName the appName that we stored the value under
  208. * @param string $key the key of the value, under which it was saved
  209. */
  210. public function deleteAppValue($appName, $key) {
  211. \OC::$server->get(AppConfig::class)->deleteKey($appName, $key);
  212. }
  213. /**
  214. * Removes all keys in appconfig belonging to the app
  215. *
  216. * @param string $appName the appName the configs are stored under
  217. */
  218. public function deleteAppValues($appName) {
  219. \OC::$server->get(AppConfig::class)->deleteApp($appName);
  220. }
  221. /**
  222. * Set a user defined value
  223. *
  224. * @param string $userId the userId of the user that we want to store the value under
  225. * @param string $appName the appName that we want to store the value under
  226. * @param string $key the key under which the value is being stored
  227. * @param string|float|int $value the value that you want to store
  228. * @param string $preCondition only update if the config value was previously the value passed as $preCondition
  229. * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met
  230. * @throws \UnexpectedValueException when trying to store an unexpected value
  231. */
  232. public function setUserValue($userId, $appName, $key, $value, $preCondition = null) {
  233. if (!is_int($value) && !is_float($value) && !is_string($value)) {
  234. throw new \UnexpectedValueException('Only integers, floats and strings are allowed as value');
  235. }
  236. // TODO - FIXME
  237. $this->fixDIInit();
  238. if ($appName === 'settings' && $key === 'email') {
  239. $value = strtolower((string) $value);
  240. }
  241. $prevValue = $this->getUserValue($userId, $appName, $key, null);
  242. if ($prevValue !== null) {
  243. if ($prevValue === (string)$value) {
  244. return;
  245. } elseif ($preCondition !== null && $prevValue !== (string)$preCondition) {
  246. throw new PreConditionNotMetException();
  247. } else {
  248. $qb = $this->connection->getQueryBuilder();
  249. $qb->update('preferences')
  250. ->set('configvalue', $qb->createNamedParameter($value))
  251. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId)))
  252. ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($appName)))
  253. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
  254. $qb->executeStatement();
  255. $this->userCache[$userId][$appName][$key] = (string)$value;
  256. return;
  257. }
  258. }
  259. $preconditionArray = [];
  260. if (isset($preCondition)) {
  261. $preconditionArray = [
  262. 'configvalue' => $preCondition,
  263. ];
  264. }
  265. $this->connection->setValues('preferences', [
  266. 'userid' => $userId,
  267. 'appid' => $appName,
  268. 'configkey' => $key,
  269. ], [
  270. 'configvalue' => $value,
  271. ], $preconditionArray);
  272. // only add to the cache if we already loaded data for the user
  273. if (isset($this->userCache[$userId])) {
  274. if (!isset($this->userCache[$userId][$appName])) {
  275. $this->userCache[$userId][$appName] = [];
  276. }
  277. $this->userCache[$userId][$appName][$key] = (string)$value;
  278. }
  279. }
  280. /**
  281. * Getting a user defined value
  282. *
  283. * @param ?string $userId the userId of the user that we want to store the value under
  284. * @param string $appName the appName that we stored the value under
  285. * @param string $key the key under which the value is being stored
  286. * @param mixed $default the default value to be returned if the value isn't set
  287. * @return string
  288. */
  289. public function getUserValue($userId, $appName, $key, $default = '') {
  290. $data = $this->getAllUserValues($userId);
  291. if (isset($data[$appName][$key])) {
  292. return $data[$appName][$key];
  293. } else {
  294. return $default;
  295. }
  296. }
  297. /**
  298. * Get the keys of all stored by an app for the user
  299. *
  300. * @param string $userId the userId of the user that we want to store the value under
  301. * @param string $appName the appName that we stored the value under
  302. * @return string[]
  303. */
  304. public function getUserKeys($userId, $appName) {
  305. $data = $this->getAllUserValues($userId);
  306. if (isset($data[$appName])) {
  307. return array_keys($data[$appName]);
  308. } else {
  309. return [];
  310. }
  311. }
  312. /**
  313. * Delete a user value
  314. *
  315. * @param string $userId the userId of the user that we want to store the value under
  316. * @param string $appName the appName that we stored the value under
  317. * @param string $key the key under which the value is being stored
  318. */
  319. public function deleteUserValue($userId, $appName, $key) {
  320. // TODO - FIXME
  321. $this->fixDIInit();
  322. $qb = $this->connection->getQueryBuilder();
  323. $qb->delete('preferences')
  324. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
  325. ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
  326. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
  327. ->executeStatement();
  328. if (isset($this->userCache[$userId][$appName])) {
  329. unset($this->userCache[$userId][$appName][$key]);
  330. }
  331. }
  332. /**
  333. * Delete all user values
  334. *
  335. * @param string $userId the userId of the user that we want to remove all values from
  336. */
  337. public function deleteAllUserValues($userId) {
  338. // TODO - FIXME
  339. $this->fixDIInit();
  340. $qb = $this->connection->getQueryBuilder();
  341. $qb->delete('preferences')
  342. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
  343. ->executeStatement();
  344. unset($this->userCache[$userId]);
  345. }
  346. /**
  347. * Delete all user related values of one app
  348. *
  349. * @param string $appName the appName of the app that we want to remove all values from
  350. */
  351. public function deleteAppFromAllUsers($appName) {
  352. // TODO - FIXME
  353. $this->fixDIInit();
  354. $qb = $this->connection->getQueryBuilder();
  355. $qb->delete('preferences')
  356. ->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
  357. ->executeStatement();
  358. foreach ($this->userCache as &$userCache) {
  359. unset($userCache[$appName]);
  360. }
  361. }
  362. /**
  363. * Returns all user configs sorted by app of one user
  364. *
  365. * @param ?string $userId the user ID to get the app configs from
  366. * @psalm-return array<string, array<string, string>>
  367. * @return array[] - 2 dimensional array with the following structure:
  368. * [ $appId =>
  369. * [ $key => $value ]
  370. * ]
  371. */
  372. public function getAllUserValues(?string $userId): array {
  373. if (isset($this->userCache[$userId])) {
  374. return $this->userCache[$userId];
  375. }
  376. if ($userId === null || $userId === '') {
  377. $this->userCache[''] = [];
  378. return $this->userCache[''];
  379. }
  380. // TODO - FIXME
  381. $this->fixDIInit();
  382. $data = [];
  383. $qb = $this->connection->getQueryBuilder();
  384. $result = $qb->select('appid', 'configkey', 'configvalue')
  385. ->from('preferences')
  386. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
  387. ->executeQuery();
  388. while ($row = $result->fetch()) {
  389. $appId = $row['appid'];
  390. if (!isset($data[$appId])) {
  391. $data[$appId] = [];
  392. }
  393. $data[$appId][$row['configkey']] = $row['configvalue'];
  394. }
  395. $this->userCache[$userId] = $data;
  396. return $data;
  397. }
  398. /**
  399. * Fetches a mapped list of userId -> value, for a specified app and key and a list of user IDs.
  400. *
  401. * @param string $appName app to get the value for
  402. * @param string $key the key to get the value for
  403. * @param array $userIds the user IDs to fetch the values for
  404. * @return array Mapped values: userId => value
  405. */
  406. public function getUserValueForUsers($appName, $key, $userIds) {
  407. // TODO - FIXME
  408. $this->fixDIInit();
  409. if (empty($userIds) || !is_array($userIds)) {
  410. return [];
  411. }
  412. $chunkedUsers = array_chunk($userIds, 50, true);
  413. $qb = $this->connection->getQueryBuilder();
  414. $qb->select('userid', 'configvalue')
  415. ->from('preferences')
  416. ->where($qb->expr()->eq('appid', $qb->createParameter('appName')))
  417. ->andWhere($qb->expr()->eq('configkey', $qb->createParameter('configKey')))
  418. ->andWhere($qb->expr()->in('userid', $qb->createParameter('userIds')));
  419. $userValues = [];
  420. foreach ($chunkedUsers as $chunk) {
  421. $qb->setParameter('appName', $appName, IQueryBuilder::PARAM_STR);
  422. $qb->setParameter('configKey', $key, IQueryBuilder::PARAM_STR);
  423. $qb->setParameter('userIds', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
  424. $result = $qb->executeQuery();
  425. while ($row = $result->fetch()) {
  426. $userValues[$row['userid']] = $row['configvalue'];
  427. }
  428. }
  429. return $userValues;
  430. }
  431. /**
  432. * Determines the users that have the given value set for a specific app-key-pair
  433. *
  434. * @param string $appName the app to get the user for
  435. * @param string $key the key to get the user for
  436. * @param string $value the value to get the user for
  437. * @return array of user IDs
  438. */
  439. public function getUsersForUserValue($appName, $key, $value) {
  440. // TODO - FIXME
  441. $this->fixDIInit();
  442. $qb = $this->connection->getQueryBuilder();
  443. $configValueColumn = ($this->connection->getDatabasePlatform() instanceof OraclePlatform)
  444. ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR)
  445. : 'configvalue';
  446. $result = $qb->select('userid')
  447. ->from('preferences')
  448. ->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
  449. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
  450. ->andWhere($qb->expr()->eq(
  451. $configValueColumn,
  452. $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR))
  453. )->orderBy('userid')
  454. ->executeQuery();
  455. $userIDs = [];
  456. while ($row = $result->fetch()) {
  457. $userIDs[] = $row['userid'];
  458. }
  459. return $userIDs;
  460. }
  461. /**
  462. * Determines the users that have the given value set for a specific app-key-pair
  463. *
  464. * @param string $appName the app to get the user for
  465. * @param string $key the key to get the user for
  466. * @param string $value the value to get the user for
  467. * @return array of user IDs
  468. */
  469. public function getUsersForUserValueCaseInsensitive($appName, $key, $value) {
  470. // TODO - FIXME
  471. $this->fixDIInit();
  472. if ($appName === 'settings' && $key === 'email') {
  473. // Email address is always stored lowercase in the database
  474. return $this->getUsersForUserValue($appName, $key, strtolower($value));
  475. }
  476. $qb = $this->connection->getQueryBuilder();
  477. $configValueColumn = ($this->connection->getDatabasePlatform() instanceof OraclePlatform)
  478. ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR)
  479. : 'configvalue';
  480. $result = $qb->select('userid')
  481. ->from('preferences')
  482. ->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
  483. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
  484. ->andWhere($qb->expr()->eq(
  485. $qb->func()->lower($configValueColumn),
  486. $qb->createNamedParameter(strtolower($value), IQueryBuilder::PARAM_STR))
  487. )->orderBy('userid')
  488. ->executeQuery();
  489. $userIDs = [];
  490. while ($row = $result->fetch()) {
  491. $userIDs[] = $row['userid'];
  492. }
  493. return $userIDs;
  494. }
  495. public function getSystemConfig() {
  496. return $this->systemConfig;
  497. }
  498. }