1
0

Application.php 10 KB

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