8000 [SecurityHttp] Fix incompatibility with 6.0 · symfony/symfony@96532e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 96532e5

Browse files
committed
[SecurityHttp] Fix incompatibility with 6.0
1 parent fb45f6b commit 96532e5

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

src/Symfony/Component/Security/Http/Authenticator/Token/PostAuthenticationToken.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public function __construct(UserInterface $user, string $firewallName, array $ro
3535
$this->firewallName = $firewallName;
3636

3737
// @deprecated since Symfony 5.4
38-
// this token is meant to be used after authentication success, so it is always authenticated
39-
$this->setAuthenticated(true, false);
38+
if (method_exists($this, 'setAuthenticated')) {
39+
// this token is meant to be used after authentication success, so it is always authenticated
40+
$this->setAuthenticated(true, false);
41+
}
4042
}
4143

4244
/**

src/Symfony/Component/Security/Http/EventListener/CheckCredentialsListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function checkPassport(CheckPassportEvent $event): void
7474
throw new BadCredentialsException('The presented password is invalid.');
7575
}
7676

77-
$salt = $user->getSalt();
77+
$salt = method_exists($user, 'getSalt') ? $user->getSalt() : '';
7878
if ($salt && !$user instanceof LegacyPasswordAuthenticatedUserInterface) {
7979
trigger_deprecation('symfony/security-http', '5.3', 'Returning a string from "getSalt()" without implementing the "%s" interface is deprecated, the "%s" class should implement it.', LegacyPasswordAuthenticatedUserInterface::class, get_debug_type($user));
8080
}

src/Symfony/Component/Security/Http/Firewall/AccessListener.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(TokenStorageInterface $tokenStorage, AccessDecisionM
5454
$this->tokenStorage = $tokenStorage;
5555
$this->accessDecisionManager = $accessDecisionManager;
5656
$this->map = $map;
57-
$this->authManager = $authManager ?? new NoopAuthenticationManager();
57+
$this->authManager = $authManager ?? (class_exists(AuthenticationManagerInterface::class) ? new NoopAuthenticationManager() : null);
5858
$this->exceptionOnNoToken = $exceptionOnNoToken;
5959
}
6060

@@ -66,7 +66,14 @@ public function supports(Request $request): ?bool
6666
[$attributes] = $this->map->getPatterns($request);
6767
$request->attributes->set('_access_control_attributes', $attributes);
6868

69-
return $attributes && ([AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY] !== $attributes && [AuthenticatedVoter::PUBLIC_ACCESS] !== $attributes) ? true : null;
69+
if ($attributes && (
70+
(\defined(AuthenticatedVoter::class.'::IS_AUTHENTICATED_ANONYMOUSLY') ? [AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY] !== $attributes : true)
71+
&& [AuthenticatedVoter::PUBLIC_ACCESS] !== $attributes
72+
)) {
73+
return true;
74+
}
75+
76+
return null;
7077
}
7178

7279
/**
@@ -86,13 +93,10 @@ public function authenticate(RequestEvent $event)
8693
$attributes = $request->attributes->get('_access_control_attributes');
8794
$request->attributes->remove('_access_control_attributes');
8895

89-
if (
90-
!$attributes
91-
|| (
92-
([AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY] === $attributes || [AuthenticatedVoter::PUBLIC_ACCESS] === $attributes)
93-
&& $event instanceof LazyResponseEvent
94-
)
95-
) {
96+
if (!$attributes || ((
97+
(\defined(AuthenticatedVoter::class.'::IS_AUTHENTICATED_ANONYMOUSLY') ? [AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY] === $attributes : false)
98+
|| [AuthenticatedVoter::PUBLIC_ACCESS] === $attributes
99+
) && $event instanceof LazyResponseEvent)) {
96100
return;
97101
}
98102

@@ -109,10 +113,13 @@ public function authenticate(RequestEvent $event)
109113
}
110114

111115
// @deprecated since Symfony 5.4
112-
if (!$token->isAuthenticated(false)) {
116+
if (method_exists($token, 'isAuthenticated') && !$token->isAuthenticated(false)) {
113117
trigger_deprecation('symfony/core', '5.4', 'Returning false from "%s()" is deprecated and won\'t have any effect in Symfony 6.0 as security tokens will always be considered authenticated.');
114-
$token = $this->authManager->authenticate($token);
115-
$this->tokenStorage->setToken($token);
118+
119+
if ($this->authManager) {
120+
$token = $this->authManager->authenticate($token);
121+
$this->tokenStorage->setToken($token);
122+
}
116123
}
117124

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

src/Symfony/Component/Security/Http/Firewall/ContextListener.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ protected function refreshUser(TokenInterface $token): ?TokenInterface
240240
if ($this->hasUserChanged($user, $newToken)) {
241241
$userDeauthenticated = true;
242242
// @deprecated since Symfony 5.4
243-
$newToken->setAuthenticated(false, false);
243+
if (method_exists($newToken, 'setAuthenticated')) {
244+
$newToken->setAuthenticated(false, false);
245+
}
244246

245247
if (null !== $this->logger) {
246248
// @deprecated since Symfony 5.3, change to $refreshedUser->getUserIdentifier() in 6.0

src/Symfony/Component/Security/Http/Tests/Firewall/AccessListenerTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,12 @@ public function testHandleWhenThereIsNoAccessMapEntryMatchingTheRequest()
166166
;
167167

168168
$token = $this->createMock(TokenInterface::class);
169-
$token
170-
->expects($this->never())
171-
->method('isAuthenticated')
172-
;
169+
if (method_exists(TokenInterface::class, 'isAuthenticated')) {
170+
$token
171+
->expects($this->never())
172+
->method('isAuthenticated')
173+
;
174+
}
173175

174176
$tokenStorage = $this->createMock(TokenStorageInterface::class);
175177
$tokenStorage
@@ -386,7 +388,7 @@ public function testLazyPublicPagesShouldNotAccessTokenStorage()
386388
->willReturn([[AuthenticatedVoter::PUBLIC_ACCESS], null])
387389
;
388390

389-
$listener = new AccessListener($tokenStorage, $this->createMock(AccessDecisionManagerInterface::class), $accessMap, $this->createMock(AuthenticationManagerInterface::class), false);
391+
$listener = new AccessListener($tokenStorage, $this->createMock(AccessDecisionManagerInterface::class), $accessMap, false);
390392
$listener(new LazyResponseEvent(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST)));
391393
}
392394

@@ -406,7 +408,7 @@ public function testLegacyLazyPublicPagesShouldNotAccessTokenStorage()
406408
->willReturn([[AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY], null])
407409
;
408410

409-
$listener = new AccessListener($tokenStorage, $this->createMock(AccessDecisionManagerInterface::class), $accessMap, $this->createMock(AuthenticationManagerInterface::class), false);
411+
$listener = new AccessListener($tokenStorage, $this->createMock(AccessDecisionManagerInterface::class), $accessMap, false);
410412
$listener(new LazyResponseEvent(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST)));
411413
}
412414
}

0 commit comments

Comments
 (0)
0