Skip to content

Conversation

@XWB
Copy link
Contributor

@XWB XWB commented Oct 29, 2019

This should resolve the following errors:

The getReachableRoles() method of the RoleHierarchyInterface is deprecated
The getRoles() method of the TokenInterface is deprecated

* @param TokenInterface $token
*
* @return array
*/
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest removing the full dobclock here, it doesn't provide enough value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolas-grekas
Copy link
Member

Thank you @XWB.

nicolas-grekas added a commit that referenced this pull request Dec 11, 2019
This PR was merged into the 3.0-dev branch.

Discussion
----------

Fixed Symfony 4.3 deprecated messages

This should resolve the following errors:

> The getReachableRoles() method of the RoleHierarchyInterface is deprecated
> The getRoles() method of the TokenInterface is deprecated

Commits
-------

d1a4c3f Fixed Symfony 4.3 deprecated messages
@nicolas-grekas nicolas-grekas merged commit d1a4c3f into symfony:master Dec 11, 2019
}
} 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.

nicolas-grekas added a commit that referenced this pull request Dec 12, 2019
This PR was merged into the 3.0-dev branch.

Discussion
----------

Fixed Symfony 4.3 wrong role variable type

This should fix work in symfony < 4.3

Original error from logs symfony 3.4 application:
```
Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError:
"Call to a member function getRole() on string" at
vendor/symfony/symfony/src/Symfony/Component/Security/Core/Role/RoleHierarchy.php line 41
{"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0):
Call to a member function getRole() on string at
vendor/symfony/symfony/src/Symfony/Component/Security/Core/Role/RoleHierarchy.php:41
```

Found in #50 (comment)

Commits
-------

489dc66 Fixed Symfony 4.3 wrong role variable type
@XWB XWB deleted the s43-roles branch December 16, 2019 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants