Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
Compatibility with Symfony 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
scheb committed Oct 30, 2020
1 parent 626772d commit 78f5832
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Security/Authorization/TwoFactorAccessDecider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Security\Http\AccessMapInterface;
use Symfony\Component\Security\Http\Firewall\AccessListener;
use Symfony\Component\Security\Http\HttpUtils;
Expand Down Expand Up @@ -50,10 +51,17 @@ public function isAccessible(Request $request, TokenInterface $token): bool
{
// Let routes pass, e.g. if a route needs to be callable during two-factor authentication
list($attributes) = $this->accessMap->getPatterns($request);

// Compatibility for Symfony 5.1
if (\defined(AccessListener::class.'::PUBLIC_ACCESS') && [AccessListener::PUBLIC_ACCESS] === $attributes) {
return true;
}

// Compatibility for Symfony 5.2+
if (\defined(AuthenticatedVoter::class.'::PUBLIC_ACCESS') && [AuthenticatedVoter::PUBLIC_ACCESS] === $attributes) {
return true;
}

if (null !== $attributes && $this->accessDecisionManager->decide($token, $attributes, $request)) {
return true;
}
Expand Down
20 changes: 19 additions & 1 deletion Tests/Security/Authorization/TwoFactorAccessDeciderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Security\Http\AccessMapInterface;
use Symfony\Component\Security\Http\Firewall\AccessListener;
use Symfony\Component\Security\Http\HttpUtils;
Expand Down Expand Up @@ -137,8 +138,25 @@ public function isAccessible_pathAccessGranted_returnTrue(): void
public function isAccessible_isPublic_returnTrue(): void
{
$this->requireAtLeastSymfony5_1();
$publicAccess = null;

$this->stubAccessMapReturnsAttributes([AccessListener::PUBLIC_ACCESS]);
// Compatibility with Symfony 5.1
if (\defined(AccessListener::class.'::PUBLIC_ACCESS')) {
$publicAccess = AccessListener::PUBLIC_ACCESS;
}

// Compatibility for Symfony 5.2+
if (\defined(AuthenticatedVoter::class.'::PUBLIC_ACCESS')) {
$publicAccess = AuthenticatedVoter::PUBLIC_ACCESS;
}

if (null === $publicAccess) {
$this->fail('Could not find PUBLIC_ACCESS constant.');

return;
}

$this->stubAccessMapReturnsAttributes([$publicAccess]);
$this->whenRequestBaseUrl('');
$this->whenGeneratedLogoutPath(self::LOGOUT_PATH);
$this->whenPathAccess(false);
Expand Down

0 comments on commit 78f5832

Please sign in to comment.