Application.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\User_LDAP\AppInfo;
  7. use Closure;
  8. use OCA\Files_External\Service\BackendService;
  9. use OCA\User_LDAP\Controller\RenewPasswordController;
  10. use OCA\User_LDAP\Events\GroupBackendRegistered;
  11. use OCA\User_LDAP\Events\UserBackendRegistered;
  12. use OCA\User_LDAP\FilesystemHelper;
  13. use OCA\User_LDAP\Group_Proxy;
  14. use OCA\User_LDAP\GroupPluginManager;
  15. use OCA\User_LDAP\Handler\ExtStorageConfigHandler;
  16. use OCA\User_LDAP\Helper;
  17. use OCA\User_LDAP\ILDAPWrapper;
  18. use OCA\User_LDAP\LDAP;
  19. use OCA\User_LDAP\LoginListener;
  20. use OCA\User_LDAP\Notification\Notifier;
  21. use OCA\User_LDAP\SetupChecks\LdapConnection;
  22. use OCA\User_LDAP\SetupChecks\LdapInvalidUuids;
  23. use OCA\User_LDAP\User\Manager;
  24. use OCA\User_LDAP\User_Proxy;
  25. use OCA\User_LDAP\UserPluginManager;
  26. use OCP\AppFramework\App;
  27. use OCP\AppFramework\Bootstrap\IBootContext;
  28. use OCP\AppFramework\Bootstrap\IBootstrap;
  29. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  30. use OCP\AppFramework\IAppContainer;
  31. use OCP\EventDispatcher\IEventDispatcher;
  32. use OCP\IAvatarManager;
  33. use OCP\IConfig;
  34. use OCP\IGroupManager;
  35. use OCP\IL10N;
  36. use OCP\Image;
  37. use OCP\IServerContainer;
  38. use OCP\IUserManager;
  39. use OCP\Notification\IManager as INotificationManager;
  40. use OCP\Share\IManager as IShareManager;
  41. use OCP\User\Events\PostLoginEvent;
  42. use Psr\Container\ContainerInterface;
  43. use Psr\Log\LoggerInterface;
  44. class Application extends App implements IBootstrap {
  45. public function __construct() {
  46. parent::__construct('user_ldap');
  47. $container = $this->getContainer();
  48. /**
  49. * Controller
  50. */
  51. $container->registerService('RenewPasswordController', function (IAppContainer $appContainer) {
  52. /** @var IServerContainer $server */
  53. $server = $appContainer->get(IServerContainer::class);
  54. return new RenewPasswordController(
  55. $appContainer->get('AppName'),
  56. $server->getRequest(),
  57. $appContainer->get('UserManager'),
  58. $server->getConfig(),
  59. $appContainer->get(IL10N::class),
  60. $appContainer->get('Session'),
  61. $server->getURLGenerator()
  62. );
  63. });
  64. $container->registerService(ILDAPWrapper::class, function (IAppContainer $appContainer) {
  65. /** @var IServerContainer $server */
  66. $server = $appContainer->get(IServerContainer::class);
  67. return new LDAP(
  68. $server->getConfig()->getSystemValueString('ldap_log_file')
  69. );
  70. });
  71. }
  72. public function register(IRegistrationContext $context): void {
  73. $context->registerNotifierService(Notifier::class);
  74. $context->registerService(
  75. Manager::class,
  76. function (ContainerInterface $c) {
  77. return new Manager(
  78. $c->get(IConfig::class),
  79. $c->get(FilesystemHelper::class),
  80. $c->get(LoggerInterface::class),
  81. $c->get(IAvatarManager::class),
  82. $c->get(Image::class),
  83. $c->get(IUserManager::class),
  84. $c->get(INotificationManager::class),
  85. $c->get(IShareManager::class),
  86. );
  87. },
  88. // the instance is specific to a lazy bound Access instance, thus cannot be shared.
  89. false
  90. );
  91. $context->registerEventListener(PostLoginEvent::class, LoginListener::class);
  92. $context->registerSetupCheck(LdapInvalidUuids::class);
  93. $context->registerSetupCheck(LdapConnection::class);
  94. }
  95. public function boot(IBootContext $context): void {
  96. $context->injectFn(function (
  97. INotificationManager $notificationManager,
  98. IAppContainer $appContainer,
  99. IEventDispatcher $dispatcher,
  100. IGroupManager $groupManager,
  101. User_Proxy $userBackend,
  102. Group_Proxy $groupBackend,
  103. Helper $helper,
  104. ): void {
  105. $configPrefixes = $helper->getServerConfigurationPrefixes(true);
  106. if (count($configPrefixes) > 0) {
  107. $userPluginManager = $appContainer->get(UserPluginManager::class);
  108. $groupPluginManager = $appContainer->get(GroupPluginManager::class);
  109. \OC_User::useBackend($userBackend);
  110. $groupManager->addBackend($groupBackend);
  111. $userBackendRegisteredEvent = new UserBackendRegistered($userBackend, $userPluginManager);
  112. $dispatcher->dispatch('OCA\\User_LDAP\\User\\User::postLDAPBackendAdded', $userBackendRegisteredEvent);
  113. $dispatcher->dispatchTyped($userBackendRegisteredEvent);
  114. $groupBackendRegisteredEvent = new GroupBackendRegistered($groupBackend, $groupPluginManager);
  115. $dispatcher->dispatchTyped($groupBackendRegisteredEvent);
  116. }
  117. });
  118. $context->injectFn(Closure::fromCallable([$this, 'registerBackendDependents']));
  119. \OCP\Util::connectHook(
  120. '\OCA\Files_Sharing\API\Server2Server',
  121. 'preLoginNameUsedAsUserName',
  122. '\OCA\User_LDAP\Helper',
  123. 'loginName2UserName'
  124. );
  125. }
  126. private function registerBackendDependents(IAppContainer $appContainer, IEventDispatcher $dispatcher): void {
  127. $dispatcher->addListener(
  128. 'OCA\\Files_External::loadAdditionalBackends',
  129. function () use ($appContainer): void {
  130. $storagesBackendService = $appContainer->get(BackendService::class);
  131. $storagesBackendService->registerConfigHandler('home', function () use ($appContainer) {
  132. return $appContainer->get(ExtStorageConfigHandler::class);
  133. });
  134. }
  135. );
  136. }
  137. }