Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c91689b

Browse files
quentinus95fabpot
authored andcommitted
[Ldap] Using Ldap stored username instead of form submitted one
1 parent 6641b79 commit c91689b

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

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

Lines changed: 47 additions & 5 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->exactly(2))
122+
->expects($this->once())
123123
->method('offsetGet')
124124
->with(0)
125125
->will($this->returnValue(new Entry('foo', array(
@@ -151,6 +151,48 @@ public function testLoadUserByUsernameFailsIfMoreThanOneLdapPasswordsInEntry()
151151
);
152152
}
153153

154+
/**
155+
* @expectedException \Symfony\Component\Security\Core\Exception\InvalidArgumentException
156+
*/
157+
public function testLoadUserByUsernameFailsIfEntryHasNoUidKeyAttribute()
158+
{
159+
$result = $this->getMock(CollectionInterface::class);
160+
$query = $this->getMock(QueryInterface::class);
161+
$query
162+
->expects($this->once())
163+
->method('execute')
164+
->will($this->returnValue($result))
165+
;
166+
$ldap = $this->getMock(LdapInterface::class);
167+
$result
168+
->expects($this->once())
169+
->method('offsetGet')
170+
->with(0)
171+
->will($this->returnValue(new Entry('foo', array())))
172+
;
173+
$result
174+
->expects($this->once())
175+
->method('count')
176+
->will($this->returnValue(1))
177+
;
178+
$ldap
179+
->expects($this->once())
180+
->method('escape')
181+
->will($this->returnValue('foo'))
182+
;
183+
$ldap
184+
->expects($this->once())
185+
->method('query')
186+
->will($this->returnValue($query))
187+
;
188+
189+
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, array(), 'sAMAccountName', '({uid_key}={username})');
190+
$this->assertInstanceOf(
191+
'Symfony\Component\Security\Core\User\User',
192+
$provider->loadUserByUsername('foo')
193+
);
194+
}
195+
154196
/**
155197
* @expectedException \Symfony\Component\Security\Core\Exception\InvalidArgumentException
156198
*/
@@ -165,7 +207,7 @@ public function testLoadUserByUsernameFailsIfEntryHasNoPasswordAttribute()
165207
;
166208
$ldap = $this->getMockBuilder(LdapInterface::class)->getMock();
167209
$result
168-
->expects($this->exactly(2))
210+
->expects($this->once())
169211
->method('offsetGet')
170212
->with(0)
171213
->will($this->returnValue(new Entry('foo', array(
@@ -207,7 +249,7 @@ public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttribute()
207249
;
208250
$ldap = $this->getMockBuilder(LdapInterface::class)->getMock();
209251
$result
210-
->expects($this->exactly(2))
252+
->expects($this->once())
211253
->method('offsetGet')
212254
->with(0)
213255
->will($this->returnValue(new Entry('foo', array(
@@ -249,7 +291,7 @@ public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttributeAndWro
249291
;
250292
$ldap = $this->getMockBuilder(LdapInterface::class)->getMock();
251293
$result
252-
->expects($this->exactly(2))
294+
->expects($this->once())
253295
->method('offsetGet')
254296
->with(0)
255297
->will($this->returnValue(new Entry('foo', array(
@@ -288,7 +330,7 @@ public function testLoadUserByUsernameIsSuccessfulWithPasswordAttribute()
288330
;
289331
$ldap = $this->getMock(LdapInterface::class);
290332
$result
291-
->expects($this->exactly(2))
333+
->expects($this->once())
292334
->method('offsetGet')
293335
->with(0)
294336
->will($this->returnValue(new Entry('foo', array(

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class LdapUserProvider implements UserProviderInterface
4747
*/
4848
public function __construct(LdapInterface $ldap, $baseDn, $searchDn = null, $searchPassword = null, array $defaultRoles = array(), $uidKey = 'sAMAccountName', $filter = '({uid_key}={username})', $passwordAttribute = null)
4949
{
50+
if (null === $uidKey) {
51+
$uidKey = 'uid';
52+
}
53+
5054
$this->ldap = $ldap;
5155
$this->baseDn = $baseDn;
5256
$this->searchDn = $searchDn;
@@ -82,7 +86,10 @@ public function loadUserByUsername($username)
8286
throw new UsernameNotFoundException('More than one user found');
8387
}
8488

85-
return $this->loadUser($entries[0]->getAttribute($this->uidKey)[0], $entries[0]);
89+
$entry = $entries[0];
90+
$username = $this->getAttributeValue($entry, $this->uidKey);
91+
92+
return $this->loadUser($username, $entry);
8693
}
8794

8895
/**
@@ -115,30 +122,30 @@ public function supportsClass($class)
115122
*/
116123
protected function loadUser($username, Entry $entry)
117124
{
118-
$password = $this->getPassword($entry);
125+
$password = null;
126+
if (null !== $this->passwordAttribute) {
127+
$password = $this->getAttributeValue($entry, $this->passwordAttribute);
128+
}
119129

120130
return new User($username, $password, $this->defaultRoles);
121131
}
122132

123133
/**
124-
* Fetches the password from an LDAP entry.
134+
* Fetches a required unique attribute value from an LDAP entry.
125135
*
126136
* @param null|Entry $entry
137+
* @param string $attribute
127138
*/
128-
private function getPassword(Entry $entry)
139+
private function getAttributeValue(Entry $entry, $attribute)
129140
{
130-
if (null === $this->passwordAttribute) {
131-
return;
132-
}
133-
134-
if (!$entry->hasAttribute($this->passwordAttribute)) {
135-
throw new InvalidArgumentException(sprintf('Missing attribute "%s" for user "%s".', $this->passwordAttribute, $entry->getDn()));
141+
if (!$entry->hasAttribute($attribute)) {
142+
throw new InvalidArgumentException(sprintf('Missing attribute "%s" for user "%s".', $attribute, $entry->getDn()));
136143
}
137144

138-
$values = $entry->getAttribute($this->passwordAttribute);
145+
$values = $entry->getAttribute($attribute);
139146

140147
if (1 !== count($values)) {
141-
throw new InvalidArgumentException(sprintf('Attribute "%s" has multiple values.', $this->passwordAttribute));
148+
throw new InvalidArgumentException(sprintf('Attribute "%s" has multiple values.', $attribute));
142149
}
143150

144151
return $values[0];

0 commit comments

Comments
 (0)