Skip to content

Commit

Permalink
Remove the ROLE_PREVIOUS_ADMIN deprecation warning from Symfony 5.1 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rcapile authored Nov 10, 2020
1 parent 9e0507e commit 6ac7073
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/DoctrineAuditBundle/User/TokenStorageUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion tests/DoctrineAuditBundle/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;

Expand Down
15 changes: 13 additions & 2 deletions tests/DoctrineAuditBundle/User/TokenStorageUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;
}
Expand Down Expand Up @@ -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());
}

Expand Down

0 comments on commit 6ac7073

Please sign in to comment.