ProviderFactory.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Share20;
  25. use OCA\FederatedFileSharing\AddressHandler;
  26. use OCA\FederatedFileSharing\DiscoveryManager;
  27. use OCA\FederatedFileSharing\FederatedShareProvider;
  28. use OCA\FederatedFileSharing\Notifications;
  29. use OCA\FederatedFileSharing\TokenHandler;
  30. use OCA\ShareByMail\ShareByMailProvider;
  31. use OCP\Share\IProviderFactory;
  32. use OC\Share20\Exception\ProviderException;
  33. use OCP\IServerContainer;
  34. /**
  35. * Class ProviderFactory
  36. *
  37. * @package OC\Share20
  38. */
  39. class ProviderFactory implements IProviderFactory {
  40. /** @var IServerContainer */
  41. private $serverContainer;
  42. /** @var DefaultShareProvider */
  43. private $defaultProvider = null;
  44. /** @var FederatedShareProvider */
  45. private $federatedProvider = null;
  46. /** @var ShareByMailProvider */
  47. private $shareByMailProvider;
  48. /**
  49. * IProviderFactory constructor.
  50. * @param IServerContainer $serverContainer
  51. */
  52. public function __construct(IServerContainer $serverContainer) {
  53. $this->serverContainer = $serverContainer;
  54. }
  55. /**
  56. * Create the default share provider.
  57. *
  58. * @return DefaultShareProvider
  59. */
  60. protected function defaultShareProvider() {
  61. if ($this->defaultProvider === null) {
  62. $this->defaultProvider = new DefaultShareProvider(
  63. $this->serverContainer->getDatabaseConnection(),
  64. $this->serverContainer->getUserManager(),
  65. $this->serverContainer->getGroupManager(),
  66. $this->serverContainer->getLazyRootFolder()
  67. );
  68. }
  69. return $this->defaultProvider;
  70. }
  71. /**
  72. * Create the federated share provider
  73. *
  74. * @return FederatedShareProvider
  75. */
  76. protected function federatedShareProvider() {
  77. if ($this->federatedProvider === null) {
  78. /*
  79. * Check if the app is enabled
  80. */
  81. $appManager = $this->serverContainer->getAppManager();
  82. if (!$appManager->isEnabledForUser('federatedfilesharing')) {
  83. return null;
  84. }
  85. /*
  86. * TODO: add factory to federated sharing app
  87. */
  88. $l = $this->serverContainer->getL10N('federatedfilessharing');
  89. $addressHandler = new AddressHandler(
  90. $this->serverContainer->getURLGenerator(),
  91. $l
  92. );
  93. $discoveryManager = new DiscoveryManager(
  94. $this->serverContainer->getMemCacheFactory(),
  95. $this->serverContainer->getHTTPClientService()
  96. );
  97. $notifications = new Notifications(
  98. $addressHandler,
  99. $this->serverContainer->getHTTPClientService(),
  100. $discoveryManager,
  101. $this->serverContainer->getJobList()
  102. );
  103. $tokenHandler = new TokenHandler(
  104. $this->serverContainer->getSecureRandom()
  105. );
  106. $this->federatedProvider = new FederatedShareProvider(
  107. $this->serverContainer->getDatabaseConnection(),
  108. $addressHandler,
  109. $notifications,
  110. $tokenHandler,
  111. $l,
  112. $this->serverContainer->getLogger(),
  113. $this->serverContainer->getLazyRootFolder(),
  114. $this->serverContainer->getConfig(),
  115. $this->serverContainer->getUserManager()
  116. );
  117. }
  118. return $this->federatedProvider;
  119. }
  120. /**
  121. * Create the federated share provider
  122. *
  123. * @return FederatedShareProvider
  124. */
  125. protected function getShareByMailProvider() {
  126. if ($this->shareByMailProvider === null) {
  127. /*
  128. * Check if the app is enabled
  129. */
  130. $appManager = $this->serverContainer->getAppManager();
  131. if (!$appManager->isEnabledForUser('sharebymail')) {
  132. return null;
  133. }
  134. $l = $this->serverContainer->getL10N('sharebymail');
  135. $this->shareByMailProvider = new ShareByMailProvider(
  136. $this->serverContainer->getDatabaseConnection(),
  137. $this->serverContainer->getSecureRandom(),
  138. $this->serverContainer->getUserManager(),
  139. $this->serverContainer->getLazyRootFolder(),
  140. $l,
  141. $this->serverContainer->getLogger(),
  142. $this->serverContainer->getMailer(),
  143. $this->serverContainer->getURLGenerator(),
  144. $this->serverContainer->getActivityManager()
  145. );
  146. }
  147. return $this->shareByMailProvider;
  148. }
  149. /**
  150. * @inheritdoc
  151. */
  152. public function getProvider($id) {
  153. $provider = null;
  154. if ($id === 'ocinternal') {
  155. $provider = $this->defaultShareProvider();
  156. } else if ($id === 'ocFederatedSharing') {
  157. $provider = $this->federatedShareProvider();
  158. } else if ($id = 'ocMailShare') {
  159. $provider = $this->getShareByMailProvider();
  160. }
  161. if ($provider === null) {
  162. throw new ProviderException('No provider with id .' . $id . ' found.');
  163. }
  164. return $provider;
  165. }
  166. /**
  167. * @inheritdoc
  168. */
  169. public function getProviderForType($shareType) {
  170. $provider = null;
  171. if ($shareType === \OCP\Share::SHARE_TYPE_USER ||
  172. $shareType === \OCP\Share::SHARE_TYPE_GROUP ||
  173. $shareType === \OCP\Share::SHARE_TYPE_LINK) {
  174. $provider = $this->defaultShareProvider();
  175. } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
  176. $provider = $this->federatedShareProvider();
  177. } else if ($shareType === \OCP\Share::SHARE_TYPE_EMAIL) {
  178. $provider = $this->getShareByMailProvider();
  179. }
  180. if ($provider === null) {
  181. throw new ProviderException('No share provider for share type ' . $shareType);
  182. }
  183. return $provider;
  184. }
  185. public function getAllProviders() {
  186. $shareByMail = $this->getShareByMailProvider();
  187. if ($shareByMail !== null) {
  188. return [$this->defaultShareProvider(), $this->federatedShareProvider(), $shareByMail];
  189. }
  190. return [$this->defaultShareProvider(), $this->federatedShareProvider()];
  191. }
  192. }