Symfony 2 – How-to check user’s role in Voter
Home
How-to make sure in a Voter that a user has the right role in the Symfony role hierarchy.
first, two links. One to the Symfony 2 Voter’s documentation , and the second to the Access Decision Manager’s documentation .
We will see here, how to check if the current user has the right role in the roles’s hierarchy.
In a first time we have to add a service to the Voter declaration DI. We need to use the “ security.access.role_hierarchy_voter ” service.
Your declaration, must look like that.
In your Voter class you have to retrieve the service. We just need to add a new attribute, like the code below.
The use of this service is pretty easy.
So, now, if the current user has the role ROLE_ADMIN, according to the hierarchy of roles, he has ROLE_USER to, so the voter method return a VoterInterface::ACCESS_GRANTED
Vladimir Popov says: 21 March 2016 at 16 h 16 min What if result is “ACCESS_DENIED”? -1 will also result true in condition. Reply Waldo says: 1 April 2016 at 7 h 33 min indeed, I will change that. Thanks Reply
What if result is “ACCESS_DENIED”? -1 will also result true in condition.
Waldo says: 1 April 2016 at 7 h 33 min indeed, I will change that. Thanks Reply
indeed, I will change that. Thanks
Your email address will not be published. Required fields are marked *
Comment
Name *
E-mail *
Website
This site uses Akismet to reduce spam. Learn how your comment data is processed .
English (United States)
Français