title: Symfony2: How to login using OAuth (HWIOAuthBundle) + custom roles (by default and loaded from DB)
body: <blockquote>
  <p><strong>Note:</strong> the questions are at the end of this text but I will explain in detail all the context for a better understanding.</p>
  </blockquote>
  <p>I'm developing a Symfony2 application consisting of 3 modules:</p>
  <blockquote>
  <ol>
  <li><strong>Module for students</strong> -&gt; Needs the role <strong>ROLE_STUDENT</strong></li>
  <li><strong>Module for teachers</strong> -&gt; Needs the role <strong>ROLE_TEACHER</strong></li>
  <li><strong>Module for administrators</strong> -&gt; Needs the role <strong>ROLE_ADMIN</strong></li>
  </ol>
  </blockquote>
  <p>The <strong>role hierarchy</strong> is as follows:</p>
  <blockquote>
  <ul>
  <li>ROLE_TEACHER: [ROLE_STUDENT]</li>
  <li>ROLE_ADMIN: [ROLE_TEACHER]</li>
  </ul>
  </blockquote>
  <p>Therefore:</p>
  <p>A student (with ROLE_STUDENT role) only can access to all the pages of his/her module, for example:</p>
  <ul>
  <li><a href="http://server.com/students/anything" rel="noreferrer">http://server.com/students/anything</a></li>
  </ul>
  <p>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:</p>
  <ul>
  <li><a href="http://server.com/students/anything" rel="noreferrer">http://server.com/students/anything</a></li>
  <li><a href="http://server.com/teachers/anything" rel="noreferrer">http://server.com/teachers/anything</a></li>
  </ul>
  <p>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.</p>
  <blockquote>
  <p><strong>The system uses OAuth to authenticate students and teachers through Twitter</strong>, using the bundle <strong>HWIOAuthBundle</strong> (using the
  <strong>OAuthUserProvider</strong> user provider that provides the bundle).</p>
  <p>I've achieved to successfully authenticate users but, <strong>by default,
  all users are automatically authenticated with</strong> the following roles:
  <strong>[ROLE_USER, ROLE_OAUTH_USER]</strong></p>
  </blockquote>
  <hr />
  <h2><strong>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:</strong></h2>
  <blockquote>
  <p><em><strong>Steps for login using OAuth:</strong></em></p>
  <p>A user logs in to the system and automatically:</p>
  <ul>
  <li><p><strong>If the user does not exist in the database:</strong><br />
  - Save the nickname in the users table of the database.<br />
  - Save the role ROLE_STUDENT (this is the role by default for me) in the users table of the database.<br />
  - <strong>Authenticate in the system using OAuth but using the ROLE_STUDENT role.</strong></p>
  </li>
  <li><p><strong>If the user exists in the database:</strong><br />
  - Check what role has associated the user in the database.<br />
  - <strong>Authenticate the user in the system using OAuth but using the role associated in the database (i.e.: ROLE_STUDENT or ROLE_TEACHER).</strong></p>
  </li>
  </ul>
  </blockquote>
  <p>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.</p>
  <hr />
  <h2><strong>The questions:</strong></h2>
  <ol>
  <li><p><strong>How I can authenticate a user via OAuth (HWIOAuthBundle) with the role that I want by default (ROLE_STUDENT as I explained above)?</strong></p>
  </li>
  <li><p>If a nickname exists in the database with a role associated (ROLE_STUDENT or ROLE_TEACHER), <strong>how I can authenticate a user via OAuth (HWIOAuthBundle) but using the role loaded from the database?</strong></p>
  </li>
  </ol>
  <p>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.</p>
  <p>Thank you in advance!</p>
  <p>PS: if you have any questions or doubts about anything, I'll be glad to explain as best as possible.</p>