fix LDAP install for systems without ldap extension (#846)

This commit is contained in:
Kevin Papst
2019-06-11 01:25:49 +02:00
committed by GitHub
parent 0c0e9c2f71
commit 833968a87d
23 changed files with 82 additions and 326 deletions

View File

@@ -26,9 +26,11 @@ before_install:
# - phpenv config-rm xdebug.ini
- composer self-update
- php -i
- php -m
install:
- composer install
- composer require zendframework/zend-ldap
script:
- composer kimai:codestyle

View File

@@ -60,8 +60,7 @@
"symfony/webpack-encore-bundle": "^1.5",
"symfony/yaml": "^4.0",
"twig/extensions": "^1.5",
"white-october/pagerfanta-bundle": "^1.1",
"zendframework/zend-ldap": "^2.10"
"white-october/pagerfanta-bundle": "^1.1"
},
"require-dev": {
"dama/doctrine-test-bundle": "^5.0",
@@ -100,7 +99,8 @@
"symfony/polyfill-php56": "*"
},
"suggest": {
"ext-mbstring": "If ext-mbstring is not available you MUST install symfony/polyfill-mbstring"
"ext-mbstring": "If ext-mbstring is not available you MUST install symfony/polyfill-mbstring",
"zendframework/zend-ldap": "For LDAP authentication with Kimai"
},
"scripts": {
"auto-scripts": {

55
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "ed199b91b3e5eb0b9d2a1eb00ea11cba",
"content-hash": "67746acda4894c88c47e3afffab130e4",
"packages": [
{
"name": "beberlei/DoctrineExtensions",
@@ -8618,59 +8618,6 @@
],
"time": "2018-04-25T15:33:34+00:00"
},
{
"name": "zendframework/zend-ldap",
"version": "2.10.0",
"source": {
"type": "git",
"url": "https://github.com/zendframework/zend-ldap.git",
"reference": "b63c7884a08d3a6bda60ebcf7d6238cf8ad89f49"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zendframework/zend-ldap/zipball/b63c7884a08d3a6bda60ebcf7d6238cf8ad89f49",
"reference": "b63c7884a08d3a6bda60ebcf7d6238cf8ad89f49",
"shasum": ""
},
"require": {
"ext-ldap": "*",
"php": "^5.6 || ^7.0"
},
"require-dev": {
"php-mock/php-mock-phpunit": "^1.1.2 || ^2.1.1",
"phpunit/phpunit": "^5.7.27 || ^6.5.8 || ^7.1.2",
"zendframework/zend-coding-standard": "~1.0.0",
"zendframework/zend-config": "^2.5",
"zendframework/zend-eventmanager": "^2.6.3 || ^3.0.1",
"zendframework/zend-stdlib": "^2.7 || ^3.0"
},
"suggest": {
"zendframework/zend-eventmanager": "Zend\\EventManager component"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.10.x-dev",
"dev-develop": "2.11.x-dev"
}
},
"autoload": {
"psr-4": {
"Zend\\Ldap\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"description": "Provides support for LDAP operations including but not limited to binding, searching and modifying entries in an LDAP directory",
"keywords": [
"ZendFramework",
"ldap",
"zf"
],
"time": "2018-07-05T05:05:12+00:00"
},
{
"name": "zircote/swagger-php",
"version": "2.0.14",

View File

@@ -5,7 +5,7 @@ security:
providers:
chain_provider:
chain:
providers: [fos_userbundle, kimai_ldap]
providers: [fos_userbundle]
kimai_ldap:
id: App\Ldap\LdapUserProvider
fos_userbundle:
@@ -31,9 +31,6 @@ security:
lifetime: 604800
path: /
# activate the LdapAuthenticationProvider
kimai_ldap: ~
# activate all configured user provider
provider: chain_provider

View File

@@ -132,9 +132,6 @@ services:
# LDAP
# ================================================================================
Zend\Ldap\Ldap:
class: App\Ldap\ZendLdap
kimai_ldap.security.authentication.provider:
class: App\Ldap\LdapAuthenticationProvider
arguments: ['@App\Security\UserChecker', '', '', '', '@App\Configuration\LdapConfiguration', '%security.authentication.hide_user_not_found%']

View File

@@ -18,3 +18,4 @@ parameters:
- '#Access to an undefined property Faker\\Generator::\$bs.#'
excludes_analyse:
- %rootDir%/../../../src/Command/KimaiImporterCommand.php
- %rootDir%/../../../src/Ldap/LdapDriver.php

View File

@@ -21,11 +21,6 @@ class LdapConfiguration
$this->settings = $settings;
}
public function isActivated(): bool
{
return (bool) $this->settings['active'];
}
public function getRoleParameters(): array
{
return (array) $this->settings['role'];

View File

@@ -494,7 +494,6 @@ class Configuration implements ConfigurationInterface
$node
->addDefaultsIfNotSet()
->children()
->booleanNode('active')->defaultFalse()->end()
->arrayNode('connection')
->addDefaultsIfNotSet()
->children()
@@ -591,19 +590,13 @@ class Configuration implements ConfigurationInterface
->end()
->validate()
->ifTrue(static function ($v) {
return $v['active'] && !extension_loaded('ldap');
return null !== $v['connection']['host'] && !extension_loaded('ldap');
})
->thenInvalid('LDAP is activated, but the LDAP PHP extension is not loaded.')
->end()
->validate()
->ifTrue(static function ($v) {
return $v['active'] && empty($v['connection']['host']);
})
->thenInvalid('The "ldap.connection.host" config must be set if LDAP is activated.')
->end()
->validate()
->ifTrue(static function ($v) {
return $v['active'] && empty($v['user']['baseDn']);
return null !== $v['connection']['host'] && empty($v['user']['baseDn']);
})
->thenInvalid('The "ldap.user.baseDn" config must be set if LDAP is activated.')
->end()

View File

@@ -113,7 +113,27 @@ class Kernel extends BaseKernel
$container->setParameter('container.autowiring.strict_mode', true);
$container->setParameter('container.dumper.inline_class_loader', true);
$confDir = $this->getProjectDir() . '/config';
$loader->load($confDir . '/packages/*' . self::CONFIG_EXTS, 'glob');
// if you want to prepend any config, you can do it here
$loader->load($confDir . '/packages/local_before' . self::CONFIG_EXTS, 'glob');
// using this one instead of $loader->load($confDir . '/packages/*' . self::CONFIG_EXTS, 'glob');
// to get rid of the local.yaml from the list, we load it afterwards explicit
$finder = (new Finder())
->files()
->in([$confDir . '/packages/'])
->name('*' . self::CONFIG_EXTS)
->notName('local*' . self::CONFIG_EXTS)
->depth('== 0')
->sortByName()
->followLinks()
;
/** @var SplFileInfo $file */
foreach ($finder as $file) {
$loader->load($file->getPathname());
}
if (is_dir($confDir . '/packages/' . $this->environment)) {
$loader->load($confDir . '/packages/' . $this->environment . '/**/*' . self::CONFIG_EXTS, 'glob');
}

View File

@@ -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();

View File

@@ -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;

View File

@@ -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';
}

View File

@@ -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());
}
}

