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

Skip to content

Commit 35c19c8

Browse files
committed
bug #39880 [DoctrineBridge] Add username to UserNameNotFoundException (qurben)
This PR was merged into the 4.4 branch. Discussion ---------- [DoctrineBridge] Add username to UserNameNotFoundException | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #39878 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch 5.x. --> Adds username to UserNameNotFoundException when thrown from EntityUserProvider. In other places there are no tests for this and I am not sure if the current setup even allows asserting if exceptions contain fields, besides the default ones. Commits ------- ee5b51a bug #39878 [doctrine-bridge] Add username to UserNameNotFoundException
2 parents 6c01479 + ee5b51a commit 35c19c8

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ public function loadUserByUsername($username)
6262
}
6363

6464
if (null === $user) {
65-
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
65+
$e = new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
66+
$e->setUsername($username);
67+
68+
throw $e;
6669
}
6770

6871
return $user;
@@ -92,7 +95,10 @@ public function refreshUser(UserInterface $user)
9295

9396
$refreshedUser = $repository->find($id);
9497
if (null === $refreshedUser) {
95-
throw new UsernameNotFoundException('User with id '.json_encode($id).' not found.');
98+
$e = new UsernameNotFoundException('User with id '.json_encode($id).' not found.');
99+
$e->setUsername(json_encode($id));
100+
101+
throw $e;
96102
}
97103
}
98104

src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/SecuredPageBundle/Security/Core/User/ArrayUserProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public function loadUserByUsername($username)
3434
$user = $this->getUser($username);
3535

3636
if (null === $user) {
37-
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
37+
$e = new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
38+
$e->setUsername($username);
39+
40+
throw $e;
3841
}
3942

4043
return $user;

src/Symfony/Component/Ldap/Security/LdapUserProvider.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,27 @@ public function loadUserByUsername($username)
7373
$query = str_replace('{username}', $username, $this->defaultSearch);
7474
$search = $this->ldap->query($this->baseDn, $query);
7575
} catch (ConnectionException $e) {
76-
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username), 0, $e);
76+
$e = new UsernameNotFoundException(sprintf('User "%s" not found.', $username), 0, $e);
77+
$e->setUsername($username);
78+
79+
throw $e;
7780
}
7881

7982
$entries = $search->execute();
8083
$count = \count($entries);
8184

8285
if (!$count) {
83-
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
86+
$e = new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
87+
$e->setUsername($username);
88+
89+
throw $e;
8490
}
8591

8692
if ($count > 1) {
87-
throw new UsernameNotFoundException('More than one user found.');
93+
$e = new UsernameNotFoundException('More than one user found.');
94+
$e->setUsername($username);
95+
96+
throw $e;
8897
}
8998

9099
$entry = $entries[0];

src/Symfony/Component/Security/Guard/Provider/GuardAuthenticationProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ private function authenticateViaGuard(AuthenticatorInterface $guardAuthenticator
105105
$user = $guardAuthenticator->getUser($token->getCredentials(), $this->userProvider);
106106

107107
if (null === $user) {
108-
throw new UsernameNotFoundException(sprintf('Null returned from "%s::getUser()".', \get_class($guardAuthenticator)));
108+
$e = new UsernameNotFoundException(sprintf('Null returned from "%s::getUser()".', \get_class($guardAuthenticator)));
109+
$e->setUsername($token->getUsername());
110+
111+
throw $e;
109112
}
110113

111114
if (!$user instanceof UserInterface) {

0 commit comments

Comments
 (0)