You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

File.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Encryption;
  29. use OCA\Files_External\Service\GlobalStoragesService;
  30. use OCP\App\IAppManager;
  31. use OCP\Cache\CappedMemoryCache;
  32. use OCP\Files\IRootFolder;
  33. use OCP\Files\NotFoundException;
  34. use OCP\Share\IManager;
  35. class File implements \OCP\Encryption\IFile {
  36. protected Util $util;
  37. private IRootFolder $rootFolder;
  38. private IManager $shareManager;
  39. /**
  40. * Cache results of already checked folders
  41. * @var CappedMemoryCache<array>
  42. */
  43. protected CappedMemoryCache $cache;
  44. private ?IAppManager $appManager = null;
  45. public function __construct(Util $util,
  46. IRootFolder $rootFolder,
  47. IManager $shareManager) {
  48. $this->util = $util;
  49. $this->cache = new CappedMemoryCache();
  50. $this->rootFolder = $rootFolder;
  51. $this->shareManager = $shareManager;
  52. }
  53. public function getAppManager(): IAppManager {
  54. // Lazy evaluate app manager as it initialize the db too early otherwise
  55. if ($this->appManager) {
  56. return $this->appManager;
  57. }
  58. $this->appManager = \OCP\Server::get(IAppManager::class);
  59. return $this->appManager;
  60. }
  61. /**
  62. * Get list of users with access to the file
  63. *
  64. * @param string $path to the file
  65. * @return array{users: string[], public: bool}
  66. */
  67. public function getAccessList($path) {
  68. // Make sure that a share key is generated for the owner too
  69. [$owner, $ownerPath] = $this->util->getUidAndFilename($path);
  70. // always add owner to the list of users with access to the file
  71. $userIds = [$owner];
  72. if (!$this->util->isFile($owner . '/' . $ownerPath)) {
  73. return ['users' => $userIds, 'public' => false];
  74. }
  75. $ownerPath = substr($ownerPath, strlen('/files'));
  76. $userFolder = $this->rootFolder->getUserFolder($owner);
  77. try {
  78. $file = $userFolder->get($ownerPath);
  79. } catch (NotFoundException $e) {
  80. $file = null;
  81. }
  82. $ownerPath = $this->util->stripPartialFileExtension($ownerPath);
  83. // first get the shares for the parent and cache the result so that we don't
  84. // need to check all parents for every file
  85. $parent = dirname($ownerPath);
  86. $parentNode = $userFolder->get($parent);
  87. if (isset($this->cache[$parent])) {
  88. $resultForParents = $this->cache[$parent];
  89. } else {
  90. $resultForParents = $this->shareManager->getAccessList($parentNode);
  91. $this->cache[$parent] = $resultForParents;
  92. }
  93. $userIds = array_merge($userIds, $resultForParents['users']);
  94. $public = $resultForParents['public'] || $resultForParents['remote'];
  95. // Find out who, if anyone, is sharing the file
  96. if ($file !== null) {
  97. $resultForFile = $this->shareManager->getAccessList($file, false);
  98. $userIds = array_merge($userIds, $resultForFile['users']);
  99. $public = $resultForFile['public'] || $resultForFile['remote'] || $public;
  100. }
  101. // check if it is a group mount
  102. if ($this->getAppManager()->isEnabledForUser("files_external")) {
  103. /** @var GlobalStoragesService $storageService */
  104. $storageService = \OC::$server->get(GlobalStoragesService::class);
  105. $storages = $storageService->getAllStorages();
  106. foreach ($storages as $storage) {
  107. if ($storage->getMountPoint() == substr($ownerPath, 0, strlen($storage->getMountPoint()))) {
  108. $mountedFor = $this->util->getUserWithAccessToMountPoint($storage->getApplicableUsers(), $storage->getApplicableGroups());
  109. $userIds = array_merge($userIds, $mountedFor);
  110. }
  111. }
  112. }
  113. // Remove duplicate UIDs
  114. $uniqueUserIds = array_unique($userIds);
  115. return ['users' => $uniqueUserIds, 'public' => $public];
  116. }
  117. }