File.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 OCP\Cache\CappedMemoryCache;
  30. use OCA\Files_External\Service\GlobalStoragesService;
  31. use OCP\Files\IRootFolder;
  32. use OCP\Files\NotFoundException;
  33. use OCP\Share\IManager;
  34. class File implements \OCP\Encryption\IFile {
  35. /** @var Util */
  36. protected $util;
  37. /** @var IRootFolder */
  38. private $rootFolder;
  39. /** @var IManager */
  40. private $shareManager;
  41. /**
  42. * cache results of already checked folders
  43. *
  44. * @var CappedMemoryCache<array>
  45. */
  46. protected CappedMemoryCache $cache;
  47. public function __construct(Util $util,
  48. IRootFolder $rootFolder,
  49. IManager $shareManager) {
  50. $this->util = $util;
  51. $this->cache = new CappedMemoryCache();
  52. $this->rootFolder = $rootFolder;
  53. $this->shareManager = $shareManager;
  54. }
  55. /**
  56. * Get list of users with access to the file
  57. *
  58. * @param string $path to the file
  59. * @return array{users: string[], public: bool}
  60. */
  61. public function getAccessList($path) {
  62. // Make sure that a share key is generated for the owner too
  63. [$owner, $ownerPath] = $this->util->getUidAndFilename($path);
  64. // always add owner to the list of users with access to the file
  65. $userIds = [$owner];
  66. if (!$this->util->isFile($owner . '/' . $ownerPath)) {
  67. return ['users' => $userIds, 'public' => false];
  68. }
  69. $ownerPath = substr($ownerPath, strlen('/files'));
  70. $userFolder = $this->rootFolder->getUserFolder($owner);
  71. try {
  72. $file = $userFolder->get($ownerPath);
  73. } catch (NotFoundException $e) {
  74. $file = null;
  75. }
  76. $ownerPath = $this->util->stripPartialFileExtension($ownerPath);
  77. // first get the shares for the parent and cache the result so that we don't
  78. // need to check all parents for every file
  79. $parent = dirname($ownerPath);
  80. $parentNode = $userFolder->get($parent);
  81. if (isset($this->cache[$parent])) {
  82. $resultForParents = $this->cache[$parent];
  83. } else {
  84. $resultForParents = $this->shareManager->getAccessList($parentNode);
  85. $this->cache[$parent] = $resultForParents;
  86. }
  87. $userIds = array_merge($userIds, $resultForParents['users']);
  88. $public = $resultForParents['public'] || $resultForParents['remote'];
  89. // Find out who, if anyone, is sharing the file
  90. if ($file !== null) {
  91. $resultForFile = $this->shareManager->getAccessList($file, false);
  92. $userIds = array_merge($userIds, $resultForFile['users']);
  93. $public = $resultForFile['public'] || $resultForFile['remote'] || $public;
  94. }
  95. // check if it is a group mount
  96. if (\OCP\App::isEnabled("files_external")) {
  97. /** @var GlobalStoragesService $storageService */
  98. $storageService = \OC::$server->get(GlobalStoragesService::class);
  99. $storages = $storageService->getAllStorages();
  100. foreach ($storages as $storage) {
  101. if ($storage->getMountPoint() == substr($ownerPath, 0, strlen($storage->getMountPoint()))) {
  102. $mountedFor = $this->util->getUserWithAccessToMountPoint($storage->getApplicableUsers(), $storage->getApplicableGroups());
  103. $userIds = array_merge($userIds, $mountedFor);
  104. }
  105. }
  106. }
  107. // Remove duplicate UIDs
  108. $uniqueUserIds = array_unique($userIds);
  109. return ['users' => $uniqueUserIds, 'public' => $public];
  110. }
  111. }