diff --git a/src/Symfony/Component/Security/CHANGELOG.md b/src/Symfony/Component/Security/CHANGELOG.md index 51cb39d89e..618f51f8d2 100644 --- a/src/Symfony/Component/Security/CHANGELOG.md +++ b/src/Symfony/Component/Security/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 5.0.0 ----- + * The `LdapUserProvider` class has been removed, use `Symfony\Component\Ldap\Security\LdapUserProvider` instead. * The `FirewallMapInterface::getListeners()` method must return an array of 3 elements. * Removed the `ContextListener::setLogoutOnUserChange()` method. * Removed the `ListenerInterface`, turn your listeners into callables instead. diff --git a/src/Symfony/Component/Security/Core/Tests/User/LdapUserProviderTest.php b/src/Symfony/Component/Security/Core/Tests/User/LdapUserProviderTest.php deleted file mode 100644 index 90f74584b6..0000000000 --- a/src/Symfony/Component/Security/Core/Tests/User/LdapUserProviderTest.php +++ /dev/null @@ -1,354 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Security\Core\Tests\User; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Ldap\Adapter\CollectionInterface; -use Symfony\Component\Ldap\Adapter\QueryInterface; -use Symfony\Component\Ldap\Entry; -use Symfony\Component\Ldap\Exception\ConnectionException; -use Symfony\Component\Ldap\LdapInterface; -use Symfony\Component\Security\Core\User\LdapUserProvider; - -/** - * @group legacy - * @requires extension ldap - */ -class LdapUserProviderTest extends TestCase -{ - public function testLoadUserByUsernameFailsIfCantConnectToLdap() - { - $this->expectException('Symfony\Component\Security\Core\Exception\UsernameNotFoundException'); - $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); - $ldap - ->expects($this->once()) - ->method('bind') - ->willThrowException(new ConnectionException()) - ; - - $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com'); - $provider->loadUserByUsername('foo'); - } - - public function testLoadUserByUsernameFailsIfNoLdapEntries() - { - $this->expectException('Symfony\Component\Security\Core\Exception\UsernameNotFoundException'); - $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); - $query = $this->getMockBuilder(QueryInterface::class)->getMock(); - $query - ->expects($this->once()) - ->method('execute') - ->willReturn($result) - ; - $result - ->expects($this->once()) - ->method('count') - ->willReturn(0) - ; - $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); - $ldap - ->expects($this->once()) - ->method('escape') - ->willReturn('foo') - ; - $ldap - ->expects($this->once()) - ->method('query') - ->willReturn($query) - ; - - $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com'); - $provider->loadUserByUsername('foo'); - } - - public function testLoadUserByUsernameFailsIfMoreThanOneLdapEntry() - { - $this->expectException('Symfony\Component\Security\Core\Exception\UsernameNotFoundException'); - $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); - $query = $this->getMockBuilder(QueryInterface::class)->getMock(); - $query - ->expects($this->once()) - ->method('execute') - ->willReturn($result) - ; - $result - ->expects($this->once()) - ->method('count') - ->willReturn(2) - ; - $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); - $ldap - ->expects($this->once()) - ->method('escape') - ->willReturn('foo') - ; - $ldap - ->expects($this->once()) - ->method('query') - ->willReturn($query) - ; - - $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com'); - $provider->loadUserByUsername('foo'); - } - - public function testLoadUserByUsernameFailsIfMoreThanOneLdapPasswordsInEntry() - { - $this->expectException('Symfony\Component\Security\Core\Exception\InvalidArgumentException'); - $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); - $query = $this->getMockBuilder(QueryInterface::class)->getMock(); - $query - ->expects($this->once()) - ->method('execute') - ->willReturn($result) - ; - $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); - $result - ->expects($this->once()) - ->method('offsetGet') - ->with(0) - ->willReturn(new Entry('foo', [ - 'sAMAccountName' => ['foo'], - 'userpassword' => ['bar', 'baz'], - ] - )) - ; - $result - ->expects($this->once()) - ->method('count') - ->willReturn(1) - ; - $ldap - ->expects($this->once()) - ->method('escape') - ->willReturn('foo') - ; - $ldap - ->expects($this->once()) - ->method('query') - ->willReturn($query) - ; - - $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, [], 'sAMAccountName', '({uid_key}={username})', 'userpassword'); - $this->assertInstanceOf( - 'Symfony\Component\Security\Core\User\User', - $provider->loadUserByUsername('foo') - ); - } - - public function testLoadUserByUsernameShouldNotFailIfEntryHasNoUidKeyAttribute() - { - $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); - $query = $this->getMockBuilder(QueryInterface::class)->getMock(); - $query - ->expects($this->once()) - ->method('execute') - ->willReturn($result) - ; - $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); - $result - ->expects($this->once()) - ->method('offsetGet') - ->with(0) - ->willReturn(new Entry('foo', [])) - ; - $result - ->expects($this->once()) - ->method('count') - ->willReturn(1) - ; - $ldap - ->expects($this->once()) - ->method('escape') - ->willReturn('foo') - ; - $ldap - ->expects($this->once()) - ->method('query') - ->willReturn($query) - ; - - $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, [], 'sAMAccountName', '({uid_key}={username})'); - $this->assertInstanceOf( - 'Symfony\Component\Security\Core\User\User', - $provider->loadUserByUsername('foo') - ); - } - - public function testLoadUserByUsernameFailsIfEntryHasNoPasswordAttribute() - { - $this->expectException('Symfony\Component\Security\Core\Exception\InvalidArgumentException'); - $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); - $query = $this->getMockBuilder(QueryInterface::class)->getMock(); - $query - ->expects($this->once()) - ->method('execute') - ->willReturn($result) - ; - $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); - $result - ->expects($this->once()) - ->method('offsetGet') - ->with(0) - ->willReturn(new Entry('foo', [ - 'sAMAccountName' => ['foo'], - ] - )) - ; - $result - ->expects($this->once()) - ->method('count') - ->willReturn(1) - ; - $ldap - ->expects($this->once()) - ->method('escape') - ->willReturn('foo') - ; - $ldap - ->expects($this->once()) - ->method('query') - ->willReturn($query) - ; - - $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, [], 'sAMAccountName', '({uid_key}={username})', 'userpassword'); - $this->assertInstanceOf( - 'Symfony\Component\Security\Core\User\User', - $provider->loadUserByUsername('foo') - ); - } - - public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttribute() - { - $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); - $query = $this->getMockBuilder(QueryInterface::class)->getMock(); - $query - ->expects($this->once()) - ->method('execute') - ->willReturn($result) - ; - $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); - $result - ->expects($this->once()) - ->method('offsetGet') - ->with(0) - ->willReturn(new Entry('foo', [ - 'sAMAccountName' => ['foo'], - ] - )) - ; - $result - ->expects($this->once()) - ->method('count') - ->willReturn(1) - ; - $ldap - ->expects($this->once()) - ->method('escape') - ->willReturn('foo') - ; - $ldap - ->expects($this->once()) - ->method('query') - ->willReturn($query) - ; - - $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com'); - $this->assertInstanceOf( - 'Symfony\Component\Security\Core\User\User', - $provider->loadUserByUsername('foo') - ); - } - - public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttributeAndWrongCase() - { - $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); - $query = $this->getMockBuilder(QueryInterface::class)->getMock(); - $query - ->expects($this->once()) - ->method('execute') - ->willReturn($result) - ; - $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); - $result - ->expects($this->once()) - ->method('offsetGet') - ->with(0) - ->willReturn(new Entry('foo', [ - 'sAMAccountName' => ['foo'], - ] - )) - ; - $result - ->expects($this->once()) - ->method('count') - ->willReturn(1) - ; - $ldap - ->expects($this->once()) - ->method('escape') - ->willReturn('Foo') - ; - $ldap - ->expects($this->once()) - ->method('query') - ->willReturn($query) - ; - - $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com'); - $this->assertSame('foo', $provider->loadUserByUsername('Foo')->getUsername()); - } - - public function testLoadUserByUsernameIsSuccessfulWithPasswordAttribute() - { - $result = $this->getMockBuilder(CollectionInterface::class)->getMock(); - $query = $this->getMockBuilder(QueryInterface::class)->getMock(); - $query - ->expects($this->once()) - ->method('execute') - ->willReturn($result) - ; - $ldap = $this->getMockBuilder(LdapInterface::class)->getMock(); - $result - ->expects($this->once()) - ->method('offsetGet') - ->with(0) - ->willReturn(new Entry('foo', [ - 'sAMAccountName' => ['foo'], - 'userpassword' => ['bar'], - 'email' => ['elsa@symfony.com'], - ] - )) - ; - $result - ->expects($this->once()) - ->method('count') - ->willReturn(1) - ; - $ldap - ->expects($this->once()) - ->method('escape') - ->willReturn('foo') - ; - $ldap - ->expects($this->once()) - ->method('query') - ->willReturn($query) - ; - - $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, [], 'sAMAccountName', '({uid_key}={username})', 'userpassword', ['email']); - $this->assertInstanceOf( - 'Symfony\Component\Security\Core\User\User', - $provider->loadUserByUsername('foo') - ); - } -} diff --git a/src/Symfony/Component/Security/Core/User/LdapUserProvider.php b/src/Symfony/Component/Security/Core/User/LdapUserProvider.php deleted file mode 100644 index 1eea0f9304..0000000000 --- a/src/Symfony/Component/Security/Core/User/LdapUserProvider.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Security\Core\User; - -@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.4, use "%s" instead.', LdapUserProvider::class, BaseLdapUserProvider::class), E_USER_DEPRECATED); - -use Symfony\Component\Ldap\Entry; -use Symfony\Component\Ldap\Security\LdapUserProvider as BaseLdapUserProvider; -use Symfony\Component\Security\Core\Exception\UnsupportedUserException; - -/** - * LdapUserProvider is a simple user provider on top of ldap. - * - * @author Grégoire Pineau - * @author Charles Sarrazin - * - * @deprecated since Symfony 4.4, use "Symfony\Component\Ldap\Security\LdapUserProvider" instead - */ -class LdapUserProvider extends BaseLdapUserProvider -{ - /** - * {@inheritdoc} - */ - public function refreshUser(UserInterface $user) - { - if (!$user instanceof User) { - throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user))); - } - - return new User($user->getUsername(), null, $user->getRoles()); - } - - /** - * {@inheritdoc} - */ - public function supportsClass(string $class) - { - return 'Symfony\Component\Security\Core\User\User' === $class; - } - - /** - * Loads a user from an LDAP entry. - * - * @return User - */ - protected function loadUser(string $username, Entry $entry) - { - $ldapUser = parent::loadUser($username, $entry); - - return new User($ldapUser->getUsername(), $ldapUser->getPassword(), $ldapUser->getRoles(), true, true, true, true, $ldapUser->getExtraFields()); - } -}