View File

@@ -729,9 +729,6 @@
"zendframework/zend-eventmanager": {
"version": "3.2.0"
},
"zendframework/zend-ldap": {
"version": "2.10.0"
},
"zircote/swagger-php": {
"version": "2.0.13"
}

View File

@@ -25,7 +25,6 @@ class LdapConfigurationTest extends TestCase
protected function getDefaultSettings()
{
return [
'active' => true,
'connection' => [
'host' => '1.2.3.4',
],
@@ -41,12 +40,8 @@ class LdapConfigurationTest extends TestCase
public function testMapping()
{
$sut = $this->getSut($this->getDefaultSettings());
$this->assertTrue($sut->isActivated());
$this->assertEquals(['foo' => 'bar'], $sut->getUserParameters());
$this->assertEquals(['bar' => 'foo'], $sut->getRoleParameters());
$this->assertEquals(['host' => '1.2.3.4'], $sut->getConnectionParameters());
$sut = $this->getSut(['active' => false]);
$this->assertFalse($sut->isActivated());
}
}

View File

@@ -122,7 +122,6 @@ class AppExtensionTest extends TestCase
'kimai.timesheet.rates' => [],
'kimai.timesheet.rounding' => [],
'kimai.ldap' => [
'active' => false,
'user' => [
'baseDn' => null,
'filter' => '',
@@ -230,7 +229,6 @@ class AppExtensionTest extends TestCase
{
$minConfig = $this->getMinConfig();
$minConfig['kimai']['ldap'] = [
'active' => true,
'connection' => [
'host' => '9.9.9.9',
'baseDn' => 'lkhiuzhkj',
@@ -257,7 +255,6 @@ class AppExtensionTest extends TestCase
{
$minConfig = $this->getMinConfig();
$minConfig['kimai']['ldap'] = [
'active' => true,
'connection' => [
'host' => '9.9.9.9',
],
@@ -281,7 +278,6 @@ class AppExtensionTest extends TestCase
{
$minConfig = $this->getMinConfig();
$minConfig['kimai']['ldap'] = [
'active' => true,
'connection' => [
'host' => '9.9.9.9',
'baseDn' => '7658765',

View File

@@ -69,7 +69,6 @@ class ConfigurationTest extends TestCase
{
$config = $this->getMinConfig();
$config['ldap'] = [
'active' => true,
'connection' => [
'host' => 'foo'
],
@@ -78,22 +77,6 @@ class ConfigurationTest extends TestCase
$this->assertConfig($config, []);
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage Invalid configuration for path "kimai.ldap": The "ldap.connection.host" config must be set if LDAP is activated.
*/
public function testValidateLdapConfigConnectionHost()
{
$config = $this->getMinConfig();
$config['ldap'] = [
'active' => true,
'connection' => [
],
];
$this->assertConfig($config, []);
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage Invalid configuration for path "kimai.ldap.connection": The ldap.connection.useSsl and ldap.connection.useStartTls options are mutually exclusive.
@@ -102,7 +85,6 @@ class ConfigurationTest extends TestCase
{
$config = $this->getMinConfig();
$config['ldap'] = [
'active' => false,
'connection' => [
'useSsl' => true,
'useStartTls' => true,
@@ -120,7 +102,6 @@ class ConfigurationTest extends TestCase
{
$config = $this->getMinConfig();
$config['ldap'] = [
'active' => true,
'user' => [
'filter' => '(sdfsdfsdf)(uid=%s)',
],
@@ -137,7 +118,6 @@ class ConfigurationTest extends TestCase
{
$config = $this->getMinConfig();
$config['ldap'] = [
'active' => true,
'user' => [
'filter' => 's(dfsdfsdf)',
],
@@ -154,7 +134,6 @@ class ConfigurationTest extends TestCase
{
$config = $this->getMinConfig();
$config['ldap'] = [
'active' => true,
'user' => [
'filter' => '(dfsdfsdf))',
],
@@ -171,7 +150,6 @@ class ConfigurationTest extends TestCase
{
$config = $this->getMinConfig();
$config['ldap'] = [
'active' => false,
'connection' => [
'accountFilterFormat' => '(sdfsdfsdf)(uid=xx)',
],
@@ -188,7 +166,6 @@ class ConfigurationTest extends TestCase
{
$config = $this->getMinConfig();
$config['ldap'] = [
'active' => true,
'connection' => [
'accountFilterFormat' => 's(dfsdfsdf)',
],
@@ -205,7 +182,6 @@ class ConfigurationTest extends TestCase
{
$config = $this->getMinConfig();
$config['ldap'] = [
'active' => true,
'connection' => [
'accountFilterFormat' => '(dfsdfsdf))',
],
@@ -218,7 +194,6 @@ class ConfigurationTest extends TestCase
{
$finalizedConfig = $this->getCompiledConfig($this->getMinConfig());
$expected = [
'active' => false,
'user' => [
'baseDn' => '',
'filter' => '',

View File

@@ -24,44 +24,11 @@ use Symfony\Component\Security\Core\User\UserChecker;
*/
class LdapAuthenticationProviderTest extends TestCase
{
public function testDeactivatedSupports()
{
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration(['active' => false]);
$userProvider = new LdapUserProvider($manager, $config);
$providerKey = 'secured_area';
$userChecker = new UserChecker();
$token = new UsernamePasswordToken('foo', 'bar', $providerKey);
$sut = new LdapAuthenticationProvider($userChecker, $providerKey, $userProvider, $manager, $config, false);
$result = $sut->supports($token);
self::assertFalse($result);
}
/**
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
* @expectedExceptionMessage The token is not supported by this authentication provider.
*/
public function testDeactivatedAuthenticate()
{
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration(['active' => false]);
$userProvider = new LdapUserProvider($manager, $config);
$providerKey = 'secured_area';
$userChecker = new UserChecker();
$token = new UsernamePasswordToken('foo', 'bar', $providerKey);
$sut = new LdapAuthenticationProvider($userChecker, $providerKey, $userProvider, $manager, $config, false);
$sut->authenticate($token);
}
public function testSupports()
{
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration(['active' => true]);
$userProvider = new LdapUserProvider($manager, $config);
$config = new LdapConfiguration([]);
$userProvider = new LdapUserProvider($manager);
$providerKey = 'secured_area';
$userChecker = new UserChecker();
@@ -76,11 +43,11 @@ class LdapAuthenticationProviderTest extends TestCase
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
* @expectedExceptionMessage The password in the token is empty. Check `erase_credentials` in your `security.yaml`
*/
public function testAuthenticateWithTokenUserButEnptyPasswordThrowsException()
public function testAuthenticateWithTokenUserButEmptyPasswordThrowsException()
{
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration(['active' => true]);
$userProvider = new LdapUserProvider($manager, $config);
$config = new LdapConfiguration([]);
$userProvider = new LdapUserProvider($manager);
$providerKey = 'secured_area';
$userChecker = new UserChecker();
@@ -99,7 +66,7 @@ class LdapAuthenticationProviderTest extends TestCase
{
$user = (new User())->setUsername('foo')->setEnabled(true);
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration(['active' => true]);
$config = new LdapConfiguration([]);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
$providerKey = 'secured_area';
@@ -120,7 +87,7 @@ class LdapAuthenticationProviderTest extends TestCase
$user = (new User())->setUsername('foo')->setEnabled(true);
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['bind'])->getMock();
$manager->expects($this->once())->method('bind')->willReturn(false);
$config = new LdapConfiguration(['active' => true]);
$config = new LdapConfiguration([]);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
$providerKey = 'secured_area';
@@ -141,7 +108,7 @@ class LdapAuthenticationProviderTest extends TestCase
$user = (new User())->setUsername('foo')->setEnabled(true);
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['bind'])->getMock();
$manager->expects($this->once())->method('bind')->willReturn(false);
$config = new LdapConfiguration(['active' => true]);
$config = new LdapConfiguration([]);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->never())->method('loadUserByUsername');
$providerKey = 'secured_area';
@@ -162,7 +129,7 @@ class LdapAuthenticationProviderTest extends TestCase
$manager->expects($this->once())->method('updateUser')->willReturnCallback(function ($updateUser) use ($user) {
self::assertSame($updateUser, $user);
});
$config = new LdapConfiguration(['active' => true]);
$config = new LdapConfiguration([]);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
$providerKey = 'secured_area';
@@ -184,7 +151,7 @@ class LdapAuthenticationProviderTest extends TestCase
$manager->expects($this->once())->method('updateUser')->willReturnCallback(function ($updateUser) use ($user) {
self::assertSame($updateUser, $user);
});
$config = new LdapConfiguration(['active' => true]);
$config = new LdapConfiguration([]);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->never())->method('loadUserByUsername');
$providerKey = 'secured_area';
@@ -204,7 +171,7 @@ class LdapAuthenticationProviderTest extends TestCase
public function testAuthenticateThrowsExceptionOnLdapNotFound()
{
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration(['active' => true]);
$config = new LdapConfiguration([]);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willThrowException(new UsernameNotFoundException('blub foo bar'));
$providerKey = 'secured_area';
@@ -224,7 +191,7 @@ class LdapAuthenticationProviderTest extends TestCase
public function testAuthenticateThrowsExceptionOnLdapDown()
{
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration(['active' => true]);
$config = new LdapConfiguration([]);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willThrowException(new \Exception('server away', 1234));
$providerKey = 'secured_area';

View File

@@ -20,13 +20,21 @@ use Zend\Ldap\Ldap;
*/
class LdapDriverTest extends TestCase
{
protected function setUp()
{
parent::setUp();
if (!class_exists('Zend\Ldap\Ldap')) {
$this->markTestSkipped('LDAP is not installed');
}
}
public function testBindSuccess()
{
$zendLdap = $this->getMockBuilder(Ldap::class)->disableOriginalConstructor()->setMethods(['bind'])->getMock();
$zendLdap->expects($this->once())->method('bind')->willReturnSelf();
$user = new User();
$sut = new LdapDriver($zendLdap);
$sut = new TestLdapDriver($zendLdap);
$result = $sut->bind($user, 'test123');
self::assertTrue($result);
}
@@ -37,7 +45,7 @@ class LdapDriverTest extends TestCase
$zendLdap->expects($this->once())->method('bind')->willThrowException(new LdapException());
$user = new User();
$sut = new LdapDriver($zendLdap);
$sut = new TestLdapDriver($zendLdap);
$result = $sut->bind($user, 'test123');
self::assertFalse($result);
}
@@ -48,7 +56,7 @@ class LdapDriverTest extends TestCase
$zendLdap->expects($this->once())->method('bind');
$zendLdap->expects($this->once())->method('searchEntries')->willReturn([1, 2, 3]);
$sut = new LdapDriver($zendLdap);
$sut = new TestLdapDriver($zendLdap);
$result = $sut->search('', '', []);
self::assertEquals(['count' => 3, 1, 2, 3], $result);
}
@@ -65,7 +73,15 @@ class LdapDriverTest extends TestCase
new LdapException($zendLdap, '', LdapException::LDAP_SERVER_DOWN)
);
$sut = new LdapDriver($zendLdap);
$sut = new TestLdapDriver($zendLdap);
$sut->search('', '', []);
}
}
class TestLdapDriver extends LdapDriver
{
public function __construct(Ldap $ldap)
{
$this->driver = $ldap;
}
}

View File

@@ -23,7 +23,6 @@ class LdapUserHydratorTest extends TestCase
public function testEmptyHydrate()
{
$config = new LdapConfiguration([
'active' => false,
'connection' => [
'host' => '1.1.1.1'
],
@@ -44,7 +43,6 @@ class LdapUserHydratorTest extends TestCase
public function testHydrate()
{
$config = new LdapConfiguration([
'active' => false,
'connection' => [
'host' => '1.1.1.1'
],
@@ -88,7 +86,6 @@ class LdapUserHydratorTest extends TestCase
public function testHydrateUser()
{
$config = new LdapConfiguration([
'active' => false,
'connection' => [
'host' => '1.1.1.1'
],

View File

@@ -9,7 +9,6 @@
namespace App\Tests\Ldap;
use App\Configuration\LdapConfiguration;
use App\Entity\User;
use App\Ldap\LdapManager;
use App\Ldap\LdapUserProvider;
@@ -20,41 +19,6 @@ use PHPUnit\Framework\TestCase;
*/
class LdapUserProviderTest extends TestCase
{
public function testDeactivatedSupportsClass()
{
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration(['active' => false]);
$sut = new LdapUserProvider($manager, $config);
self::assertFalse($sut->supportsClass(User::class));
}
/**
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
* @expectedExceptionMessage LDAP is deactivated, user "test" not searched
*/
public function testDeactivatedLoadUserByUsername()
{
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration(['active' => false]);
$sut = new LdapUserProvider($manager, $config);
$sut->loadUserByUsername('test');
}
/**
* @expectedException \Symfony\Component\Security\Core\Exception\UnsupportedUserException
* @expectedExceptionMessage Instances of "App\Entity\User" are not supported.
*/
public function testDeactivatedRefreshUser()
{
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration(['active' => false]);
$sut = new LdapUserProvider($manager, $config);
$sut->refreshUser(new User());
}
/**
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
* @expectedExceptionMessage User "test" not found
@@ -63,9 +27,8 @@ class LdapUserProviderTest extends TestCase
{
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['findUserByUsername'])->getMock();
$manager->expects($this->once())->method('findUserByUsername')->willReturn(null);
$config = new LdapConfiguration(['active' => true]);
$sut = new LdapUserProvider($manager, $config);
$sut = new LdapUserProvider($manager);
$sut->loadUserByUsername('test');
}
@@ -76,9 +39,8 @@ class LdapUserProviderTest extends TestCase
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['findUserByUsername'])->getMock();
$manager->expects($this->once())->method('findUserByUsername')->willReturn($user);
$config = new LdapConfiguration(['active' => true]);
$sut = new LdapUserProvider($manager, $config);
$sut = new LdapUserProvider($manager);
$actual = $sut->loadUserByUsername('test');
self::assertInstanceOf(User::class, $actual);
self::assertSame($user, $actual);
@@ -91,9 +53,8 @@ class LdapUserProviderTest extends TestCase
$user->setPreferenceValue('ldap.dn', 'sdfdsf');
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['updateUser'])->getMock();
$config = new LdapConfiguration(['active' => true]);
$sut = new LdapUserProvider($manager, $config);
$sut = new LdapUserProvider($manager);
$actual = $sut->refreshUser($user);
self::assertInstanceOf(User::class, $actual);

View File

@@ -1,48 +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\Tests\Ldap;
use App\Configuration\LdapConfiguration;
use App\Ldap\ZendLdap;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Ldap\ZendLdap
*/
class ZendLdapTest extends TestCase
{
public function testConstructDeactivated()
{
$config = new LdapConfiguration([
'active' => false,
'connection' => [
'host' => '1.1.1.1'
]
]);
$sut = new ZendLdap($config);
$options = $sut->getOptions();
self::assertNull($options['host']);
}
public function testConstructActivatedPassesOptions()
{
$config = new LdapConfiguration([
'active' => true,
'connection' => [
'host' => '1.1.1.1'
]
]);
$sut = new ZendLdap($config);
$options = $sut->getOptions();
self::assertEquals('1.1.1.1', $options['host']);
}
}

View File

@@ -8,3 +8,5 @@ parameters:
- '#Access to an undefined property Faker\\Generator::\$stateAbbr.#'
- '#Access to an undefined property Faker\\Generator::\$catchPhrase.#'
- '#Access to an undefined property Faker\\Generator::\$bs.#'
excludes_analyse:
- %rootDir%/../../../tests/Ldap/LdapDriverTest.php