Skip to content

Commit b61d4ef

Browse files
wouterjchalasr
authored andcommitted
[Security] Rename UserInterface::getUsername() to getUserIdentifier()
1 parent a132f8d commit b61d4ef

File tree

4 files changed

+46
-30
lines changed

4 files changed

+46
-30
lines changed

Security/CheckLdapCredentialsListener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ public function onCheckPassport(CheckPassportEvent $event)
8383
} else {
8484
throw new LogicException('Using the "query_string" config without using a "search_dn" and a "search_password" is not supported.');
8585
}
86-
$username = $ldap->escape($user->getUsername(), '', LdapInterface::ESCAPE_FILTER);
86+
// @deprecated since 5.3, change to $user->getUserIdentifier() in 6.0
87+
$username = $ldap->escape(method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(), '', LdapInterface::ESCAPE_FILTER);
8788
$query = str_replace('{username}', $username, $ldapBadge->getQueryString());
8889
$result = $ldap->query($ldapBadge->getDnString(), $query)->execute();
8990
if (1 !== $result->count()) {
@@ -92,7 +93,8 @@ public function onCheckPassport(CheckPassportEvent $event)
9293

9394
$dn = $result[0]->getDn();
9495
} else {
95-
$username = $ldap->escape($user->getUsername(), '', LdapInterface::ESCAPE_DN);
96+
// @deprecated since 5.3, change to $user->getUserIdentifier() in 6.0
97+
$username = $ldap->escape(method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(), '', LdapInterface::ESCAPE_DN);
9698
$dn = str_replace('{username}', $username, $ldapBadge->getDnString());
9799
}
98100

Security/LdapUser.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public function getSalt(): ?string
7575
* {@inheritdoc}
7676
*/
7777
public function getUsername(): string
78+
{
79+
trigger_deprecation('symfony/security-core', '5.3', 'Method "%s()" is deprecated and will be removed in 6.0, use getUserIdentifier() instead.', __METHOD__);
80+
81+
return $this->username;
82+
}
83+
84+
public function getUserIdentifier(): string
7885
{
7986
return $this->username;
8087
}

Security/LdapUserProvider.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\Ldap\LdapInterface;
1818
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;
1919
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
20-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
20+
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
2121
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
2222
use Symfony\Component\Security\Core\User\UserInterface;
2323
use Symfony\Component\Security\Core\User\UserProviderInterface;
@@ -48,7 +48,7 @@ public function __construct(LdapInterface $ldap, string $baseDn, string $searchD
4848
}
4949

5050
if (null === $filter) {
51-
$filter = '({uid_key}={username})';
51+
$filter = '({uid_key}={user_identifier})';
5252
}
5353

