1
0

Application.php 5.6 KB

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