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.

325 lines
9.4 KiB

3 years ago
  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 Daniel Kesselberg <mail@danielkesselberg.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Maxence Lange <maxence@nextcloud.com>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\Share20;
  33. use OC\Share20\Exception\ProviderException;
  34. use OCA\FederatedFileSharing\AddressHandler;
  35. use OCA\FederatedFileSharing\FederatedShareProvider;
  36. use OCA\FederatedFileSharing\Notifications;
  37. use OCA\FederatedFileSharing\TokenHandler;
  38. use OCA\ShareByMail\Settings\SettingsManager;
  39. use OCA\ShareByMail\ShareByMailProvider;
  40. use OCP\Defaults;
  41. use OCP\EventDispatcher\IEventDispatcher;
  42. use OCP\IServerContainer;
  43. use OCP\Share\IProviderFactory;
  44. use OCP\Share\IShare;
  45. /**
  46. * Class ProviderFactory
  47. *
  48. * @package OC\Share20
  49. */
  50. class ProviderFactory implements IProviderFactory {
  51. /** @var IServerContainer */
  52. private $serverContainer;
  53. /** @var DefaultShareProvider */
  54. private $defaultProvider = null;
  55. /** @var FederatedShareProvider */
  56. private $federatedProvider = null;
  57. /** @var ShareByMailProvider */
  58. private $shareByMailProvider;
  59. /** @var \OCA\Circles\ShareByCircleProvider */
  60. private $shareByCircleProvider = null;
  61. /** @var bool */
  62. private $circlesAreNotAvailable = false;
  63. /** @var \OCA\Talk\Share\RoomShareProvider */
  64. private $roomShareProvider = null;
  65. /**
  66. * IProviderFactory constructor.
  67. *
  68. * @param IServerContainer $serverContainer
  69. */
  70. public function __construct(IServerContainer $serverContainer) {
  71. $this->serverContainer = $serverContainer;
  72. }
  73. /**
  74. * Create the default share provider.
  75. *
  76. * @return DefaultShareProvider
  77. */
  78. protected function defaultShareProvider() {
  79. if ($this->defaultProvider === null) {
  80. $this->defaultProvider = new DefaultShareProvider(
  81. $this->serverContainer->getDatabaseConnection(),
  82. $this->serverContainer->getUserManager(),
  83. $this->serverContainer->getGroupManager(),
  84. $this->serverContainer->getLazyRootFolder(),
  85. $this->serverContainer->getMailer(),
  86. $this->serverContainer->query(Defaults::class),
  87. $this->serverContainer->getL10NFactory(),
  88. $this->serverContainer->getURLGenerator(),
  89. $this->serverContainer->getConfig()
  90. );
  91. }
  92. return $this->defaultProvider;
  93. }
  94. /**
  95. * Create the federated share provider
  96. *
  97. * @return FederatedShareProvider
  98. */
  99. protected function federatedShareProvider() {
  100. if ($this->federatedProvider === null) {
  101. /*
  102. * Check if the app is enabled
  103. */
  104. $appManager = $this->serverContainer->getAppManager();
  105. if (!$appManager->isEnabledForUser('federatedfilesharing')) {
  106. return null;
  107. }
  108. /*
  109. * TODO: add factory to federated sharing app
  110. */
  111. $l = $this->serverContainer->getL10N('federatedfilesharing');
  112. $addressHandler = new AddressHandler(
  113. $this->serverContainer->getURLGenerator(),
  114. $l,
  115. $this->serverContainer->getCloudIdManager()
  116. );
  117. $notifications = new Notifications(
  118. $addressHandler,
  119. $this->serverContainer->getHTTPClientService(),
  120. $this->serverContainer->query(\OCP\OCS\IDiscoveryService::class),
  121. $this->serverContainer->getJobList(),
  122. \OC::$server->getCloudFederationProviderManager(),
  123. \OC::$server->getCloudFederationFactory(),
  124. $this->serverContainer->query(IEventDispatcher::class)
  125. );
  126. $tokenHandler = new TokenHandler(
  127. $this->serverContainer->getSecureRandom()
  128. );
  129. $this->federatedProvider = new FederatedShareProvider(
  130. $this->serverContainer->getDatabaseConnection(),
  131. $addressHandler,
  132. $notifications,
  133. $tokenHandler,
  134. $l,
  135. $this->serverContainer->getLogger(),
  136. $this->serverContainer->getLazyRootFolder(),
  137. $this->serverContainer->getConfig(),
  138. $this->serverContainer->getUserManager(),
  139. $this->serverContainer->getCloudIdManager(),
  140. $this->serverContainer->getGlobalScaleConfig(),
  141. $this->serverContainer->getCloudFederationProviderManager()
  142. );
  143. }
  144. return $this->federatedProvider;
  145. }
  146. /**
  147. * Create the federated share provider
  148. *
  149. * @return ShareByMailProvider
  150. */
  151. protected function getShareByMailProvider() {
  152. if ($this->shareByMailProvider === null) {
  153. /*
  154. * Check if the app is enabled
  155. */
  156. $appManager = $this->serverContainer->getAppManager();
  157. if (!$appManager->isEnabledForUser('sharebymail')) {
  158. return null;
  159. }
  160. $settingsManager = new SettingsManager($this->serverContainer->getConfig());
  161. $this->shareByMailProvider = new ShareByMailProvider(
  162. $this->serverContainer->getDatabaseConnection(),
  163. $this->serverContainer->getSecureRandom(),
  164. $this->serverContainer->getUserManager(),
  165. $this->serverContainer->getLazyRootFolder(),
  166. $this->serverContainer->getL10N('sharebymail'),
  167. $this->serverContainer->getLogger(),
  168. $this->serverContainer->getMailer(),
  169. $this->serverContainer->getURLGenerator(),
  170. $this->serverContainer->getActivityManager(),
  171. $settingsManager,
  172. $this->serverContainer->query(Defaults::class),
  173. $this->serverContainer->getHasher(),
  174. $this->serverContainer->get(IEventDispatcher::class)
  175. );
  176. }
  177. return $this->shareByMailProvider;
  178. }
  179. /**
  180. * Create the circle share provider
  181. *
  182. * @return FederatedShareProvider
  183. *
  184. * @suppress PhanUndeclaredClassMethod
  185. */
  186. protected function getShareByCircleProvider() {
  187. if ($this->circlesAreNotAvailable) {
  188. return null;
  189. }
  190. if (!$this->serverContainer->getAppManager()->isEnabledForUser('circles') ||
  191. !class_exists('\OCA\Circles\ShareByCircleProvider')
  192. ) {
  193. $this->circlesAreNotAvailable = true;
  194. return null;
  195. }
  196. if ($this->shareByCircleProvider === null) {
  197. $this->shareByCircleProvider = new \OCA\Circles\ShareByCircleProvider(
  198. $this->serverContainer->getDatabaseConnection(),
  199. $this->serverContainer->getSecureRandom(),
  200. $this->serverContainer->getUserManager(),
  201. $this->serverContainer->getLazyRootFolder(),
  202. $this->serverContainer->getL10N('circles'),
  203. $this->serverContainer->getLogger(),
  204. $this->serverContainer->getURLGenerator()
  205. );
  206. }
  207. return $this->shareByCircleProvider;
  208. }
  209. /**
  210. * Create the room share provider
  211. *
  212. * @return RoomShareProvider
  213. */
  214. protected function getRoomShareProvider() {
  215. if ($this->roomShareProvider === null) {
  216. /*
  217. * Check if the app is enabled
  218. */
  219. $appManager = $this->serverContainer->getAppManager();
  220. if (!$appManager->isEnabledForUser('spreed')) {
  221. return null;
  222. }
  223. try {
  224. $this->roomShareProvider = $this->serverContainer->query('\OCA\Talk\Share\RoomShareProvider');
  225. } catch (\OCP\AppFramework\QueryException $e) {
  226. return null;
  227. }
  228. }
  229. return $this->roomShareProvider;
  230. }
  231. /**
  232. * @inheritdoc
  233. */
  234. public function getProvider($id) {
  235. $provider = null;
  236. if ($id === 'ocinternal') {
  237. $provider = $this->defaultShareProvider();
  238. } elseif ($id === 'ocFederatedSharing') {
  239. $provider = $this->federatedShareProvider();
  240. } elseif ($id === 'ocMailShare') {
  241. $provider = $this->getShareByMailProvider();
  242. } elseif ($id === 'ocCircleShare') {
  243. $provider = $this->getShareByCircleProvider();
  244. } elseif ($id === 'ocRoomShare') {
  245. $provider = $this->getRoomShareProvider();
  246. }
  247. if ($provider === null) {
  248. throw new ProviderException('No provider with id .' . $id . ' found.');
  249. }
  250. return $provider;
  251. }
  252. /**
  253. * @inheritdoc
  254. */
  255. public function getProviderForType($shareType) {
  256. $provider = null;
  257. if ($shareType === IShare::TYPE_USER ||
  258. $shareType === IShare::TYPE_GROUP ||
  259. $shareType === IShare::TYPE_LINK
  260. ) {
  261. $provider = $this->defaultShareProvider();
  262. } elseif ($shareType === IShare::TYPE_REMOTE || $shareType === IShare::TYPE_REMOTE_GROUP) {
  263. $provider = $this->federatedShareProvider();
  264. } elseif ($shareType === IShare::TYPE_EMAIL) {
  265. $provider = $this->getShareByMailProvider();
  266. } elseif ($shareType === IShare::TYPE_CIRCLE) {
  267. $provider = $this->getShareByCircleProvider();
  268. } elseif ($shareType === IShare::TYPE_ROOM) {
  269. $provider = $this->getRoomShareProvider();
  270. }
  271. if ($provider === null) {
  272. throw new ProviderException('No share provider for share type ' . $shareType);
  273. }
  274. return $provider;
  275. }
  276. public function getAllProviders() {
  277. $shares = [$this->defaultShareProvider(), $this->federatedShareProvider()];
  278. $shareByMail = $this->getShareByMailProvider();
  279. if ($shareByMail !== null) {
  280. $shares[] = $shareByMail;
  281. }
  282. $shareByCircle = $this->getShareByCircleProvider();
  283. if ($shareByCircle !== null) {
  284. $shares[] = $shareByCircle;
  285. }
  286. $roomShare = $this->getRoomShareProvider();
  287. if ($roomShare !== null) {
  288. $shares[] = $roomShare;
  289. }
  290. return $shares;
  291. }
  292. }