Application.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Clark Tomlinson <fallen013@gmail.com>
  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\Encryption\AppInfo;
  25. use OC\Files\View;
  26. use OCA\Encryption\Controller\RecoveryController;
  27. use OCA\Encryption\Controller\SettingsController;
  28. use OCA\Encryption\Controller\StatusController;
  29. use OCA\Encryption\Crypto\Crypt;
  30. use OCA\Encryption\Crypto\DecryptAll;
  31. use OCA\Encryption\Crypto\EncryptAll;
  32. use OCA\Encryption\Crypto\Encryption;
  33. use OCA\Encryption\HookManager;
  34. use OCA\Encryption\Hooks\UserHooks;
  35. use OCA\Encryption\KeyManager;
  36. use OCA\Encryption\Recovery;
  37. use OCA\Encryption\Session;
  38. use OCA\Encryption\Users\Setup;
  39. use OCA\Encryption\Util;
  40. use OCP\App;
  41. use OCP\AppFramework\IAppContainer;
  42. use OCP\Encryption\IManager;
  43. use OCP\IConfig;
  44. use Symfony\Component\Console\Helper\QuestionHelper;
  45. class Application extends \OCP\AppFramework\App {
  46. /** @var IManager */
  47. private $encryptionManager;
  48. /** @var IConfig */
  49. private $config;
  50. /**
  51. * @param array $urlParams
  52. * @param bool $encryptionSystemReady
  53. */
  54. public function __construct($urlParams = array(), $encryptionSystemReady = true) {
  55. parent::__construct('encryption', $urlParams);
  56. $this->encryptionManager = \OC::$server->getEncryptionManager();
  57. $this->config = \OC::$server->getConfig();
  58. $this->registerServices();
  59. if($encryptionSystemReady === false) {
  60. /** @var Session $session */
  61. $session = $this->getContainer()->query('Session');
  62. $session->setStatus(Session::RUN_MIGRATION);
  63. }
  64. if ($this->encryptionManager->isEnabled() && $encryptionSystemReady) {
  65. /** @var Setup $setup */
  66. $setup = $this->getContainer()->query('UserSetup');
  67. $setup->setupSystem();
  68. }
  69. }
  70. /**
  71. * register hooks
  72. */
  73. public function registerHooks() {
  74. if (!$this->config->getSystemValue('maintenance', false)) {
  75. $container = $this->getContainer();
  76. $server = $container->getServer();
  77. // Register our hooks and fire them.
  78. $hookManager = new HookManager();
  79. $hookManager->registerHook([
  80. new UserHooks($container->query('KeyManager'),
  81. $server->getUserManager(),
  82. $server->getLogger(),
  83. $container->query('UserSetup'),
  84. $server->getUserSession(),
  85. $container->query('Util'),
  86. $container->query('Session'),
  87. $container->query('Crypt'),
  88. $container->query('Recovery'))
  89. ]);
  90. $hookManager->fireHooks();
  91. } else {
  92. // Logout user if we are in maintenance to force re-login
  93. $this->getContainer()->getServer()->getUserSession()->logout();
  94. }
  95. }
  96. public function registerEncryptionModule() {
  97. $container = $this->getContainer();
  98. $this->encryptionManager->registerEncryptionModule(
  99. Encryption::ID,
  100. Encryption::DISPLAY_NAME,
  101. function() use ($container) {
  102. return new Encryption(
  103. $container->query('Crypt'),
  104. $container->query('KeyManager'),
  105. $container->query('Util'),
  106. $container->query('Session'),
  107. $container->query('EncryptAll'),
  108. $container->query('DecryptAll'),
  109. $container->getServer()->getLogger(),
  110. $container->getServer()->getL10N($container->getAppName())
  111. );
  112. });
  113. }
  114. public function registerServices() {
  115. $container = $this->getContainer();
  116. $container->registerService('Crypt',
  117. function (IAppContainer $c) {
  118. $server = $c->getServer();
  119. return new Crypt($server->getLogger(),
  120. $server->getUserSession(),
  121. $server->getConfig(),
  122. $server->getL10N($c->getAppName()));
  123. });
  124. $container->registerService('Session',
  125. function (IAppContainer $c) {
  126. $server = $c->getServer();
  127. return new Session($server->getSession());
  128. }
  129. );
  130. $container->registerService('KeyManager',
  131. function (IAppContainer $c) {
  132. $server = $c->getServer();
  133. return new KeyManager($server->getEncryptionKeyStorage(),
  134. $c->query('Crypt'),
  135. $server->getConfig(),
  136. $server->getUserSession(),
  137. new Session($server->getSession()),
  138. $server->getLogger(),
  139. $c->query('Util')
  140. );
  141. });
  142. $container->registerService('Recovery',
  143. function (IAppContainer $c) {
  144. $server = $c->getServer();
  145. return new Recovery(
  146. $server->getUserSession(),
  147. $c->query('Crypt'),
  148. $server->getSecureRandom(),
  149. $c->query('KeyManager'),
  150. $server->getConfig(),
  151. $server->getEncryptionKeyStorage(),
  152. $server->getEncryptionFilesHelper(),
  153. new View());
  154. });
  155. $container->registerService('RecoveryController', function (IAppContainer $c) {
  156. $server = $c->getServer();
  157. return new RecoveryController(
  158. $c->getAppName(),
  159. $server->getRequest(),
  160. $server->getConfig(),
  161. $server->getL10N($c->getAppName()),
  162. $c->query('Recovery'));
  163. });
  164. $container->registerService('StatusController', function (IAppContainer $c) {
  165. $server = $c->getServer();
  166. return new StatusController(
  167. $c->getAppName(),
  168. $server->getRequest(),
  169. $server->getL10N($c->getAppName()),
  170. $c->query('Session')
  171. );
  172. });
  173. $container->registerService('SettingsController', function (IAppContainer $c) {
  174. $server = $c->getServer();
  175. return new SettingsController(
  176. $c->getAppName(),
  177. $server->getRequest(),
  178. $server->getL10N($c->getAppName()),
  179. $server->getUserManager(),
  180. $server->getUserSession(),
  181. $c->query('KeyManager'),
  182. $c->query('Crypt'),
  183. $c->query('Session'),
  184. $server->getSession(),
  185. $c->query('Util')
  186. );
  187. });
  188. $container->registerService('UserSetup',
  189. function (IAppContainer $c) {
  190. $server = $c->getServer();
  191. return new Setup($server->getLogger(),
  192. $server->getUserSession(),
  193. $c->query('Crypt'),
  194. $c->query('KeyManager'));
  195. });
  196. $container->registerService('Util',
  197. function (IAppContainer $c) {
  198. $server = $c->getServer();
  199. return new Util(
  200. new View(),
  201. $c->query('Crypt'),
  202. $server->getLogger(),
  203. $server->getUserSession(),
  204. $server->getConfig(),
  205. $server->getUserManager());
  206. });
  207. $container->registerService('EncryptAll',
  208. function (IAppContainer $c) {
  209. $server = $c->getServer();
  210. return new EncryptAll(
  211. $c->query('UserSetup'),
  212. $c->getServer()->getUserManager(),
  213. new View(),
  214. $c->query('KeyManager'),
  215. $c->query('Util'),
  216. $server->getConfig(),
  217. $server->getMailer(),
  218. $server->getL10N('encryption'),
  219. new QuestionHelper(),
  220. $server->getSecureRandom()
  221. );
  222. }
  223. );
  224. $container->registerService('DecryptAll',
  225. function (IAppContainer $c) {
  226. return new DecryptAll(
  227. $c->query('Util'),
  228. $c->query('KeyManager'),
  229. $c->query('Crypt'),
  230. $c->query('Session'),
  231. new QuestionHelper()
  232. );
  233. }
  234. );
  235. }
  236. public function registerSettings() {
  237. // Register settings scripts
  238. App::registerPersonal('encryption', 'settings/settings-personal');
  239. }
  240. }