1
0

Application.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 OCP\Util;
  43. use Psr\Container\ContainerInterface;
  44. use Psr\Log\LoggerInterface;
  45. class Application extends App implements IBootstrap {
  46. public function __construct() {
  47. parent::__construct('user_ldap');
  48. $container = $this->getContainer();
  49. /**
  50. * Controller
  51. */
  52. $container->registerService('RenewPasswordController', function (IAppContainer $appContainer) {
  53. /** @var IServerContainer $server */
  54. $server = $appContainer->get(IServerContainer::class);
  55. return new RenewPasswordController(
  56. $appContainer->get('AppName'),
  57. $server->getRequest(),
  58. $appContainer->get('UserManager'),
  59. $server->getConfig(),
  60. $appContainer->get(IL10N::class),
  61. $appContainer->get('Session'),
  62. $server->getURLGenerator()
  63. );
  64. });
  65. $container->registerService(ILDAPWrapper::class, function (IAppContainer $appContainer) {
  66. /** @var IServerContainer $server */
  67. $server = $appContainer->get(IServerContainer::class);
  68. return new LDAP(
  69. $server->getConfig()->getSystemValueString('ldap_log_file')
  70. );
  71. });
  72. }
  73. public function register(IRegistrationContext $context): void {
  74. $context->registerNotifierService(Notifier::class);
  75. $context->registerService(
  76. Manager::class,
  77. function (ContainerInterface $c) {
  78. return new Manager(
  79. $c->get(IConfig::class),
  80. $c->get(FilesystemHelper::class),
  81. $c->get(LoggerInterface::class),
  82. $c->get(IAvatarManager::class),
  83. $c->get(Image::class),
  84. $c->get(IUserManager::class),
  85. $c->get(INotificationManager::class),
  86. $c->get(IShareManager::class),
  87. );
  88. },
  89. // the instance is specific to a lazy bound Access instance, thus cannot be shared.
  90. false
  91. );
  92. $context->registerEventListener(PostLoginEvent::class, LoginListener::class);
  93. $context->registerSetupCheck(LdapInvalidUuids::class);
  94. $context->registerSetupCheck(LdapConnection::class);
  95. }
  96. public function boot(IBootContext $context): void {
  97. $context->injectFn(function (
  98. INotificationManager $notificationManager,
  99. IAppContainer $appContainer,
  100. IEventDispatcher $dispatcher,
  101. IGroupManager $groupManager,
  102. User_Proxy $userBackend,
  103. Group_Proxy $groupBackend,
  104. Helper $helper,
  105. ): void {
  106. $configPrefixes = $helper->getServerConfigurationPrefixes(true);
  107. if (count($configPrefixes) > 0) {
  108. $userPluginManager = $appContainer->get(UserPluginManager::class);
  109. $groupPluginManager = $appContainer->get(GroupPluginManager::class);
  110. \OC_User::useBackend($userBackend);
  111. $groupManager->addBackend($groupBackend);
  112. $userBackendRegisteredEvent = new UserBackendRegistered($userBackend, $userPluginManager);
  113. $dispatcher->dispatch('OCA\\User_LDAP\\User\\User::postLDAPBackendAdded', $userBackendRegisteredEvent);
  114. $dispatcher->dispatchTyped($userBackendRegisteredEvent);
  115. $groupBackendRegisteredEvent = new GroupBackendRegistered($groupBackend, $groupPluginManager);
  116. $dispatcher->dispatchTyped($groupBackendRegisteredEvent);
  117. }
  118. });
  119. $context->injectFn(Closure::fromCallable([$this, 'registerBackendDependents']));
  120. Util::connectHook(
  121. '\OCA\Files_Sharing\API\Server2Server',
  122. 'preLoginNameUsedAsUserName',
  123. '\OCA\User_LDAP\Helper',
  124. 'loginName2UserName'
  125. );
  126. }
  127. private function registerBackendDependents(IAppContainer $appContainer, IEventDispatcher $dispatcher): void {
  128. $dispatcher->addListener(
  129. 'OCA\\Files_External::loadAdditionalBackends',
  130. function () use ($appContainer): void {
  131. $storagesBackendService = $appContainer->get(BackendService::class);
  132. $storagesBackendService->registerConfigHandler('home', function () use ($appContainer) {
  133. return $appContainer->get(ExtStorageConfigHandler::class);
  134. });
  135. }
  136. );
  137. }
  138. }