Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit 58cc6f4

Browse files
committed
[Security] removed usage of the deprecated SecurityContextInterface
1 parent 81b5ab9 commit 58cc6f4

33 files changed

+388
-325
lines changed

Core/SecurityContext.php

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

1212
namespace Symfony\Component\Security\Core;
1313

14+
trigger_error('The '.__NAMESPACE__.'\SecurityContext class is deprecated since version 2.6 and will be removed in 3.0. Use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage or Symfony\Component\Security\Core\Authorization\AuthorizationChecker instead.', E_USER_DEPRECATED);
15+
1416
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
1517
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
1618
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
@@ -76,8 +78,6 @@ public function __construct($tokenStorage, $authorizationChecker, $alwaysAuthent
7678
*/
7779
public function getToken()
7880
{
79-
trigger_error('The '.__METHOD__.' method is deprecated since version 2.6 and will be removed in 3.0. Use the Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage::getToken() method instead.', E_USER_DEPRECATED);
80-
8181
return $this->tokenStorage->getToken();
8282
}
8383

@@ -88,8 +88,6 @@ public function getToken()
8888
*/
8989
public function setToken(TokenInterface $token = null)
9090
{
91-
trigger_error('The '.__METHOD__.' method is deprecated since version 2.6 and will be removed in 3.0. Use the Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage::setToken() method instead.', E_USER_DEPRECATED);
92-
9391
return $this->tokenStorage->setToken($token);
9492
}
9593

@@ -100,8 +98,6 @@ public function setToken(TokenInterface $token = null)
10098
*/
10199
public function isGranted($attributes, $object = null)
102100
{
103-
trigger_error('The '.__METHOD__.' method is deprecated since version 2.6 and will be removed in 3.0. Use the Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface::isGranted() method instead.', E_USER_DEPRECATED);
104-
105101
return $this->authorizationChecker->isGranted($attributes, $object);
106102
}
107103
}

Core/SecurityContextInterface.php

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

1212
namespace Symfony\Component\Security\Core;
1313

