I'm currently working on a Symfony2 project. It is based on Sonata and uses SonataUserBundle/FOSUserBundle for managing users. The initial development has been badly done and I'm in the process of refactoring a lot of it.
The application defines three level of users:
- Administrators
- Consultants
- Businesses
Those are defined as Groups with FOSUserBundle and have a bunch of roles attached to them.
Now everywhere in the code, the previous developers have used (hardcoded) the database IDs of the groups to make cases like this:
$userGroup = $em->getRepository('ApplicationSonataUserBundle:Group')->findOneByName($group_name);
$userGroupId = $userGroup->getId();
if ($userGroupId == 1) {
// Administrator case
...
} else if ($userGroupId == 7) {
// Consultant case
...
}
This is obviously very bad.
My problem is that I'm sure how I can refactor this in a good fashion.
I see three possible way of doing so:
Work directly with the roles defined: ROLE_ADMIN, ROLE_CONSULTANT, ROLE_BUSINESS;
Still use the groups but have some constants with the group name to avoid repeating them all over the place;
Use a multi-user system on top of FOSUserBundle. I quickly reviewed RollerworksMultiUserBundle and PUGXMultiUserBundle. Although I don't know if this is not a bit overhead as my users actually share the same information.
If you guys can point me in the right direction, it would be much appreciated. Maybe there is a complete other way to achieve this properly.