File.php 3.7 KB

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