14+
trigger_error('The '.__NAMESPACE__.'\SecurityContextInterface interface is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
15+
1416
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1517
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1618

Core/Tests/SecurityContextTest.php renamed to Core/Tests/LegacySecurityContextTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
1616
use Symfony\Component\Security\Core\SecurityContext;
1717

18-
class SecurityContextTest extends \PHPUnit_Framework_TestCase
18+
class LegacySecurityContextTest extends \PHPUnit_Framework_TestCase
1919
{
2020
private $tokenStorage;
2121
private $authorizationChecker;
2222
private $securityContext;
2323

2424
public function setUp()
2525
{
26+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
27+
2628
$this->tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
2729
$this->authorizationChecker = $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
2830
$this->securityContext = new SecurityContext($this->tokenStorage, $this->authorizationChecker);

Core/Tests/Validator/Constraints/UserPasswordValidatorTest.php

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

1212
namespace Symfony\Component\Security\Core\Tests\Validator\Constraints;
1313

14+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1415
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
1516
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
16-
use Symfony\Component\Security\Core\SecurityContextInterface;
1717
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
1818
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
1919
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
@@ -28,9 +28,9 @@ abstract class UserPasswordValidatorTest extends AbstractConstraintValidatorTest
2828
const SALT = '^S4lt$';
2929

3030
/**
31-
* @var SecurityContextInterface
31+
* @var TokenStorageInterface
3232
*/
33-
protected $securityContext;
33+
protected $tokenStorage;
3434

3535
/**
3636
* @var PasswordEncoderInterface
@@ -44,13 +44,13 @@ abstract class UserPasswordValidatorTest extends AbstractConstraintValidatorTest
4444

4545
protected function createValidator()
4646
{
47-
return new UserPasswordValidator($this->securityContext, $this->encoderFactory);
47+
return new UserPasswordValidator($this->tokenStorage, $this->encoderFactory);
4848
}
4949

5050
protected function setUp()
5151
{
5252
$user = $this->createUser();
53-
$this->securityContext = $this->createSecurityContext($user);
53+
$this->tokenStorage = $this->createTokenStorage($user);
5454
$this->encoder = $this->createPasswordEncoder();
5555
$this->encoderFactory = $this->createEncoderFactory($this->encoder);
5656

@@ -97,7 +97,7 @@ public function testUserIsNotValid()
9797
{
9898
$user = $this->getMock('Foo\Bar\User');
9999

100-
$this->securityContext = $this->createSecurityContext($user);
100+
$this->tokenStorage = $this->createTokenStorage($user);
101101
$this->validator = $this->createValidator();
102102
$this->validator->initialize($this->context);
103103

@@ -141,11 +141,11 @@ protected function createEncoderFactory($encoder = null)
141141
return $mock;
142142
}
143143

144-
protected function createSecurityContext($user = null)
144+
protected function createTokenStorage($user = null)
145145
{
146146
$token = $this->createAuthenticationToken($user);
147147

148-
$mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
148+
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
149149
$mock
150150
->expects($this->any())
151151
->method('getToken')

Core/Validator/Constraints/UserPasswordValidator.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,25 @@
1414
use Symfony\Component\Security\Core\User\UserInterface;
1515
use Symfony\Component\Security\Core\SecurityContextInterface;
1616
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
17+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1718
use Symfony\Component\Validator\Constraint;
1819
use Symfony\Component\Validator\ConstraintValidator;
1920
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
2021
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
2122

2223
class UserPasswordValidator extends ConstraintValidator
2324
{
24-
private $securityContext;
25+
private $tokenStorage;
2526
private $encoderFactory;
2627

27-
public function __construct(SecurityContextInterface $securityContext, EncoderFactoryInterface $encoderFactory)
28+
/**
29+
* @param SecurityContextInterface|TokenStorageInterface
30+
*
31+
* Passing a SecurityContextInterface as a first argument was deprecated in 2.7 and will be removed in 3.0
32+
*/
33+
public function __construct($tokenStorage, EncoderFactoryInterface $encoderFactory)
2834
{
29-
$this->securityContext = $securityContext;
35+
$this->tokenStorage = $tokenStorage;
3036
$this->encoderFactory = $encoderFactory;
3137
}
3238

@@ -39,7 +45,7 @@ public function validate($password, Constraint $constraint)
3945
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UserPassword');
4046
}
4147

42-
$user = $this->securityContext->getToken()->getUser();
48+
$user = $this->tokenStorage->getToken()->getUser();
4349

4450
if (!$user instanceof UserInterface) {
4551
throw new ConstraintDefinitionException('The User object must implement the UserInterface interface.');

Http/Firewall/AbstractAuthenticationListener.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Security\Core\Security;
1919
use Symfony\Component\Security\Core\SecurityContextInterface;
2020
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
21+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2122
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2223
use Symfony\Component\Security\Core\Exception\AuthenticationException;
2324
use Symfony\Component\Security\Core\Exception\SessionUnavailableException;
@@ -56,7 +57,7 @@ abstract class AbstractAuthenticationListener implements ListenerInterface
5657
protected $providerKey;
5758
protected $httpUtils;
5859

59-
private $securityContext;
60+
private $tokenStorage;
6061
private $sessionStrategy;
6162
private $dispatcher;
6263
private $successHandler;
@@ -66,27 +67,29 @@ abstract class AbstractAuthenticationListener implements ListenerInterface
6667
/**
6768
* Constructor.
6869
*
69-
* @param SecurityContextInterface $securityContext A SecurityContext instance
70-
* @param AuthenticationManagerInterface $authenticationManager An AuthenticationManagerInterface instance
71-
* @param SessionAuthenticationStrategyInterface $sessionStrategy
72-
* @param HttpUtils $httpUtils An HttpUtilsInterface instance
73-
* @param string $providerKey
74-
* @param AuthenticationSuccessHandlerInterface $successHandler
75-
* @param AuthenticationFailureHandlerInterface $failureHandler
76-
* @param array $options An array of options for the processing of a
77-
* successful, or failed authentication attempt
78-
* @param LoggerInterface $logger A LoggerInterface instance
79-
* @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
70+
* @param SecurityContextInterface|TokenStorageInterface $tokenStorage A SecurityContext or a TokenStorageInterface instance
71+
* @param AuthenticationManagerInterface $authenticationManager An AuthenticationManagerInterface instance
72+
* @param SessionAuthenticationStrategyInterface $sessionStrategy
73+
* @param HttpUtils $httpUtils An HttpUtilsInterface instance
74+
* @param string $providerKey
75+
* @param AuthenticationSuccessHandlerInterface $successHandler
76+
* @param AuthenticationFailureHandlerInterface $failureHandler
77+
* @param array $options An array of options for the processing of a
78+
* successful, or failed authentication attempt
79+
* @param LoggerInterface $logger A LoggerInterface instance
80+
* @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
8081
*
8182
* @throws \InvalidArgumentException
83+
*
84+
* Passing a SecurityContextInterface as a first argument was deprecated in 2.7 and will be removed in 3.0
8285
*/
83-
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, SessionAuthenticationStrategyInterface $sessionStrategy, HttpUtils $httpUtils, $providerKey, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler, array $options = array(), LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null)
86+
public function __construct($tokenStorage, AuthenticationManagerInterface $authenticationManager, SessionAuthenticationStrategyInterface $sessionStrategy, HttpUtils $httpUtils, $providerKey, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler, array $options = array(), LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null)
8487
{
8588
if (empty($providerKey)) {
8689
throw new \InvalidArgumentException('$providerKey must not be empty.');
8790
}
8891

89-
$this->securityContext = $securityContext;
92+
$this->tokenStorage = $tokenStorage;
9093
$this->authenticationManager = $authenticationManager;
9194
$this->sessionStrategy = $sessionStrategy;
9295
$this->providerKey = $providerKey;
@@ -196,9 +199,9 @@ private function onFailure(Request $request, AuthenticationException $failed)
196199
$this->logger->info(sprintf('Authentication request failed: %s', $failed->getMessage()));
197200
}
198201

199-
$token = $this->securityContext->getToken();
202+
$token = $this->tokenStorage->getToken();
200203
if ($token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey()) {
201-
$this->securityContext->setToken(null);
204+
$this->tokenStorage->setToken(null);
202205
}
203206

204207
$response = $this->failureHandler->onAuthenticationFailure($request, $failed);
@@ -216,7 +219,7 @@ private function onSuccess(Request $request, TokenInterface $token)
216219
$this->logger->info(sprintf('User "%s" has been authenticated successfully', $token->getUsername()));
217220
}
218221

219-
$this->securityContext->setToken($token);
222+
$this->tokenStorage->setToken($token);
220223

221224
$session = $request->getSession();
222225
$session->remove(Security::AUTHENTICATION_ERROR);

Http/Firewall/AbstractPreAuthenticatedListener.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Security\Core\SecurityContextInterface;
1515
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
1616
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
17+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1718
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1819
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
1920
use Symfony\Component\Security\Http\SecurityEvents;
@@ -33,14 +34,19 @@
3334
abstract class AbstractPreAuthenticatedListener implements ListenerInterface
3435
{
3536
protected $logger;
36-
private $securityContext;
37+
private $tokenStorage;
3738
private $authenticationManager;
3839
private $providerKey;
3940
private $dispatcher;
4041

41-
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, $providerKey, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null)
42+
/**
43+
* @param SecurityContextInterface|TokenStorageInterface
44+
*
45+
* Passing a SecurityContextInterface as a first argument was deprecated in 2.7 and will be removed in 3.0
46+
*/
47+
public function __construct($tokenStorage, AuthenticationManagerInterface $authenticationManager, $providerKey, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null)
4248
{
43-
$this->securityContext = $securityContext;
49+
$this->tokenStorage = $tokenStorage;
4450
$this->authenticationManager = $authenticationManager;
4551
$this->providerKey = $providerKey;
4652
$this->logger = $logger;
@@ -57,7 +63,7 @@ final public function handle(GetResponseEvent $event)
5763
$request = $event->getRequest();
5864

5965
if (null !== $this->logger) {
60-
$this->logger->debug(sprintf('Checking secure context token: %s', $this->securityContext->getToken()));
66+
$this->logger->debug(sprintf('Checking secure context token: %s', $this->tokenStorage->getToken()));
6167
}
6268

6369
try {
@@ -68,7 +74,7 @@ final public function handle(GetResponseEvent $event)
6874
return;
6975
}
7076

71-
if (null !== $token = $this->securityContext->getToken()) {
77+
if (null !== $token = $this->tokenStorage->getToken()) {
7278
if ($token instanceof PreAuthenticatedToken && $this->providerKey == $token->getProviderKey() && $token->isAuthenticated() && $token->getUsername() === $user) {
7379
return;
7480
}
@@ -84,7 +90,7 @@ final public function handle(GetResponseEvent $event)
8490
if (null !== $this->logger) {
8591
$this->logger->info(sprintf('Authentication success: %s', $token));
8692
}
87-
$this->securityContext->setToken($token);
93+
$this->tokenStorage->setToken($token);
8894

8995
if (null !== $this->dispatcher) {
9096
$loginEvent = new InteractiveLoginEvent($request, $token);
@@ -102,9 +108,9 @@ final public function handle(GetResponseEvent $event)
102108
*/
103109
private function clearToken(AuthenticationException $exception)
104110
{
105-
$token = $this->securityContext->getToken();
111+
$token = $this->tokenStorage->getToken();
106112
if ($token instanceof PreAuthenticatedToken && $this->providerKey === $token->getProviderKey()) {
107-
$this->securityContext->setToken(null);
113+
$this->tokenStorage->setToken(null);
108114

109115
if (null !== $this->logger) {
110116
$this->logger->info(sprintf("Cleared security context due to exception: %s", $exception->getMessage()));

Http/Firewall/AccessListener.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
1616
use Symfony\Component\Security\Http\AccessMapInterface;
1717
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
18+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1819
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1920
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
2021
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
@@ -26,14 +27,19 @@
2627
*/
2728
class AccessListener implements ListenerInterface
2829
{
29-
private $context;
30+
private $tokenStorage;
3031
private $accessDecisionManager;
3132
private $map;
3233
private $authManager;
3334

34-
public function __construct(SecurityContextInterface $context, AccessDecisionManagerInterface $accessDecisionManager, AccessMapInterface $map, AuthenticationManagerInterface $authManager)
35+
/**
36+
* @param SecurityContextInterface|TokenStorageInterface
37+
*
38+
* Passing a SecurityContextInterface as a first argument was deprecated in 2.7 and will be removed in 3.0
39+
*/
40+
public function __construct($tokenStorage, AccessDecisionManagerInterface $accessDecisionManager, AccessMapInterface $map, AuthenticationManagerInterface $authManager)
3541
{
36-
$this->context = $context;
42+
$this->tokenStorage = $tokenStorage;
3743
$this->accessDecisionManager = $accessDecisionManager;
3844
$this->map = $map;
3945
$this->authManager = $authManager;
@@ -49,7 +55,7 @@ public function __construct(SecurityContextInterface $context, AccessDecisionMan
4955
*/
5056
public function handle(GetResponseEvent $event)
5157
{
52-
if (null === $token = $this->context->getToken()) {
58+
if (null === $token = $this->tokenStorage->getToken()) {
5359
throw new AuthenticationCredentialsNotFoundException('A Token was not found in the SecurityContext.');
5460
}
5561

@@ -63,7 +69,7 @@ public function handle(GetResponseEvent $event)
6369

6470
if (!$token->isAuthenticated()) {
6571
$token = $this->authManager->authenticate($token);
66-
$this->context->setToken($token);
72+
$this->tokenStorage->setToken($token);
6773
}
6874

6975
if (!$this->accessDecisionManager->decide($token, $attributes, $request)) {

0 commit comments

Comments
 (0)