Skip to content

Commit f166166

Browse files
Merge branch '2.7' into 2.8
* 2.7: [Config] Handle nullable node name + fix inheritdocs [Security] added userChecker to SimpleAuthenticationProvider [Debug] fix test Fix typo in test method name Fixes #26563 (open_basedir restriction in effect) [Debug] Reset previous exception handler ealier to prevent infinite loop add hint in Github pull request template [Validator] Fix docblock of ClassMetadata#members [BrowserKit] Fix cookie path handling when $domain is null [DoctrineBridge] Don't rely on ClassMetadataInfo->hasField in DoctrineOrmTypeGuesser anymore [BrowserKit] Improves CookieJar::get [BrowserKit] Fix Cookie's PHPDoc [DomCrawler] Change bad wording in ChoiceFormField::untick [DomCrawler] Fix the PHPDoc of ChoiceFormField::setValue [DomCrawler] Avoid a useless call to strtolower [FrameworkBundle] HttpCache is not longer abstract [DomCrawler] extract(): fix a bug when the attribute list is empty [Config] Backport string|null api for node names
2 parents 576ac85 + 8e7e189 commit f166166

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

Authentication/Provider/SimpleAuthenticationProvider.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Security\Core\Authentication\Provider;
1313

14+
use Symfony\Component\Security\Core\User\UserChecker;
15+
use Symfony\Component\Security\Core\User\UserCheckerInterface;
1416
use Symfony\Component\Security\Core\User\UserProviderInterface;
1517
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1618
use Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface;
@@ -24,23 +26,29 @@ class SimpleAuthenticationProvider implements AuthenticationProviderInterface
2426
private $simpleAuthenticator;
2527
private $userProvider;
2628
private $providerKey;
29+
private $userChecker;
2730

28-
public function __construct(SimpleAuthenticatorInterface $simpleAuthenticator, UserProviderInterface $userProvider, $providerKey)
31+
public function __construct(SimpleAuthenticatorInterface $simpleAuthenticator, UserProviderInterface $userProvider, $providerKey, UserCheckerInterface $userChecker = null)
2932
{
3033
$this->simpleAuthenticator = $simpleAuthenticator;
3134
$this->userProvider = $userProvider;
3235
$this->providerKey = $providerKey;
36+
$this->userChecker = $userChecker ?: new UserChecker();
3337
}
3438

3539
public function authenticate(TokenInterface $token)
3640
{
3741
$authToken = $this->simpleAuthenticator->authenticateToken($token, $this->userProvider, $this->providerKey);
3842

39-
if ($authToken instanceof TokenInterface) {
40-
return $authToken;
43+
if (!$authToken instanceof TokenInterface) {
44+
throw new AuthenticationException('Simple authenticator failed to return an authenticated token.');
4145
}
4246

43-
throw new AuthenticationException('Simple authenticator failed to return an authenticated token.');
47+
$user = $authToken->getUser();
48+
$this->userChecker->checkPreAuth($user);
49+
$this->userChecker->checkPostAuth($user);
50+
51+
return $authToken;
4452
}
4553

4654
public function supports(TokenInterface $token)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Tests\Authentication\Provider;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Security\Core\Exception\DisabledException;
16+
use Symfony\Component\Security\Core\Authentication\Provider\SimpleAuthenticationProvider;
17+
use Symfony\Component\Security\Core\Exception\LockedException;
18+
19+
class SimpleAuthenticationProviderTest extends TestCase
20+
{
21+
/**
22+
* @expectedException \Symfony\Component\Security\Core\Exception\DisabledException
23+
*/
24+
public function testAuthenticateWhenPreChecksFails()
25+
{
26+
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
27+
28+
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
29+
$token->expects($this->any())
30+
->method('getUser')
31+
->will($this->returnValue($user));
32+
33+
$userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock();
34+
$userChecker->expects($this->once())
35+
->method('checkPreAuth')
36+
->will($this->throwException(new DisabledException()));
37+
38+
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
39+
$authenticator->expects($this->once())
40+
->method('authenticateToken')
41+
->will($this->returnValue($token));
42+
43+
$provider = $this->getProvider($authenticator, null, $userChecker);
44+
45+
$provider->authenticate($token);
46+
}
47+
48+
/**
49+
* @expectedException \Symfony\Component\Security\Core\Exception\LockedException
50+
*/
51+
public function testAuthenticateWhenPostChecksFails()
52+
{
53+
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
54+
55+
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
56+
$token->expects($this->any())
57+
->method('getUser')
58+
->will($this->returnValue($user));
59+
60+
$userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock();
61+
$userChecker->expects($this->once())
62+
->method('checkPostAuth')
63+
->will($this->throwException(new LockedException()));
64+
65+
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
66+
$authenticator->expects($this->once())
67+
->method('authenticateToken')
68+
->will($this->returnValue($token));
69+
70+
$provider = $this->getProvider($authenticator, null, $userChecker);
71+
72+
$provider->authenticate($token);
73+
}
74+
75+
protected function getProvider($simpleAuthenticator = null, $userProvider = null, $userChecker = null, $key = 'test')
76+
{
77+
if (null === $userChecker) {
78+
$userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock();
79+
}
80+
if (null === $simpleAuthenticator) {
81+
$simpleAuthenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
82+
}
83+
if (null === $userProvider) {
84+
$userProvider = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock();
85+
}
86+
87+
return new SimpleAuthenticationProvider($simpleAuthenticator, $userProvider, $key, $userChecker);
88+
}
89+
}

0 commit comments

Comments
 (0)