Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit21f6216

Browse files
committed
[Security] Load the user before pre/post auth checks when needed
1 parent34bb83d commit21f6216

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

‎src/Symfony/Component/Security/Core/Authentication/Provider/SimpleAuthenticationProvider.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
namespaceSymfony\Component\Security\Core\Authentication\Provider;
1313

14+
useSymfony\Component\Security\Core\Exception\AuthenticationServiceException;
15+
useSymfony\Component\Security\Core\Exception\UsernameNotFoundException;
1416
useSymfony\Component\Security\Core\User\UserChecker;
1517
useSymfony\Component\Security\Core\User\UserCheckerInterface;
18+
useSymfony\Component\Security\Core\User\UserInterface;
1619
useSymfony\Component\Security\Core\User\UserProviderInterface;
1720
useSymfony\Component\Security\Core\Authentication\Token\TokenInterface;
1821
useSymfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface;
@@ -45,6 +48,24 @@ public function authenticate(TokenInterface $token)
4548
}
4649

4750
$user =$authToken->getUser();
51+
52+
if (!$userinstanceof UserInterface) {
53+
try {
54+
$user =$this->userProvider->loadUserByUsername($user);
55+
56+
if (!$userinstanceof UserInterface) {
57+
thrownewAuthenticationServiceException('The user provider must return a UserInterface object.');
58+
}
59+
}catch (UsernameNotFoundException$e) {
60+
$e->setUsername($user);
61+
throw$e;
62+
}catch (\Exception$e) {
63+
$e =newAuthenticationServiceException($e->getMessage(),0,$e);
64+
$e->setToken($token);
65+
throw$e;
66+
}
67+
}
68+
4869
$this->userChecker->checkPreAuth($user);
4970
$this->userChecker->checkPostAuth($user);
5071

‎src/Symfony/Component/Security/Core/Tests/Authentication/Provider/SimpleAuthenticationProviderTest.php‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
useSymfony\Component\Security\Core\Exception\DisabledException;
1616
useSymfony\Component\Security\Core\Authentication\Provider\SimpleAuthenticationProvider;
1717
useSymfony\Component\Security\Core\Exception\LockedException;
18+
useSymfony\Component\Security\Core\Exception\UsernameNotFoundException;
1819

1920
class SimpleAuthenticationProviderTestextends TestCase
2021
{
@@ -72,6 +73,54 @@ public function testAuthenticateWhenPostChecksFails()
7273
$provider->authenticate($token);
7374
}
7475

76+
publicfunctiontestAuthenticateFromString()
77+
{
78+
$user =$this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
79+
80+
$token =$this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
81+
$token->expects($this->any())
82+
->method('getUser')
83+
->will($this->returnValue('foo'));
84+
85+
$authenticator =$this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
86+
$authenticator->expects($this->once())
87+
->method('authenticateToken')
88+
->will($this->returnValue($token));
89+
90+
$userProvider =$this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock();
91+
$userProvider->expects($this->once())
92+
->method('loadUserByUsername')
93+
->willReturn($this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock());
94+
$provider =$this->getProvider($authenticator,$userProvider);
95+
96+
$this->assertSame($token,$provider->authenticate($token));
97+
}
98+
99+
/**
100+
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
101+
*/
102+
publicfunctiontestUsernameNotFound()
103+
{
104+
$user =$this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
105+
106+
$token =$this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
107+
$token->expects($this->any())
108+
->method('getUser')
109+
->will($this->returnValue('foo'));
110+
111+
$authenticator =$this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
112+
$authenticator->expects($this->once())
113+
->method('authenticateToken')
114+
->will($this->returnValue($token));
115+
116+
$userProvider =$this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock();
117+
$userProvider->expects($this->once())
118+
->method('loadUserByUsername')
119+
->willThrowException(newUsernameNotFoundException());
120+
121+
$this->assertSame($token,$this->getProvider($authenticator,$userProvider)->authenticate($token));
122+
}
123+
75124
protectedfunctiongetProvider($simpleAuthenticator =null,$userProvider =null,$userChecker =null,$key ='test')
76125
{
77126
if (null ===$userChecker) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp