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.

ProviderFactory.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. /** @var \OCA\Circles\ShareByCircleProvider;
  49. * ShareByCircleProvider */
  50. private $shareByCircleProvider;
  51. /**
  52. * IProviderFactory constructor.
  53. *
  54. * @param IServerContainer $serverContainer
  55. */
  56. public function __construct(IServerContainer $serverContainer) {
  57. $this->serverContainer = $serverContainer;
  58. }
  59. /**
  60. * Create the default share provider.
  61. *
  62. * @return DefaultShareProvider
  63. */
  64. protected function defaultShareProvider() {
  65. if ($this->defaultProvider === null) {
  66. $this->defaultProvider = new DefaultShareProvider(
  67. $this->serverContainer->getDatabaseConnection(),
  68. $this->serverContainer->getUserManager(),
  69. $this->serverContainer->getGroupManager(),
  70. $this->serverContainer->getLazyRootFolder()
  71. );
  72. }
  73. return $this->defaultProvider;
  74. }
  75. /**
  76. * Create the federated share provider
  77. *
  78. * @return FederatedShareProvider
  79. */
  80. protected function federatedShareProvider() {
  81. if ($this->federatedProvider === null) {
  82. /*
  83. * Check if the app is enabled
  84. */
  85. $appManager = $this->serverContainer->getAppManager();
  86. if (!$appManager->isEnabledForUser('federatedfilesharing')) {
  87. return null;
  88. }
  89. /*
  90. * TODO: add factory to federated sharing app
  91. */
  92. $l = $this->serverContainer->getL10N('federatedfilessharing');
  93. $addressHandler = new AddressHandler(
  94. $this->serverContainer->getURLGenerator(),
  95. $l,
  96. $this->serverContainer->getCloudIdManager()
  97. );
  98. $discoveryManager = new DiscoveryManager(
  99. $this->serverContainer->getMemCacheFactory(),
  100. $this->serverContainer->getHTTPClientService()
  101. );
  102. $notifications = new Notifications(
  103. $addressHandler,
  104. $this->serverContainer->getHTTPClientService(),
  105. $discoveryManager,
  106. $this->serverContainer->getJobList()
  107. );
  108. $tokenHandler = new TokenHandler(
  109. $this->serverContainer->getSecureRandom()
  110. );
  111. $this->federatedProvider = new FederatedShareProvider(
  112. $this->serverContainer->getDatabaseConnection(),
  113. $addressHandler,
  114. $notifications,
  115. $tokenHandler,
  116. $l,
  117. $this->serverContainer->getLogger(),
  118. $this->serverContainer->getLazyRootFolder(),
  119. $this->serverContainer->getConfig(),
  120. $this->serverContainer->getUserManager(),
  121. $this->serverContainer->getCloudIdManager()
  122. );
  123. }
  124. return $this->federatedProvider;
  125. }
  126. /**
  127. * Create the federated share provider
  128. *
  129. * @return ShareByMailProvider
  130. */
  131. protected function getShareByMailProvider() {
  132. if ($this->shareByMailProvider === null) {
  133. /*
  134. * Check if the app is enabled
  135. */
  136. $appManager = $this->serverContainer->getAppManager();
  137. if (!$appManager->isEnabledForUser('sharebymail')) {
  138. return null;
  139. }
  140. $l = $this->serverContainer->getL10N('sharebymail');
  141. $this->shareByMailProvider = new ShareByMailProvider(
  142. $this->serverContainer->getDatabaseConnection(),
  143. $this->serverContainer->getSecureRandom(),
  144. $this->serverContainer->getUserManager(),
  145. $this->serverContainer->getLazyRootFolder(),
  146. $l,
  147. $this->serverContainer->getLogger(),
  148. $this->serverContainer->getMailer(),
  149. $this->serverContainer->getURLGenerator(),
  150. $this->serverContainer->getActivityManager()
  151. );
  152. }
  153. return $this->shareByMailProvider;
  154. }
  155. /**
  156. * Create the circle share provider
  157. *
  158. * @return FederatedShareProvider
  159. */
  160. protected function getShareByCircleProvider() {
  161. $appManager = $this->serverContainer->getAppManager();
  162. if (!$appManager->isEnabledForUser('circles')) {
  163. return null;
  164. }
  165. if ($this->shareByCircleProvider === null) {
  166. $this->shareByCircleProvider = new \OCA\Circles\ShareByCircleProvider(
  167. $this->serverContainer->getDatabaseConnection(),
  168. $this->serverContainer->getSecureRandom(),
  169. $this->serverContainer->getUserManager(),
  170. $this->serverContainer->getLazyRootFolder(),
  171. $this->serverContainer->getL10N('circles'),
  172. $this->serverContainer->getLogger(),
  173. $this->serverContainer->getURLGenerator()
  174. );
  175. }
  176. return $this->shareByCircleProvider;
  177. }
  178. /**
  179. * @inheritdoc
  180. */
  181. public function getProvider($id) {
  182. $provider = null;
  183. if ($id === 'ocinternal') {
  184. $provider = $this->defaultShareProvider();
  185. } else if ($id === 'ocFederatedSharing') {
  186. $provider = $this->federatedShareProvider();
  187. } else if ($id === 'ocMailShare') {
  188. $provider = $this->getShareByMailProvider();
  189. } else if ($id === 'ocCircleShare') {
  190. $provider = $this->getShareByCircleProvider();
  191. }
  192. if ($provider === null) {
  193. throw new ProviderException('No provider with id .' . $id . ' found.');
  194. }
  195. return $provider;
  196. }
  197. /**
  198. * @inheritdoc
  199. */
  200. public function getProviderForType($shareType) {
  201. $provider = null;
  202. if ($shareType === \OCP\Share::SHARE_TYPE_USER ||
  203. $shareType === \OCP\Share::SHARE_TYPE_GROUP ||
  204. $shareType === \OCP\Share::SHARE_TYPE_LINK
  205. ) {
  206. $provider = $this->defaultShareProvider();
  207. } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
  208. $provider = $this->federatedShareProvider();
  209. } else if ($shareType === \OCP\Share::SHARE_TYPE_EMAIL) {
  210. $provider = $this->getShareByMailProvider();
  211. } else if ($shareType === \OCP\Share::SHARE_TYPE_CIRCLE) {
  212. $provider = $this->getShareByCircleProvider();
  213. }
  214. if ($provider === null) {
  215. throw new ProviderException('No share provider for share type ' . $shareType);
  216. }
  217. return $provider;
  218. }
  219. public function getAllProviders() {
  220. $shares = [$this->defaultShareProvider(), $this->federatedShareProvider()];
  221. $shareByMail = $this->getShareByMailProvider();
  222. if ($shareByMail !== null) {
  223. $shares[] = $shareByMail;
  224. }
  225. $shareByCircle = $this->getShareByCircleProvider();
  226. if ($shareByCircle !== null) {
  227. $shares[] = $shareByCircle;
  228. }
  229. return $shares;
  230. }
  231. }