fix LDAP install for systems without ldap extension (#846)
This commit is contained in:
@@ -12,7 +12,6 @@ namespace App\Ldap;
|
||||
use App\Configuration\LdapConfiguration;
|
||||
use App\Entity\User;
|
||||
use Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
||||
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
|
||||
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
|
||||
@@ -49,15 +48,6 @@ class LdapAuthenticationProvider extends UserAuthenticationProvider
|
||||
$this->userProvider = $userProvider;
|
||||
}
|
||||
|
||||
public function supports(TokenInterface $token)
|
||||
{
|
||||
if (!$this->config->isActivated()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parent::supports($token);
|
||||
}
|
||||
|
||||
protected function retrieveUser($username, UsernamePasswordToken $token)
|
||||
{
|
||||
$user = $token->getUser();
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
namespace App\Ldap;
|
||||
|
||||
use App\Configuration\LdapConfiguration;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Zend\Ldap\Exception\LdapException;
|
||||
@@ -22,20 +23,22 @@ class LdapDriver
|
||||
/**
|
||||
* @var Ldap
|
||||
*/
|
||||
private $driver;
|
||||
protected $driver;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* @param Ldap $driver Initialized Zend::Ldap Object
|
||||
* @param LoggerInterface $logger optional logger for write debug messages
|
||||
*/
|
||||
public function __construct(Ldap $driver, LoggerInterface $logger = null)
|
||||
public function __construct(LdapConfiguration $config, LoggerInterface $logger = null)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
if (!class_exists('Zend\Ldap\Ldap')) {
|
||||
throw new \Exception(
|
||||
'Zend\Ldap\Ldap is missing, install it with "composer require zendframework/zend-ldap" ' .
|
||||
'or deactivate LDAP, see https://www.kimai.org/documentation/ldap.html'
|
||||
);
|
||||
}
|
||||
$this->driver = new Ldap($config->getConnectionParameters());
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
@@ -64,7 +67,7 @@ class LdapDriver
|
||||
// searchEntries don't return 'count' key as specified by php native function ldap_get_entries()
|
||||
$entries['count'] = count($entries);
|
||||
} catch (LdapException $exception) {
|
||||
$this->zendExceptionHandler($exception);
|
||||
$this->ldapExceptionHandler($exception);
|
||||
|
||||
throw new LdapDriverException('An error occurred with the search operation.');
|
||||
}
|
||||
@@ -85,16 +88,13 @@ class LdapDriver
|
||||
|
||||
return $bind instanceof Ldap;
|
||||
} catch (LdapException $exception) {
|
||||
$this->zendExceptionHandler($exception, $password);
|
||||
$this->ldapExceptionHandler($exception, $password);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Treat a Zend Ldap Exception.
|
||||
*/
|
||||
protected function zendExceptionHandler(LdapException $exception, string $password = null): void
|
||||
protected function ldapExceptionHandler(LdapException $exception, string $password = null): void
|
||||
{
|
||||
$sanitizedException = null !== $password ? new SanitizingException($exception, $password) : $exception;
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
namespace App\Ldap;
|
||||
|
||||
use App\Configuration\LdapConfiguration;
|
||||
use App\Entity\User;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
|
||||
@@ -37,24 +36,14 @@ class LdapUserProvider implements UserProviderInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
public function __construct(LdapManager $ldapManager, LdapConfiguration $config, LoggerInterface $logger = null)
|
||||
public function __construct(LdapManager $ldapManager, LoggerInterface $logger = null)
|
||||
{
|
||||
$this->ldapManager = $ldapManager;
|
||||
$this->logger = $logger;
|
||||
$this->activated = $config->isActivated();
|
||||
}
|
||||
|
||||
public function loadUserByUsername($username)
|
||||
{
|
||||
// this method is called at least for unknown user, no matter what supportsClass() returns,
|
||||
// so we have to check if LDAP is activated here as well
|
||||
if (!$this->activated) {
|
||||
$ex = new UsernameNotFoundException(sprintf('LDAP is deactivated, user "%s" not searched', $username));
|
||||
$ex->setUsername($username);
|
||||
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
$user = $this->ldapManager->findUserByUsername($username);
|
||||
|
||||
if (empty($user)) {
|
||||
@@ -99,10 +88,6 @@ class LdapUserProvider implements UserProviderInterface
|
||||
|
||||
public function supportsClass($class)
|
||||
{
|
||||
if (!$this->activated) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $class === User::class || $class === 'App\Entity\User';
|
||||
}
|
||||
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Ldap;
|
||||
|
||||
use App\Configuration\LdapConfiguration;
|
||||
use Zend\Ldap\Ldap;
|
||||
|
||||
/**
|
||||
* Overwritten to prevent errors in case:
|
||||
* LDAP is deactivated and LDAP extension is not loaded
|
||||
*/
|
||||
class ZendLdap extends Ldap
|
||||
{
|
||||
public function __construct(LdapConfiguration $config)
|
||||
{
|
||||
if (!$config->isActivated()) {
|
||||
return;
|
||||
}
|
||||
|
||||
parent::__construct($config->getConnectionParameters());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user