Application.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author zulan <git@zulan.net>
  16. * @author Stephan Orbaugh <stephan.orbaugh@nextcloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCA\Settings\AppInfo;
  34. use OC\AppFramework\Utility\TimeFactory;
  35. use OC\Authentication\Events\AppPasswordCreatedEvent;
  36. use OC\Authentication\Token\IProvider;
  37. use OC\Server;
  38. use OCA\Settings\Hooks;
  39. use OCA\Settings\Listener\AppPasswordCreatedActivityListener;
  40. use OCA\Settings\Listener\GroupRemovedListener;
  41. use OCA\Settings\Listener\UserAddedToGroupActivityListener;
  42. use OCA\Settings\Listener\UserRemovedFromGroupActivityListener;
  43. use OCA\Settings\Mailer\NewUserMailHelper;
  44. use OCA\Settings\Middleware\SubadminMiddleware;
  45. use OCA\Settings\Search\AppSearch;
  46. use OCA\Settings\Search\SectionSearch;
  47. use OCA\Settings\Search\UserSearch;
  48. use OCA\Settings\SetupChecks\AppDirsWithDifferentOwner;
  49. use OCA\Settings\SetupChecks\BruteForceThrottler;
  50. use OCA\Settings\SetupChecks\CheckUserCertificates;
  51. use OCA\Settings\SetupChecks\CodeIntegrity;
  52. use OCA\Settings\SetupChecks\CronErrors;
  53. use OCA\Settings\SetupChecks\CronInfo;
  54. use OCA\Settings\SetupChecks\DatabaseHasMissingColumns;
  55. use OCA\Settings\SetupChecks\DatabaseHasMissingIndices;
  56. use OCA\Settings\SetupChecks\DatabaseHasMissingPrimaryKeys;
  57. use OCA\Settings\SetupChecks\DatabasePendingBigIntConversions;
  58. use OCA\Settings\SetupChecks\DataDirectoryProtected;
  59. use OCA\Settings\SetupChecks\DebugMode;
  60. use OCA\Settings\SetupChecks\DefaultPhoneRegionSet;
  61. use OCA\Settings\SetupChecks\EmailTestSuccessful;
  62. use OCA\Settings\SetupChecks\FileLocking;
  63. use OCA\Settings\SetupChecks\ForwardedForHeaders;
  64. use OCA\Settings\SetupChecks\HttpsUrlGeneration;
  65. use OCA\Settings\SetupChecks\InternetConnectivity;
  66. use OCA\Settings\SetupChecks\JavaScriptModules;
  67. use OCA\Settings\SetupChecks\JavaScriptSourceMaps;
  68. use OCA\Settings\SetupChecks\LegacySSEKeyFormat;
  69. use OCA\Settings\SetupChecks\MaintenanceWindowStart;
  70. use OCA\Settings\SetupChecks\MemcacheConfigured;
  71. use OCA\Settings\SetupChecks\MysqlUnicodeSupport;
  72. use OCA\Settings\SetupChecks\OcxProviders;
  73. use OCA\Settings\SetupChecks\OverwriteCliUrl;
  74. use OCA\Settings\SetupChecks\PhpDefaultCharset;
  75. use OCA\Settings\SetupChecks\PhpDisabledFunctions;
  76. use OCA\Settings\SetupChecks\PhpFreetypeSupport;
  77. use OCA\Settings\SetupChecks\PhpGetEnv;
  78. use OCA\Settings\SetupChecks\PhpMemoryLimit;
  79. use OCA\Settings\SetupChecks\PhpModules;
  80. use OCA\Settings\SetupChecks\PhpOpcacheSetup;
  81. use OCA\Settings\SetupChecks\PhpOutdated;
  82. use OCA\Settings\SetupChecks\PhpOutputBuffering;
  83. use OCA\Settings\SetupChecks\PushService;
  84. use OCA\Settings\SetupChecks\RandomnessSecure;
  85. use OCA\Settings\SetupChecks\ReadOnlyConfig;
  86. use OCA\Settings\SetupChecks\SecurityHeaders;
  87. use OCA\Settings\SetupChecks\SupportedDatabase;
  88. use OCA\Settings\SetupChecks\SystemIs64bit;
  89. use OCA\Settings\SetupChecks\TempSpaceAvailable;
  90. use OCA\Settings\SetupChecks\TransactionIsolation;
  91. use OCA\Settings\SetupChecks\WellKnownUrls;
  92. use OCA\Settings\SetupChecks\Woff2Loading;
  93. use OCA\Settings\UserMigration\AccountMigrator;
  94. use OCA\Settings\WellKnown\ChangePasswordHandler;
  95. use OCA\Settings\WellKnown\SecurityTxtHandler;
  96. use OCP\AppFramework\App;
  97. use OCP\AppFramework\Bootstrap\IBootContext;
  98. use OCP\AppFramework\Bootstrap\IBootstrap;
  99. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  100. use OCP\AppFramework\IAppContainer;
  101. use OCP\Defaults;
  102. use OCP\Group\Events\GroupDeletedEvent;
  103. use OCP\Group\Events\UserAddedEvent;
  104. use OCP\Group\Events\UserRemovedEvent;
  105. use OCP\IServerContainer;
  106. use OCP\Settings\IManager;
  107. use OCP\Util;
  108. class Application extends App implements IBootstrap {
  109. public const APP_ID = 'settings';
  110. /**
  111. * @param array $urlParams
  112. */
  113. public function __construct(array $urlParams = []) {
  114. parent::__construct(self::APP_ID, $urlParams);
  115. }
  116. public function register(IRegistrationContext $context): void {
  117. // Register Middleware
  118. $context->registerServiceAlias('SubadminMiddleware', SubadminMiddleware::class);
  119. $context->registerMiddleware(SubadminMiddleware::class);
  120. $context->registerSearchProvider(SectionSearch::class);
  121. $context->registerSearchProvider(AppSearch::class);
  122. $context->registerSearchProvider(UserSearch::class);
  123. // Register listeners
  124. $context->registerEventListener(AppPasswordCreatedEvent::class, AppPasswordCreatedActivityListener::class);
  125. $context->registerEventListener(UserAddedEvent::class, UserAddedToGroupActivityListener::class);
  126. $context->registerEventListener(UserRemovedEvent::class, UserRemovedFromGroupActivityListener::class);
  127. $context->registerEventListener(GroupDeletedEvent::class, GroupRemovedListener::class);
  128. // Register well-known handlers
  129. $context->registerWellKnownHandler(SecurityTxtHandler::class);
  130. $context->registerWellKnownHandler(ChangePasswordHandler::class);
  131. /**
  132. * Core class wrappers
  133. */
  134. /** FIXME: Remove once OC_SubAdmin is non-static and mockable */
  135. $context->registerService('isSubAdmin', function () {
  136. $userObject = \OC::$server->getUserSession()->getUser();
  137. $isSubAdmin = false;
  138. if ($userObject !== null) {
  139. $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
  140. }
  141. return $isSubAdmin;
  142. });
  143. $context->registerService(IProvider::class, function (IAppContainer $appContainer) {
  144. /** @var IServerContainer $serverContainer */
  145. $serverContainer = $appContainer->query(IServerContainer::class);
  146. return $serverContainer->query(IProvider::class);
  147. });
  148. $context->registerService(IManager::class, function (IAppContainer $appContainer) {
  149. /** @var IServerContainer $serverContainer */
  150. $serverContainer = $appContainer->query(IServerContainer::class);
  151. return $serverContainer->getSettingsManager();
  152. });
  153. $context->registerService(NewUserMailHelper::class, function (IAppContainer $appContainer) {
  154. /** @var Server $server */
  155. $server = $appContainer->query(IServerContainer::class);
  156. /** @var Defaults $defaults */
  157. $defaults = $server->query(Defaults::class);
  158. return new NewUserMailHelper(
  159. $defaults,
  160. $server->getURLGenerator(),
  161. $server->getL10NFactory(),
  162. $server->getMailer(),
  163. $server->getSecureRandom(),
  164. new TimeFactory(),
  165. $server->getConfig(),
  166. $server->getCrypto(),
  167. Util::getDefaultEmailAddress('no-reply')
  168. );
  169. });
  170. $context->registerSetupCheck(AppDirsWithDifferentOwner::class);
  171. $context->registerSetupCheck(BruteForceThrottler::class);
  172. $context->registerSetupCheck(CheckUserCertificates::class);
  173. $context->registerSetupCheck(CodeIntegrity::class);
  174. $context->registerSetupCheck(CronErrors::class);
  175. $context->registerSetupCheck(CronInfo::class);
  176. $context->registerSetupCheck(DatabaseHasMissingColumns::class);
  177. $context->registerSetupCheck(DatabaseHasMissingIndices::class);
  178. $context->registerSetupCheck(DatabaseHasMissingPrimaryKeys::class);
  179. $context->registerSetupCheck(DatabasePendingBigIntConversions::class);
  180. $context->registerSetupCheck(DataDirectoryProtected::class);
  181. $context->registerSetupCheck(DebugMode::class);
  182. $context->registerSetupCheck(DefaultPhoneRegionSet::class);
  183. $context->registerSetupCheck(EmailTestSuccessful::class);
  184. $context->registerSetupCheck(FileLocking::class);
  185. $context->registerSetupCheck(ForwardedForHeaders::class);
  186. $context->registerSetupCheck(HttpsUrlGeneration::class);
  187. $context->registerSetupCheck(InternetConnectivity::class);
  188. $context->registerSetupCheck(JavaScriptSourceMaps::class);
  189. $context->registerSetupCheck(JavaScriptModules::class);
  190. $context->registerSetupCheck(LegacySSEKeyFormat::class);
  191. $context->registerSetupCheck(MaintenanceWindowStart::class);
  192. $context->registerSetupCheck(MemcacheConfigured::class);
  193. $context->registerSetupCheck(MysqlUnicodeSupport::class);
  194. $context->registerSetupCheck(OcxProviders::class);
  195. $context->registerSetupCheck(OverwriteCliUrl::class);
  196. $context->registerSetupCheck(PhpDefaultCharset::class);
  197. $context->registerSetupCheck(PhpDisabledFunctions::class);
  198. $context->registerSetupCheck(PhpFreetypeSupport::class);
  199. $context->registerSetupCheck(PhpGetEnv::class);
  200. $context->registerSetupCheck(PhpMemoryLimit::class);
  201. $context->registerSetupCheck(PhpModules::class);
  202. $context->registerSetupCheck(PhpOpcacheSetup::class);
  203. $context->registerSetupCheck(PhpOutdated::class);
  204. $context->registerSetupCheck(PhpOutputBuffering::class);
  205. $context->registerSetupCheck(RandomnessSecure::class);
  206. $context->registerSetupCheck(ReadOnlyConfig::class);
  207. $context->registerSetupCheck(SecurityHeaders::class);
  208. $context->registerSetupCheck(SupportedDatabase::class);
  209. $context->registerSetupCheck(SystemIs64bit::class);
  210. $context->registerSetupCheck(TempSpaceAvailable::class);
  211. $context->registerSetupCheck(TransactionIsolation::class);
  212. $context->registerSetupCheck(PushService::class);
  213. $context->registerSetupCheck(WellKnownUrls::class);
  214. $context->registerSetupCheck(Woff2Loading::class);
  215. $context->registerUserMigrator(AccountMigrator::class);
  216. }
  217. public function boot(IBootContext $context): void {
  218. Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
  219. Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
  220. }
  221. /**
  222. * @param array $parameters
  223. * @throws \InvalidArgumentException
  224. * @throws \BadMethodCallException
  225. * @throws \Exception
  226. * @throws \OCP\AppFramework\QueryException
  227. */
  228. public function onChangePassword(array $parameters) {
  229. /** @var Hooks $hooks */
  230. $hooks = $this->getContainer()->query(Hooks::class);
  231. $hooks->onChangePassword($parameters['uid']);
  232. }
  233. /**
  234. * @param array $parameters
  235. * @throws \InvalidArgumentException
  236. * @throws \BadMethodCallException
  237. * @throws \Exception
  238. * @throws \OCP\AppFramework\QueryException
  239. */
  240. public function onChangeInfo(array $parameters) {
  241. if ($parameters['feature'] !== 'eMailAddress') {
  242. return;
  243. }
  244. /** @var Hooks $hooks */
  245. $hooks = $this->getContainer()->query(Hooks::class);
  246. $hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
  247. }
  248. }