DBConfigService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Robin Appelman <icewind@owncloud.com>
  5. * @author Robin McCorkell <robin@mccorkell.me.uk>
  6. *
  7. * @copyright Copyright (c) 2016, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files_External\Service;
  24. use OCP\DB\QueryBuilder\IQueryBuilder;
  25. use OCP\IDBConnection;
  26. use OCP\Security\ICrypto;
  27. /**
  28. * Stores the mount config in the database
  29. */
  30. class DBConfigService {
  31. const MOUNT_TYPE_ADMIN = 1;
  32. const MOUNT_TYPE_PERSONAl = 2;
  33. const APPLICABLE_TYPE_GLOBAL = 1;
  34. const APPLICABLE_TYPE_GROUP = 2;
  35. const APPLICABLE_TYPE_USER = 3;
  36. /**
  37. * @var IDBConnection
  38. */
  39. private $connection;
  40. /**
  41. * @var ICrypto
  42. */
  43. private $crypto;
  44. /**
  45. * DBConfigService constructor.
  46. *
  47. * @param IDBConnection $connection
  48. * @param ICrypto $crypto
  49. */
  50. public function __construct(IDBConnection $connection, ICrypto $crypto) {
  51. $this->connection = $connection;
  52. $this->crypto = $crypto;
  53. }
  54. /**
  55. * @param int $mountId
  56. * @return array
  57. */
  58. public function getMountById($mountId) {
  59. $builder = $this->connection->getQueryBuilder();
  60. $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type'])
  61. ->from('external_mounts', 'm')
  62. ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
  63. $mounts = $this->getMountsFromQuery($query);
  64. if (count($mounts) > 0) {
  65. return $mounts[0];
  66. } else {
  67. return null;
  68. }
  69. }
  70. /**
  71. * Get all configured mounts
  72. *
  73. * @return array
  74. */
  75. public function getAllMounts() {
  76. $builder = $this->connection->getQueryBuilder();
  77. $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type'])
  78. ->from('external_mounts');
  79. return $this->getMountsFromQuery($query);
  80. }
  81. /**
  82. * Get admin defined mounts
  83. *
  84. * @return array
  85. */
  86. public function getAdminMounts() {
  87. $builder = $this->connection->getQueryBuilder();
  88. $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type'])
  89. ->from('external_mounts')
  90. ->where($builder->expr()->eq('type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT)));
  91. return $this->getMountsFromQuery($query);
  92. }
  93. protected function getForQuery(IQueryBuilder $builder, $type, $value) {
  94. $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type'])
  95. ->from('external_mounts', 'm')
  96. ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id'))
  97. ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT)));
  98. if (is_null($value)) {
  99. $query = $query->andWhere($builder->expr()->isNull('a.value'));
  100. } else {
  101. $query = $query->andWhere($builder->expr()->eq('a.value', $builder->createNamedParameter($value)));
  102. }
  103. return $query;
  104. }
  105. /**
  106. * Get mounts by applicable
  107. *
  108. * @param int $type any of the self::APPLICABLE_TYPE_ constants
  109. * @param string|null $value user_id, group_id or null for global mounts
  110. * @return array
  111. */
  112. public function getMountsFor($type, $value) {
  113. $builder = $this->connection->getQueryBuilder();
  114. $query = $this->getForQuery($builder, $type, $value);
  115. return $this->getMountsFromQuery($query);
  116. }
  117. /**
  118. * Get admin defined mounts by applicable
  119. *
  120. * @param int $type any of the self::APPLICABLE_TYPE_ constants
  121. * @param string|null $value user_id, group_id or null for global mounts
  122. * @return array
  123. */
  124. public function getAdminMountsFor($type, $value) {
  125. $builder = $this->connection->getQueryBuilder();
  126. $query = $this->getForQuery($builder, $type, $value);
  127. $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT)));
  128. return $this->getMountsFromQuery($query);
  129. }
  130. /**
  131. * Get admin defined mounts for multiple applicable
  132. *
  133. * @param int $type any of the self::APPLICABLE_TYPE_ constants
  134. * @param string[] $values user_ids or group_ids
  135. * @return array
  136. */
  137. public function getAdminMountsForMultiple($type, array $values) {
  138. $builder = $this->connection->getQueryBuilder();
  139. $params = array_map(function ($value) use ($builder) {
  140. return $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR);
  141. }, $values);
  142. $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type'])
  143. ->from('external_mounts', 'm')
  144. ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id'))
  145. ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT)))
  146. ->andWhere($builder->expr()->in('a.value', $params));
  147. $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT)));
  148. return $this->getMountsFromQuery($query);
  149. }
  150. /**
  151. * Get user defined mounts by applicable
  152. *
  153. * @param int $type any of the self::APPLICABLE_TYPE_ constants
  154. * @param string|null $value user_id, group_id or null for global mounts
  155. * @return array
  156. */
  157. public function getUserMountsFor($type, $value) {
  158. $builder = $this->connection->getQueryBuilder();
  159. $query = $this->getForQuery($builder, $type, $value);
  160. $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_PERSONAl, IQueryBuilder::PARAM_INT)));
  161. return $this->getMountsFromQuery($query);
  162. }
  163. /**
  164. * Add a mount to the database
  165. *
  166. * @param string $mountPoint
  167. * @param string $storageBackend
  168. * @param string $authBackend
  169. * @param int $priority
  170. * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL
  171. * @return int the id of the new mount
  172. */
  173. public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) {
  174. if (!$priority) {
  175. $priority = 100;
  176. }
  177. $builder = $this->connection->getQueryBuilder();
  178. $query = $builder->insert('external_mounts')
  179. ->values([
  180. 'mount_point' => $builder->createNamedParameter($mountPoint, IQueryBuilder::PARAM_STR),
  181. 'storage_backend' => $builder->createNamedParameter($storageBackend, IQueryBuilder::PARAM_STR),
  182. 'auth_backend' => $builder->createNamedParameter($authBackend, IQueryBuilder::PARAM_STR),
  183. 'priority' => $builder->createNamedParameter($priority, IQueryBuilder::PARAM_INT),
  184. 'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT)
  185. ]);
  186. $query->execute();
  187. return (int)$this->connection->lastInsertId('external_mounts');
  188. }
  189. /**
  190. * Remove a mount from the database
  191. *
  192. * @param int $mountId
  193. */
  194. public function removeMount($mountId) {
  195. $builder = $this->connection->getQueryBuilder();
  196. $query = $builder->delete('external_mounts')
  197. ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
  198. $query->execute();
  199. $query = $builder->delete('external_applicable')
  200. ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
  201. $query->execute();
  202. $query = $builder->delete('external_config')
  203. ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
  204. $query->execute();
  205. $query = $builder->delete('external_options')
  206. ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
  207. $query->execute();
  208. }
  209. /**
  210. * @param int $mountId
  211. * @param string $newMountPoint
  212. */
  213. public function setMountPoint($mountId, $newMountPoint) {
  214. $builder = $this->connection->getQueryBuilder();
  215. $query = $builder->update('external_mounts')
  216. ->set('mount_point', $builder->createNamedParameter($newMountPoint))
  217. ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
  218. $query->execute();
  219. }
  220. /**
  221. * @param int $mountId
  222. * @param string $newAuthBackend
  223. */
  224. public function setAuthBackend($mountId, $newAuthBackend) {
  225. $builder = $this->connection->getQueryBuilder();
  226. $query = $builder->update('external_mounts')
  227. ->set('auth_backend', $builder->createNamedParameter($newAuthBackend))
  228. ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
  229. $query->execute();
  230. }
  231. /**
  232. * @param int $mountId
  233. * @param string $key
  234. * @param string $value
  235. */
  236. public function setConfig($mountId, $key, $value) {
  237. if ($key === 'password') {
  238. $value = $this->encryptValue($value);
  239. }
  240. $count = $this->connection->insertIfNotExist('*PREFIX*external_config', [
  241. 'mount_id' => $mountId,
  242. 'key' => $key,
  243. 'value' => $value
  244. ], ['mount_id', 'key']);
  245. if ($count === 0) {
  246. $builder = $this->connection->getQueryBuilder();
  247. $query = $builder->update('external_config')
  248. ->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))
  249. ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)))
  250. ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR)));
  251. $query->execute();
  252. }
  253. }
  254. /**
  255. * @param int $mountId
  256. * @param string $key
  257. * @param string $value
  258. */
  259. public function setOption($mountId, $key, $value) {
  260. $count = $this->connection->insertIfNotExist('*PREFIX*external_options', [
  261. 'mount_id' => $mountId,
  262. 'key' => $key,
  263. 'value' => json_encode($value)
  264. ], ['mount_id', 'key']);
  265. if ($count === 0) {
  266. $builder = $this->connection->getQueryBuilder();
  267. $query = $builder->update('external_options')
  268. ->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR))
  269. ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)))
  270. ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR)));
  271. $query->execute();
  272. }
  273. }
  274. public function addApplicable($mountId, $type, $value) {
  275. $this->connection->insertIfNotExist('*PREFIX*external_applicable', [
  276. 'mount_id' => $mountId,
  277. 'type' => $type,
  278. 'value' => $value
  279. ], ['mount_id', 'type', 'value']);
  280. }
  281. public function removeApplicable($mountId, $type, $value) {
  282. $builder = $this->connection->getQueryBuilder();
  283. $query = $builder->delete('external_applicable')
  284. ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)))
  285. ->andWhere($builder->expr()->eq('type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT)));
  286. if (is_null($value)) {
  287. $query = $query->andWhere($builder->expr()->isNull('value'));
  288. } else {
  289. $query = $query->andWhere($builder->expr()->eq('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR)));
  290. }
  291. $query->execute();
  292. }
  293. private function getMountsFromQuery(IQueryBuilder $query) {
  294. $result = $query->execute();
  295. $mounts = $result->fetchAll();
  296. $uniqueMounts = [];
  297. foreach ($mounts as $mount) {
  298. $id = $mount['mount_id'];
  299. if (!isset($uniqueMounts[$id])) {
  300. $uniqueMounts[$id] = $mount;
  301. }
  302. }
  303. $uniqueMounts = array_values($uniqueMounts);
  304. $mountIds = array_map(function ($mount) {
  305. return $mount['mount_id'];
  306. }, $uniqueMounts);
  307. $mountIds = array_values(array_unique($mountIds));
  308. $applicable = $this->getApplicableForMounts($mountIds);
  309. $config = $this->getConfigForMounts($mountIds);
  310. $options = $this->getOptionsForMounts($mountIds);
  311. return array_map(function ($mount, $applicable, $config, $options) {
  312. $mount['type'] = (int)$mount['type'];
  313. $mount['priority'] = (int)$mount['priority'];
  314. $mount['applicable'] = $applicable;
  315. $mount['config'] = $config;
  316. $mount['options'] = $options;
  317. return $mount;
  318. }, $uniqueMounts, $applicable, $config, $options);
  319. }
  320. /**
  321. * Get mount options from a table grouped by mount id
  322. *
  323. * @param string $table
  324. * @param string[] $fields
  325. * @param int[] $mountIds
  326. * @return array [$mountId => [['field1' => $value1, ...], ...], ...]
  327. */
  328. private function selectForMounts($table, array $fields, array $mountIds) {
  329. if (count($mountIds) === 0) {
  330. return [];
  331. }
  332. $builder = $this->connection->getQueryBuilder();
  333. $fields[] = 'mount_id';
  334. $placeHolders = array_map(function ($id) use ($builder) {
  335. return $builder->createPositionalParameter($id, IQueryBuilder::PARAM_INT);
  336. }, $mountIds);
  337. $query = $builder->select($fields)
  338. ->from($table)
  339. ->where($builder->expr()->in('mount_id', $placeHolders));
  340. $rows = $query->execute()->fetchAll();
  341. $result = [];
  342. foreach ($mountIds as $mountId) {
  343. $result[$mountId] = [];
  344. }
  345. foreach ($rows as $row) {
  346. if (isset($row['type'])) {
  347. $row['type'] = (int)$row['type'];
  348. }
  349. $result[$row['mount_id']][] = $row;
  350. }
  351. return $result;
  352. }
  353. /**
  354. * @param int[] $mountIds
  355. * @return array [$id => [['type' => $type, 'value' => $value], ...], ...]
  356. */
  357. public function getApplicableForMounts($mountIds) {
  358. return $this->selectForMounts('external_applicable', ['type', 'value'], $mountIds);
  359. }
  360. /**
  361. * @param int[] $mountIds
  362. * @return array [$id => ['key1' => $value1, ...], ...]
  363. */
  364. public function getConfigForMounts($mountIds) {
  365. $mountConfigs = $this->selectForMounts('external_config', ['key', 'value'], $mountIds);
  366. return array_map([$this, 'createKeyValueMap'], $mountConfigs);
  367. }
  368. /**
  369. * @param int[] $mountIds
  370. * @return array [$id => ['key1' => $value1, ...], ...]
  371. */
  372. public function getOptionsForMounts($mountIds) {
  373. $mountOptions = $this->selectForMounts('external_options', ['key', 'value'], $mountIds);
  374. $optionsMap = array_map([$this, 'createKeyValueMap'], $mountOptions);
  375. return array_map(function (array $options) {
  376. return array_map(function ($option) {
  377. return json_decode($option);
  378. }, $options);
  379. }, $optionsMap);
  380. }
  381. /**
  382. * @param array $keyValuePairs [['key'=>$key, 'value=>$value], ...]
  383. * @return array ['key1' => $value1, ...]
  384. */
  385. private function createKeyValueMap(array $keyValuePairs) {
  386. $decryptedPairts = array_map(function ($pair) {
  387. if ($pair['key'] === 'password') {
  388. $pair['value'] = $this->decryptValue($pair['value']);
  389. }
  390. return $pair;
  391. }, $keyValuePairs);
  392. $keys = array_map(function ($pair) {
  393. return $pair['key'];
  394. }, $decryptedPairts);
  395. $values = array_map(function ($pair) {
  396. return $pair['value'];
  397. }, $decryptedPairts);
  398. return array_combine($keys, $values);
  399. }
  400. private function encryptValue($value) {
  401. return $this->crypto->encrypt($value);
  402. }
  403. private function decryptValue($value) {
  404. try {
  405. return $this->crypto->decrypt($value);
  406. } catch (\Exception $e) {
  407. return $value;
  408. }
  409. }
  410. }