My project has many roles: admin, HR, manager, employee. How do I implement it in a generalized way in Symfony2 such that new roles can be added in future?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Symfony2 represents authenticated users using The
UserInterfaceinterface.This interface asks you to implement the
getRolesmethod, which returns all the roles a user has.Based on how you implement this UserInterface, you can grab these roles from a database, a web service, or whatever you want.
One simple way to provide these users is the
in-memoryprovider:Another way is to use the EntityProvider. For more details look at this cookbook entry.
In this last example, user roles are statically stored in a harcoded array, but they could come from another table, using table associations. That’s how it’s done in the FOSUserBundle.
You can even define a role hierarchy:
So that any user that has role ROLE_ADMIN has also inherited the role ROLE_USER.
For more info look at the docs.