title: Symfony2 security - There is no user provider for user "Ibw\UserBundle\Entity\User"
body: <p>I'm creating a custom entity provider for my <code>User</code> entity. But when I enter the username and password in the login form I get this exception :</p>
  
  <pre><code>There is no user provider for user "Ibw\UserBundle\Entity\User".
  </code></pre>
  
  <p>When I switch back to the default Doctrine entity provider, Everything goes well.</p>
  
  <p>First here's my <code>security.yml</code>:</p>
  
  <pre><code>security:
      role_hierarchy:
          ROLE_ADMIN:       ROLE_USER
          ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  
      firewalls:
          dev:
              pattern:  ^/(_(profiler|wdt)|css|images|js)/
              security: false
          secured_area:
              pattern:    ^/
              anonymous: ~
              form_login:
                  login_path:   /login
                  check_path:   /login_check
                  default_target_path:  ibw_job_index
              logout:
                  path:   /logout
                  target: /
  
      access_control:
          - { path: ^/admin, roles: ROLE_ADMIN }
  
      providers:
          default_provider:
              entity: { class: Ibw\UserBundle\Entity\User }
  
      encoders:
          Ibw\UserBundle\Entity\User: sha512
  </code></pre>
  
  <p>Here's the <code>Ibw\UserBundle\Entity\User</code> class:</p>
  
  <pre><code>&lt;?php
  namespace Ibw\UserBundle\Entity;
  
  use Doctrine\Bundle\DoctrineBundle\Registry;
  use Symfony\Component\DependencyInjection\ContainerInterface;
  use Symfony\Component\Security\Core\Role\Role;
  use Symfony\Component\Security\Core\User\UserInterface;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Class User
   * @ORM\Table(name="users")
   * @ORM\Entity(repositoryClass="Ibw\UserBundle\Repository\UserRepository")
   */
  class User implements UserInterface, \Serializable
  {
      /**
       * @ORM\Column(type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
      /**
       * @ORM\Column(type="string", length=25, unique=true)
       */
      protected $username;
  
      /**
       * @ORM\Column(type="string", length=40)
       */
      protected $salt;
  
      /**
       * @ORM\Column(type="string", length=128)
       */
      protected $password;
  
      public function __construct(ContainerInterface $container)
      {
          $this-&gt;isActive = true;
          $this-&gt;salt = md5(uniqid(null, true));
          $this-&gt;container = $container;
      }
  
      public function setUsername($username)
      {
          $this-&gt;username = $username;
          return $this;
      }
  
      public function setPassword($password)
      {
          $encoder = $this-&gt;container-&gt;get('security.encoder_factory')-&gt;getEncoder($this);
          $this-&gt;password = $encoder-&gt;encodePassword($password, $this-&gt;getSalt());
          return $this;
      }
  
      /**
       * @return Role[] The user roles
       */
      public function getRoles()
      {
          return array('ROLE_ADMIN');
      }
  
      public function getPassword()
      {
          return $this-&gt;password;
      }
  
      public function getSalt()
      {
          return $this-&gt;salt;
      }
  
      public function getUsername()
      {
          return $this-&gt;username;
      }
  
      public function eraseCredentials()
      {
          // TODO: Implement eraseCredentials() method.
      }
  
      public function serialize()
      {
          return serialize(array($this-&gt;id));
      }
  
      public function unserialize($serialized)
      {
          list($this-&gt;id) = unserialize($serialized);
      }
  
  
      public function getId()
      {
          return $this-&gt;id;
      }
  
      public function setSalt($salt)
      {
          $this-&gt;salt = $salt;
  
          return $this;
      }
  }
  </code></pre>
  
  <p>And finally, here's my <code>Ibw\UserBundle\Repository\UserRepository</code> class:</p>
  
  <pre><code>&lt;?php
  
  namespace Ibw\UserBundle\Repository;
  
  use Doctrine\ORM\EntityRepository;
  use Doctrine\ORM\NoResultException;
  use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  use Symfony\Component\Security\Core\User\UserInterface;
  use Symfony\Component\Security\Core\User\UserProviderInterface;
  
  class UserRepository extends EntityRepository implements UserProviderInterface
  {
  
      public function loadUserByUsername($username)
      {
          $q = $this-&gt;createQueryBuilder('u')
                      -&gt;select('u')
                      -&gt;where('u.username = :username')
                      -&gt;setParameter('username', $username)
                      -&gt;getQuery();
  
          try
          {
              $user = $q-&gt;getSingleResult();
          }
          catch (NoResultException $e)
          {
              $message = sprintf('Unable to find active admin identified by %s', $username);
              throw new UsernameNotFoundException($message, 0, $e);
          }
          return $user;
      }
  
  
      public function refreshUser(UserInterface $user)
      {
          $class = get_class($user);
          if(!$this-&gt;supportsClass($user))
          {
              $message = sprintf('Unsupported class type : %s', $class);
              throw new UnsupportedUserException($message);
          }
  
          return $this-&gt;find($user-&gt;getId());
      }
  
  
      public function supportsClass($class)
      {
          return $this-&gt;getEntityName() === $class || is_subclass_of($class, $this-&gt;getEntityName());
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>I tried to define it as a service also, in <code>JobeetBundle/Resources/config/services.yml</code>:</p>
  
  <pre><code>parameters:
      ibw.user.provider.class: Ibw\UserBundle\Repository\UserRepository
      ibw.user.provider.entity.class: Ibw\UserBundle\Entity\User
  
  services:
      ibw.user.provider:
          class: %ibw.user.provider.class%
          factory_service: doctrine
          factory_method: getRepository
          arguments:
              - %ibw.user.provider.entity.class%
  </code></pre>
  
  <p>And in <code>jobeet/app/config/security.yml</code>:</p>
  
  <pre><code>providers:
      default_provider:
           id: ibw.user.provider
  encoders:
      Ibw\UserBundle\Entity\User: sha512
  </code></pre>
  
  <p>But unfortunately it gives me the exact same error after I post the login form.</p>