1
0

ProviderFactory.php 7.9 KB

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