5454
$this->ldap = $ldap;
@@ -66,15 +66,22 @@ public function __construct(LdapInterface $ldap, string $baseDn, string $searchD
6666
* {@inheritdoc}
6767
*/
6868
public function loadUserByUsername(string $username)
69+
{
70+
trigger_deprecation('symfony/ldap', '5.3', 'Method "%s()" is deprecated, use loadUserByIdentifier() instead.', __METHOD__);
71+
72+
return $this->loadUserByIdentifier($username);
73+
}
74+
75+
public function loadUserByIdentifier(string $identifier): UserInterface
6976
{
7077
try {
7178
$this->ldap->bind($this->searchDn, $this->searchPassword);
72-
$username = $this->ldap->escape($username, '', LdapInterface::ESCAPE_FILTER);
73-
$query = str_replace('{username}', $username, $this->defaultSearch);
79+
$identifier = $this->ldap->escape($identifier, '', LdapInterface::ESCAPE_FILTER);
80+
$query = str_replace(['{username}', '{user_identifier}'], $identifier, $this->defaultSearch);
7481
$search = $this->ldap->query($this->baseDn, $query);
7582
} catch (ConnectionException $e) {
76-
$e = new UsernameNotFoundException(sprintf('User "%s" not found.', $username), 0, $e);
77-
$e->setUsername($username);
83+
$e = new UserNotFoundException(sprintf('User "%s" not found.', $identifier), 0, $e);
84+
$e->setUserIdentifier($identifier);
7885

7986
throw $e;
8087
}
@@ -83,15 +90,15 @@ public function loadUserByUsername(string $username)
8390
$count = \count($entries);
8491

8592
if (!$count) {
86-
$e = new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
87-
$e->setUsername($username);
93+
$e = new UserNotFoundException(sprintf('User "%s" not found.', $identifier));
94+
$e->setUserIdentifier($identifier);
8895

8996
throw $e;
9097
}
9198

9299
if ($count > 1) {
93-
$e = new UsernameNotFoundException('More than one user found.');
94-
$e->setUsername($username);
100+
$e = new UserNotFoundException('More than one user found.');
101+
$e->setUserIdentifier($identifier);
95102

96103
throw $e;
97104
}
@@ -100,12 +107,12 @@ public function loadUserByUsername(string $username)
100107

101108
try {
102109
if (null !== $this->uidKey) {
103-
$username = $this->getAttributeValue($entry, $this->uidKey);
110+
$identifier = $this->getAttributeValue($entry, $this->uidKey);
104111
}
105112
} catch (InvalidArgumentException $e) {
106113
}
107114

108-
return $this->loadUser($username, $entry);
115+
return $this->loadUser($identifier, $entry);
109116
}
110117

111118
/**
@@ -117,7 +124,7 @@ public function refreshUser(UserInterface $user)
117124
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_debug_type($user)));
118125
}
119126

120-
return new LdapUser($user->getEntry(), $user->getUsername(), $user->getPassword(), $user->getRoles(), $user->getExtraFields());
127+
return new LdapUser($user->getEntry(), $user->getUserIdentifier(), $user->getPassword(), $user->getRoles(), $user->getExtraFields());
121128
}
122129

123130
/**
@@ -157,7 +164,7 @@ public function supportsClass(string $class)
157164
*
158165
* @return UserInterface
159166
*/
160-
protected function loadUser(string $username, Entry $entry)
167+
protected function loadUser(string $identifier, Entry $entry)
161168
{
162169
$password = null;
163170
$extraFields = [];
@@ -170,7 +177,7 @@ protected function loadUser(string $username, Entry $entry)
170177
$extraFields[$field] = $this->getAttributeValue($entry, $field);
171178
}
172179

173-
return new LdapUser($entry, $username, $password, $this->defaultRoles, $extraFields);
180+
return new LdapUser($entry, $identifier, $password, $this->defaultRoles, $extraFields);
174181
}
175182

176183
private function getAttributeValue(Entry $entry, string $attribute)

Tests/Security/LdapUserProviderTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Symfony\Component\Ldap\Security\LdapUser;
2121
use Symfony\Component\Ldap\Security\LdapUserProvider;
2222
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;
23-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
23+
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
2424

