Application.php 11 KB

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