diff --git a/src/DoctrineAuditBundle/User/TokenStorageUserProvider.php b/src/DoctrineAuditBundle/User/TokenStorageUserProvider.php index d9660bce..7d1e86b6 100644 --- a/src/DoctrineAuditBundle/User/TokenStorageUserProvider.php +++ b/src/DoctrineAuditBundle/User/TokenStorageUserProvider.php @@ -38,7 +38,12 @@ public function getUser(): ?UserInterface } $impersonation = ''; - if ($this->security->isGranted('ROLE_PREVIOUS_ADMIN')) { + // The ROLE_PREVIOUS_ADMIN role is deprecated since Symfony 5.1 and will be removed in version 6.0. + $impersonationAttribute + = \defined('\Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter::IS_IMPERSONATOR') + ? 'IS_IMPERSONATOR' + : 'ROLE_PREVIOUS_ADMIN'; + if ($this->security->isGranted($impersonationAttribute)) { // Symfony > 4.3 if ($token instanceof SwitchUserToken) { $impersonatorUser = $token->getOriginalToken()->getUser(); diff --git a/tests/DoctrineAuditBundle/CoreTest.php b/tests/DoctrineAuditBundle/CoreTest.php index 7949268b..cb8a022e 100644 --- a/tests/DoctrineAuditBundle/CoreTest.php +++ b/tests/DoctrineAuditBundle/CoreTest.php @@ -232,11 +232,16 @@ protected function createAuditConfiguration(array $options = [], ?EntityManager $user->setRoles(['ROLE_ADMIN']); $tokenStorage->setToken(new UsernamePasswordToken($user, '12345', 'provider', $user->getRoles())); + // The ROLE_PREVIOUS_ADMIN role is deprecated since Symfony 5.1 and will be removed in version 6.0. + $impersonationAttribute + = \defined('\Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter::IS_IMPERSONATOR') + ? 'IS_IMPERSONATOR' + : 'ROLE_PREVIOUS_ADMIN'; $authorizationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock(); $authorizationChecker ->expects(static::any()) ->method('isGranted') - ->with('ROLE_PREVIOUS_ADMIN') + ->with($impersonationAttribute) ->willReturn(true) ; diff --git a/tests/DoctrineAuditBundle/User/TokenStorageUserProviderTest.php b/tests/DoctrineAuditBundle/User/TokenStorageUserProviderTest.php index 36a26fad..240b0f3c 100644 --- a/tests/DoctrineAuditBundle/User/TokenStorageUserProviderTest.php +++ b/tests/DoctrineAuditBundle/User/TokenStorageUserProviderTest.php @@ -23,12 +23,18 @@ final class TokenStorageUserProviderTest extends TestCase protected function setUp(): void { + // The ROLE_PREVIOUS_ADMIN role is deprecated since 5.1 and will be removed in version 6.0. + $impersonationAttribute + = \defined('\Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter::IS_IMPERSONATOR') + ? 'IS_IMPERSONATOR' + : 'ROLE_PREVIOUS_ADMIN'; + $this->tokenStorage = new TokenStorage(); $this->authorizationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock(); $this->authorizationChecker ->expects(self::any()) ->method('isGranted') - ->with('ROLE_PREVIOUS_ADMIN') + ->with($impersonationAttribute) ->willReturn(true) ; } @@ -57,7 +63,12 @@ public function testGetUserWhenUserIsDefined(): void if (class_exists('\Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken')) { $token2 = new SwitchUserToken($user2, '12345', 'provider', $user2->getRoles(), $token1); } else { - $user2->setRoles(['ROLE_USER', 'ROLE_PREVIOUS_ADMIN', new SwitchUserRole('ROLE_ADMIN', $token1)]); + // The ROLE_PREVIOUS_ADMIN role is deprecated since Symfony 5.1 and will be removed in version 6.0. + $impersonationAttribute + = \defined('\Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter::IS_IMPERSONATOR') + ? 'IS_IMPERSONATOR' + : 'ROLE_PREVIOUS_ADMIN'; + $user2->setRoles(['ROLE_USER', $impersonationAttribute, new SwitchUserRole('ROLE_ADMIN', $token1)]); $token2 = new UsernamePasswordToken($user2, '12345', 'provider', $user2->getRoles()); }