MountProviderCollection.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * @param IUser $user
  69. * @param IMountProvider[] $providers
  70. * @return IMountPoint[]
  71. */
  72. private function getUserMountsForProviders(IUser $user, array $providers): array {
  73. $loader = $this->loader;
  74. $mounts = array_map(function (IMountProvider $provider) use ($user, $loader) {
  75. return $provider->getMountsForUser($user, $loader);
  76. }, $providers);
  77. $mounts = array_filter($mounts, function ($result) {
  78. return is_array($result);
  79. });
  80. $mounts = array_reduce($mounts, function (array $mounts, array $providerMounts) {
  81. return array_merge($mounts, $providerMounts);
  82. }, []);
  83. return $this->filterMounts($user, $mounts);
  84. }
  85. public function getMountsForUser(IUser $user): array {
  86. return $this->getUserMountsForProviders($user, $this->providers);
  87. }
  88. public function getUserMountsForProviderClasses(IUser $user, array $mountProviderClasses): array {
  89. $providers = array_filter(
  90. $this->providers,
  91. fn (IMountProvider $mountProvider) => (in_array(get_class($mountProvider), $mountProviderClasses))
  92. );
  93. return $this->getUserMountsForProviders($user, $providers);
  94. }
  95. public function addMountForUser(IUser $user, IMountManager $mountManager, callable $providerFilter = null) {
  96. // shared mount provider gets to go last since it needs to know existing files
  97. // to check for name collisions
  98. $firstMounts = [];
  99. if ($providerFilter) {
  100. $providers = array_filter($this->providers, $providerFilter);
  101. } else {
  102. $providers = $this->providers;
  103. }
  104. $firstProviders = array_filter($providers, function (IMountProvider $provider) {
  105. return (get_class($provider) !== 'OCA\Files_Sharing\MountProvider');
  106. });
  107. $lastProviders = array_filter($providers, function (IMountProvider $provider) {
  108. return (get_class($provider) === 'OCA\Files_Sharing\MountProvider');
  109. });
  110. foreach ($firstProviders as $provider) {
  111. $mounts = $provider->getMountsForUser($user, $this->loader);
  112. if (is_array($mounts)) {
  113. $firstMounts = array_merge($firstMounts, $mounts);
  114. }
  115. }
  116. $firstMounts = $this->filterMounts($user, $firstMounts);
  117. array_walk($firstMounts, [$mountManager, 'addMount']);
  118. $lateMounts = [];
  119. foreach ($lastProviders as $provider) {
  120. $mounts = $provider->getMountsForUser($user, $this->loader);
  121. if (is_array($mounts)) {
  122. $lateMounts = array_merge($lateMounts, $mounts);
  123. }
  124. }
  125. $lateMounts = $this->filterMounts($user, $lateMounts);
  126. array_walk($lateMounts, [$mountManager, 'addMount']);
  127. return array_merge($lateMounts, $firstMounts);
  128. }
  129. /**
  130. * Get the configured home mount for this user
  131. *
  132. * @param \OCP\IUser $user
  133. * @return \OCP\Files\Mount\IMountPoint
  134. * @since 9.1.0
  135. */
  136. public function getHomeMountForUser(IUser $user) {
  137. /** @var \OCP\Files\Config\IHomeMountProvider[] $providers */
  138. $providers = array_reverse($this->homeProviders); // call the latest registered provider first to give apps an opportunity to overwrite builtin
  139. foreach ($providers as $homeProvider) {
  140. if ($mount = $homeProvider->getHomeMountForUser($user, $this->loader)) {
  141. $mount->setMountPoint('/' . $user->getUID()); //make sure the mountpoint is what we expect
  142. return $mount;
  143. }
  144. }
  145. throw new \Exception('No home storage configured for user ' . $user);
  146. }
  147. /**
  148. * Add a provider for mount points
  149. *
  150. * @param \OCP\Files\Config\IMountProvider $provider
  151. */
  152. public function registerProvider(IMountProvider $provider) {
  153. $this->providers[] = $provider;
  154. $this->emit('\OC\Files\Config', 'registerMountProvider', [$provider]);
  155. }
  156. public function registerMountFilter(callable $filter) {
  157. $this->mountFilters[] = $filter;
  158. }
  159. private function filterMounts(IUser $user, array $mountPoints) {
  160. return array_filter($mountPoints, function (IMountPoint $mountPoint) use ($user) {
  161. foreach ($this->mountFilters as $filter) {
  162. if ($filter($mountPoint, $user) === false) {
  163. return false;
  164. }
  165. }
  166. return true;
  167. });
  168. }
  169. /**
  170. * Add a provider for home mount points
  171. *
  172. * @param \OCP\Files\Config\IHomeMountProvider $provider
  173. * @since 9.1.0
  174. */
  175. public function registerHomeProvider(IHomeMountProvider $provider) {
  176. $this->homeProviders[] = $provider;
  177. $this->emit('\OC\Files\Config', 'registerHomeMountProvider', [$provider]);
  178. }
  179. /**
  180. * Get the mount cache which can be used to search for mounts without setting up the filesystem
  181. *
  182. * @return IUserMountCache
  183. */
  184. public function getMountCache() {
  185. return $this->mountCache;
  186. }
  187. public function registerRootProvider(IRootMountProvider $provider) {
  188. $this->rootProviders[] = $provider;
  189. }
  190. /**
  191. * Get all root mountpoints
  192. *
  193. * @return \OCP\Files\Mount\IMountPoint[]
  194. * @since 20.0.0
  195. */
  196. public function getRootMounts(): array {
  197. $loader = $this->loader;
  198. $mounts = array_map(function (IRootMountProvider $provider) use ($loader) {
  199. return $provider->getRootMounts($loader);
  200. }, $this->rootProviders);
  201. $mounts = array_reduce($mounts, function (array $mounts, array $providerMounts) {
  202. return array_merge($mounts, $providerMounts);
  203. }, []);
  204. return $mounts;
  205. }
  206. public function clearProviders() {
  207. $this->providers = [];
  208. $this->homeProviders = [];
  209. $this->rootProviders = [];
  210. }
  211. public function getProviders(): array {
  212. return $this->providers;
  213. }
  214. }