Application.php 11 KB

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