Application.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 BadMethodCallException;
  30. use OC\AppFramework\Utility\TimeFactory;
  31. use OC\Authentication\Token\IProvider;
  32. use OC\Authentication\Token\IToken;
  33. use OC\Server;
  34. use OC\Settings\Activity\GroupProvider;
  35. use OC\Settings\Activity\GroupSetting;
  36. use OC\Settings\Activity\Provider;
  37. use OC\Settings\Activity\SecurityFilter;
  38. use OC\Settings\Activity\SecurityProvider;
  39. use OC\Settings\Activity\SecuritySetting;
  40. use OC\Settings\Activity\Setting;
  41. use OC\Settings\Mailer\NewUserMailHelper;
  42. use OC\Settings\Middleware\SubadminMiddleware;
  43. use OCP\Activity\IManager as IActivityManager;
  44. use OCP\AppFramework\App;
  45. use OCP\Defaults;
  46. use OCP\IContainer;
  47. use OCP\IGroup;
  48. use OCP\ILogger;
  49. use OCP\IUser;
  50. use OCP\Settings\IManager;
  51. use OCP\Util;
  52. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  53. use Symfony\Component\EventDispatcher\GenericEvent;
  54. /**
  55. * @package OC\Settings
  56. */
  57. class Application extends App {
  58. /**
  59. * @param array $urlParams
  60. */
  61. public function __construct(array $urlParams=[]){
  62. parent::__construct('settings', $urlParams);
  63. $container = $this->getContainer();
  64. // Register Middleware
  65. $container->registerAlias('SubadminMiddleware', SubadminMiddleware::class);
  66. $container->registerMiddleWare('SubadminMiddleware');
  67. /**
  68. * Core class wrappers
  69. */
  70. /** FIXME: Remove once OC_User is non-static and mockable */
  71. $container->registerService('isAdmin', function() {
  72. return \OC_User::isAdminUser(\OC_User::getUser());
  73. });
  74. /** FIXME: Remove once OC_SubAdmin is non-static and mockable */
  75. $container->registerService('isSubAdmin', function(IContainer $c) {
  76. $userObject = \OC::$server->getUserSession()->getUser();
  77. $isSubAdmin = false;
  78. if($userObject !== null) {
  79. $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
  80. }
  81. return $isSubAdmin;
  82. });
  83. $container->registerService('userCertificateManager', function(IContainer $c) {
  84. return $c->query('ServerContainer')->getCertificateManager();
  85. }, false);
  86. $container->registerService('systemCertificateManager', function (IContainer $c) {
  87. return $c->query('ServerContainer')->getCertificateManager(null);
  88. }, false);
  89. $container->registerService(IProvider::class, function (IContainer $c) {
  90. return $c->query('ServerContainer')->query(IProvider::class);
  91. });
  92. $container->registerService(IManager::class, function (IContainer $c) {
  93. return $c->query('ServerContainer')->getSettingsManager();
  94. });
  95. $container->registerService(NewUserMailHelper::class, function (IContainer $c) {
  96. /** @var Server $server */
  97. $server = $c->query('ServerContainer');
  98. /** @var Defaults $defaults */
  99. $defaults = $server->query(Defaults::class);
  100. return new NewUserMailHelper(
  101. $defaults,
  102. $server->getURLGenerator(),
  103. $server->getL10NFactory(),
  104. $server->getMailer(),
  105. $server->getSecureRandom(),
  106. new TimeFactory(),
  107. $server->getConfig(),
  108. $server->getCrypto(),
  109. Util::getDefaultEmailAddress('no-reply')
  110. );
  111. });
  112. /** @var EventDispatcherInterface $eventDispatcher */
  113. $eventDispatcher = $container->getServer()->getEventDispatcher();
  114. $eventDispatcher->addListener('app_password_created', function (GenericEvent $event) use ($container) {
  115. if (($token = $event->getSubject()) instanceof IToken) {
  116. /** @var IActivityManager $activityManager */
  117. $activityManager = $container->query(IActivityManager::class);
  118. /** @var ILogger $logger */
  119. $logger = $container->query(ILogger::class);
  120. $activity = $activityManager->generateEvent();
  121. $activity->setApp('settings')
  122. ->setType('security')
  123. ->setAffectedUser($token->getUID())
  124. ->setAuthor($token->getUID())
  125. ->setSubject(Provider::APP_TOKEN_CREATED, ['name' => $token->getName()])
  126. ->setObject('app_token', $token->getId());
  127. try {
  128. $activityManager->publish($activity);
  129. } catch (BadMethodCallException $e) {
  130. $logger->logException($e, ['message' => 'could not publish activity', 'level' => ILogger::WARN]);
  131. }
  132. }
  133. });
  134. }
  135. public function register() {
  136. $activityManager = $this->getContainer()->getServer()->getActivityManager();
  137. $activityManager->registerSetting(Setting::class); // FIXME move to info.xml
  138. $activityManager->registerProvider(Provider::class); // FIXME move to info.xml
  139. $activityManager->registerFilter(SecurityFilter::class); // FIXME move to info.xml
  140. $activityManager->registerSetting(SecuritySetting::class); // FIXME move to info.xml
  141. $activityManager->registerProvider(SecurityProvider::class); // FIXME move to info.xml
  142. $activityManager->registerSetting(GroupSetting::class); // FIXME move to info.xml
  143. $activityManager->registerProvider(GroupProvider::class); // FIXME move to info.xml
  144. Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
  145. Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
  146. $groupManager = $this->getContainer()->getServer()->getGroupManager();
  147. $groupManager->listen('\OC\Group', 'postRemoveUser', [$this, 'removeUserFromGroup']);
  148. $groupManager->listen('\OC\Group', 'postAddUser', [$this, 'addUserToGroup']);
  149. Util::connectHook('\OCP\Config', 'js', $this, 'extendJsConfig');
  150. }
  151. public function addUserToGroup(IGroup $group, IUser $user): void {
  152. /** @var Hooks $hooks */
  153. $hooks = $this->getContainer()->query(Hooks::class);
  154. $hooks->addUserToGroup($group, $user);
  155. }
  156. public function removeUserFromGroup(IGroup $group, IUser $user): void {
  157. /** @var Hooks $hooks */
  158. $hooks = $this->getContainer()->query(Hooks::class);
  159. $hooks->removeUserFromGroup($group, $user);
  160. }
  161. /**
  162. * @param array $parameters
  163. * @throws \InvalidArgumentException
  164. * @throws \BadMethodCallException
  165. * @throws \Exception
  166. * @throws \OCP\AppFramework\QueryException
  167. */
  168. public function onChangePassword(array $parameters) {
  169. /** @var Hooks $hooks */
  170. $hooks = $this->getContainer()->query(Hooks::class);
  171. $hooks->onChangePassword($parameters['uid']);
  172. }
  173. /**
  174. * @param array $parameters
  175. * @throws \InvalidArgumentException
  176. * @throws \BadMethodCallException
  177. * @throws \Exception
  178. * @throws \OCP\AppFramework\QueryException
  179. */
  180. public function onChangeInfo(array $parameters) {
  181. if ($parameters['feature'] !== 'eMailAddress') {
  182. return;
  183. }
  184. /** @var Hooks $hooks */
  185. $hooks = $this->getContainer()->query(Hooks::class);
  186. $hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
  187. }
  188. /**
  189. * @param array $settings
  190. */
  191. public function extendJsConfig(array $settings) {
  192. $appConfig = json_decode($settings['array']['oc_appconfig'], true);
  193. $publicWebFinger = \OC::$server->getConfig()->getAppValue('core', 'public_webfinger', '');
  194. if (!empty($publicWebFinger)) {
  195. $appConfig['core']['public_webfinger'] = $publicWebFinger;
  196. }
  197. $settings['array']['oc_appconfig'] = json_encode($appConfig);
  198. }
  199. }