Application.php 5.5 KB

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