8000 [Ldap] load users with the good username case · symfony/symfony@6641b79 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6641b79

Browse files
quentinus95fabpot
authored andcommitted
[Ldap] load users with the good username case
1 parent d228d34 commit 6641b79

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/Symfony/Component/Security/Core/Tests/User/LdapUserProviderTest.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function testLoadUserByUsernameFailsIfMoreThanOneLdapPasswordsInEntry()
119119
;
120120
$ldap = $this->getMockBuilder(LdapInterface::class)->getMock();
121121
$result
122-
->expects($this->once())
122+
->expects($this->exactly(2))
123123
->method('offsetGet')
124124
->with(0)
125125
->will($this->returnValue(new Entry('foo', array(
@@ -165,7 +165,7 @@ public function testLoadUserByUsernameFailsIfEntryHasNoPasswordAttribute()
165165
;
166166
$ldap = $this->getMockBuilder(LdapInterface::class)->getMock();
167167
$result
168-
->expects($this->once())
168+
->expects($this->exactly(2))
169169
->method('offsetGet')
170170
->with(0)
171171
->will($this->returnValue(new Entry('foo', array(
@@ -207,7 +207,7 @@ public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttribute()
207207
;
208208
$ldap = $this->getMockBuilder(LdapInterface::class)->getMock();
209209
$result
210-
->expects($this->once())
210+
->expects($this->exactly(2))
211211
->method('offsetGet')
212212
->with(0)
213213
->will($this->returnValue(new Entry('foo', array(
@@ -238,7 +238,7 @@ public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttribute()
238238
);
239239
}
240240

241-
public function testLoadUserByUsernameIsSuccessfulWithPasswordAttribute()
241+
public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttributeAndWrongCase()
242242
{
243243
$result = $this->getMockBuilder(CollectionInterface::class)->getMock();
244244
$query = $this->getMockBuilder(QueryInterface::class)->getMock();
@@ -249,7 +249,46 @@ public function testLoadUserByUsernameIsSuccessfulWithPasswordAttribute()
249249
;
250250
$ldap = $this->getMockBuilder(LdapInterface::class)->getMock();
251251
$result
252+
->expects($this->exactly(2))
253+
->method('offsetGet')
254+
->with(0)
255+
->will($this->returnValue(new Entry('foo', array(
256+
'sAMAccountName' => array('foo'),
257+
)
258+
)))
259+
;
260+
$result
261+
->expects($this->once())
262+
->method('count')
263+
->will($this->returnValue(1))
264+
;
265+
$ldap
266+
->expects($this->once())
267+
->method('escape')
268+
->will($this->returnValue('Foo'))
269+
;
270+
$ldap
271+
->expects($this->once())
272+
->method('query')
273+
->will($this->returnValue($query))
274+
;
275+
276+
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com');
277+
$this->assertSame('foo', $provider->loadUserByUsername('Foo')->getUsername());
278+
}
279+
280+
public function testLoadUserByUsernameIsSuccessfulWithPasswordAttribute()
281+
{
282+
$result = $this->getMock(CollectionInterface::class);
283+
$query = $this->getMock(QueryInterface::class);
284+
$query
252285
->expects($this->once())
286+
->method('execute')
287+
->will($this->returnValue($result))
288+
;
289+
$ldap = $this->getMock(LdapInterface::class);
290+
$result
291+
->expects($this->exactly(2))
253292
->method('offsetGet')
254293
->with(0)
255294
->will($this->returnValue(new Entry('foo', array(

src/Symfony/Component/Security/Core/User/LdapUserProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class LdapUserProvider implements UserProviderInterface
3131
private $searchDn;
3232
private $searchPassword;
3333
private $defaultRoles;
34+
private $uidKey;
3435
private $defaultSearch;
3536
private $passwordAttribute;
3637

@@ -51,6 +52,7 @@ public function __construct(LdapInterface $ldap, $baseDn, $searchDn = null, $sea
5152
$this->searchDn = $searchDn;
5253
$this->searchPassword = $searchPassword;
5354
$this->defaultRoles = $defaultRoles;
55+
$this->uidKey = $uidKey;
5456
$this->defaultSearch = str_replace('{uid_key}', $uidKey, $filter);
5557
$this->passwordAttribute = $passwordAttribute;
5658
}
@@ -80,7 +82,7 @@ public function loadUserByUsername($username)
8082
throw new UsernameNotFoundException('More than one user found');
8183
}
8284

83-
return $this->loadUser($username, $entries[0]);
85+
return $this->loadUser($entries[0]->getAttribute($this->uidKey)[0], $entries[0]);
8486
}
8587

8688
/**

0 commit comments

Comments
 (0)
0