OrphanHelper.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Sharing;
  8. use OC\User\NoUserException;
  9. use OCP\DB\QueryBuilder\IQueryBuilder;
  10. use OCP\Files\Config\IUserMountCache;
  11. use OCP\Files\IRootFolder;
  12. use OCP\IDBConnection;
  13. class OrphanHelper {
  14. public function __construct(
  15. private IDBConnection $connection,
  16. private IRootFolder $rootFolder,
  17. private IUserMountCache $userMountCache,
  18. ) {
  19. }
  20. public function isShareValid(string $owner, int $fileId): bool {
  21. try {
  22. $userFolder = $this->rootFolder->getUserFolder($owner);
  23. } catch (NoUserException $e) {
  24. return false;
  25. }
  26. $node = $userFolder->getFirstNodeById($fileId);
  27. return $node !== null;
  28. }
  29. /**
  30. * @param int[] $ids
  31. * @return void
  32. */
  33. public function deleteShares(array $ids): void {
  34. $query = $this->connection->getQueryBuilder();
  35. $query->delete('share')
  36. ->where($query->expr()->in('id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
  37. $query->executeStatement();
  38. }
  39. public function fileExists(int $fileId): bool {
  40. $query = $this->connection->getQueryBuilder();
  41. $query->select('fileid')
  42. ->from('filecache')
  43. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  44. return $query->executeQuery()->fetchOne() !== false;
  45. }
  46. /**
  47. * @return \Traversable<int, array{id: int, owner: string, fileid: int, target: string}>
  48. */
  49. public function getAllShares() {
  50. $query = $this->connection->getQueryBuilder();
  51. $query->select('id', 'file_source', 'uid_owner', 'file_target')
  52. ->from('share')
  53. ->where($query->expr()->eq('item_type', $query->createNamedParameter('file')))
  54. ->orWhere($query->expr()->eq('item_type', $query->createNamedParameter('folder')));
  55. $result = $query->executeQuery();
  56. while ($row = $result->fetch()) {
  57. yield [
  58. 'id' => (int)$row['id'],
  59. 'owner' => (string)$row['uid_owner'],
  60. 'fileid' => (int)$row['file_source'],
  61. 'target' => (string)$row['file_target'],
  62. ];
  63. }
  64. }
  65. public function findOwner(int $fileId): ?string {
  66. $mounts = $this->userMountCache->getMountsForFileId($fileId);
  67. if (!$mounts) {
  68. return null;
  69. }
  70. foreach ($mounts as $mount) {
  71. $userHomeMountPoint = '/' . $mount->getUser()->getUID() . '/';
  72. if ($mount->getMountPoint() === $userHomeMountPoint) {
  73. return $mount->getUser()->getUID();
  74. }
  75. }
  76. return null;
  77. }
  78. public function updateShareOwner(int $shareId, string $owner): void {
  79. $query = $this->connection->getQueryBuilder();
  80. $query->update('share')
  81. ->set('uid_owner', $query->createNamedParameter($owner))
  82. ->where($query->expr()->eq('id', $query->createNamedParameter($shareId, IQueryBuilder::PARAM_INT)));
  83. $query->executeStatement();
  84. }
  85. }