ProviderFactory.php 9.2 KB

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