1
0

MountProvider.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\External;
  8. use OCP\DB\QueryBuilder\IQueryBuilder;
  9. use OCP\Federation\ICloudIdManager;
  10. use OCP\Files\Config\IMountProvider;
  11. use OCP\Files\Storage\IStorageFactory;
  12. use OCP\IDBConnection;
  13. use OCP\IUser;
  14. class MountProvider implements IMountProvider {
  15. public const STORAGE = '\OCA\Files_Sharing\External\Storage';
  16. /**
  17. * @var \OCP\IDBConnection
  18. */
  19. private $connection;
  20. /**
  21. * @var callable
  22. */
  23. private $managerProvider;
  24. /**
  25. * @var ICloudIdManager
  26. */
  27. private $cloudIdManager;
  28. /**
  29. * @param \OCP\IDBConnection $connection
  30. * @param callable $managerProvider due to setup order we need a callable that return the manager instead of the manager itself
  31. * @param ICloudIdManager $cloudIdManager
  32. */
  33. public function __construct(IDBConnection $connection, callable $managerProvider, ICloudIdManager $cloudIdManager) {
  34. $this->connection = $connection;
  35. $this->managerProvider = $managerProvider;
  36. $this->cloudIdManager = $cloudIdManager;
  37. }
  38. public function getMount(IUser $user, $data, IStorageFactory $storageFactory) {
  39. $managerProvider = $this->managerProvider;
  40. $manager = $managerProvider();
  41. $data['manager'] = $manager;
  42. $mountPoint = '/' . $user->getUID() . '/files/' . ltrim($data['mountpoint'], '/');
  43. $data['mountpoint'] = $mountPoint;
  44. $data['cloudId'] = $this->cloudIdManager->getCloudId($data['owner'], $data['remote']);
  45. $data['certificateManager'] = \OC::$server->getCertificateManager();
  46. $data['HttpClientService'] = \OC::$server->getHTTPClientService();
  47. return new Mount(self::STORAGE, $mountPoint, $data, $manager, $storageFactory);
  48. }
  49. public function getMountsForUser(IUser $user, IStorageFactory $loader) {
  50. $qb = $this->connection->getQueryBuilder();
  51. $qb->select('remote', 'share_token', 'password', 'mountpoint', 'owner')
  52. ->from('share_external')
  53. ->where($qb->expr()->eq('user', $qb->createNamedParameter($user->getUID())))
  54. ->andWhere($qb->expr()->eq('accepted', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)));
  55. $result = $qb->executeQuery();
  56. $mounts = [];
  57. while ($row = $result->fetch()) {
  58. $row['manager'] = $this;
  59. $row['token'] = $row['share_token'];
  60. $mounts[] = $this->getMount($user, $row, $loader);
  61. }
  62. $result->closeCursor();
  63. return $mounts;
  64. }
  65. }