application.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Clark Tomlinson <fallen013@gmail.com>
  5. * @author Lukas Reschke <lukas@owncloud.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Encryption\AppInfo;
  26. use OC\Files\View;
  27. use OCA\Encryption\Controller\RecoveryController;
  28. use OCA\Encryption\Controller\SettingsController;
  29. use OCA\Encryption\Controller\StatusController;
  30. use OCA\Encryption\Crypto\Crypt;
  31. use OCA\Encryption\Crypto\Encryption;
  32. use OCA\Encryption\HookManager;
  33. use OCA\Encryption\Hooks\UserHooks;
  34. use OCA\Encryption\KeyManager;
  35. use OCA\Encryption\Recovery;
  36. use OCA\Encryption\Session;
  37. use OCA\Encryption\Users\Setup;
  38. use OCA\Encryption\Util;
  39. use OCP\App;
  40. use OCP\AppFramework\IAppContainer;
  41. use OCP\Encryption\IManager;
  42. use OCP\IConfig;
  43. class Application extends \OCP\AppFramework\App {
  44. /** @var IManager */
  45. private $encryptionManager;
  46. /** @var IConfig */
  47. private $config;
  48. /**
  49. * @param array $urlParams
  50. * @param bool $encryptionSystemReady
  51. */
  52. public function __construct($urlParams = array(), $encryptionSystemReady = true) {
  53. parent::__construct('encryption', $urlParams);
  54. $this->encryptionManager = \OC::$server->getEncryptionManager();
  55. $this->config = \OC::$server->getConfig();
  56. $this->registerServices();
  57. if($encryptionSystemReady === false) {
  58. /** @var Session $session */
  59. $session = $this->getContainer()->query('Session');
  60. $session->setStatus(Session::RUN_MIGRATION);
  61. }
  62. }
  63. /**
  64. * register hooks
  65. */
  66. public function registerHooks() {
  67. if (!$this->config->getSystemValue('maintenance', false)) {
  68. $container = $this->getContainer();
  69. $server = $container->getServer();
  70. // Register our hooks and fire them.
  71. $hookManager = new HookManager();
  72. $hookManager->registerHook([
  73. new UserHooks($container->query('KeyManager'),
  74. $server->getLogger(),
  75. $container->query('UserSetup'),
  76. $server->getUserSession(),
  77. $container->query('Util'),
  78. $container->query('Session'),
  79. $container->query('Crypt'),
  80. $container->query('Recovery'))
  81. ]);
  82. $hookManager->fireHooks();
  83. } else {
  84. // Logout user if we are in maintenance to force re-login
  85. $this->getContainer()->getServer()->getUserSession()->logout();
  86. }
  87. }
  88. public function registerEncryptionModule() {
  89. $container = $this->getContainer();
  90. $this->encryptionManager->registerEncryptionModule(
  91. Encryption::ID,
  92. Encryption::DISPLAY_NAME,
  93. function() use ($container) {
  94. return new Encryption(
  95. $container->query('Crypt'),
  96. $container->query('KeyManager'),
  97. $container->query('Util'),
  98. $container->getServer()->getLogger(),
  99. $container->getServer()->getL10N($container->getAppName())
  100. );
  101. });
  102. }
  103. public function registerServices() {
  104. $container = $this->getContainer();
  105. $container->registerService('Crypt',
  106. function (IAppContainer $c) {
  107. $server = $c->getServer();
  108. return new Crypt($server->getLogger(),
  109. $server->getUserSession(),
  110. $server->getConfig());
  111. });
  112. $container->registerService('Session',
  113. function (IAppContainer $c) {
  114. $server = $c->getServer();
  115. return new Session($server->getSession());
  116. }
  117. );
  118. $container->registerService('KeyManager',
  119. function (IAppContainer $c) {
  120. $server = $c->getServer();
  121. return new KeyManager($server->getEncryptionKeyStorage(),
  122. $c->query('Crypt'),
  123. $server->getConfig(),
  124. $server->getUserSession(),
  125. new Session($server->getSession()),
  126. $server->getLogger(),
  127. $c->query('Util')
  128. );
  129. });
  130. $container->registerService('Recovery',
  131. function (IAppContainer $c) {
  132. $server = $c->getServer();
  133. return new Recovery(
  134. $server->getUserSession(),
  135. $c->query('Crypt'),
  136. $server->getSecureRandom(),
  137. $c->query('KeyManager'),
  138. $server->getConfig(),
  139. $server->getEncryptionKeyStorage(),
  140. $server->getEncryptionFilesHelper(),
  141. new View());
  142. });
  143. $container->registerService('RecoveryController', function (IAppContainer $c) {
  144. $server = $c->getServer();
  145. return new RecoveryController(
  146. $c->getAppName(),
  147. $server->getRequest(),
  148. $server->getConfig(),
  149. $server->getL10N($c->getAppName()),
  150. $c->query('Recovery'));
  151. });
  152. $container->registerService('StatusController', function (IAppContainer $c) {
  153. $server = $c->getServer();
  154. return new StatusController(
  155. $c->getAppName(),
  156. $server->getRequest(),
  157. $server->getL10N($c->getAppName()),
  158. $c->query('Session')
  159. );
  160. });
  161. $container->registerService('SettingsController', function (IAppContainer $c) {
  162. $server = $c->getServer();
  163. return new SettingsController(
  164. $c->getAppName(),
  165. $server->getRequest(),
  166. $server->getL10N($c->getAppName()),
  167. $server->getUserManager(),
  168. $server->getUserSession(),
  169. $c->query('KeyManager'),
  170. $c->query('Crypt'),
  171. $c->query('Session')
  172. );
  173. });
  174. $container->registerService('UserSetup',
  175. function (IAppContainer $c) {
  176. $server = $c->getServer();
  177. return new Setup($server->getLogger(),
  178. $server->getUserSession(),
  179. $c->query('Crypt'),
  180. $c->query('KeyManager'));
  181. });
  182. $container->registerService('Util',
  183. function (IAppContainer $c) {
  184. $server = $c->getServer();
  185. return new Util(
  186. new View(),
  187. $c->query('Crypt'),
  188. $server->getLogger(),
  189. $server->getUserSession(),
  190. $server->getConfig(),
  191. $server->getUserManager());
  192. });
  193. }
  194. public function registerSettings() {
  195. // Register settings scripts
  196. App::registerAdmin('encryption', 'settings/settings-admin');
  197. App::registerPersonal('encryption', 'settings/settings-personal');
  198. }
  199. }