Application.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author zulan <git@zulan.net>
  16. * @author Stephan Orbaugh <stephan.orbaugh@nextcloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCA\Settings\AppInfo;
  34. use OC\AppFramework\Utility\TimeFactory;
  35. use OC\Authentication\Events\AppPasswordCreatedEvent;
  36. use OC\Authentication\Token\IProvider;
  37. use OC\Server;
  38. use OCA\Settings\Hooks;
  39. use OCA\Settings\Listener\AppPasswordCreatedActivityListener;
  40. use OCA\Settings\Listener\GroupRemovedListener;
  41. use OCA\Settings\Listener\UserAddedToGroupActivityListener;
  42. use OCA\Settings\Listener\UserRemovedFromGroupActivityListener;
  43. use OCA\Settings\Mailer\NewUserMailHelper;
  44. use OCA\Settings\Middleware\SubadminMiddleware;
  45. use OCA\Settings\Search\AppSearch;
  46. use OCA\Settings\Search\SectionSearch;
  47. use OCA\Settings\Search\UserSearch;
  48. use OCA\Settings\SetupChecks\AppDirsWithDifferentOwner;
  49. use OCA\Settings\SetupChecks\BruteForceThrottler;
  50. use OCA\Settings\SetupChecks\CheckUserCertificates;
  51. use OCA\Settings\SetupChecks\CodeIntegrity;
  52. use OCA\Settings\SetupChecks\CronErrors;
  53. use OCA\Settings\SetupChecks\CronInfo;
  54. use OCA\Settings\SetupChecks\DatabaseHasMissingColumns;
  55. use OCA\Settings\SetupChecks\DatabaseHasMissingIndices;
  56. use OCA\Settings\SetupChecks\DatabaseHasMissingPrimaryKeys;
  57. use OCA\Settings\SetupChecks\DatabasePendingBigIntConversions;
  58. use OCA\Settings\SetupChecks\DefaultPhoneRegionSet;
  59. use OCA\Settings\SetupChecks\EmailTestSuccessful;
  60. use OCA\Settings\SetupChecks\FileLocking;
  61. use OCA\Settings\SetupChecks\ForwardedForHeaders;
  62. use OCA\Settings\SetupChecks\InternetConnectivity;
  63. use OCA\Settings\SetupChecks\JavaScriptModules;
  64. use OCA\Settings\SetupChecks\LegacySSEKeyFormat;
  65. use OCA\Settings\SetupChecks\MaintenanceWindowStart;
  66. use OCA\Settings\SetupChecks\MemcacheConfigured;
  67. use OCA\Settings\SetupChecks\OverwriteCliUrl;
  68. use OCA\Settings\SetupChecks\PhpDefaultCharset;
  69. use OCA\Settings\SetupChecks\PhpFreetypeSupport;
  70. use OCA\Settings\SetupChecks\PhpGetEnv;
  71. use OCA\Settings\SetupChecks\PhpMemoryLimit;
  72. use OCA\Settings\SetupChecks\PhpModules;
  73. use OCA\Settings\SetupChecks\PhpOpcacheSetup;
  74. use OCA\Settings\SetupChecks\PhpOutdated;
  75. use OCA\Settings\SetupChecks\PhpOutputBuffering;
  76. use OCA\Settings\SetupChecks\RandomnessSecure;
  77. use OCA\Settings\SetupChecks\ReadOnlyConfig;
  78. use OCA\Settings\SetupChecks\SupportedDatabase;
  79. use OCA\Settings\SetupChecks\SystemIs64bit;
  80. use OCA\Settings\SetupChecks\TransactionIsolation;
  81. use OCA\Settings\UserMigration\AccountMigrator;
  82. use OCA\Settings\WellKnown\ChangePasswordHandler;
  83. use OCA\Settings\WellKnown\SecurityTxtHandler;
  84. use OCP\AppFramework\App;
  85. use OCP\AppFramework\Bootstrap\IBootContext;
  86. use OCP\AppFramework\Bootstrap\IBootstrap;
  87. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  88. use OCP\AppFramework\IAppContainer;
  89. use OCP\Defaults;
  90. use OCP\Group\Events\GroupDeletedEvent;
  91. use OCP\Group\Events\UserAddedEvent;
  92. use OCP\Group\Events\UserRemovedEvent;
  93. use OCP\IServerContainer;
  94. use OCP\Settings\IManager;
  95. use OCP\Util;
  96. class Application extends App implements IBootstrap {
  97. public const APP_ID = 'settings';
  98. /**
  99. * @param array $urlParams
  100. */
  101. public function __construct(array $urlParams = []) {
  102. parent::__construct(self::APP_ID, $urlParams);
  103. }
  104. public function register(IRegistrationContext $context): void {
  105. // Register Middleware
  106. $context->registerServiceAlias('SubadminMiddleware', SubadminMiddleware::class);
  107. $context->registerMiddleware(SubadminMiddleware::class);
  108. $context->registerSearchProvider(SectionSearch::class);
  109. $context->registerSearchProvider(AppSearch::class);
  110. $context->registerSearchProvider(UserSearch::class);
  111. // Register listeners
  112. $context->registerEventListener(AppPasswordCreatedEvent::class, AppPasswordCreatedActivityListener::class);
  113. $context->registerEventListener(UserAddedEvent::class, UserAddedToGroupActivityListener::class);
  114. $context->registerEventListener(UserRemovedEvent::class, UserRemovedFromGroupActivityListener::class);
  115. $context->registerEventListener(GroupDeletedEvent::class, GroupRemovedListener::class);
  116. // Register well-known handlers
  117. $context->registerWellKnownHandler(SecurityTxtHandler::class);
  118. $context->registerWellKnownHandler(ChangePasswordHandler::class);
  119. /**
  120. * Core class wrappers
  121. */
  122. /** FIXME: Remove once OC_User is non-static and mockable */
  123. $context->registerService('isAdmin', function () {
  124. return \OC_User::isAdminUser(\OC_User::getUser());
  125. });
  126. /** FIXME: Remove once OC_SubAdmin is non-static and mockable */
  127. $context->registerService('isSubAdmin', function () {
  128. $userObject = \OC::$server->getUserSession()->getUser();
  129. $isSubAdmin = false;
  130. if ($userObject !== null) {
  131. $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
  132. }
  133. return $isSubAdmin;
  134. });
  135. $context->registerService(IProvider::class, function (IAppContainer $appContainer) {
  136. /** @var IServerContainer $serverContainer */
  137. $serverContainer = $appContainer->query(IServerContainer::class);
  138. return $serverContainer->query(IProvider::class);
  139. });
  140. $context->registerService(IManager::class, function (IAppContainer $appContainer) {
  141. /** @var IServerContainer $serverContainer */
  142. $serverContainer = $appContainer->query(IServerContainer::class);
  143. return $serverContainer->getSettingsManager();
  144. });
  145. $context->registerService(NewUserMailHelper::class, function (IAppContainer $appContainer) {
  146. /** @var Server $server */
  147. $server = $appContainer->query(IServerContainer::class);
  148. /** @var Defaults $defaults */
  149. $defaults = $server->query(Defaults::class);
  150. return new NewUserMailHelper(
  151. $defaults,
  152. $server->getURLGenerator(),
  153. $server->getL10NFactory(),
  154. $server->getMailer(),
  155. $server->getSecureRandom(),
  156. new TimeFactory(),
  157. $server->getConfig(),
  158. $server->getCrypto(),
  159. Util::getDefaultEmailAddress('no-reply')
  160. );
  161. });
  162. $context->registerSetupCheck(AppDirsWithDifferentOwner::class);
  163. $context->registerSetupCheck(BruteForceThrottler::class);
  164. $context->registerSetupCheck(CheckUserCertificates::class);
  165. $context->registerSetupCheck(CodeIntegrity::class);
  166. $context->registerSetupCheck(CronErrors::class);
  167. $context->registerSetupCheck(CronInfo::class);
  168. $context->registerSetupCheck(DatabaseHasMissingColumns::class);
  169. $context->registerSetupCheck(DatabaseHasMissingIndices::class);
  170. $context->registerSetupCheck(DatabaseHasMissingPrimaryKeys::class);
  171. $context->registerSetupCheck(DatabasePendingBigIntConversions::class);
  172. $context->registerSetupCheck(DefaultPhoneRegionSet::class);
  173. $context->registerSetupCheck(EmailTestSuccessful::class);
  174. $context->registerSetupCheck(FileLocking::class);
  175. $context->registerSetupCheck(ForwardedForHeaders::class);
  176. $context->registerSetupCheck(InternetConnectivity::class);
  177. $context->registerSetupCheck(JavaScriptModules::class);
  178. $context->registerSetupCheck(LegacySSEKeyFormat::class);
  179. $context->registerSetupCheck(MaintenanceWindowStart::class);
  180. $context->registerSetupCheck(MemcacheConfigured::class);
  181. $context->registerSetupCheck(OverwriteCliUrl::class);
  182. $context->registerSetupCheck(PhpDefaultCharset::class);
  183. $context->registerSetupCheck(PhpFreetypeSupport::class);
  184. $context->registerSetupCheck(PhpGetEnv::class);
  185. $context->registerSetupCheck(PhpMemoryLimit::class);
  186. $context->registerSetupCheck(PhpModules::class);
  187. $context->registerSetupCheck(PhpOpcacheSetup::class);
  188. $context->registerSetupCheck(PhpOutdated::class);
  189. $context->registerSetupCheck(PhpOutputBuffering::class);
  190. $context->registerSetupCheck(RandomnessSecure::class);
  191. $context->registerSetupCheck(ReadOnlyConfig::class);
  192. $context->registerSetupCheck(SupportedDatabase::class);
  193. $context->registerSetupCheck(SystemIs64bit::class);
  194. $context->registerSetupCheck(TransactionIsolation::class);
  195. $context->registerUserMigrator(AccountMigrator::class);
  196. }
  197. public function boot(IBootContext $context): void {
  198. Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
  199. Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
  200. }
  201. /**
  202. * @param array $parameters
  203. * @throws \InvalidArgumentException
  204. * @throws \BadMethodCallException
  205. * @throws \Exception
  206. * @throws \OCP\AppFramework\QueryException
  207. */
  208. public function onChangePassword(array $parameters) {
  209. /** @var Hooks $hooks */
  210. $hooks = $this->getContainer()->query(Hooks::class);
  211. $hooks->onChangePassword($parameters['uid']);
  212. }
  213. /**
  214. * @param array $parameters
  215. * @throws \InvalidArgumentException
  216. * @throws \BadMethodCallException
  217. * @throws \Exception
  218. * @throws \OCP\AppFramework\QueryException
  219. */
  220. public function onChangeInfo(array $parameters) {
  221. if ($parameters['feature'] !== 'eMailAddress') {
  222. return;
  223. }
  224. /** @var Hooks $hooks */
  225. $hooks = $this->getContainer()->query(Hooks::class);
  226. $hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
  227. }
  228. }