Application.php 5.9 KB

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