Skip to content

Commit 59f0e80

Browse files
committed
[Security] Deprecate legacy signatures
1 parent f8deef3 commit 59f0e80

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

Authorization/AuthorizationChecker.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,29 @@ class AuthorizationChecker implements AuthorizationCheckerInterface
3232
private $alwaysAuthenticate;
3333
private $exceptionOnNoToken;
3434

35-
public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, AccessDecisionManagerInterface $accessDecisionManager, bool $alwaysAuthenticate = false, bool $exceptionOnNoToken = true)
35+
public function __construct(TokenStorageInterface $tokenStorage, /*AccessDecisionManagerInterface*/ $accessDecisionManager, /*bool*/ $alwaysAuthenticate = false, /*bool*/ $exceptionOnNoToken = true)
3636
{
37+
if ($accessDecisionManager instanceof AuthenticationManagerInterface) {
38+
trigger_deprecation('symfony/security-core', '5.4', 'The $autenticationManager argument of "%s" is deprecated.', __METHOD__);
39+
40+
$this->authenticationManager = $accessDecisionManager;
41+
$accessDecisionManager = $alwaysAuthenticate;
42+
$alwaysAuthenticate = $exceptionOnNoToken;
43+
$exceptionOnNoToken = \func_num_args() > 4 ? func_get_arg(4) : true;
44+
}
45+
3746
if (false !== $alwaysAuthenticate) {
3847
trigger_deprecation('symfony/security-core', '5.4', 'Not setting the 4th argument of "%s" to "false" is deprecated.', __METHOD__);
3948
}
4049
if (false !== $exceptionOnNoToken) {
4150
trigger_deprecation('symfony/security-core', '5.4', 'Not setting the 5th argument of "%s" to "false" is deprecated.', __METHOD__);
4251
}
4352

53+
if (!$accessDecisionManager instanceof AccessDecisionManagerInterface) {
54+
throw new \TypeError(sprintf('Argument 2 of "%s" must be instance of "%s", "%s" given.', __METHOD__, AccessDecisionManagerInterface::class, get_debug_type($accessDecisionManager)));
55+
}
56+
4457
$this->tokenStorage = $tokenStorage;
45-
$this->authenticationManager = $authenticationManager;
4658
$this->accessDecisionManager = $accessDecisionManager;
4759
$this->alwaysAuthenticate = $alwaysAuthenticate;
4860
$this->exceptionOnNoToken = $exceptionOnNoToken;

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ CHANGELOG
44
5.4
55
---
66

7-
* Deprecate setting the 4th argument (`$alwaysAuthenticate`) to `true` and not setting the
8-
5th argument (`$exceptionOnNoToken`) to `false` of `AuthorizationChecker`
7+
* Deprecate the `$authenticationManager` argument of the `AuthorizationChecker` constructor
8+
* Deprecate setting the `$alwaysAuthenticate` argument to `true` and not setting the
9+
`$exceptionOnNoToken` argument to `false` of `AuthorizationChecker`
910
* Deprecate methods `TokenInterface::isAuthenticated()` and `setAuthenticated`,
1011
tokens will always be considered authenticated in 6.0
1112

Tests/Authorization/AuthorizationCheckerTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ class AuthorizationCheckerTest extends TestCase
2929

3030
protected function setUp(): void
3131
{
32-
$this->authenticationManager = $this->createMock(AuthenticationManagerInterface::class);
3332
$this->accessDecisionManager = $this->createMock(AccessDecisionManagerInterface::class);
3433
$this->tokenStorage = new TokenStorage();
3534

3635
$this->authorizationChecker = new AuthorizationChecker(
3736
$this->tokenStorage,
38-
$this->authenticationManager,
3937
$this->accessDecisionManager,
4038
false,
4139
false
@@ -52,7 +50,9 @@ public function testVoteAuthenticatesTokenIfNecessary()
5250

5351
$newToken = new UsernamePasswordToken('username', 'password', 'provider');
5452

55-
$this->authenticationManager
53+
$authenticationManager = $this->createMock(AuthenticationManagerInterface::class);
54+
$this->authorizationChecker = new AuthorizationChecker($this->tokenStorage, $authenticationManager, $this->accessDecisionManager, false, false);
55+
$authenticationManager
5656
->expects($this->once())
5757
->method('authenticate')
5858
->with($this->equalTo($token))
@@ -81,11 +81,7 @@ public function testVoteAuthenticatesTokenIfNecessary()
8181
*/
8282
public function testLegacyVoteWithoutAuthenticationToken()
8383
{
84-
$authorizationChecker = new AuthorizationChecker(
85-
$this->tokenStorage,
86-
$this->authenticationManager,
87-
$this->accessDecisionManager
88-
);
84+
$authorizationChecker = new AuthorizationChecker($this->tokenStorage, $this->accessDecisionManager);
8985

9086
$this->expectException(AuthenticationCredentialsNotFoundException::class);
9187

@@ -94,7 +90,7 @@ public function testLegacyVoteWithoutAuthenticationToken()
9490

9591
public function testVoteWithoutAuthenticationToken()
9692
{
97-
$authorizationChecker = new AuthorizationChecker($this->tokenStorage, $this->authenticationManager, $this->accessDecisionManager, false, false);
93+
$authorizationChecker = new AuthorizationChecker($this->tokenStorage, $this->accessDecisionManager, false, false);
9894

9995
$this->accessDecisionManager
10096
->expects($this->once())

Tests/Authorization/ExpressionLanguageTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Security\Core\Tests\Authorization;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
1615
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
1716
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
1817
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
@@ -37,7 +36,7 @@ public function testIsAuthenticated($token, $expression, $result)
3736
$tokenStorage = new TokenStorage();
3837
$tokenStorage->setToken($token);
3938
$accessDecisionManager = new AccessDecisionManager([new RoleVoter(), new AuthenticatedVoter($trustResolver)]);
40-
$authChecker = new AuthorizationChecker($tokenStorage, $this->createMock(AuthenticationManagerInterface::class), $accessDecisionManager, false, false);
39+
$authChecker = new AuthorizationChecker($tokenStorage, $accessDecisionManager, false, false);
4140

4241
$context = [];
4342
$context['auth_checker'] = $authChecker;

0 commit comments

Comments
 (0)