9

My problem is capture user logout. the code what i have is:

public  function onAuthenticationFailure(Request $request, AuthenticationException $exception){

    return new Response($this->translator->trans($exception->getMessage()));
}

public function logout(Request $request, Response $response, TokenInterface $token)
{
    $empleado = $token->getUser();
    $log = new Log();
    $log->setFechalog(new \DateTime('now'));
    $log->setTipo("Out");
    $log->setEntidad("");
    $log->setEmpleado($empleado);
    $this->em->persist($log);
    $this->em->flush();
}

public function onLogoutSuccess(Request $request) {
    return new RedirectResponse($this->router->generate('login'));
}

The problem is I can not access the user token TokenInterface when you are running the logout function?

2
  • 1
    the soluction problem is the service security context thanks. Commented Oct 5, 2012 at 9:29
  • 1
    Is $token->getUser() returning null? Or the $token is null? Commented Oct 7, 2012 at 22:41

3 Answers 3

13

To get token, you must inject with security context.

1. Create class Logout listener, something like this:

namespace Yourproject\Yourbundle\Services;
...
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\Security\Core\SecurityContext;

class LogoutListener implements LogoutSuccessHandlerInterface {

  private $security;  

  public function __construct(SecurityContext $security) {
    $this->security = $security;
  }

  public function onLogoutSuccess(Request $request) {
     $user = $this->security->getToken()->getUser();

     //add code to handle $user here
     //...

     $response =  RedirectResponse($this->router->generate('login'));

    return $response;
  }
}

2. And then in service.yml, add this line:

....
logout_listener:
   class:  Yourproject\Yourbundle\Services\LogoutListener
   arguments:  [@security.context]

That's it, may it helps.

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

4 Comments

This won't work, for logout_listener service you have no defined event. So the listener won't be fired at all.
@artworkadシ, i have registered this event on service.yml, so it will be fired when user do logout. i have used this on several project symphony, and there is no problem.
In my opinion it is necessary adding ALSO the lines in the answer on Jul 24 '14 at 15:22. It worked for me at symfony 2.5.
In Symfony 3 you have to use ['@security.authorization_checker'] instead of security.context
8

See http://symfony.com/doc/current/reference/configuration/security.html

security.yml

secured_area:

    logout:
        path:   /logout
        **success_handler: logout_listener** 

Comments

0

Take a look here were you can overwrite any controller of the bundle:

http://symfony.com/doc/current/cookbook/bundles/inheritance.html

Comments

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.