Application.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /**
  50. * register setting scripts
  51. */
  52. public function registerSettings() {
  53. App::registerAdmin('federation', 'settings/settings-admin');
  54. }
  55. private function registerService() {
  56. $container = $this->getContainer();
  57. $container->registerService('addServerMiddleware', function(IAppContainer $c) {
  58. return new AddServerMiddleware(
  59. $c->getAppName(),
  60. \OC::$server->getL10N($c->getAppName()),
  61. \OC::$server->getLogger()
  62. );
  63. });
  64. $container->registerService('DbHandler', function(IAppContainer $c) {
  65. return new DbHandler(
  66. \OC::$server->getDatabaseConnection(),
  67. \OC::$server->getL10N($c->getAppName())
  68. );
  69. });
  70. $container->registerService('TrustedServers', function(IAppContainer $c) {
  71. $server = $c->getServer();
  72. return new TrustedServers(
  73. $c->query('DbHandler'),
  74. $server->getHTTPClientService(),
  75. $server->getLogger(),
  76. $server->getJobList(),
  77. $server->getSecureRandom(),
  78. $server->getConfig(),
  79. $server->getEventDispatcher()
  80. );
  81. });
  82. $container->registerService('SettingsController', function (IAppContainer $c) {
  83. $server = $c->getServer();
  84. return new SettingsController(
  85. $c->getAppName(),
  86. $server->getRequest(),
  87. $server->getL10N($c->getAppName()),
  88. $c->query('TrustedServers')
  89. );
  90. });
  91. }
  92. private function registerMiddleware() {
  93. $container = $this->getContainer();
  94. $container->registerMiddleware('addServerMiddleware');
  95. }
  96. /**
  97. * register OCS API Calls
  98. */
  99. public function registerOCSApi() {
  100. $container = $this->getContainer();
  101. $server = $container->getServer();
  102. $auth = new OCSAuthAPI(
  103. $server->getRequest(),
  104. $server->getSecureRandom(),
  105. $server->getJobList(),
  106. $container->query('TrustedServers'),
  107. $container->query('DbHandler'),
  108. $server->getLogger()
  109. );
  110. API::register('get',
  111. '/apps/federation/api/v1/shared-secret',
  112. array($auth, 'getSharedSecret'),
  113. 'federation',
  114. API::GUEST_AUTH
  115. );
  116. API::register('post',
  117. '/apps/federation/api/v1/request-shared-secret',
  118. array($auth, 'requestSharedSecret'),
  119. 'federation',
  120. API::GUEST_AUTH
  121. );
  122. }
  123. /**
  124. * listen to federated_share_added hooks to auto-add new servers to the
  125. * list of trusted servers.
  126. */
  127. public function registerHooks() {
  128. $container = $this->getContainer();
  129. $hooksManager = new Hooks($container->query('TrustedServers'));
  130. Util::connectHook(
  131. 'OCP\Share',
  132. 'federated_share_added',
  133. $hooksManager,
  134. 'addServerHook'
  135. );
  136. $dispatcher = $this->getContainer()->getServer()->getEventDispatcher();
  137. $dispatcher->addListener('OCA\DAV\Connector\Sabre::authInit', function($event) use($container) {
  138. if ($event instanceof SabrePluginEvent) {
  139. $authPlugin = $event->getServer()->getPlugin('auth');
  140. if ($authPlugin instanceof Plugin) {
  141. $h = new DbHandler($container->getServer()->getDatabaseConnection(),
  142. $container->getServer()->getL10N('federation')
  143. );
  144. $authPlugin->addBackend(new FedAuth($h));
  145. }
  146. }
  147. });
  148. }
  149. /**
  150. * @return SyncFederationAddressBooks
  151. */
  152. public function getSyncService() {
  153. $syncService = \OC::$server->query('CardDAVSyncService');
  154. $dbHandler = $this->getContainer()->query('DbHandler');
  155. return new SyncFederationAddressBooks($dbHandler, $syncService);
  156. }
  157. }