Application.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\Settings\AppInfo;
  9. use OC\AppFramework\Utility\TimeFactory;
  10. use OC\Authentication\Events\AppPasswordCreatedEvent;
  11. use OC\Authentication\Token\IProvider;
  12. use OC\Server;
  13. use OCA\Settings\Hooks;
  14. use OCA\Settings\Listener\AppPasswordCreatedActivityListener;
  15. use OCA\Settings\Listener\GroupRemovedListener;
  16. use OCA\Settings\Listener\UserAddedToGroupActivityListener;
  17. use OCA\Settings\Listener\UserRemovedFromGroupActivityListener;
  18. use OCA\Settings\Mailer\NewUserMailHelper;
  19. use OCA\Settings\Middleware\SubadminMiddleware;
  20. use OCA\Settings\Search\AppSearch;
  21. use OCA\Settings\Search\SectionSearch;
  22. use OCA\Settings\Search\UserSearch;
  23. use OCA\Settings\SetupChecks\AppDirsWithDifferentOwner;
  24. use OCA\Settings\SetupChecks\BruteForceThrottler;
  25. use OCA\Settings\SetupChecks\CheckUserCertificates;
  26. use OCA\Settings\SetupChecks\CodeIntegrity;
  27. use OCA\Settings\SetupChecks\CronErrors;
  28. use OCA\Settings\SetupChecks\CronInfo;
  29. use OCA\Settings\SetupChecks\DatabaseHasMissingColumns;
  30. use OCA\Settings\SetupChecks\DatabaseHasMissingIndices;
  31. use OCA\Settings\SetupChecks\DatabaseHasMissingPrimaryKeys;
  32. use OCA\Settings\SetupChecks\DatabasePendingBigIntConversions;
  33. use OCA\Settings\SetupChecks\DataDirectoryProtected;
  34. use OCA\Settings\SetupChecks\DebugMode;
  35. use OCA\Settings\SetupChecks\DefaultPhoneRegionSet;
  36. use OCA\Settings\SetupChecks\EmailTestSuccessful;
  37. use OCA\Settings\SetupChecks\FileLocking;
  38. use OCA\Settings\SetupChecks\ForwardedForHeaders;
  39. use OCA\Settings\SetupChecks\HttpsUrlGeneration;
  40. use OCA\Settings\SetupChecks\InternetConnectivity;
  41. use OCA\Settings\SetupChecks\JavaScriptModules;
  42. use OCA\Settings\SetupChecks\JavaScriptSourceMaps;
  43. use OCA\Settings\SetupChecks\LegacySSEKeyFormat;
  44. use OCA\Settings\SetupChecks\MaintenanceWindowStart;
  45. use OCA\Settings\SetupChecks\MemcacheConfigured;
  46. use OCA\Settings\SetupChecks\MimeTypeMigrationAvailable;
  47. use OCA\Settings\SetupChecks\MysqlUnicodeSupport;
  48. use OCA\Settings\SetupChecks\OcxProviders;
  49. use OCA\Settings\SetupChecks\OverwriteCliUrl;
  50. use OCA\Settings\SetupChecks\PhpDefaultCharset;
  51. use OCA\Settings\SetupChecks\PhpDisabledFunctions;
  52. use OCA\Settings\SetupChecks\PhpFreetypeSupport;
  53. use OCA\Settings\SetupChecks\PhpGetEnv;
  54. use OCA\Settings\SetupChecks\PhpMemoryLimit;
  55. use OCA\Settings\SetupChecks\PhpModules;
  56. use OCA\Settings\SetupChecks\PhpOpcacheSetup;
  57. use OCA\Settings\SetupChecks\PhpOutdated;
  58. use OCA\Settings\SetupChecks\PhpOutputBuffering;
  59. use OCA\Settings\SetupChecks\PhpSAPI;
  60. use OCA\Settings\SetupChecks\PushService;
  61. use OCA\Settings\SetupChecks\RandomnessSecure;
  62. use OCA\Settings\SetupChecks\ReadOnlyConfig;
  63. use OCA\Settings\SetupChecks\SchedulingTableSize;
  64. use OCA\Settings\SetupChecks\SecurityHeaders;
  65. use OCA\Settings\SetupChecks\SupportedDatabase;
  66. use OCA\Settings\SetupChecks\SystemIs64bit;
  67. use OCA\Settings\SetupChecks\TempSpaceAvailable;
  68. use OCA\Settings\SetupChecks\TransactionIsolation;
  69. use OCA\Settings\SetupChecks\WellKnownUrls;
  70. use OCA\Settings\SetupChecks\Woff2Loading;
  71. use OCA\Settings\UserMigration\AccountMigrator;
  72. use OCA\Settings\WellKnown\ChangePasswordHandler;
  73. use OCA\Settings\WellKnown\SecurityTxtHandler;
  74. use OCP\AppFramework\App;
  75. use OCP\AppFramework\Bootstrap\IBootContext;
  76. use OCP\AppFramework\Bootstrap\IBootstrap;
  77. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  78. use OCP\AppFramework\IAppContainer;
  79. use OCP\Defaults;
  80. use OCP\Group\Events\GroupDeletedEvent;
  81. use OCP\Group\Events\UserAddedEvent;
  82. use OCP\Group\Events\UserRemovedEvent;
  83. use OCP\IServerContainer;
  84. use OCP\Settings\IManager;
  85. use OCP\Util;
  86. class Application extends App implements IBootstrap {
  87. public const APP_ID = 'settings';
  88. /**
  89. * @param array $urlParams
  90. */
  91. public function __construct(array $urlParams = []) {
  92. parent::__construct(self::APP_ID, $urlParams);
  93. }
  94. public function register(IRegistrationContext $context): void {
  95. // Register Middleware
  96. $context->registerServiceAlias('SubadminMiddleware', SubadminMiddleware::class);
  97. $context->registerMiddleware(SubadminMiddleware::class);
  98. $context->registerSearchProvider(SectionSearch::class);
  99. $context->registerSearchProvider(AppSearch::class);
  100. $context->registerSearchProvider(UserSearch::class);
  101. // Register listeners
  102. $context->registerEventListener(AppPasswordCreatedEvent::class, AppPasswordCreatedActivityListener::class);
  103. $context->registerEventListener(UserAddedEvent::class, UserAddedToGroupActivityListener::class);
  104. $context->registerEventListener(UserRemovedEvent::class, UserRemovedFromGroupActivityListener::class);
  105. $context->registerEventListener(GroupDeletedEvent::class, GroupRemovedListener::class);
  106. // Register well-known handlers
  107. $context->registerWellKnownHandler(SecurityTxtHandler::class);
  108. $context->registerWellKnownHandler(ChangePasswordHandler::class);
  109. /**
  110. * Core class wrappers
  111. */
  112. /** FIXME: Remove once OC_SubAdmin is non-static and mockable */
  113. $context->registerService('isSubAdmin', function () {
  114. $userObject = \OC::$server->getUserSession()->getUser();
  115. $isSubAdmin = false;
  116. if ($userObject !== null) {
  117. $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
  118. }
  119. return $isSubAdmin;
  120. });
  121. $context->registerService(IProvider::class, function (IAppContainer $appContainer) {
  122. /** @var IServerContainer $serverContainer */
  123. $serverContainer = $appContainer->query(IServerContainer::class);
  124. return $serverContainer->query(IProvider::class);
  125. });
  126. $context->registerService(IManager::class, function (IAppContainer $appContainer) {
  127. /** @var IServerContainer $serverContainer */
  128. $serverContainer = $appContainer->query(IServerContainer::class);
  129. return $serverContainer->getSettingsManager();
  130. });
  131. $context->registerService(NewUserMailHelper::class, function (IAppContainer $appContainer) {
  132. /** @var Server $server */
  133. $server = $appContainer->query(IServerContainer::class);
  134. /** @var Defaults $defaults */
  135. $defaults = $server->query(Defaults::class);
  136. return new NewUserMailHelper(
  137. $defaults,
  138. $server->getURLGenerator(),
  139. $server->getL10NFactory(),
  140. $server->getMailer(),
  141. $server->getSecureRandom(),
  142. new TimeFactory(),
  143. $server->getConfig(),
  144. $server->getCrypto(),
  145. Util::getDefaultEmailAddress('no-reply')
  146. );
  147. });
  148. $context->registerSetupCheck(AppDirsWithDifferentOwner::class);
  149. $context->registerSetupCheck(BruteForceThrottler::class);
  150. $context->registerSetupCheck(CheckUserCertificates::class);
  151. $context->registerSetupCheck(CodeIntegrity::class);
  152. $context->registerSetupCheck(CronErrors::class);
  153. $context->registerSetupCheck(CronInfo::class);
  154. $context->registerSetupCheck(DatabaseHasMissingColumns::class);
  155. $context->registerSetupCheck(DatabaseHasMissingIndices::class);
  156. $context->registerSetupCheck(DatabaseHasMissingPrimaryKeys::class);
  157. $context->registerSetupCheck(DatabasePendingBigIntConversions::class);
  158. $context->registerSetupCheck(DataDirectoryProtected::class);
  159. $context->registerSetupCheck(DebugMode::class);
  160. $context->registerSetupCheck(DefaultPhoneRegionSet::class);
  161. $context->registerSetupCheck(EmailTestSuccessful::class);
  162. $context->registerSetupCheck(FileLocking::class);
  163. $context->registerSetupCheck(ForwardedForHeaders::class);
  164. $context->registerSetupCheck(HttpsUrlGeneration::class);
  165. $context->registerSetupCheck(InternetConnectivity::class);
  166. $context->registerSetupCheck(JavaScriptSourceMaps::class);
  167. $context->registerSetupCheck(JavaScriptModules::class);
  168. $context->registerSetupCheck(LegacySSEKeyFormat::class);
  169. $context->registerSetupCheck(MaintenanceWindowStart::class);
  170. $context->registerSetupCheck(MemcacheConfigured::class);
  171. $context->registerSetupCheck(MimeTypeMigrationAvailable::class);
  172. $context->registerSetupCheck(MysqlUnicodeSupport::class);
  173. $context->registerSetupCheck(OcxProviders::class);
  174. $context->registerSetupCheck(OverwriteCliUrl::class);
  175. $context->registerSetupCheck(PhpDefaultCharset::class);
  176. $context->registerSetupCheck(PhpDisabledFunctions::class);
  177. $context->registerSetupCheck(PhpFreetypeSupport::class);
  178. $context->registerSetupCheck(PhpGetEnv::class);
  179. $context->registerSetupCheck(PhpMemoryLimit::class);
  180. $context->registerSetupCheck(PhpModules::class);
  181. $context->registerSetupCheck(PhpOpcacheSetup::class);
  182. $context->registerSetupCheck(PhpOutdated::class);
  183. $context->registerSetupCheck(PhpOutputBuffering::class);
  184. $context->registerSetupCheck(PhpSAPI::class);
  185. $context->registerSetupCheck(RandomnessSecure::class);
  186. $context->registerSetupCheck(ReadOnlyConfig::class);
  187. $context->registerSetupCheck(SecurityHeaders::class);
  188. $context->registerSetupCheck(SchedulingTableSize::class);
  189. $context->registerSetupCheck(SupportedDatabase::class);
  190. $context->registerSetupCheck(SystemIs64bit::class);
  191. $context->registerSetupCheck(TempSpaceAvailable::class);
  192. $context->registerSetupCheck(TransactionIsolation::class);
  193. $context->registerSetupCheck(PushService::class);
  194. $context->registerSetupCheck(WellKnownUrls::class);
  195. $context->registerSetupCheck(Woff2Loading::class);
  196. $context->registerUserMigrator(AccountMigrator::class);
  197. }
  198. public function boot(IBootContext $context): void {
  199. Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
  200. Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
  201. }
  202. /**
  203. * @param array $parameters
  204. * @throws \InvalidArgumentException
  205. * @throws \BadMethodCallException
  206. * @throws \Exception
  207. * @throws \OCP\AppFramework\QueryException
  208. */
  209. public function onChangePassword(array $parameters) {
  210. /** @var Hooks $hooks */
  211. $hooks = $this->getContainer()->query(Hooks::class);
  212. $hooks->onChangePassword($parameters['uid']);
  213. }
  214. /**
  215. * @param array $parameters
  216. * @throws \InvalidArgumentException
  217. * @throws \BadMethodCallException
  218. * @throws \Exception
  219. * @throws \OCP\AppFramework\QueryException
  220. */
  221. public function onChangeInfo(array $parameters) {
  222. if ($parameters['feature'] !== 'eMailAddress') {
  223. return;
  224. }
  225. /** @var Hooks $hooks */
  226. $hooks = $this->getContainer()->query(Hooks::class);
  227. $hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
  228. }
  229. }