ProviderFactory.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 OCP\Share\IProviderFactory;
  31. use OC\Share20\Exception\ProviderException;
  32. use OCP\IServerContainer;
  33. /**
  34. * Class ProviderFactory
  35. *
  36. * @package OC\Share20
  37. */
  38. class ProviderFactory implements IProviderFactory {
  39. /** @var IServerContainer */
  40. private $serverContainer;
  41. /** @var DefaultShareProvider */
  42. private $defaultProvider = null;
  43. /** @var FederatedShareProvider */
  44. private $federatedProvider = null;
  45. /**
  46. * IProviderFactory constructor.
  47. * @param IServerContainer $serverContainer
  48. */
  49. public function __construct(IServerContainer $serverContainer) {
  50. $this->serverContainer = $serverContainer;
  51. }
  52. /**
  53. * Create the default share provider.
  54. *
  55. * @return DefaultShareProvider
  56. */
  57. protected function defaultShareProvider() {
  58. if ($this->defaultProvider === null) {
  59. $this->defaultProvider = new DefaultShareProvider(
  60. $this->serverContainer->getDatabaseConnection(),
  61. $this->serverContainer->getUserManager(),
  62. $this->serverContainer->getGroupManager(),
  63. $this->serverContainer->getLazyRootFolder()
  64. );
  65. }
  66. return $this->defaultProvider;
  67. }
  68. /**
  69. * Create the federated share provider
  70. *
  71. * @return FederatedShareProvider
  72. */
  73. protected function federatedShareProvider() {
  74. if ($this->federatedProvider === null) {
  75. /*
  76. * Check if the app is enabled
  77. */
  78. $appManager = $this->serverContainer->getAppManager();
  79. if (!$appManager->isEnabledForUser('federatedfilesharing')) {
  80. return null;
  81. }
  82. /*
  83. * TODO: add factory to federated sharing app
  84. */
  85. $l = $this->serverContainer->getL10N('federatedfilessharing');
  86. $addressHandler = new AddressHandler(
  87. $this->serverContainer->getURLGenerator(),
  88. $l
  89. );
  90. $discoveryManager = new DiscoveryManager(
  91. $this->serverContainer->getMemCacheFactory(),
  92. $this->serverContainer->getHTTPClientService()
  93. );
  94. $notifications = new Notifications(
  95. $addressHandler,
  96. $this->serverContainer->getHTTPClientService(),
  97. $discoveryManager,
  98. $this->serverContainer->getJobList()
  99. );
  100. $tokenHandler = new TokenHandler(
  101. $this->serverContainer->getSecureRandom()
  102. );
  103. $this->federatedProvider = new FederatedShareProvider(
  104. $this->serverContainer->getDatabaseConnection(),
  105. $addressHandler,
  106. $notifications,
  107. $tokenHandler,
  108. $l,
  109. $this->serverContainer->getLogger(),
  110. $this->serverContainer->getLazyRootFolder(),
  111. $this->serverContainer->getConfig(),
  112. $this->serverContainer->getUserManager()
  113. );
  114. }
  115. return $this->federatedProvider;
  116. }
  117. /**
  118. * @inheritdoc
  119. */
  120. public function getProvider($id) {
  121. $provider = null;
  122. if ($id === 'ocinternal') {
  123. $provider = $this->defaultShareProvider();
  124. } else if ($id === 'ocFederatedSharing') {
  125. $provider = $this->federatedShareProvider();
  126. }
  127. if ($provider === null) {
  128. throw new ProviderException('No provider with id .' . $id . ' found.');
  129. }
  130. return $provider;
  131. }
  132. /**
  133. * @inheritdoc
  134. */
  135. public function getProviderForType($shareType) {
  136. $provider = null;
  137. if ($shareType === \OCP\Share::SHARE_TYPE_USER ||
  138. $shareType === \OCP\Share::SHARE_TYPE_GROUP ||
  139. $shareType === \OCP\Share::SHARE_TYPE_LINK) {
  140. $provider = $this->defaultShareProvider();
  141. } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
  142. $provider = $this->federatedShareProvider();
  143. }
  144. if ($provider === null) {
  145. throw new ProviderException('No share provider for share type ' . $shareType);
  146. }
  147. return $provider;
  148. }
  149. }