Last active
December 21, 2015 06:08
-
-
Save gondo/6261528 to your computer and use it in GitHub Desktop.
Symfony2 using security.contex in login listener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// option 1: to replace/extend DefaultAuthenticationSuccessHandler | |
// http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/ | |
// http://stackoverflow.com/questions/15918617/symfony2-extending-defaultauthenticationsuccesshandler | |
// | |
// option 2: to use 2 listeners, 1 for checking admin and second to observer response event and change it | |
// http://forum.symfony-project.org/viewtopic.php?t=37383 | |
namespace My\UserBundle\EventListener; | |
use Symfony\Component\Security\Core\Event\AuthenticationEvent; | |
use Symfony\Component\Security\Core\SecurityContext; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | |
use Symfony\Bundle\FrameworkBundle\Routing\Router; | |
class LoginListener | |
{ | |
/** | |
* @var string | |
*/ | |
protected $redirect; | |
/** | |
* Router | |
* | |
* @var Router | |
*/ | |
protected $router; | |
/** | |
* @var SecurityContext | |
*/ | |
protected $securityContext; | |
/** | |
* @param SecurityContext $securityContext | |
* @param Router $router The router | |
*/ | |
public function __construct(SecurityContext $securityContext, Router $router) | |
{ | |
$this->securityContext = $securityContext; | |
$this->router = $router; | |
} | |
public function handle(AuthenticationEvent $event) | |
{ | |
$token = $event->getAuthenticationToken(); | |
$this->securityContext->setToken($token); | |
if ($this->securityContext->isGranted(array('ROLE_ADMIN'))) { | |
$this->redirect = 'admin'; | |
} | |
$this->securityContext->setToken(null); | |
} | |
public function onKernelResponse(FilterResponseEvent $event) | |
{ | |
if (null !== $this->redirect) { | |
$url = $this->router->generate($this->redirect); | |
$event->setResponse(new RedirectResponse($url)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
security: | |
encoders: | |
FOS\UserBundle\Model\UserInterface: sha512 | |
role_hierarchy: | |
ROLE_ADMIN: ROLE_USER | |
ROLE_SUPER_ADMIN: ROLE_ADMIN | |
providers: | |
fos_userbundle: | |
id: fos_user.user_provider.username_email | |
firewalls: | |
main: | |
pattern: ^/ | |
form_login: | |
default_target_path: / | |
always_use_default_target_path: true | |
provider: fos_userbundle | |
csrf_provider: form.csrf_provider | |
logout: true | |
anonymous: true | |
access_control: | |
- { path: /_wdt/.*, role: IS_AUTHENTICATED_ANONYMOUSLY } | |
- { path: /_profiler/.*, role: IS_AUTHENTICATED_ANONYMOUSLY } | |
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } | |
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } | |
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } | |
- { path: ^/admin, role: ROLE_ADMIN } | |
- { path: ^/, role: ROLE_USER } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
my.login: | |
class: My\UserBundle\EventListener\LoginListener | |
arguments: [@security.context, @router] | |
tags: | |
- { name: kernel.event_listener, event: security.authentication.success, method: handle } | |
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment