Skip to content

Fixed Symfony 4.3 deprecated messages #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions Domain/SecurityIdentityRetrievalStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Acl\Model\SecurityIdentityRetrievalStrategyInterface;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;

Expand Down Expand Up @@ -57,8 +58,16 @@ public function getSecurityIdentities(TokenInterface $token)
}

// add all reachable roles
foreach ($this->roleHierarchy->getReachableRoles($token->getRoles()) as $role) {
$sids[] = new RoleSecurityIdentity($role);
$roles = $this->getRoleNames($token);
if (method_exists($this->roleHierarchy, 'getReachableRoleNames')) {
foreach ($this->roleHierarchy->getReachableRoleNames($roles) as $role) {
$sids[] = new RoleSecurityIdentity($role);
}
} else {
// Symfony < 4.3 BC layer
foreach ($this->roleHierarchy->getReachableRoles($roles) as $role) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/symfony/security-core/blob/4.2/Role/RoleHierarchy.php#L41
getReachableRoles expected array of RoleInterface. Array of string given.
I think $roles = $this->getRoleNames($token); need move into condition if (method_exists($this->roleHierarchy, 'getReachableRoleNames')) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please send a PR if there's something to fix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick reply. Yes, of course, I’m doing it right now...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Andrej-in-ua That might trigger another deprecation warning. I think changing the getRoleNames method should do the trick.

You can try to change this:

    private function getRoleNames(TokenInterface $token): array
    {
        if (method_exists($token, 'getRoleNames')) {
            return $token->getRoleNames();
        }
        // Symfony < 4.3 BC layer
        return array_map(function (Role $role) {
            return $role->getRole();
        }, $token->getRoles());
    }

Into this:

    private function getRoleNames(TokenInterface $token): array
    {
        if (method_exists($token, 'getRoleNames')) {
            return $token->getRoleNames();
        }

        return $token->getRoles();
    }

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XWB In this case, we will rely on the fact that if there is a method getRoleNames exists, then there will definitely exists getReachableRoleNames. I do not know all the nuances so well. But if you confirm that it is ok, then I do PR.
At least right after I deal with the tests :/

In my project both solution worked.

$sids[] = new RoleSecurityIdentity($role);
}
}

// add built-in special roles
Expand All @@ -75,4 +84,16 @@ public function getSecurityIdentities(TokenInterface $token)

return $sids;
}

private function getRoleNames(TokenInterface $token): array
{
if (method_exists($token, 'getRoleNames')) {
return $token->getRoleNames();
}

// Symfony < 4.3 BC layer
return array_map(function (Role $role) {
return $role->getRole();
}, $token->getRoles());
}
}
20 changes: 15 additions & 5 deletions Tests/Domain/SecurityIdentityRetrievalStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,21 @@ public function testGetSecurityIdentities($user, array $roles, $authenticationSt
->setMockClassName($class)
->getMock();
}
$token
->expects($this->once())
->method('getRoles')
->will($this->returnValue(array('foo')))
;

if (method_exists($token, 'getRoleNames')) {
$token
->expects($this->once())
->method('getRoleNames')
->will($this->returnValue(array('foo')))
;
} else {
$token
->expects($this->once())
->method('getRoles')
->will($this->returnValue(array('foo')))
;
}

if ('anonymous' === $authenticationStatus) {
$token
->expects($this->never())
Expand Down