Application.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@owncloud.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Settings;
  29. use OC\AppFramework\Utility\TimeFactory;
  30. use OC\Authentication\Token\IProvider;
  31. use OC\Server;
  32. use OC\Settings\Activity\GroupProvider;
  33. use OC\Settings\Activity\GroupSetting;
  34. use OC\Settings\Activity\Provider;
  35. use OC\Settings\Activity\SecurityFilter;
  36. use OC\Settings\Activity\SecurityProvider;
  37. use OC\Settings\Activity\SecuritySetting;
  38. use OC\Settings\Activity\Setting;
  39. use OC\Settings\Mailer\NewUserMailHelper;
  40. use OC\Settings\Middleware\SubadminMiddleware;
  41. use OCP\AppFramework\App;
  42. use OCP\Defaults;
  43. use OCP\IContainer;
  44. use OCP\IGroup;
  45. use OCP\IUser;
  46. use OCP\Settings\IManager;
  47. use OCP\Util;
  48. /**
  49. * @package OC\Settings
  50. */
  51. class Application extends App {
  52. /**
  53. * @param array $urlParams
  54. */
  55. public function __construct(array $urlParams=[]){
  56. parent::__construct('settings', $urlParams);
  57. $container = $this->getContainer();
  58. // Register Middleware
  59. $container->registerAlias('SubadminMiddleware', SubadminMiddleware::class);
  60. $container->registerMiddleWare('SubadminMiddleware');
  61. /**
  62. * Core class wrappers
  63. */
  64. /** FIXME: Remove once OC_User is non-static and mockable */
  65. $container->registerService('isAdmin', function() {
  66. return \OC_User::isAdminUser(\OC_User::getUser());
  67. });
  68. /** FIXME: Remove once OC_SubAdmin is non-static and mockable */
  69. $container->registerService('isSubAdmin', function(IContainer $c) {
  70. $userObject = \OC::$server->getUserSession()->getUser();
  71. $isSubAdmin = false;
  72. if($userObject !== null) {
  73. $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
  74. }
  75. return $isSubAdmin;
  76. });
  77. $container->registerService('userCertificateManager', function(IContainer $c) {
  78. return $c->query('ServerContainer')->getCertificateManager();
  79. }, false);
  80. $container->registerService('systemCertificateManager', function (IContainer $c) {
  81. return $c->query('ServerContainer')->getCertificateManager(null);
  82. }, false);
  83. $container->registerService(IProvider::class, function (IContainer $c) {
  84. return $c->query('ServerContainer')->query(IProvider::class);
  85. });
  86. $container->registerService(IManager::class, function (IContainer $c) {
  87. return $c->query('ServerContainer')->getSettingsManager();
  88. });
  89. $container->registerService(NewUserMailHelper::class, function (IContainer $c) {
  90. /** @var Server $server */
  91. $server = $c->query('ServerContainer');
  92. /** @var Defaults $defaults */
  93. $defaults = $server->query(Defaults::class);
  94. return new NewUserMailHelper(
  95. $defaults,
  96. $server->getURLGenerator(),
  97. $server->getL10NFactory(),
  98. $server->getMailer(),
  99. $server->getSecureRandom(),
  100. new TimeFactory(),
  101. $server->getConfig(),
  102. $server->getCrypto(),
  103. Util::getDefaultEmailAddress('no-reply')
  104. );
  105. });
  106. }
  107. public function register() {
  108. $activityManager = $this->getContainer()->getServer()->getActivityManager();
  109. $activityManager->registerSetting(Setting::class); // FIXME move to info.xml
  110. $activityManager->registerProvider(Provider::class); // FIXME move to info.xml
  111. $activityManager->registerFilter(SecurityFilter::class); // FIXME move to info.xml
  112. $activityManager->registerSetting(SecuritySetting::class); // FIXME move to info.xml
  113. $activityManager->registerProvider(SecurityProvider::class); // FIXME move to info.xml
  114. $activityManager->registerSetting(GroupSetting::class); // FIXME move to info.xml
  115. $activityManager->registerProvider(GroupProvider::class); // FIXME move to info.xml
  116. Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
  117. Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
  118. $groupManager = $this->getContainer()->getServer()->getGroupManager();
  119. $groupManager->listen('\OC\Group', 'postRemoveUser', [$this, 'removeUserFromGroup']);
  120. $groupManager->listen('\OC\Group', 'postAddUser', [$this, 'addUserToGroup']);
  121. Util::connectHook('\OCP\Config', 'js', $this, 'extendJsConfig');
  122. }
  123. public function addUserToGroup(IGroup $group, IUser $user) {
  124. /** @var Hooks $hooks */
  125. $hooks = $this->getContainer()->query(Hooks::class);
  126. $hooks->addUserToGroup($group, $user);
  127. }
  128. public function removeUserFromGroup(IGroup $group, IUser $user) {
  129. /** @var Hooks $hooks */
  130. $hooks = $this->getContainer()->query(Hooks::class);
  131. $hooks->removeUserFromGroup($group, $user);
  132. }
  133. /**
  134. * @param array $parameters
  135. * @throws \InvalidArgumentException
  136. * @throws \BadMethodCallException
  137. * @throws \Exception
  138. * @throws \OCP\AppFramework\QueryException
  139. */
  140. public function onChangePassword(array $parameters) {
  141. /** @var Hooks $hooks */
  142. $hooks = $this->getContainer()->query(Hooks::class);
  143. $hooks->onChangePassword($parameters['uid']);
  144. }
  145. /**
  146. * @param array $parameters
  147. * @throws \InvalidArgumentException
  148. * @throws \BadMethodCallException
  149. * @throws \Exception
  150. * @throws \OCP\AppFramework\QueryException
  151. */
  152. public function onChangeInfo(array $parameters) {
  153. if ($parameters['feature'] !== 'eMailAddress') {
  154. return;
  155. }
  156. /** @var Hooks $hooks */
  157. $hooks = $this->getContainer()->query(Hooks::class);
  158. $hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
  159. }
  160. /**
  161. * @param array $settings
  162. */
  163. public function extendJsConfig(array $settings) {
  164. $appConfig = json_decode($settings['array']['oc_appconfig'], true);
  165. $publicWebFinger = \OC::$server->getConfig()->getAppValue('core', 'public_webfinger', '');
  166. if (!empty($publicWebFinger)) {
  167. $appConfig['core']['public_webfinger'] = $publicWebFinger;
  168. }
  169. $settings['array']['oc_appconfig'] = json_encode($appConfig);
  170. }
  171. }