8000 make LdapBindAuthenticationProvider capable of searching for the quer… · symfony/symfony@a70f80a · GitHub
[go: up one dir, main page]

Skip to content

Commit a70f80a

Browse files
committed
make LdapBindAuthenticationProvider capable of searching for the query string
1 parent b9b6ebd commit a70f80a

File tree

2 files changed

+31
-2lines changed

2 files changed

+31
-2
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/FormLoginLdapFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class FormLoginLdapFactory extends FormLoginFactory
2727
protected function createAuthProvider(ContainerBuilder $container, $id, $config, $userProviderId)
2828
{
2929
$provider = 'security.authentication.provider.ldap_bind.'.$id;
30-
$container
30+
$definition = $container
3131
->setDefinition($provider, new ChildDefinition('security.authentication.provider.ldap_bind'))
3232
->replaceArgument(0, new Reference($userProviderId))
3333
->replaceArgument(1, new Reference('security.user_checker.'.$id))
@@ -36,6 +36,10 @@ protected function createAuthProvider(ContainerBuilder $container, $id, $config,
3636
->replaceArgument(4, $config['dn_string'])
3737
;
3838

39+
if (!empty($config['query_string'])) {
40+
$definition->addMethodCall('setQueryString', array($config['query_string']));
41+
}
42+
3943
return $provider;
4044
}
4145

@@ -47,6 +51,7 @@ public function addConfiguration(NodeDefinition $node)
4751
->children()
4852
->scalarNode('service')->defaultValue('ldap')->end()
4953
->scalarNode('dn_string')->defaultValue('{username}')->end()
54+
->scalarNode('query_string')->end()
5055
->end()
5156
;
5257
}

src/Symfony/Component/Security/Core/Authentication/Provider/LdapBindAuthenticationProvider.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class LdapBindAuthenticationProvider extends UserAuthenticationProvider
3333
private $userProvider;
3434
private $ldap;
3535
private $dnString;
36+
private $queryString;
3637

3738
/**
3839
* Constructor.
@@ -53,6 +54,16 @@ public function __construct(UserProviderInterface $userProvider, UserCheckerInte
5354
$this->dnString = $dnString;
5455
}
5556

57+
/**
58+
* Set a query string to use in order to find a DN for the username.
59+
*
60+
* @param string $queryString
61+
*/
62+
public function setQueryString($queryString)
63+
{
64+
$this->queryString = $queryString;
65+
}
66+
5667
/**
5768
* {@inheritdoc}
5869
*/
@@ -79,7 +90,20 @@ protected function checkAuthentication(UserInterface $user, UsernamePasswordToke
7990

8091
try {
8192
$username = $this->ldap->escape($username, '', LdapInterface::ESCAPE_DN);
82-
$dn = str_replace('{username}', $username, $this->dnString);
93+
94+
if ($this->queryString) {
95+
$query = str_replace('{username}', $username, $this->queryString);
96+
97+
$query = $this->ldap->query($this->dnString, $query);
98+
$result = $query->execute();
99+
if (1 !== $result->count()) {
100+
throw new BadCredentialsException('The presented username is invalid.');
101+
}
102+
103+
$dn = $result[0]->getDn();
104+
} else {
105+
$dn = str_replace('{username}', $username, $this->dnString);
106+
}
83107

84108
$this->ldap->bind($dn, $password);
85109
} catch (ConnectionException $e) {

0 commit comments

Comments
 (0)
0