From a26fe5d6260f2cc186f5baa169b544892a5a82df Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 25 Jan 2021 21:06:07 +0100 Subject: [PATCH] make ILDAPProviderFactory usable when there is no ldap setup Signed-off-by: Robin Appelman --- apps/user_ldap/lib/LDAPProviderFactory.php | 4 ++ lib/private/LDAP/NullLDAPProviderFactory.php | 41 ++++++++++++++++++++ lib/private/Server.php | 15 +++++-- lib/public/LDAP/ILDAPProviderFactory.php | 9 ++++- 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 lib/private/LDAP/NullLDAPProviderFactory.php diff --git a/apps/user_ldap/lib/LDAPProviderFactory.php b/apps/user_ldap/lib/LDAPProviderFactory.php index a64425afad0e8..d70e45c6a96d5 100644 --- a/apps/user_ldap/lib/LDAPProviderFactory.php +++ b/apps/user_ldap/lib/LDAPProviderFactory.php @@ -41,4 +41,8 @@ public function __construct(IServerContainer $serverContainer) { public function getLDAPProvider(): ILDAPProvider { return $this->serverContainer->get(LDAPProvider::class); } + + public function isAvailable(): bool { + return true; + } } diff --git a/lib/private/LDAP/NullLDAPProviderFactory.php b/lib/private/LDAP/NullLDAPProviderFactory.php new file mode 100644 index 0000000000000..fb52ad2bc574d --- /dev/null +++ b/lib/private/LDAP/NullLDAPProviderFactory.php @@ -0,0 +1,41 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OC\LDAP; + + +use OCP\IServerContainer; +use OCP\LDAP\ILDAPProviderFactory; + +class NullLDAPProviderFactory implements ILDAPProviderFactory { + public function __construct(IServerContainer $serverContainer) { + } + + public function getLDAPProvider() { + throw new \Exception("No LDAP provider is available"); + } + + public function isAvailable(): bool { + return false; + } +} diff --git a/lib/private/Server.php b/lib/private/Server.php index ba954165799eb..111906567cb92 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -103,6 +103,7 @@ use OC\IntegrityCheck\Helpers\AppLocator; use OC\IntegrityCheck\Helpers\EnvironmentHelper; use OC\IntegrityCheck\Helpers\FileAccessHelper; +use OC\LDAP\NullLDAPProviderFactory; use OC\Lock\DBLockingProvider; use OC\Lock\MemcacheLockingProvider; use OC\Lock\NoopLockingProvider; @@ -203,6 +204,8 @@ use OCP\IUserManager; use OCP\IUserSession; use OCP\L10N\IFactory; +use OCP\LDAP\ILDAPProvider; +use OCP\LDAP\ILDAPProviderFactory; use OCP\Lock\ILockingProvider; use OCP\Log\ILogFactory; use OCP\Mail\IMailer; @@ -996,14 +999,20 @@ public function __construct($webRoot, \OC\Config $config) { /** @deprecated 19.0.0 */ $this->registerDeprecatedAlias('Mailer', IMailer::class); - $this->registerService('LDAPProvider', function (ContainerInterface $c) { + /** @deprecated 21.0.0 */ + $this->registerDeprecatedAlias('LDAPProvider', ILDAPProvider::class); + + $this->registerService(ILDAPProviderFactory::class, function (ContainerInterface $c) { $config = $c->get(\OCP\IConfig::class); $factoryClass = $config->getSystemValue('ldapProviderFactory', null); if (is_null($factoryClass)) { - throw new \Exception('ldapProviderFactory not set'); + return new NullLDAPProviderFactory($this); } /** @var \OCP\LDAP\ILDAPProviderFactory $factory */ - $factory = new $factoryClass($this); + return new $factoryClass($this); + }); + $this->registerService(ILDAPProvider::class, function (ContainerInterface $c) { + $factory = $c->get(ILDAPProviderFactory::class); return $factory->getLDAPProvider(); }); $this->registerService(ILockingProvider::class, function (ContainerInterface $c) { diff --git a/lib/public/LDAP/ILDAPProviderFactory.php b/lib/public/LDAP/ILDAPProviderFactory.php index f005cf07885f4..c65a253957525 100644 --- a/lib/public/LDAP/ILDAPProviderFactory.php +++ b/lib/public/LDAP/ILDAPProviderFactory.php @@ -44,7 +44,7 @@ interface ILDAPProviderFactory { * @since 11.0.0 */ public function __construct(IServerContainer $serverContainer); - + /** * creates and returns an instance of the ILDAPProvider * @@ -52,4 +52,11 @@ public function __construct(IServerContainer $serverContainer); * @since 11.0.0 */ public function getLDAPProvider(); + + /** + * Check if an ldap provider is available + * + * @return bool + */ + public function isAvailable(): bool; }