Application.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Roger Szabo <roger.szabo@web.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Roger Szabo <roger.szabo@web.de>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\User_LDAP\AppInfo;
  28. use Closure;
  29. use OCA\Files_External\Service\BackendService;
  30. use OCA\User_LDAP\Controller\RenewPasswordController;
  31. use OCA\User_LDAP\Events\GroupBackendRegistered;
  32. use OCA\User_LDAP\Events\UserBackendRegistered;
  33. use OCA\User_LDAP\FilesystemHelper;
  34. use OCA\User_LDAP\Group_Proxy;
  35. use OCA\User_LDAP\GroupPluginManager;
  36. use OCA\User_LDAP\Handler\ExtStorageConfigHandler;
  37. use OCA\User_LDAP\Helper;
  38. use OCA\User_LDAP\ILDAPWrapper;
  39. use OCA\User_LDAP\LDAP;
  40. use OCA\User_LDAP\LoginListener;
  41. use OCA\User_LDAP\Notification\Notifier;
  42. use OCA\User_LDAP\SetupChecks\LdapInvalidUuids;
  43. use OCA\User_LDAP\User\Manager;
  44. use OCA\User_LDAP\User_Proxy;
  45. use OCA\User_LDAP\UserPluginManager;
  46. use OCP\AppFramework\App;
  47. use OCP\AppFramework\Bootstrap\IBootContext;
  48. use OCP\AppFramework\Bootstrap\IBootstrap;
  49. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  50. use OCP\AppFramework\IAppContainer;
  51. use OCP\EventDispatcher\IEventDispatcher;
  52. use OCP\IAvatarManager;
  53. use OCP\IConfig;
  54. use OCP\IGroupManager;
  55. use OCP\IL10N;
  56. use OCP\Image;
  57. use OCP\IServerContainer;
  58. use OCP\IUserManager;
  59. use OCP\Notification\IManager as INotificationManager;
  60. use OCP\Share\IManager as IShareManager;
  61. use OCP\User\Events\PostLoginEvent;
  62. use Psr\Container\ContainerInterface;
  63. use Psr\Log\LoggerInterface;
  64. class Application extends App implements IBootstrap {
  65. public function __construct() {
  66. parent::__construct('user_ldap');
  67. $container = $this->getContainer();
  68. /**
  69. * Controller
  70. */
  71. $container->registerService('RenewPasswordController', function (IAppContainer $appContainer) {
  72. /** @var IServerContainer $server */
  73. $server = $appContainer->get(IServerContainer::class);
  74. return new RenewPasswordController(
  75. $appContainer->get('AppName'),
  76. $server->getRequest(),
  77. $appContainer->get('UserManager'),
  78. $server->getConfig(),
  79. $appContainer->get(IL10N::class),
  80. $appContainer->get('Session'),
  81. $server->getURLGenerator()
  82. );
  83. });
  84. $container->registerService(ILDAPWrapper::class, function (IAppContainer $appContainer) {
  85. /** @var IServerContainer $server */
  86. $server = $appContainer->get(IServerContainer::class);
  87. return new LDAP(
  88. $server->getConfig()->getSystemValueString('ldap_log_file')
  89. );
  90. });
  91. }
  92. public function register(IRegistrationContext $context): void {
  93. $context->registerNotifierService(Notifier::class);
  94. $context->registerService(
  95. Manager::class,
  96. function (ContainerInterface $c) {
  97. return new Manager(
  98. $c->get(IConfig::class),
  99. $c->get(FilesystemHelper::class),
  100. $c->get(LoggerInterface::class),
  101. $c->get(IAvatarManager::class),
  102. $c->get(Image::class),
  103. $c->get(IUserManager::class),
  104. $c->get(INotificationManager::class),
  105. $c->get(IShareManager::class),
  106. );
  107. },
  108. // the instance is specific to a lazy bound Access instance, thus cannot be shared.
  109. false
  110. );
  111. $context->registerEventListener(PostLoginEvent::class, LoginListener::class);
  112. $context->registerSetupCheck(LdapInvalidUuids::class);
  113. }
  114. public function boot(IBootContext $context): void {
  115. $context->injectFn(function (
  116. INotificationManager $notificationManager,
  117. IAppContainer $appContainer,
  118. IEventDispatcher $dispatcher,
  119. IGroupManager $groupManager,
  120. User_Proxy $userBackend,
  121. Group_Proxy $groupBackend,
  122. Helper $helper
  123. ) {
  124. $configPrefixes = $helper->getServerConfigurationPrefixes(true);
  125. if (count($configPrefixes) > 0) {
  126. $userPluginManager = $appContainer->get(UserPluginManager::class);
  127. $groupPluginManager = $appContainer->get(GroupPluginManager::class);
  128. \OC_User::useBackend($userBackend);
  129. $groupManager->addBackend($groupBackend);
  130. $userBackendRegisteredEvent = new UserBackendRegistered($userBackend, $userPluginManager);
  131. $dispatcher->dispatch('OCA\\User_LDAP\\User\\User::postLDAPBackendAdded', $userBackendRegisteredEvent);
  132. $dispatcher->dispatchTyped($userBackendRegisteredEvent);
  133. $groupBackendRegisteredEvent = new GroupBackendRegistered($groupBackend, $groupPluginManager);
  134. $dispatcher->dispatchTyped($groupBackendRegisteredEvent);
  135. }
  136. });
  137. $context->injectFn(Closure::fromCallable([$this, 'registerBackendDependents']));
  138. \OCP\Util::connectHook(
  139. '\OCA\Files_Sharing\API\Server2Server',
  140. 'preLoginNameUsedAsUserName',
  141. '\OCA\User_LDAP\Helper',
  142. 'loginName2UserName'
  143. );
  144. }
  145. private function registerBackendDependents(IAppContainer $appContainer, IEventDispatcher $dispatcher): void {
  146. $dispatcher->addListener(
  147. 'OCA\\Files_External::loadAdditionalBackends',
  148. function () use ($appContainer) {
  149. $storagesBackendService = $appContainer->get(BackendService::class);
  150. $storagesBackendService->registerConfigHandler('home', function () use ($appContainer) {
  151. return $appContainer->get(ExtStorageConfigHandler::class);
  152. });
  153. }
  154. );
  155. }
  156. }