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.

226 lines
6.6 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Files\Config;
  26. use OC\Hooks\Emitter;
  27. use OC\Hooks\EmitterTrait;
  28. use OCP\Files\Config\IHomeMountProvider;
  29. use OCP\Files\Config\IMountProvider;
  30. use OCP\Files\Config\IMountProviderCollection;
  31. use OCP\Files\Config\IRootMountProvider;
  32. use OCP\Files\Config\IUserMountCache;
  33. use OCP\Files\Mount\IMountManager;
  34. use OCP\Files\Mount\IMountPoint;
  35. use OCP\Files\Storage\IStorageFactory;
  36. use OCP\IUser;
  37. class MountProviderCollection implements IMountProviderCollection, Emitter {
  38. use EmitterTrait;
  39. /**
  40. * @var \OCP\Files\Config\IHomeMountProvider[]
  41. */
  42. private $homeProviders = [];
  43. /**
  44. * @var \OCP\Files\Config\IMountProvider[]
  45. */
  46. private $providers = [];
  47. /** @var \OCP\Files\Config\IRootMountProvider[] */
  48. private $rootProviders = [];
  49. /**
  50. * @var \OCP\Files\Storage\IStorageFactory
  51. */
  52. private $loader;
  53. /**
  54. * @var \OCP\Files\Config\IUserMountCache
  55. */
  56. private $mountCache;
  57. /** @var callable[] */
  58. private $mountFilters = [];
  59. /**
  60. * @param \OCP\Files\Storage\IStorageFactory $loader
  61. * @param IUserMountCache $mountCache
  62. */
  63. public function __construct(IStorageFactory $loader, IUserMountCache $mountCache) {
  64. $this->loader = $loader;
  65. $this->mountCache = $mountCache;
  66. }
  67. /**
  68. * Get all configured mount points for the user
  69. *
  70. * @param \OCP\IUser $user
  71. * @return \OCP\Files\Mount\IMountPoint[]
  72. */
  73. public function getMountsForUser(IUser $user) {
  74. $loader = $this->loader;
  75. $mounts = array_map(function (IMountProvider $provider) use ($user, $loader) {
  76. return $provider->getMountsForUser($user, $loader);
  77. }, $this->providers);
  78. $mounts = array_filter($mounts, function ($result) {
  79. return is_array($result);
  80. });
  81. $mounts = array_reduce($mounts, function (array $mounts, array $providerMounts) {
  82. return array_merge($mounts, $providerMounts);
  83. }, []);
  84. return $this->filterMounts($user, $mounts);
  85. }
  86. public function addMountForUser(IUser $user, IMountManager $mountManager) {
  87. // shared mount provider gets to go last since it needs to know existing files
  88. // to check for name collisions
  89. $firstMounts = [];
  90. $firstProviders = array_filter($this->providers, function (IMountProvider $provider) {
  91. return (get_class($provider) !== 'OCA\Files_Sharing\MountProvider');
  92. });
  93. $lastProviders = array_filter($this->providers, function (IMountProvider $provider) {
  94. return (get_class($provider) === 'OCA\Files_Sharing\MountProvider');
  95. });
  96. foreach ($firstProviders as $provider) {
  97. $mounts = $provider->getMountsForUser($user, $this->loader);
  98. if (is_array($mounts)) {
  99. $firstMounts = array_merge($firstMounts, $mounts);
  100. }
  101. }
  102. $firstMounts = $this->filterMounts($user, $firstMounts);
  103. array_walk($firstMounts, [$mountManager, 'addMount']);
  104. $lateMounts = [];
  105. foreach ($lastProviders as $provider) {
  106. $mounts = $provider->getMountsForUser($user, $this->loader);
  107. if (is_array($mounts)) {
  108. $lateMounts = array_merge($lateMounts, $mounts);
  109. }
  110. }
  111. $lateMounts = $this->filterMounts($user, $lateMounts);
  112. array_walk($lateMounts, [$mountManager, 'addMount']);
  113. return array_merge($lateMounts, $firstMounts);
  114. }
  115. /**
  116. * Get the configured home mount for this user
  117. *
  118. * @param \OCP\IUser $user
  119. * @return \OCP\Files\Mount\IMountPoint
  120. * @since 9.1.0
  121. */
  122. public function getHomeMountForUser(IUser $user) {
  123. /** @var \OCP\Files\Config\IHomeMountProvider[] $providers */
  124. $providers = array_reverse($this->homeProviders); // call the latest registered provider first to give apps an opportunity to overwrite builtin
  125. foreach ($providers as $homeProvider) {
  126. if ($mount = $homeProvider->getHomeMountForUser($user, $this->loader)) {
  127. $mount->setMountPoint('/' . $user->getUID()); //make sure the mountpoint is what we expect
  128. return $mount;
  129. }
  130. }
  131. throw new \Exception('No home storage configured for user ' . $user);
  132. }
  133. /**
  134. * Add a provider for mount points
  135. *
  136. * @param \OCP\Files\Config\IMountProvider $provider
  137. */
  138. public function registerProvider(IMountProvider $provider) {
  139. $this->providers[] = $provider;
  140. $this->emit('\OC\Files\Config', 'registerMountProvider', [$provider]);
  141. }
  142. public function registerMountFilter(callable $filter) {
  143. $this->mountFilters[] = $filter;
  144. }
  145. private function filterMounts(IUser $user, array $mountPoints) {
  146. return array_filter($mountPoints, function (IMountPoint $mountPoint) use ($user) {
  147. foreach ($this->mountFilters as $filter) {
  148. if ($filter($mountPoint, $user) === false) {
  149. return false;
  150. }
  151. }
  152. return true;
  153. });
  154. }
  155. /**
  156. * Add a provider for home mount points
  157. *
  158. * @param \OCP\Files\Config\IHomeMountProvider $provider
  159. * @since 9.1.0
  160. */
  161. public function registerHomeProvider(IHomeMountProvider $provider) {
  162. $this->homeProviders[] = $provider;
  163. $this->emit('\OC\Files\Config', 'registerHomeMountProvider', [$provider]);
  164. }
  165. /**
  166. * Cache mounts for user
  167. *
  168. * @param IUser $user
  169. * @param IMountPoint[] $mountPoints
  170. */
  171. public function registerMounts(IUser $user, array $mountPoints) {
  172. $this->mountCache->registerMounts($user, $mountPoints);
  173. }
  174. /**
  175. * Get the mount cache which can be used to search for mounts without setting up the filesystem
  176. *
  177. * @return IUserMountCache
  178. */
  179. public function getMountCache() {
  180. return $this->mountCache;
  181. }
  182. public function registerRootProvider(IRootMountProvider $provider) {
  183. $this->rootProviders[] = $provider;
  184. }
  185. /**
  186. * Get all root mountpoints
  187. *
  188. * @return \OCP\Files\Mount\IMountPoint[]
  189. * @since 20.0.0
  190. */
  191. public function getRootMounts(): array {
  192. $loader = $this->loader;
  193. $mounts = array_map(function (IRootMountProvider $provider) use ($loader) {
  194. return $provider->getRootMounts($loader);
  195. }, $this->rootProviders);
  196. $mounts = array_reduce($mounts, function (array $mounts, array $providerMounts) {
  197. return array_merge($mounts, $providerMounts);
  198. }, []);
  199. return $mounts;
  200. }
  201. }