Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Foo\FooBundleBundle\Listener;
  4.  
  5. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  6. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  7. use Symfony\Component\Routing\Router;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\Security\Core\SecurityContext;
  10.  
  11. /**
  12.  * SecurityListener.
  13.  *
  14.  * @author Dustin Dobervich
  15.  */
  16. class SecurityListener
  17. {  
  18.     /**
  19.      * @var Router $router
  20.      */
  21.     private $router;
  22.    
  23.     /**
  24.      * @var SecurityContext $security
  25.      */
  26.     private $security;
  27.    
  28.     /**
  29.      * @var boolean $redirectToAdmin
  30.      */
  31.     private $redirectToAdmin = false;
  32.    
  33.     /**
  34.      * Constructs a new instance of SecurityListener.
  35.      *
  36.      * @param Router $router The router
  37.      * @param SecurityContext $security The security context
  38.      */
  39.     public function __construct(Router $router, SecurityContext $security)
  40.     {
  41.         $this->router = $router;
  42.         $this->security = $security;
  43.     }
  44.    
  45.     /**
  46.      * Invoked after a successful login.
  47.      *
  48.      * @param InteractiveLoginEvent $event The event
  49.      */
  50.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  51.     {
  52.         if ($this->security->isGranted('ROLE_ADMIN')) {
  53.             $this->redirectToAdmin = true;
  54.         }
  55.     }
  56.    
  57.     /**
  58.      * Invoked after the response has been created.
  59.      *
  60.      * @param FilterResponseEvent $event The event
  61.      */
  62.     public function onCoreResponse(FilterResponseEvent $event)
  63.     {
  64.         if($this->redirectToAdmin) {
  65.             $event->setResponse(newRedirectResponse($this->router->generate('admin_homepage')));
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement