ProviderFactory.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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\IServerContainer;
  47. use OCP\Share\IManager;
  48. use OCP\Share\IProviderFactory;
  49. use OCP\Share\IShare;
  50. use OCP\Share\IShareProvider;
  51. use Psr\Log\LoggerInterface;
  52. /**
  53. * Class ProviderFactory
  54. *
  55. * @package OC\Share20
  56. */
  57. class ProviderFactory implements IProviderFactory {
  58. /** @var IServerContainer */
  59. private $serverContainer;
  60. /** @var DefaultShareProvider */
  61. private $defaultProvider = null;
  62. /** @var FederatedShareProvider */
  63. private $federatedProvider = null;
  64. /** @var ShareByMailProvider */
  65. private $shareByMailProvider;
  66. /** @var \OCA\Circles\ShareByCircleProvider */
  67. private $shareByCircleProvider = null;
  68. /** @var bool */
  69. private $circlesAreNotAvailable = false;
  70. /** @var \OCA\Talk\Share\RoomShareProvider */
  71. private $roomShareProvider = null;
  72. private $registeredShareProviders = [];
  73. private $shareProviders = [];
  74. /**
  75. * IProviderFactory constructor.
  76. *
  77. * @param IServerContainer $serverContainer
  78. */
  79. public function __construct(IServerContainer $serverContainer) {
  80. $this->serverContainer = $serverContainer;
  81. }
  82. public function registerProvider(string $shareProviderClass): void {
  83. $this->registeredShareProviders[] = $shareProviderClass;
  84. }
  85. /**
  86. * Create the default share provider.
  87. *
  88. * @return DefaultShareProvider
  89. */
  90. protected function defaultShareProvider() {
  91. if ($this->defaultProvider === null) {
  92. $this->defaultProvider = new DefaultShareProvider(
  93. $this->serverContainer->getDatabaseConnection(),
  94. $this->serverContainer->getUserManager(),
  95. $this->serverContainer->getGroupManager(),
  96. $this->serverContainer->getLazyRootFolder(),
  97. $this->serverContainer->getMailer(),
  98. $this->serverContainer->query(Defaults::class),
  99. $this->serverContainer->getL10NFactory(),
  100. $this->serverContainer->getURLGenerator(),
  101. $this->serverContainer->query(ITimeFactory::class),
  102. $this->serverContainer->get(IManager::class),
  103. );
  104. }
  105. return $this->defaultProvider;
  106. }
  107. /**
  108. * Create the federated share provider
  109. *
  110. * @return FederatedShareProvider
  111. */
  112. protected function federatedShareProvider() {
  113. if ($this->federatedProvider === null) {
  114. /*
  115. * Check if the app is enabled
  116. */
  117. $appManager = $this->serverContainer->getAppManager();
  118. if (!$appManager->isEnabledForUser('federatedfilesharing')) {
  119. return null;
  120. }
  121. /*
  122. * TODO: add factory to federated sharing app
  123. */
  124. $l = $this->serverContainer->getL10N('federatedfilesharing');
  125. $addressHandler = new AddressHandler(
  126. $this->serverContainer->getURLGenerator(),
  127. $l,
  128. $this->serverContainer->getCloudIdManager()
  129. );
  130. $notifications = new Notifications(
  131. $addressHandler,
  132. $this->serverContainer->getHTTPClientService(),
  133. $this->serverContainer->query(\OCP\OCS\IDiscoveryService::class),
  134. $this->serverContainer->getJobList(),
  135. \OC::$server->getCloudFederationProviderManager(),
  136. \OC::$server->getCloudFederationFactory(),
  137. $this->serverContainer->query(IEventDispatcher::class),
  138. $this->serverContainer->get(LoggerInterface::class),
  139. );
  140. $tokenHandler = new TokenHandler(
  141. $this->serverContainer->getSecureRandom()
  142. );
  143. $this->federatedProvider = new FederatedShareProvider(
  144. $this->serverContainer->getDatabaseConnection(),
  145. $addressHandler,
  146. $notifications,
  147. $tokenHandler,
  148. $l,
  149. $this->serverContainer->getLazyRootFolder(),
  150. $this->serverContainer->getConfig(),
  151. $this->serverContainer->getUserManager(),
  152. $this->serverContainer->getCloudIdManager(),
  153. $this->serverContainer->getGlobalScaleConfig(),
  154. $this->serverContainer->getCloudFederationProviderManager(),
  155. $this->serverContainer->get(LoggerInterface::class),
  156. );
  157. }
  158. return $this->federatedProvider;
  159. }
  160. /**
  161. * Create the federated share provider
  162. *
  163. * @return ShareByMailProvider
  164. */
  165. protected function getShareByMailProvider() {
  166. if ($this->shareByMailProvider === null) {
  167. /*
  168. * Check if the app is enabled
  169. */
  170. $appManager = $this->serverContainer->getAppManager();
  171. if (!$appManager->isEnabledForUser('sharebymail')) {
  172. return null;
  173. }
  174. $settingsManager = new SettingsManager($this->serverContainer->getConfig());
  175. $this->shareByMailProvider = new ShareByMailProvider(
  176. $this->serverContainer->getConfig(),
  177. $this->serverContainer->getDatabaseConnection(),
  178. $this->serverContainer->getSecureRandom(),
  179. $this->serverContainer->getUserManager(),
  180. $this->serverContainer->getLazyRootFolder(),
  181. $this->serverContainer->getL10N('sharebymail'),
  182. $this->serverContainer->get(LoggerInterface::class),
  183. $this->serverContainer->getMailer(),
  184. $this->serverContainer->getURLGenerator(),
  185. $this->serverContainer->getActivityManager(),
  186. $settingsManager,
  187. $this->serverContainer->query(Defaults::class),
  188. $this->serverContainer->getHasher(),
  189. $this->serverContainer->get(IEventDispatcher::class),
  190. $this->serverContainer->get(IManager::class)
  191. );
  192. }
  193. return $this->shareByMailProvider;
  194. }
  195. /**
  196. * Create the circle share provider
  197. *
  198. * @return FederatedShareProvider
  199. *
  200. * @suppress PhanUndeclaredClassMethod
  201. */
  202. protected function getShareByCircleProvider() {
  203. if ($this->circlesAreNotAvailable) {
  204. return null;
  205. }
  206. if (!$this->serverContainer->getAppManager()->isEnabledForUser('circles') ||
  207. !class_exists('\OCA\Circles\ShareByCircleProvider')
  208. ) {
  209. $this->circlesAreNotAvailable = true;
  210. return null;
  211. }
  212. if ($this->shareByCircleProvider === null) {
  213. $this->shareByCircleProvider = new \OCA\Circles\ShareByCircleProvider(
  214. $this->serverContainer->getDatabaseConnection(),
  215. $this->serverContainer->getSecureRandom(),
  216. $this->serverContainer->getUserManager(),
  217. $this->serverContainer->getLazyRootFolder(),
  218. $this->serverContainer->getL10N('circles'),
  219. $this->serverContainer->getLogger(),
  220. $this->serverContainer->getURLGenerator()
  221. );
  222. }
  223. return $this->shareByCircleProvider;
  224. }
  225. /**
  226. * Create the room share provider
  227. *
  228. * @return RoomShareProvider
  229. */
  230. protected function getRoomShareProvider() {
  231. if ($this->roomShareProvider === null) {
  232. /*
  233. * Check if the app is enabled
  234. */
  235. $appManager = $this->serverContainer->getAppManager();
  236. if (!$appManager->isEnabledForUser('spreed')) {
  237. return null;
  238. }
  239. try {
  240. /**
  241. * @psalm-suppress UndefinedClass
  242. */
  243. $this->roomShareProvider = $this->serverContainer->get(RoomShareProvider::class);
  244. } catch (\Throwable $e) {
  245. $this->serverContainer->get(LoggerInterface::class)->error(
  246. $e->getMessage(),
  247. ['exception' => $e]
  248. );
  249. return null;
  250. }
  251. }
  252. return $this->roomShareProvider;
  253. }
  254. /**
  255. * @inheritdoc
  256. */
  257. public function getProvider($id) {
  258. $provider = null;
  259. if (isset($this->shareProviders[$id])) {
  260. return $this->shareProviders[$id];
  261. }
  262. if ($id === 'ocinternal') {
  263. $provider = $this->defaultShareProvider();
  264. } elseif ($id === 'ocFederatedSharing') {
  265. $provider = $this->federatedShareProvider();
  266. } elseif ($id === 'ocMailShare') {
  267. $provider = $this->getShareByMailProvider();
  268. } elseif ($id === 'ocCircleShare') {
  269. $provider = $this->getShareByCircleProvider();
  270. } elseif ($id === 'ocRoomShare') {
  271. $provider = $this->getRoomShareProvider();
  272. }
  273. foreach ($this->registeredShareProviders as $shareProvider) {
  274. try {
  275. /** @var IShareProvider $instance */
  276. $instance = $this->serverContainer->get($shareProvider);
  277. $this->shareProviders[$instance->identifier()] = $instance;
  278. } catch (\Throwable $e) {
  279. $this->serverContainer->get(LoggerInterface::class)->error(
  280. $e->getMessage(),
  281. ['exception' => $e]
  282. );
  283. }
  284. }
  285. if (isset($this->shareProviders[$id])) {
  286. $provider = $this->shareProviders[$id];
  287. }
  288. if ($provider === null) {
  289. throw new ProviderException('No provider with id .' . $id . ' found.');
  290. }
  291. return $provider;
  292. }
  293. /**
  294. * @inheritdoc
  295. */
  296. public function getProviderForType($shareType) {
  297. $provider = null;
  298. if ($shareType === IShare::TYPE_USER ||
  299. $shareType === IShare::TYPE_GROUP ||
  300. $shareType === IShare::TYPE_LINK
  301. ) {
  302. $provider = $this->defaultShareProvider();
  303. } elseif ($shareType === IShare::TYPE_REMOTE || $shareType === IShare::TYPE_REMOTE_GROUP) {
  304. $provider = $this->federatedShareProvider();
  305. } elseif ($shareType === IShare::TYPE_EMAIL) {
  306. $provider = $this->getShareByMailProvider();
  307. } elseif ($shareType === IShare::TYPE_CIRCLE) {
  308. $provider = $this->getShareByCircleProvider();
  309. } elseif ($shareType === IShare::TYPE_ROOM) {
  310. $provider = $this->getRoomShareProvider();
  311. } elseif ($shareType === IShare::TYPE_DECK) {
  312. $provider = $this->getProvider('deck');
  313. } elseif ($shareType === IShare::TYPE_SCIENCEMESH) {
  314. $provider = $this->getProvider('sciencemesh');
  315. }
  316. if ($provider === null) {
  317. throw new ProviderException('No share provider for share type ' . $shareType);
  318. }
  319. return $provider;
  320. }
  321. public function getAllProviders() {
  322. $shares = [$this->defaultShareProvider(), $this->federatedShareProvider()];
  323. $shareByMail = $this->getShareByMailProvider();
  324. if ($shareByMail !== null) {
  325. $shares[] = $shareByMail;
  326. }
  327. $shareByCircle = $this->getShareByCircleProvider();
  328. if ($shareByCircle !== null) {
  329. $shares[] = $shareByCircle;
  330. }
  331. $roomShare = $this->getRoomShareProvider();
  332. if ($roomShare !== null) {
  333. $shares[] = $roomShare;
  334. }
  335. foreach ($this->registeredShareProviders as $shareProvider) {
  336. try {
  337. /** @var IShareProvider $instance */
  338. $instance = $this->serverContainer->get($shareProvider);
  339. } catch (\Throwable $e) {
  340. $this->serverContainer->get(LoggerInterface::class)->error(
  341. $e->getMessage(),
  342. ['exception' => $e]
  343. );
  344. continue;
  345. }
  346. if (!isset($this->shareProviders[$instance->identifier()])) {
  347. $this->shareProviders[$instance->identifier()] = $instance;
  348. }
  349. $shares[] = $this->shareProviders[$instance->identifier()];
  350. }
  351. return $shares;
  352. }
  353. }