Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2009-2014 Steven TITREN - www.webaki.com
  4. * @package Webaki\UserBundle\Redirection
  5. * @author Steven Titren <contact@webaki.com>
  6. */
  7. namespace AppBundle\Listener;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\RouterInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  13. class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
  14. {
  15. /**
  16. * @var \Symfony\Component\Routing\RouterInterface
  17. */
  18. private $router;
  19. /**
  20. * @param RouterInterface $router
  21. */
  22. public function __construct(RouterInterface $router)
  23. {
  24. $this->router = $router;
  25. }
  26. /**
  27. * @param Request $request
  28. * @param TokenInterface $token
  29. * @return RedirectResponse
  30. */
  31. public function onAuthenticationSuccess(Request $request, TokenInterface $token)
  32. {
  33. // Get list of roles for current user
  34. $roles = $token->getRoles();
  35. // Tranform this list in array
  36. $rolesTab = array_map(function ($role) {
  37. return $role->getRole();
  38. }, $roles);
  39. // If is a admin or super admin we redirect to the backoffice area
  40. if (in_array('ROLE_ADMIN', $rolesTab, true) || in_array('ROLE_SUPER_ADMIN', $rolesTab, true))
  41. $redirection = new RedirectResponse($this->router->generate('admin_homepage'));
  42. // otherwise, if is a commercial user we redirect to the crm area
  43. // elseif (in_array('ROLE_COMMERCIAL', $rolesTab, true))
  44. // $redirection = new RedirectResponse($this->router->generate('crm_homepage'));
  45. // // otherwise we redirect user to the member area
  46. else
  47. $redirection = new RedirectResponse($this->router->generate('homepage'));
  48.  
  49. return $redirection;
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement