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?

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

Need Your Help

ContentManager Out of Memory before loading any data

c# windows-phone-7 xna

I'm trying to load data in XNA, however, whenever I use the ContentManager it throws an out of memory exception. This occurs when I load my first resource. It's a 32x32 pixel image.

updating php to 5.3 not working

php security session

I get this error when using some library called: Mcrypt extension:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.