Application.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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\PushService;
  60. use OCA\Settings\SetupChecks\RandomnessSecure;
  61. use OCA\Settings\SetupChecks\ReadOnlyConfig;
  62. use OCA\Settings\SetupChecks\SchedulingTableSize;
  63. use OCA\Settings\SetupChecks\SecurityHeaders;
  64. use OCA\Settings\SetupChecks\SupportedDatabase;
  65. use OCA\Settings\SetupChecks\SystemIs64bit;
  66. use OCA\Settings\SetupChecks\TempSpaceAvailable;
  67. use OCA\Settings\SetupChecks\TransactionIsolation;
  68. use OCA\Settings\SetupChecks\WellKnownUrls;
  69. use OCA\Settings\SetupChecks\Woff2Loading;
  70. use OCA\Settings\UserMigration\AccountMigrator;
  71. use OCA\Settings\WellKnown\ChangePasswordHandler;
  72. use OCA\Settings\WellKnown\SecurityTxtHandler;
  73. use OCP\AppFramework\App;
  74. use OCP\AppFramework\Bootstrap\IBootContext;
  75. use OCP\AppFramework\Bootstrap\IBootstrap;
  76. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  77. use OCP\AppFramework\IAppContainer;
  78. use OCP\Defaults;
  79. use OCP\Group\Events\GroupDeletedEvent;
  80. use OCP\Group\Events\UserAddedEvent;
  81. use OCP\Group\Events\UserRemovedEvent;
  82. use OCP\IServerContainer;
  83. use OCP\Settings\IManager;
  84. use OCP\Util;
  85. class Application extends App implements IBootstrap {
  86. public const APP_ID = 'settings';
  87. /**
  88. * @param array $urlParams
  89. */
  90. public function __construct(array $urlParams = []) {
  91. parent::__construct(self::APP_ID, $urlParams);
  92. }
  93. public function register(IRegistrationContext $context): void {
  94. // Register Middleware
  95. $context->registerServiceAlias('SubadminMiddleware', SubadminMiddleware::class);
  96. $context->registerMiddleware(SubadminMiddleware::class);
  97. $context->registerSearchProvider(SectionSearch::class);
  98. $context->registerSearchProvider(AppSearch::class);
  99. $context->registerSearchProvider(UserSearch::class);
  100. // Register listeners
  101. $context->registerEventListener(AppPasswordCreatedEvent::class, AppPasswordCreatedActivityListener::class);
  102. $context->registerEventListener(UserAddedEvent::class, UserAddedToGroupActivityListener::class);
  103. $context->registerEventListener(UserRemovedEvent::class, UserRemovedFromGroupActivityListener::class);
  104. $context->registerEventListener(GroupDeletedEvent::class, GroupRemovedListener::class);
  105. // Register well-known handlers
  106. $context->registerWellKnownHandler(SecurityTxtHandler::class);
  107. $context->registerWellKnownHandler(ChangePasswordHandler::class);
  108. /**
  109. * Core class wrappers
  110. */
  111. /** FIXME: Remove once OC_SubAdmin is non-static and mockable */
  112. $context->registerService('isSubAdmin', function () {
  113. $userObject = \OC::$server->getUserSession()->getUser();
  114. $isSubAdmin = false;
  115. if ($userObject !== null) {
  116. $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
  117. }
  118. return $isSubAdmin;
  119. });
  120. $context->registerService(IProvider::class, function (IAppContainer $appContainer) {
  121. /** @var IServerContainer $serverContainer */
  122. $serverContainer = $appContainer->query(IServerContainer::class);
  123. return $serverContainer->query(IProvider::class);
  124. });
  125. $context->registerService(IManager::class, function (IAppContainer $appContainer) {
  126. /** @var IServerContainer $serverContainer */
  127. $serverContainer = $appContainer->query(IServerContainer::class);
  128. return $serverContainer->getSettingsManager();
  129. });
  130. $context->registerService(NewUserMailHelper::class, function (IAppContainer $appContainer) {
  131. /** @var Server $server */
  132. $server = $appContainer->query(IServerContainer::class);
  133. /** @var Defaults $defaults */
  134. $defaults = $server->query(Defaults::class);
  135. return new NewUserMailHelper(
  136. $defaults,
  137. $server->getURLGenerator(),
  138. $server->getL10NFactory(),
  139. $server->getMailer(),
  140. $server->getSecureRandom(),
  141. new TimeFactory(),
  142. $server->getConfig(),
  143. $server->getCrypto(),
  144. Util::getDefaultEmailAddress('no-reply')
  145. );
  146. });
  147. $context->registerSetupCheck(AppDirsWithDifferentOwner::class);
  148. $context->registerSetupCheck(BruteForceThrottler::class);
  149. $context->registerSetupCheck(CheckUserCertificates::class);
  150. $context->registerSetupCheck(CodeIntegrity::class);
  151. $context->registerSetupCheck(CronErrors::class);
  152. $context->registerSetupCheck(CronInfo::class);
  153. $context->registerSetupCheck(DatabaseHasMissingColumns::class);
  154. $context->registerSetupCheck(DatabaseHasMissingIndices::class);
  155. $context->registerSetupCheck(DatabaseHasMissingPrimaryKeys::class);
  156. $context->registerSetupCheck(DatabasePendingBigIntConversions::class);
  157. $context->registerSetupCheck(DataDirectoryProtected::class);
  158. $context->registerSetupCheck(DebugMode::class);
  159. $context->registerSetupCheck(DefaultPhoneRegionSet::class);
  160. $context->registerSetupCheck(EmailTestSuccessful::class);
  161. $context->registerSetupCheck(FileLocking::class);
  162. $context->registerSetupCheck(ForwardedForHeaders::class);
  163. $context->registerSetupCheck(HttpsUrlGeneration::class);
  164. $context->registerSetupCheck(InternetConnectivity::class);
  165. $context->registerSetupCheck(JavaScriptSourceMaps::class);
  166. $context->registerSetupCheck(JavaScriptModules::class);
  167. $context->registerSetupCheck(LegacySSEKeyFormat::class);
  168. $context->registerSetupCheck(MaintenanceWindowStart::class);
  169. $context->registerSetupCheck(MemcacheConfigured::class);
  170. $context->registerSetupCheck(MimeTypeMigrationAvailable::class);
  171. $context->registerSetupCheck(MysqlUnicodeSupport::class);
  172. $context->registerSetupCheck(OcxProviders::class);
  173. $context->registerSetupCheck(OverwriteCliUrl::class);
  174. $context->registerSetupCheck(PhpDefaultCharset::class);
  175. $context->registerSetupCheck(PhpDisabledFunctions::class);
  176. $context->registerSetupCheck(PhpFreetypeSupport::class);
  177. $context->registerSetupCheck(PhpGetEnv::class);
  178. $context->registerSetupCheck(PhpMemoryLimit::class);
  179. $context->registerSetupCheck(PhpModules::class);
  180. $context->registerSetupCheck(PhpOpcacheSetup::class);
  181. $context->registerSetupCheck(PhpOutdated::class);
  182. $context->registerSetupCheck(PhpOutputBuffering::class);
  183. $context->registerSetupCheck(RandomnessSecure::class);
  184. $context->registerSetupCheck(ReadOnlyConfig::class);
  185. $context->registerSetupCheck(SecurityHeaders::class);
  186. $context->registerSetupCheck(SchedulingTableSize::class);
  187. $context->registerSetupCheck(SupportedDatabase::class);
  188. $context->registerSetupCheck(SystemIs64bit::class);
  189. $context->registerSetupCheck(TempSpaceAvailable::class);
  190. $context->registerSetupCheck(TransactionIsolation::class);
  191. $context->registerSetupCheck(PushService::class);
  192. $context->registerSetupCheck(WellKnownUrls::class);
  193. $context->registerSetupCheck(Woff2Loading::class);
  194. $context->registerUserMigrator(AccountMigrator::class);
  195. }
  196. public function boot(IBootContext $context): void {
  197. Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
  198. Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
  199. }
  200. /**
  201. * @param array $parameters
  202. * @throws \InvalidArgumentException
  203. * @throws \BadMethodCallException
  204. * @throws \Exception
  205. * @throws \OCP\AppFramework\QueryException
  206. */
  207. public function onChangePassword(array $parameters) {
  208. /** @var Hooks $hooks */
  209. $hooks = $this->getContainer()->query(Hooks::class);
  210. $hooks->onChangePassword($parameters['uid']);
  211. }
  212. /**
  213. * @param array $parameters
  214. * @throws \InvalidArgumentException
  215. * @throws \BadMethodCallException
  216. * @throws \Exception
  217. * @throws \OCP\AppFramework\QueryException
  218. */
  219. public function onChangeInfo(array $parameters) {
  220. if ($parameters['feature'] !== 'eMailAddress') {
  221. return;
  222. }
  223. /** @var Hooks $hooks */
  224. $hooks = $this->getContainer()->query(Hooks::class);
  225. $hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
  226. }
  227. }