Created
December 20, 2013 20:57
-
-
Save marydn/8061424 to your computer and use it in GitHub Desktop.
Custom URL redirect by role after success login on Symfony 2 using a service listener without FOSUser Bundle.
This file contains 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
# src/Acme/DemoBundle/Security/Authentication/Handler/LoginSuccessHandler.php | |
<?php | |
namespace Acme\DemoBundle\Security\Authentication\Handler; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Symfony\Component\Routing\Router; | |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
use Symfony\Component\Security\Core\SecurityContext; | |
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; | |
class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface | |
{ | |
protected | |
$router, | |
$security; | |
public function __construct(Router $router, SecurityContext $security) | |
{ | |
$this->router = $router; | |
$this->security = $security; | |
} | |
public function onAuthenticationSuccess(Request $request, TokenInterface $token) | |
{ | |
// URL for redirect the user to where they were before the login process begun if you want. | |
// $referer_url = $request->headers->get('referer'); | |
// Default target for unknown roles. Everyone else go there. | |
$url = 'homepage'; | |
if($this->security->isGranted('ROLE_USER')) { | |
$url = 'user_homepage'; | |
} | |
elseif($this->security->isGranted('ROLE_ADMIN')) { | |
$url = 'admin_dashboard'; | |
} | |
$response = new RedirectResponse($this->router->generate($url)); | |
return $response; | |
} | |
} |
This file contains 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
# app/config/security.yml | |
security: | |
... | |
firewalls: | |
main: # name of your firewall | |
form_login: | |
... | |
use_referer: false | |
success_handler: login_success_handler |
This file contains 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
# src/Acme/DemoBundle/Resources/config/services.yml | |
parameters: | |
acme_demo.authentication.handler.login_success_handler.class: Acme\DemoBundle\Security\Authentication\Handler\LoginSuccessHandler | |
services: | |
login_success_handler: | |
class: %acme_demo.authentication.handler.login_success_handler.class% | |
arguments: [@router, @security.context] | |
tags: | |
- { name: 'monolog.logger', channel: 'security' } |
You are the best!!! Thank you very much! On symfony 4 works fine!
Thanks for sharing!! Also might want to take a look at:
https://stackoverflow.com/questions/15918617/symfony2-extending-defaultauthenticationsuccesshandler
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can you make this work in symfony 4??