AuthenticationSuccessHandler.php 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Muzich\CoreBundle\Security\Http\Authentication;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Doctrine\ORM\EntityManager;
  10. use Muzich\CoreBundle\Entity\User;
  11. /**
  12. * Custom authentication success handler
  13. */
  14. class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
  15. {
  16. private $router;
  17. private $em;
  18. /**
  19. * Constructor
  20. * @param RouterInterface $router
  21. * @param EntityManager $em
  22. */
  23. public function __construct(RouterInterface $router, EntityManager $em)
  24. {
  25. $this->router = $router;
  26. $this->em = $em;
  27. }
  28. /**
  29. * This is called when an interactive authentication attempt succeeds. This
  30. * is called by authentication listeners inheriting from AbstractAuthenticationListener.
  31. * @param Request $request
  32. * @param TokenInterface $token
  33. * @return Response The response to return
  34. */
  35. function onAuthenticationSuccess(Request $request, TokenInterface $token)
  36. {
  37. if ($request->isXmlHttpRequest())
  38. {
  39. $response = new Response(json_encode(array('status' => 'success')));
  40. $response->headers->set('Content-Type', 'application/json; charset=utf-8');
  41. return $response;
  42. }
  43. return new RedirectResponse($this->router->generate('home'));
  44. }
  45. }