ProviderFactory.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author John Molakvoæ <skjnldsv@protonmail.com>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Maxence Lange <maxence@nextcloud.com>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Robin Appelman <robin@icewind.nl>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author Samuel <faust64@gmail.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OC\Share20;
  35. use OC\Share20\Exception\ProviderException;
  36. use OCA\FederatedFileSharing\AddressHandler;
  37. use OCA\FederatedFileSharing\FederatedShareProvider;
  38. use OCA\FederatedFileSharing\Notifications;
  39. use OCA\FederatedFileSharing\TokenHandler;
  40. use OCA\ShareByMail\Settings\SettingsManager;
  41. use OCA\ShareByMail\ShareByMailProvider;
  42. use OCA\Talk\Share\RoomShareProvider;
  43. use OCP\AppFramework\Utility\ITimeFactory;
  44. use OCP\Defaults;
  45. use OCP\EventDispatcher\IEventDispatcher;
  46. use OCP\Federation\ICloudFederationFactory;
  47. use OCP\Files\IRootFolder;
  48. use OCP\IServerContainer;
  49. use OCP\Security\IHasher;
  50. use OCP\Share\IManager;
  51. use OCP\Share\IProviderFactory;
  52. use OCP\Share\IShare;
  53. use OCP\Share\IShareProvider;
  54. use Psr\Log\LoggerInterface;
  55. /**
  56. * Class ProviderFactory
  57. *
  58. * @package OC\Share20
  59. */
  60. class ProviderFactory implements IProviderFactory {
  61. /** @var IServerContainer */
  62. private $serverContainer;
  63. /** @var DefaultShareProvider */
  64. private $defaultProvider = null;
  65. /** @var FederatedShareProvider */
  66. private $federatedProvider = null;
  67. /** @var ShareByMailProvider */
  68. private $shareByMailProvider;
  69. /** @var \OCA\Circles\ShareByCircleProvider */
  70. private $shareByCircleProvider = null;
  71. /** @var bool */
  72. private $circlesAreNotAvailable = false;
  73. /** @var \OCA\Talk\Share\RoomShareProvider */
  74. private $roomShareProvider = null;
  75. private $registeredShareProviders = [];
  76. private $shareProviders = [];
  77. /**
  78. * IProviderFactory constructor.
  79. *
  80. * @param IServerContainer $serverContainer
  81. */
  82. public function __construct(IServerContainer $serverContainer) {
  83. $this->serverContainer = $serverContainer;
  84. }
  85. public function registerProvider(string $shareProviderClass): void {
  86. $this->registeredShareProviders[] = $shareProviderClass;
  87. }
  88. /**
  89. * Create the default share provider.
  90. *
  91. * @return DefaultShareProvider
  92. */
  93. protected function defaultShareProvider() {
  94. if ($this->defaultProvider === null) {
  95. $this->defaultProvider = new DefaultShareProvider(
  96. $this->serverContainer->getDatabaseConnection(),
  97. $this->serverContainer->getUserManager(),
  98. $this->serverContainer->getGroupManager(),
  99. $this->serverContainer->get(IRootFolder::class),
  100. $this->serverContainer->getMailer(),
  101. $this->serverContainer->query(Defaults::class),
  102. $this->serverContainer->getL10NFactory(),
  103. $this->serverContainer->getURLGenerator(),
  104. $this->serverContainer->query(ITimeFactory::class),
  105. );
  106. }
  107. return $this->defaultProvider;
  108. }
  109. /**
  110. * Create the federated share provider
  111. *
  112. * @return FederatedShareProvider
  113. */
  114. protected function federatedShareProvider() {
  115. if ($this->federatedProvider === null) {
  116. /*
  117. * Check if the app is enabled
  118. */
  119. $appManager = $this->serverContainer->getAppManager();
  120. if (!$appManager->isEnabledForUser('federatedfilesharing')) {
  121. return null;
  122. }
  123. /*
  124. * TODO: add factory to federated sharing app
  125. */
  126. $l = $this->serverContainer->getL10N('federatedfilesharing');
  127. $addressHandler = new AddressHandler(
  128. $this->serverContainer->getURLGenerator(),
  129. $l,
  130. $this->serverContainer->getCloudIdManager()
  131. );
  132. $notifications = new Notifications(
  133. $addressHandler,
  134. $this->serverContainer->getHTTPClientService(),
  135. $this->serverContainer->query(\OCP\OCS\IDiscoveryService::class),
  136. $this->serverContainer->getJobList(),
  137. \OC::$server->getCloudFederationProviderManager(),
  138. \OC::$server->get(ICloudFederationFactory::class),
  139. $this->serverContainer->query(IEventDispatcher::class),
  140. $this->serverContainer->get(LoggerInterface::class),
  141. );
  142. $tokenHandler = new TokenHandler(
  143. $this->serverContainer->getSecureRandom()
  144. );
  145. $this->federatedProvider = new FederatedShareProvider(
  146. $this->serverContainer->getDatabaseConnection(),
  147. $addressHandler,
  148. $notifications,
  149. $tokenHandler,
  150. $l,
  151. $this->serverContainer->get(IRootFolder::class),
  152. $this->serverContainer->getConfig(),
  153. $this->serverContainer->getUserManager(),
  154. $this->serverContainer->getCloudIdManager(),
  155. $this->serverContainer->getGlobalScaleConfig(),
  156. $this->serverContainer->getCloudFederationProviderManager(),
  157. $this->serverContainer->get(LoggerInterface::class),
  158. );
  159. }
  160. return $this->federatedProvider;
  161. }
  162. /**
  163. * Create the federated share provider
  164. *
  165. * @return ShareByMailProvider
  166. */
  167. protected function getShareByMailProvider() {
  168. if ($this->shareByMailProvider === null) {
  169. /*
  170. * Check if the app is enabled
  171. */
  172. $appManager = $this->serverContainer->getAppManager();
  173. if (!$appManager->isEnabledForUser('sharebymail')) {
  174. return null;
  175. }
  176. $settingsManager = new SettingsManager($this->serverContainer->getConfig());
  177. $this->shareByMailProvider = new ShareByMailProvider(
  178. $this->serverContainer->getConfig(),
  179. $this->serverContainer->getDatabaseConnection(),
  180. $this->serverContainer->getSecureRandom(),
  181. $this->serverContainer->getUserManager(),
  182. $this->serverContainer->get(IRootFolder::class),
  183. $this->serverContainer->getL10N('sharebymail'),
  184. $this->serverContainer->get(LoggerInterface::class),
  185. $this->serverContainer->getMailer(),
  186. $this->serverContainer->getURLGenerator(),
  187. $this->serverContainer->getActivityManager(),
  188. $settingsManager,
  189. $this->serverContainer->query(Defaults::class),
  190. $this->serverContainer->get(IHasher::class),
  191. $this->serverContainer->get(IEventDispatcher::class),
  192. $this->serverContainer->get(IManager::class)
  193. );
  194. }
  195. return $this->shareByMailProvider;
  196. }
  197. /**
  198. * Create the circle share provider
  199. *
  200. * @return FederatedShareProvider
  201. *
  202. * @suppress PhanUndeclaredClassMethod
  203. */
  204. protected function getShareByCircleProvider() {
  205. if ($this->circlesAreNotAvailable) {
  206. return null;
  207. }
  208. if (!$this->serverContainer->getAppManager()->isEnabledForUser('circles') ||
  209. !class_exists('\OCA\Circles\ShareByCircleProvider')
  210. ) {
  211. $this->circlesAreNotAvailable = true;
  212. return null;
  213. }
  214. if ($this->shareByCircleProvider === null) {
  215. $this->shareByCircleProvider = new \OCA\Circles\ShareByCircleProvider(
  216. $this->serverContainer->getDatabaseConnection(),
  217. $this->serverContainer->getSecureRandom(),
  218. $this->serverContainer->getUserManager(),
  219. $this->serverContainer->get(IRootFolder::class),
  220. $this->serverContainer->getL10N('circles'),
  221. $this->serverContainer->getLogger(),
  222. $this->serverContainer->getURLGenerator()
  223. );
  224. }
  225. return $this->shareByCircleProvider;
  226. }
  227. /**
  228. * Create the room share provider
  229. *
  230. * @return RoomShareProvider
  231. */
  232. protected function getRoomShareProvider() {
  233. if ($this->roomShareProvider === null) {
  234. /*
  235. * Check if the app is enabled
  236. */
  237. $appManager = $this->serverContainer->getAppManager();
  238. if (!$appManager->isEnabledForUser('spreed')) {
  239. return null;
  240. }
  241. try {
  242. /**
  243. * @psalm-suppress UndefinedClass
  244. */
  245. $this->roomShareProvider = $this->serverContainer->get(RoomShareProvider::class);
  246. } catch (\Throwable $e) {
  247. $this->serverContainer->get(LoggerInterface::class)->error(
  248. $e->getMessage(),
  249. ['exception' => $e]
  250. );
  251. return null;
  252. }
  253. }
  254. return $this->roomShareProvider;
  255. }
  256. /**
  257. * @inheritdoc
  258. */
  259. public function getProvider($id) {
  260. $provider = null;
  261. if (isset($this->shareProviders[$id])) {
  262. return $this->shareProviders[$id];
  263. }
  264. if ($id === 'ocinternal') {
  265. $provider = $this->defaultShareProvider();
  266. } elseif ($id === 'ocFederatedSharing') {
  267. $provider = $this->federatedShareProvider();
  268. } elseif ($id === 'ocMailShare') {
  269. $provider = $this->getShareByMailProvider();
  270. } elseif ($id === 'ocCircleShare') {
  271. $provider = $this->getShareByCircleProvider();
  272. } elseif ($id === 'ocRoomShare') {
  273. $provider = $this->getRoomShareProvider();
  274. }
  275. foreach ($this->registeredShareProviders as $shareProvider) {
  276. try {
  277. /** @var IShareProvider $instance */
  278. $instance = $this->serverContainer->get($shareProvider);
  279. $this->shareProviders[$instance->identifier()] = $instance;
  280. } catch (\Throwable $e) {
  281. $this->serverContainer->get(LoggerInterface::class)->error(
  282. $e->getMessage(),
  283. ['exception' => $e]
  284. );
  285. }
  286. }
  287. if (isset($this->shareProviders[$id])) {
  288. $provider = $this->shareProviders[$id];
  289. }
  290. if ($provider === null) {
  291. throw new ProviderException('No provider with id .' . $id . ' found.');
  292. }
  293. return $provider;
  294. }
  295. /**
  296. * @inheritdoc
  297. */
  298. public function getProviderForType($shareType) {
  299. $provider = null;
  300. if ($shareType === IShare::TYPE_USER ||
  301. $shareType === IShare::TYPE_GROUP ||
  302. $shareType === IShare::TYPE_LINK
  303. ) {
  304. $provider = $this->defaultShareProvider();
  305. } elseif ($shareType === IShare::TYPE_REMOTE || $shareType === IShare::TYPE_REMOTE_GROUP) {
  306. $provider = $this->federatedShareProvider();
  307. } elseif ($shareType === IShare::TYPE_EMAIL) {
  308. $provider = $this->getShareByMailProvider();
  309. } elseif ($shareType === IShare::TYPE_CIRCLE) {
  310. $provider = $this->getShareByCircleProvider();
  311. } elseif ($shareType === IShare::TYPE_ROOM) {
  312. $provider = $this->getRoomShareProvider();
  313. } elseif ($shareType === IShare::TYPE_DECK) {
  314. $provider = $this->getProvider('deck');
  315. } elseif ($shareType === IShare::TYPE_SCIENCEMESH) {
  316. $provider = $this->getProvider('sciencemesh');
  317. }
  318. if ($provider === null) {
  319. throw new ProviderException('No share provider for share type ' . $shareType);
  320. }
  321. return $provider;
  322. }
  323. public function getAllProviders() {
  324. $shares = [$this->defaultShareProvider(), $this->federatedShareProvider()];
  325. $shareByMail = $this->getShareByMailProvider();
  326. if ($shareByMail !== null) {
  327. $shares[] = $shareByMail;
  328. }
  329. $shareByCircle = $this->getShareByCircleProvider();
  330. if ($shareByCircle !== null) {
  331. $shares[] = $shareByCircle;
  332. }
  333. $roomShare = $this->getRoomShareProvider();
  334. if ($roomShare !== null) {
  335. $shares[] = $roomShare;
  336. }
  337. foreach ($this->registeredShareProviders as $shareProvider) {
  338. try {
  339. /** @var IShareProvider $instance */
  340. $instance = $this->serverContainer->get($shareProvider);
  341. } catch (\Throwable $e) {
  342. $this->serverContainer->get(LoggerInterface::class)->error(
  343. $e->getMessage(),
  344. ['exception' => $e]
  345. );
  346. continue;
  347. }
  348. if (!isset($this->shareProviders[$instance->identifier()])) {
  349. $this->shareProviders[$instance->identifier()] = $instance;
  350. }
  351. $shares[] = $this->shareProviders[$instance->identifier()];
  352. }
  353. return $shares;
  354. }
  355. }