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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

so, I will be able to do: $this->getDoctrine()->getEntityManager()->getRepository('DomixBlogBundle:User')->find($username) ?
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.
see here for injecting the entity manager into your service stackoverflow.com/questions/10427282/…
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?
i don't know why, but i can't log in (always say Bad credentials)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.