Symfony2 FOSUserBundle Role entities

jmoz picture jmoz · Dec 5, 2011 · Viewed 9.9k times · Source

I'm currently trying to figure out the best way to implement doctrine persisted Role entities as a M2M relationship compatible with FOSUserBundle. Previously I was using just strings with the default implementation and was persisting it with a doctrine array mapping.

Now I need to have roles as seperate entites as we want to build an admin backend where others can grant users roles.

Basically, it's a pain in the ass. The FOS interfaces are built for string representations, not Role entities. Change the implementation, you break a lot of stuff i.e. FOS commands to promote users. And it's hard to figure out exactly which pieces of the interfaces are needed to allow the symfony2 security system to continue working correctly.

I could rewrite the role management code and use Role entities as much as possible, e.g.:

$user->addRole(new Role('ROLE_FOO'));

But that breaks commands and possibly existing code?

Or continue using:

$user->addRole('ROLE_FOO');

And couple role/entity manager code in addRole() (bad design).

I've noticed this is a grey area (Role entities with FOS) and has been mentioned on the symfony2 boards and round here, but no decent solutions.

Anyone had any experience or can think of a decent solution?

Answer

jmoz picture jmoz · Dec 13, 2011

I decided to go with a mix of an array/ArrayCollection implementation. I tried to follow the existing interfaces as much as possible so as not to break the security system. I have documented my solution at http://blog.jmoz.co.uk/symfony2-fosuserbundle-role-entities