2525
/**
2626
* @requires extension ldap
@@ -29,7 +29,7 @@ class LdapUserProviderTest extends TestCase
2929
{
3030
public function testLoadUserByUsernameFailsIfCantConnectToLdap()
3131
{
32-
$this->expectException(UsernameNotFoundException::class);
32+
$this->expectException(UserNotFoundException::class);
3333

3434
$ldap = $this->createMock(LdapInterface::class);
3535
$ldap
@@ -39,12 +39,12 @@ public function testLoadUserByUsernameFailsIfCantConnectToLdap()
3939
;
4040

4141
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com');
42-
$provider->loadUserByUsername('foo');
42+
$provider->loadUserByIdentifier('foo');
4343
}
4444

4545
public function testLoadUserByUsernameFailsIfNoLdapEntries()
4646
{
47-
$this->expectException(UsernameNotFoundException::class);
47+
$this->expectException(UserNotFoundException::class);
4848

4949
$result = $this->createMock(CollectionInterface::class);
5050
$query = $this->createMock(QueryInterface::class);
@@ -71,12 +71,12 @@ public function testLoadUserByUsernameFailsIfNoLdapEntries()
7171
;
7272

7373
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com');
74-
$provider->loadUserByUsername('foo');
74+
$provider->loadUserByIdentifier('foo');
7575
}
7676

7777
public function testLoadUserByUsernameFailsIfMoreThanOneLdapEntry()
7878
{
79-
$this->expectException(UsernameNotFoundException::class);
79+
$this->expectException(UserNotFoundException::class);
8080

8181
$result = $this->createMock(CollectionInterface::class);
8282
$query = $this->createMock(QueryInterface::class);
@@ -103,7 +103,7 @@ public function testLoadUserByUsernameFailsIfMoreThanOneLdapEntry()
103103
;
104104

105105
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com');
106-
$provider->loadUserByUsername('foo');
106+
$provider->loadUserByIdentifier('foo');
107107
}
108108

109109
public function testLoadUserByUsernameFailsIfMoreThanOneLdapPasswordsInEntry()
@@ -144,7 +144,7 @@ public function testLoadUserByUsernameFailsIfMoreThanOneLdapPasswordsInEntry()
144144
;
145145

146146
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, [], 'sAMAccountName', '({uid_key}={username})', 'userpassword');
147-
$this->assertInstanceOf(LdapUser::class, $provider->loadUserByUsername('foo'));
147+
$this->assertInstanceOf(LdapUser::class, $provider->loadUserByIdentifier('foo'));
148148
}
149149

150150
public function testLoadUserByUsernameShouldNotFailIfEntryHasNoUidKeyAttribute()
@@ -180,7 +180,7 @@ public function testLoadUserByUsernameShouldNotFailIfEntryHasNoUidKeyAttribute()
180180
;
181181

182182
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, [], 'sAMAccountName', '({uid_key}={username})');
183-
$this->assertInstanceOf(LdapUser::class, $provider->loadUserByUsername('foo'));
183+
$this->assertInstanceOf(LdapUser::class, $provider->loadUserByIdentifier('foo'));
184184
}
185185

186186
public function testLoadUserByUsernameFailsIfEntryHasNoPasswordAttribute()
@@ -218,7 +218,7 @@ public function testLoadUserByUsernameFailsIfEntryHasNoPasswordAttribute()
218218
;
219219

220220
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, [], 'sAMAccountName', '({uid_key}={username})', 'userpassword');
221-
$this->assertInstanceOf(LdapUser::class, $provider->loadUserByUsername('foo'));
221+
$this->assertInstanceOf(LdapUser::class, $provider->loadUserByIdentifier('foo'));
222222
}
223223

224224
public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttribute()
@@ -254,7 +254,7 @@ public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttribute()
254254
;
255255

256256
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com');
257-
$this->assertInstanceOf(LdapUser::class, $provider->loadUserByUsername('foo'));
257+
$this->assertInstanceOf(LdapUser::class, $provider->loadUserByIdentifier('foo'));
258258
}
259259

260260
public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttributeAndWrongCase()
@@ -290,7 +290,7 @@ public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttributeAndWro
290290
;
291291

292292
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com');
293-
$this->assertSame('foo', $provider->loadUserByUsername('Foo')->getUsername());
293+
$this->assertSame('foo', $provider->loadUserByIdentifier('Foo')->getUserIdentifier());
294294
}
295295

296296
public function testLoadUserByUsernameIsSuccessfulWithPasswordAttribute()
@@ -330,7 +330,7 @@ public function testLoadUserByUsernameIsSuccessfulWithPasswordAttribute()
330330
;
331331

332332
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, [], 'sAMAccountName', '({uid_key}={username})', 'userpassword', ['email']);
333-
$this->assertInstanceOf(LdapUser::class, $provider->loadUserByUsername('foo'));
333+
$this->assertInstanceOf(LdapUser::class, $provider->loadUserByIdentifier('foo'));
334334
}
335335

336336
public function testRefreshUserShouldReturnUserWithSameProperties()

0 commit comments

Comments
 (0)