1
0

Application.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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 OCA\Federation\AppInfo;
  25. use OCA\Federation\API\OCSAuthAPI;
  26. use OCA\Federation\Controller\SettingsController;
  27. use OCA\Federation\DAV\FedAuth;
  28. use OCA\Federation\DbHandler;
  29. use OCA\Federation\Hooks;
  30. use OCA\Federation\Middleware\AddServerMiddleware;
  31. use OCA\Federation\SyncFederationAddressBooks;
  32. use OCA\Federation\SyncJob;
  33. use OCA\Federation\TrustedServers;
  34. use OCP\API;
  35. use OCP\App;
  36. use OCP\AppFramework\IAppContainer;
  37. use OCP\SabrePluginEvent;
  38. use OCP\Util;
  39. use Sabre\DAV\Auth\Plugin;
  40. class Application extends \OCP\AppFramework\App {
  41. /**
  42. * @param array $urlParams
  43. */
  44. public function __construct($urlParams = array()) {
  45. parent::__construct('federation', $urlParams);
  46. $this->registerService();
  47. $this->registerMiddleware();
  48. }
  49. private function registerService() {
  50. $container = $this->getContainer();
  51. $container->registerService('addServerMiddleware', function(IAppContainer $c) {
  52. return new AddServerMiddleware(
  53. $c->getAppName(),
  54. \OC::$server->getL10N($c->getAppName()),
  55. \OC::$server->getLogger()
  56. );
  57. });
  58. $container->registerService('DbHandler', function(IAppContainer $c) {
  59. return new DbHandler(
  60. \OC::$server->getDatabaseConnection(),
  61. \OC::$server->getL10N($c->getAppName())
  62. );
  63. });
  64. $container->registerService('TrustedServers', function(IAppContainer $c) {
  65. $server = $c->getServer();
  66. return new TrustedServers(
  67. $c->query('DbHandler'),
  68. $server->getHTTPClientService(),
  69. $server->getLogger(),
  70. $server->getJobList(),
  71. $server->getSecureRandom(),
  72. $server->getConfig(),
  73. $server->getEventDispatcher()
  74. );
  75. });
  76. $container->registerService('SettingsController', function (IAppContainer $c) {
  77. $server = $c->getServer();
  78. return new SettingsController(
  79. $c->getAppName(),
  80. $server->getRequest(),
  81. $server->getL10N($c->getAppName()),
  82. $c->query('TrustedServers')
  83. );
  84. });
  85. }
  86. private function registerMiddleware() {
  87. $container = $this->getContainer();
  88. $container->registerMiddleware('addServerMiddleware');
  89. }
  90. /**
  91. * register OCS API Calls
  92. */
  93. public function registerOCSApi() {
  94. $container = $this->getContainer();
  95. $server = $container->getServer();
  96. $auth = new OCSAuthAPI(
  97. $server->getRequest(),
  98. $server->getSecureRandom(),
  99. $server->getJobList(),
  100. $container->query('TrustedServers'),
  101. $container->query('DbHandler'),
  102. $server->getLogger()
  103. );
  104. API::register('get',
  105. '/apps/federation/api/v1/shared-secret',
  106. array($auth, 'getSharedSecret'),
  107. 'federation',
  108. API::GUEST_AUTH
  109. );
  110. API::register('post',
  111. '/apps/federation/api/v1/request-shared-secret',
  112. array($auth, 'requestSharedSecret'),
  113. 'federation',
  114. API::GUEST_AUTH
  115. );
  116. }
  117. /**
  118. * listen to federated_share_added hooks to auto-add new servers to the
  119. * list of trusted servers.
  120. */
  121. public function registerHooks() {
  122. $container = $this->getContainer();
  123. $hooksManager = new Hooks($container->query('TrustedServers'));
  124. Util::connectHook(
  125. 'OCP\Share',
  126. 'federated_share_added',
  127. $hooksManager,
  128. 'addServerHook'
  129. );
  130. $dispatcher = $this->getContainer()->getServer()->getEventDispatcher();
  131. $dispatcher->addListener('OCA\DAV\Connector\Sabre::authInit', function($event) use($container) {
  132. if ($event instanceof SabrePluginEvent) {
  133. $authPlugin = $event->getServer()->getPlugin('auth');
  134. if ($authPlugin instanceof Plugin) {
  135. $h = new DbHandler($container->getServer()->getDatabaseConnection(),
  136. $container->getServer()->getL10N('federation')
  137. );
  138. $authPlugin->addBackend(new FedAuth($h));
  139. }
  140. }
  141. });
  142. }
  143. /**
  144. * @return SyncFederationAddressBooks
  145. */
  146. public function getSyncService() {
  147. $syncService = \OC::$server->query('CardDAVSyncService');
  148. $dbHandler = $this->getContainer()->query('DbHandler');
  149. return new SyncFederationAddressBooks($dbHandler, $syncService);
  150. }
  151. }