Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonata User Bundle + Admin Bundle admin redirect after login

I'm trying to make sonata work like this:
- if a regular user logs in he's redirected to "/"
- if an admin logs in he's redirected to "/admin/dashboard"

I tried to make it with firewalls that are in app/config/security.yml and here's what i come to:

        # 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 obviously throwing 'access denied' for non-admin users. Is there any way to fix it in this yml file or shall i search for some different way of checking user roles?

like image 677
nonab Avatar asked Dec 15 '22 19:12

nonab


1 Answers

One way to redirect user on basis of role you can implement your own authentication handler and check role of the user in onAuthenticationSuccess() function and redirect depending on the nature of 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 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
like image 118
M Khalid Junaid Avatar answered Dec 30 '22 11:12

M Khalid Junaid