Sonata User Bundle + Admin Bundle admin redirect after login

I am trying to get the sonata to work like this:
- if a normal user logs in, it is redirected to "/"
- if the admin is logged in, it is redirected to the page "/ admin / dashboard"

I tried to do it with firewalls which are in app / config / security.yml and this is what I came up with:

        # This firewall is used to handle the admin login area
        # This part is handled by the Sonata User Bundle
        admin:
        pattern:            /(.*)
        context:            user
        form_login:
            provider:       fos_userbundle
            login_path:     /login
            use_forward:    false
            check_path:     /login_check
            failure_path:   null
            default_target_path:   /admin/dashboard
        logout:
            path:           /admin/logout
            target:           /
        anonymous:    true

        # This firewall is used to handle the public login area
        # This part is handled by the FOS User Bundle
        main:
        pattern:      .*
        context:        user
        form_login:
            provider:       fos_userbundle
            login_path:     /login
            use_forward:    false
            check_path:     /login_check
            failure_path:   null
            default_target_path: /
            always_use_default_target_path:   true
        logout:
            path: /logout
            target: /

      

now every logged-in user is redirected to / admin, explicitly dropping "access denied" for non-admin users. Is there a way to fix this in this yml file or should I be looking for another way to check user roles?

+3


source to share


1 answer


One way to redirect the user based on the role is you can implement your own authentication handler and check the user's role in the function onAuthenticationSuccess()

and redirect depending on the nature of the user

namespace YourNamespace\YourBundle\Services;

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

class AuthenticationHandler implements  AuthenticationSuccessHandlerInterface {
    protected $container;

    public function __construct( $container ) {
        $this->container = $container;
    }

    public function onAuthenticationSuccess( Request $request, TokenInterface $token ) {
        $user = $token->getUser();
        if($user->isGranted( 'ROLE_ADMIN' )){
            $url = $this->container->get( 'router' )->generate( 'sonata_admin_dashboard' );
        }else{
            $url = $this->container->get( 'router' )->generate( 'your_welcome_route' );
        }
        return new RedirectResponse( $url );

    }
}

      

define a service for your authentication handler



services:
    admin_success_handler:
        class: YourNamespace\YourBundle\Services\AuthenticationHandler
        arguments: [ '@service_container' ]

      

And in your firewall define success_handler

        admin:
        pattern:            /(.*)
        context:            user
        form_login:
            provider:       fos_userbundle
            login_path:     /login
            use_forward:    false
            check_path:     /login_check
            failure_path:   null
            default_target_path:   /admin/dashboard
            success_handler: admin_success_handler
        logout:
            path:           /admin/logout
            target:           /
        anonymous:    true

      

+8


source







All Articles