2

I'm trying to do my own custom user provider, but I'm stuck at this part of the Symfony tutorial (Create a User Provider).

What do I need to do at the $userdata variable? A database connection?

I don't know what I need to do at this place:

public function loadUserByUsername($username)
{
    // here
    // $userData = ...
    if ($userData) {
        //here too
        //$password = ...
        return new DomixBlogUser($username, $password, $salt, $roles)
    } else {
        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
    }
}

edit #1

public function loadUserByUsername($username)
{
    $userData = $this->_em->getRepository("DomixBlogBundle:User")->findOneBy(array('username' => $username));
    var_dump($userData);
    if ($userData) {
        $salt = '54hg5g4hfjh4g5sdgf45gd4h84gjhdf54gf4g2f2gfdhggfdg';
        $password = $userData->getPassword().'{'.$salt.'}';

        return new DomixBlogUser($username, $password, $salt, $roles);
    } else {
        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
    }
}

that's good?

j0k
  • 22,600
  • 28
  • 79
  • 90
Stickly
  • 311
  • 1
  • 2
  • 13

1 Answers1

2

Yes, most likely it would be from a database connection. Really though it could be from anywhere. What it's saying is that your loadUserByUsername method should get some "user data" from whatever location/service is storing your users based on the given username and then return that.

public function loadUserByUsername($username)
{
    $userData = $this->_em->getRepository("DomixBlogBundle:User")->findOneBy(array('username' => $username));

    if (null !== $userData) {

        return $userData;
    } else {
        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
    }
}

You can see an example of this in the default symfony entity user provider.

MDrollette
  • 6,887
  • 1
  • 36
  • 49
  • so, I will be able to do: $this->getDoctrine()->getEntityManager()->getRepository('DomixBlogBundle:User')->find($username) ? – Stickly Jun 24 '12 at 20:38
  • in order to have access to the entity manager you will need to inject it into the UserProviders constructor in the service definition. `$this->getDoctrine()` is only a helper method in the Controller. – MDrollette Jun 24 '12 at 20:42
  • see here for injecting the entity manager into your service http://stackoverflow.com/questions/10427282/symfony-2-entitymanager-injection-in-service – MDrollette Jun 24 '12 at 20:46
  • for now, I wrote: $userData = $this->_em->getRepository("DomixBlogBundle:User")->findOneBy(array('username' => $username));, return null if no user (good). how I can use $password? – Stickly Jun 24 '12 at 22:21
  • That looks fine to me. The password is dealt with later on. This method is only concerned with getting the user object from the database. The actual authentication happens elsewhere. You can see the default entity user manager as an example here https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php#L52 – MDrollette Jun 24 '12 at 22:39
  • not exactly. You are already fetching the user from the database. So you don't need to manually create another user. Just check that $userData !== null and then return it. – MDrollette Jun 24 '12 at 23:29
  • I just need to `return $userData?` – Stickly Jun 24 '12 at 23:35
  • 1
    i don't know why, but i can't log in (always say `Bad credentials`) – Stickly Jun 25 '12 at 14:23