MountProvider.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Sharing\External;
  26. use OCP\DB\QueryBuilder\IQueryBuilder;
  27. use OCP\Federation\ICloudIdManager;
  28. use OCP\Files\Config\IMountProvider;
  29. use OCP\Files\Storage\IStorageFactory;
  30. use OCP\IDBConnection;
  31. use OCP\IUser;
  32. class MountProvider implements IMountProvider {
  33. public const STORAGE = '\OCA\Files_Sharing\External\Storage';
  34. /**
  35. * @var \OCP\IDBConnection
  36. */
  37. private $connection;
  38. /**
  39. * @var callable
  40. */
  41. private $managerProvider;
  42. /**
  43. * @var ICloudIdManager
  44. */
  45. private $cloudIdManager;
  46. /**
  47. * @param \OCP\IDBConnection $connection
  48. * @param callable $managerProvider due to setup order we need a callable that return the manager instead of the manager itself
  49. * @param ICloudIdManager $cloudIdManager
  50. */
  51. public function __construct(IDBConnection $connection, callable $managerProvider, ICloudIdManager $cloudIdManager) {
  52. $this->connection = $connection;
  53. $this->managerProvider = $managerProvider;
  54. $this->cloudIdManager = $cloudIdManager;
  55. }
  56. public function getMount(IUser $user, $data, IStorageFactory $storageFactory) {
  57. $managerProvider = $this->managerProvider;
  58. $manager = $managerProvider();
  59. $data['manager'] = $manager;
  60. $mountPoint = '/' . $user->getUID() . '/files/' . ltrim($data['mountpoint'], '/');
  61. $data['mountpoint'] = $mountPoint;
  62. $data['cloudId'] = $this->cloudIdManager->getCloudId($data['owner'], $data['remote']);
  63. $data['certificateManager'] = \OC::$server->getCertificateManager();
  64. $data['HttpClientService'] = \OC::$server->getHTTPClientService();
  65. return new Mount(self::STORAGE, $mountPoint, $data, $manager, $storageFactory);
  66. }
  67. public function getMountsForUser(IUser $user, IStorageFactory $loader) {
  68. $qb = $this->connection->getQueryBuilder();
  69. $qb->select('remote', 'share_token', 'password', 'mountpoint', 'owner')
  70. ->from('share_external')
  71. ->where($qb->expr()->eq('user', $qb->createNamedParameter($user->getUID())))
  72. ->andWhere($qb->expr()->eq('accepted', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)));
  73. $result = $qb->executeQuery();
  74. $mounts = [];
  75. while ($row = $result->fetch()) {
  76. $row['manager'] = $this;
  77. $row['token'] = $row['share_token'];
  78. $mounts[] = $this->getMount($user, $row, $loader);
  79. }
  80. $result->closeCursor();
  81. return $mounts;
  82. }
  83. }