Ticker

6/recent/ticker-posts

Redirect after login/logout by role in Symfony 4.x

Create your custom AuthenticationSuccessHandlerInterface
App\Security\LoginHandler\ZeroblogAuthenticationSuccessHandler.php

namespace App\Security\LoginHandler;


use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;

class ZeroblogAuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
{
    /**
     * @var \Symfony\Component\Routing\RouterInterface
     */
    private $router;

    /**
     * @param RouterInterface $router
     */
    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    /**
     * @param Request $request
     * @param TokenInterface $token
     * @return RedirectResponse
     */
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        $user = $this->security->getUser();
        $response = new RedirectResponse($this->router->generate('crm_homepage', ['auth' => User::AUTH_ZEROBLOG_A1]));
        
        if ($user->getAuthArea() === SSOUser::AUTH_ZEROBLOG_A2)
            $response = new RedirectResponse($this->router->generate('crm_homepage', ['auth' => SSOUser::AUTH_ZEROBLOG_A2]));
        
        return $redirection;
    }
} 

Create your custom LogoutSuccessHandlerInterface
App\Security\LogoutHandler\ZeroblogLogoutSuccessHandler.php

namespace App\Security\LogoutHandler;


use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;

class ZeroblogLogoutSuccessHandler implements LogoutSuccessHandlerInterface
{
    /**
     * @var \Symfony\Component\Routing\RouterInterface
     */
    private $router;

    /**
     * @var \Symfony\Component\Security\Core\SecurityContextInterface
     */
    private $security;

    /**
     * @param SecurityContextInterface $security
     */
    public function __construct(RouterInterface $router, SecurityContextInterface $security)
    {
        $this->router = $router;
        $this->security = $security;
    }

    /**
     * @param Request $request
     * @return RedirectResponse
     */
    public function onLogoutSuccess(Request $request)
    {
        $user = $this->security->getUser();
        $response = new RedirectResponse($this->router->generate('after_logout', ['auth' => User::AUTH_ZEROBLOG_A1]));
        
        if ($user->getAuthArea() === SSOUser::AUTH_ZEROBLOG_A2)
            $response = new RedirectResponse($this->router->generate('after_logout', ['auth' => SSOUser::AUTH_ZEROBLOG_A2]));

        return $response;
    }
} 
Edit service.yaml

	redirect.after.login:
		class: App\Security\LoginHandler\ZeroblogAuthenticationSuccessHandler
		arguments: ["@router", "@security.helper"]
        
	redirect.after.logout:
		class: App\Security\LogoutHandler\ZeroblogLogoutSuccessHandler
		arguments: ["@router", "@security.helper"]
Edit security.yaml

	form_login:
		provider: zeroblog_provider
		login_path: /login
		check_path: /login_check
		success_handler: redirect.after.login
	logout:
		path: /logout
		target: /
		success_handler: redirect.after.logout

Post a Comment

0 Comments