1
0

ProviderFactory.php 11 KB

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