diff --git a/.travis.yml b/.travis.yml index 81077278..9be7e1e0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/composer.json b/composer.json index 06a2849e..827e8691 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/composer.lock b/composer.lock index c6b98f86..53fa4cfd 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 09f80d42..9ffea123 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -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 diff --git a/config/services.yaml b/config/services.yaml index 571f4eab..4d79a39d 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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%'] diff --git a/phpstan.neon b/phpstan.neon index fdb6dab8..081540a4 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -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 diff --git a/src/Configuration/LdapConfiguration.php b/src/Configuration/LdapConfiguration.php index 124e88f8..b546aacf 100644 --- a/src/Configuration/LdapConfiguration.php +++ b/src/Configuration/LdapConfiguration.php @@ -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']; diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index fe193d16..4d6ef499 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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() diff --git a/src/Kernel.php b/src/Kernel.php index 7c73e9e5..aef2d993 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -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'); } diff --git a/src/Ldap/LdapAuthenticationProvider.php b/src/Ldap/LdapAuthenticationProvider.php index 9449bd9c..c67d4554 100644 --- a/src/Ldap/LdapAuthenticationProvider.php +++ b/src/Ldap/LdapAuthenticationProvider.php @@ -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(); diff --git a/src/Ldap/LdapDriver.php b/src/Ldap/LdapDriver.php index 46e206fc..d3f164b8 100644 --- a/src/Ldap/LdapDriver.php +++ b/src/Ldap/LdapDriver.php @@ -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; diff --git a/src/Ldap/LdapUserProvider.php b/src/Ldap/LdapUserProvider.php index 7aaf94a2..debc3f54 100644 --- a/src/Ldap/LdapUserProvider.php +++ b/src/Ldap/LdapUserProvider.php @@ -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'; } diff --git a/src/Ldap/ZendLdap.php b/src/Ldap/ZendLdap.php deleted file mode 100644 index 1652ec27..00000000 --- a/src/Ldap/ZendLdap.php +++ /dev/null @@ -1,29 +0,0 @@ -isActivated()) { - return; - } - - parent::__construct($config->getConnectionParameters()); - } -} diff --git a/symfony.lock b/symfony.lock index c3c49818..cc0f81b4 100644 --- a/symfony.lock +++ b/symfony.lock @@ -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" } diff --git a/tests/Configuration/LdapConfigurationTest.php b/tests/Configuration/LdapConfigurationTest.php index f127b4a7..2065824a 100644 --- a/tests/Configuration/LdapConfigurationTest.php +++ b/tests/Configuration/LdapConfigurationTest.php @@ -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()); } } diff --git a/tests/DependencyInjection/AppExtensionTest.php b/tests/DependencyInjection/AppExtensionTest.php index a825f978..6548a57c 100644 --- a/tests/DependencyInjection/AppExtensionTest.php +++ b/tests/DependencyInjection/AppExtensionTest.php @@ -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', diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index b764f63c..2b3cf1c6 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -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' => '', diff --git a/tests/Ldap/LdapAuthenticationProviderTest.php b/tests/Ldap/LdapAuthenticationProviderTest.php index 42b4eea7..db04ffc0 100644 --- a/tests/Ldap/LdapAuthenticationProviderTest.php +++ b/tests/Ldap/LdapAuthenticationProviderTest.php @@ -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'; diff --git a/tests/Ldap/LdapDriverTest.php b/tests/Ldap/LdapDriverTest.php index b7356e37..e5e85171 100644 --- a/tests/Ldap/LdapDriverTest.php +++ b/tests/Ldap/LdapDriverTest.php @@ -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; + } +} diff --git a/tests/Ldap/LdapUserHydratorTest.php b/tests/Ldap/LdapUserHydratorTest.php index aae7a280..314b2c87 100644 --- a/tests/Ldap/LdapUserHydratorTest.php +++ b/tests/Ldap/LdapUserHydratorTest.php @@ -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' ], diff --git a/tests/Ldap/LdapUserProviderTest.php b/tests/Ldap/LdapUserProviderTest.php index fcf310bb..a7de505c 100644 --- a/tests/Ldap/LdapUserProviderTest.php +++ b/tests/Ldap/LdapUserProviderTest.php @@ -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); diff --git a/tests/Ldap/ZendLdapTest.php b/tests/Ldap/ZendLdapTest.php deleted file mode 100644 index b4b31ea4..00000000 --- a/tests/Ldap/ZendLdapTest.php +++ /dev/null @@ -1,48 +0,0 @@ - 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']); - } -} diff --git a/tests/phpstan.neon b/tests/phpstan.neon index 3a16544b..88228ba1 100644 --- a/tests/phpstan.neon +++ b/tests/phpstan.neon @@ -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