Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Foo\FooBundleBundle\Listener;
- use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
- use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
- use Symfony\Component\Routing\Router;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Symfony\Component\Security\Core\SecurityContext;
- /**
- * SecurityListener.
- *
- * @author Dustin Dobervich
- */
- class SecurityListener
- {
- /**
- * @var Router $router
- */
- private $router;
- /**
- * @var SecurityContext $security
- */
- private $security;
- /**
- * @var boolean $redirectToAdmin
- */
- private $redirectToAdmin = false;
- /**
- * Constructs a new instance of SecurityListener.
- *
- * @param Router $router The router
- * @param SecurityContext $security The security context
- */
- public function __construct(Router $router, SecurityContext $security)
- {
- $this->router = $router;
- $this->security = $security;
- }
- /**
- * Invoked after a successful login.
- *
- * @param InteractiveLoginEvent $event The event
- */
- public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
- {
- if ($this->security->isGranted('ROLE_ADMIN')) {
- $this->redirectToAdmin = true;
- }
- }
- /**
- * Invoked after the response has been created.
- *
- * @param FilterResponseEvent $event The event
- */
- public function onCoreResponse(FilterResponseEvent $event)
- {
- if($this->redirectToAdmin) {
- $event->setResponse(newRedirectResponse($this->router->generate('admin_homepage')));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement