| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 
 | namespace Test\UserBundle\ClassesUser;
 
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Test\UserBundle\Entity\User;
 
class WebserviceUserProvider implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        // effectuez un appel à votre service web ici
        $userData = array('username'=>'monnom', 'password'=>'monpass', 'salt'=>'', 'roles' => array('ROLE_USER'));
        // supposons qu'il retourne un tableau en cas de succès, ou bien
        // « false » s'il n'y a pas d'utilisateur
 
        if ($userData != false) {
 
					$password = $userData['password'];
					$salt = $userData['salt'];
					$roles = $userData['roles'];
 
            // ...
 
          return new User($username, $password, $salt, $roles);
        }
 
        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
    }
 
    public function refreshUser(UserInterface $user)
    {
        if (!$user instanceof User) {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
        }
 
        return $this->loadUserByUsername($user->getUsername());
    }
 
    public function supportsClass($class)
    {
        return $class === 'Test\UserBundle\Entity\User';
    }
} | 
Partager