Note: the questions are at the end of this text but I will explain in detail all the context for a better understanding.
I'm developing a Symfony2 application consisting of 3 modules:
- Module for students -> Needs the role ROLE_STUDENT
- Module for teachers -> Needs the role ROLE_TEACHER
- Module for administrators -> Needs the role ROLE_ADMIN
The role hierarchy is as follows:
- ROLE_TEACHER: [ROLE_STUDENT]
- ROLE_ADMIN: [ROLE_TEACHER]
Therefore:
A student (with ROLE_STUDENT role) only can access to all the pages of his/her module, for example:
A teacher (with ROLE_TEACHER role) can access to all the pages of the students' module and all the pages of the teachers' module, such as:
An administrator (with ROLE_ADMIN role) can access to all the pages of the students' module, all the pages of the teachers' module and the admin backend.
The system uses OAuth to authenticate students and teachers through Twitter, using the bundle HWIOAuthBundle (using the OAuthUserProvider user provider that provides the bundle).
I've achieved to successfully authenticate users but, by default, all users are automatically authenticated with the following roles: [ROLE_USER, ROLE_OAUTH_USER]
So here is what I have done. Below, I'm going to explain what I want that my app do but I don't know the way:
Steps for login using OAuth:
A user logs in to the system and automatically:
If the user does not exist in the database:
- Save the nickname in the users table of the database.
- Save the role ROLE_STUDENT (this is the role by default for me) in the users table of the database.
- Authenticate in the system using OAuth but using the ROLE_STUDENT role.If the user exists in the database:
- Check what role has associated the user in the database.
- Authenticate the user in the system using OAuth but using the role associated in the database (i.e.: ROLE_STUDENT or ROLE_TEACHER).
The administrator (from the administration backend) can see the list of nicknames (used by Twitter but saved in the database) and the assigned roles for each nickname. The administrator should be able to change the role of users between ROLE_STUDENT and ROLE_TEACHER.
The questions:
How I can authenticate a user via OAuth (HWIOAuthBundle) with the role that I want by default (ROLE_STUDENT as I explained above)?
If a nickname exists in the database with a role associated (ROLE_STUDENT or ROLE_TEACHER), how I can authenticate a user via OAuth (HWIOAuthBundle) but using the role loaded from the database?
I've been reading a lot about this subject but I'm new with Symfony2 and I don't know what is the best and easiest way to solve it.
Thank you in advance!
PS: if you have any questions or doubts about anything, I'll be glad to explain as best as possible.
UPDATE:
I tried using this code in one of my recent projects and it didn't work anymore. The reason is that HWIOAuthBundle has updated a few times and the config files are not the same. I put the code above as well as login to a few other social networks in a github which you can find at HWIOAuthBundleByExample.
I have a week experience with symfony2 and in the past days that's what i was working myself. Found your question today (when i was still researching).
I'm gonna present you what I had to do, based on what data, and how i did it. After that, I'll try giving you some key links and I hope you'll manage to modelate for your needs.
My app needs Facebook login and ADMIN role. Because will only be a few administrators, I only need a few Facebook IDs when authentificating, so I store them in a yaml array. (See at the end how you can load them from database).
Here's what I did:
So you can pretty much see that when facebook login response comes, I do the database checks (based on the facebook graph ID), and add it if needed. Also, I set some things in sessions (you won't need this), and after that I must also return a HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUser object (this is where the sf2 gets it's roles). So I extend it (in this way i have acces to $isAdmin). As you said, you need roles for each user and you must also edit them. For that, you can implement getRoles() with a ManyToMany relationship (give access to doctrine entityManager via constructor). You can see that applied here: http://symfony.com/doc/current/cookbook/security/entity_provider.html#managing-roles-in-the-database .
As I said, you must tweak it a lot (my only app is facebook-only login and with a in_memory like security access), but I wish myself I had a code like that when i started. So I really hope this hels you. Post your questions, if any.