eZ\Publish\Core\MVC\Symfony\Security\User\Provider::loadUserByUsername PHP Méthode

loadUserByUsername() public méthode

$user can be either the user ID or an instance of \eZ\Publish\Core\MVC\Symfony\Security\User (anonymous user we try to check access via SecurityContext::isGranted()).
public loadUserByUsername ( string | User $user ) : eZ\Publish\Core\MVC\Symfony\Security\UserInterface
$user string | eZ\Publish\Core\MVC\Symfony\Security\User Either the user ID to load an instance of User object. A value of -1 represents an anonymous user.
Résultat eZ\Publish\Core\MVC\Symfony\Security\UserInterface
    public function loadUserByUsername($user)
    {
        try {
            // SecurityContext always tries to authenticate anonymous users when checking granted access.
            // In that case $user is an instance of \eZ\Publish\Core\MVC\Symfony\Security\User.
            // We don't need to reload the user here.
            if ($user instanceof UserInterface) {
                return $user;
            }
            return new User($this->repository->getUserService()->loadUserByLogin($user), array('ROLE_USER'));
        } catch (NotFoundException $e) {
            throw new UsernameNotFoundException($e->getMessage(), 0, $e);
        }
    }

Usage Example

 public function testLoadUserByUsername()
 {
     $username = '******';
     $apiUser = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\User');
     $this->userService->expects($this->once())->method('loadUserByLogin')->with($username)->will($this->returnValue($apiUser));
     $user = $this->userProvider->loadUserByUsername($username);
     $this->assertInstanceOf('eZ\\Publish\\Core\\MVC\\Symfony\\Security\\UserInterface', $user);
     $this->assertSame($apiUser, $user->getAPIUser());
     $this->assertSame(array('ROLE_USER'), $user->getRoles());
 }