title: Symfony 2 - Loading roles from database
body: <p>My roles are stored in the database and I am trying to load them dynamically upon login. What I'm doing is querying for the roles and setting them on the user object in my user provider as seen here:</p>
  
  <pre><code>public function loadUserByUsername($username) {
      $q = $this
          -&gt;createQueryBuilder('u')
          -&gt;where('u.username = :username')
          -&gt;setParameter('username', $username)
          -&gt;getQuery()
      ;
  
      try {
          // The Query::getSingleResult() method throws an exception
          // if there is no record matching the criteria.
          $user = $q-&gt;getSingleResult();
  
          // Permissions
          $permissions = $this-&gt;_em-&gt;getRepository('E:ModulePermission')-&gt;getPermissionsForUser($user);
  
          if ($permissions !== null) {
              foreach ($permissions as $permission) {
                  $name = strtoupper(str_replace(" ", "_", $permission['name']));
  
                  $role = "ROLE_%s_%s";
  
                  if ($permission['view']) {
                      $user-&gt;addRole(sprintf($role, $name, 'VIEW'));
                  }
  
                  if ($permission['add']) {
                      $user-&gt;addRole(sprintf($role, $name, 'ADD'));
                  }
  
                  if ($permission['edit']) {
                      $user-&gt;addRole(sprintf($role, $name, 'EDIT'));
                  }
  
                  if ($permission['delete']) {
                      $user-&gt;addRole(sprintf($role, $name, 'DELETE'));
                  }
              }
          }
  
      } catch (NoResultException $e) {
          throw new UsernameNotFoundException(sprintf('Unable to find an active admin Entity:User object identified by "%s".', $username), null, 0, $e);
      }
  
      return $user;
  }
  </code></pre>
  
  <p>And the user entity:</p>
  
  <pre><code>class User implements AdvancedUserInterface, \Serializable {
      ....
  
      protected $roles;
  
      ....
  
      public function __construct() {
          $this-&gt;salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
          $this-&gt;roles = array();
      }
  
  
      ....
  
  
      public function getRoles() {
          $roles = $this-&gt;roles;
  
          // Ensure we having something
          $roles[] = static::ROLE_DEFAULT;
  
          return array_unique($roles);
      }
  
      public function addRole($role) {
          $role = strtoupper($role);
          $roles = $this-&gt;getRoles();
          if ($role === static::ROLE_DEFAULT) {
              return $this;
          }
  
          if (!in_array($role, $roles, true)) {
              $this-&gt;roles[] = $role;
          }
  
          return $this;
      }
  
      public function hasRole($role) {
          $role = strtoupper($role);
          $roles = $this-&gt;getRoles();
          return in_array($role, $roles, true);
      }
  }
  </code></pre>
  
  <p>This works fine and dandy and I see the correct roles when I do:</p>
  
  <pre><code>$this-&gt;get('security.context')-&gt;getUser()-&gt;getRoles()
  </code></pre>
  
  <p>The problem (I think), is that the token does not know about these roles. Because calling getRoles() on the token is showing only ROLE_USER, which is the default role. </p>
  
  <p>It seems to me that the token is being created before the user is loaded by the UserProvider. I've looked through a lot of the security component but I can't for the life of me find the right part of the process to hook into to set these roles correctly so that the token knows about them.</p>
  
  <p><strong>Update</strong> Following the Load roles from database doc works fine, but this does not match my use case as shown here. My schema differs as each role has additional permissions (view/add/edit/delete) and this is why I am attempting the approach here. I don't want to have to alter my schema just to work with Symfony's security. I'd rather understand why these roles are not properly bound (not sure the correct doctrine word here) on my user object at this point.</p>