I'm using Symfony 2.1 for a project. I use the FOSUserBundle for managing users & SonataAdminBundle for administration usage.
I have some questions about that:
As an admin, I want to set roles from users in users edit form. How can I have access to roles in
role_hierarchy
? And how can I use them as choice fields so the admin can set roles to users?When I show roles in a list, it is shown as string like this:
[0 => ROLE_SUPER_ADMIN] [1 => ROLE_USER]
How can I change it to this?
ROLE_SUPER_ADMIN, ROLE_USER
I mean, having just the value of the array.
Just to overplay it a bit, here is my enhanced version of Romain Bruckert and Sash which gives you an array like this:
This helps you find all roles, that include a specific role:![enter image description here](https://i.stack.imgur.com/2qgFc.png)
I know its much code, it could be done much better, but maybe it helps somebody or you can at least use pieces of this code.
The second answer is below. Add lines in sonata admin yml file .
and in
user_roles.html.twig
files add below linesthen into your admin controller and in
configureListFields
function add this linehope this will solve your problem
Based on the answer of @parisssss although it was wrong, here is a working solution. The Sonata input field will show all the roles that are under the given role if you save one role.
On the other hand, in the database will be stored only the most important role. But that makes absolute sense in the Sf way.
And in another method:
See it in action:
Romain Bruckert's solution is almost perfect, except that it doesn't allow to set roles, which are roots of role hierrarchy. For instance ROLE_SUPER_ADMIN. Here's the fixed method flattenRoles, which also returns root roles:
Edit: TrtG already posted this fix in comments
i found an answer for my first question!(but the second one in not answered yet..) i add the roles like below in
configureFormFields
function :I would be very happy if anyone answers the second question :)
As for the second question I added a method in the User class that looks like this
And then you can declare in your
configureListFields
function: