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.

128 lines
3.9 KiB

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