eZ\Publish\Core\MVC\Symfony\Security\EventListener\SecurityListener::onInteractiveLogin PHP Method

onInteractiveLogin() public method

Will dispatch an event allowing listeners to return a valid eZ user for current authenticated user. Will by default let the repository load the anonymous user.
public onInteractiveLogin ( Symfony\Component\Security\Http\Event\InteractiveLoginEvent $event )
$event Symfony\Component\Security\Http\Event\InteractiveLoginEvent
    public function onInteractiveLogin(BaseInteractiveLoginEvent $event)
    {
        $token = $event->getAuthenticationToken();
        $originalUser = $token->getUser();
        if ($originalUser instanceof eZUser || !$originalUser instanceof UserInterface) {
            return;
        }
        /*
         * 1. Send the event.
         * 2. If no eZ user is returned, load Anonymous user.
         * 3. Inject eZ user in repository.
         * 4. Create the UserWrapped user object (implementing eZ UserInterface) with loaded eZ user.
         * 5. Create new token with UserWrapped user
         * 6. Inject the new token in security context
         */
        $subLoginEvent = new InteractiveLoginEvent($event->getRequest(), $token);
        $this->eventDispatcher->dispatch(MVCEvents::INTERACTIVE_LOGIN, $subLoginEvent);
        if ($subLoginEvent->hasAPIUser()) {
            $apiUser = $subLoginEvent->getAPIUser();
        } else {
            $apiUser = $this->repository->getUserService()->loadUser($this->configResolver->getParameter('anonymous_user_id'));
        }
        $this->repository->setCurrentUser($apiUser);
        $providerKey = method_exists($token, 'getProviderKey') ? $token->getProviderKey() : __CLASS__;
        $interactiveToken = new InteractiveLoginToken($this->getUser($originalUser, $apiUser), get_class($token), $token->getCredentials(), $providerKey, $token->getRoles());
        $interactiveToken->setAttributes($token->getAttributes());
        $this->tokenStorage->setToken($interactiveToken);
    }

Usage Example

 public function testOnInteractiveLogin()
 {
     $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
     $token = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface');
     $token->expects($this->once())->method('getUser')->will($this->returnValue($user));
     $token->expects($this->once())->method('getRoles')->will($this->returnValue(array('ROLE_USER')));
     $token->expects($this->once())->method('getAttributes')->will($this->returnValue(array('foo' => 'bar')));
     $event = new BaseInteractiveLoginEvent(new Request(), $token);
     $anonymousUserId = 10;
     $this->configResolver->expects($this->once())->method('getParameter')->with('anonymous_user_id')->will($this->returnValue($anonymousUserId));
     $apiUser = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\User');
     $userService = $this->getMock('eZ\\Publish\\API\\Repository\\UserService');
     $userService->expects($this->once())->method('loadUser')->with($anonymousUserId)->will($this->returnValue($apiUser));
     $this->repository->expects($this->once())->method('getUserService')->will($this->returnValue($userService));
     $this->repository->expects($this->once())->method('setCurrentUser')->with($apiUser);
     $this->tokenStorage->expects($this->once())->method('setToken')->with($this->isInstanceOf('eZ\\Publish\\Core\\MVC\\Symfony\\Security\\InteractiveLoginToken'));
     $this->listener->onInteractiveLogin($event);
 }