Search code examples
symfonyfosuserbundlesonata-adminsymfony-security

How to redirect after successful login setting default_target_path on all firewalls using FOSUserBundle and SonataAdminBundle in Symfony2 Security


This is my security firewall configuration:

firewalls:
    # Disabling the security for the web debug toolbar, the profiler and Assetic.
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false

    # -> custom firewall for the admin area of the URL
    qis:
        pattern:            /qis(.*)
        context:            user
        form_login:
            provider:       fos_userbundle
            login_path:     /login
            check_path:     /qis/login_check
        logout:
            path:           /qis/logout
        anonymous:          false

    # 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:    true
            default_target_path: /qis
            #always_use_default_target_path: false
            check_path:     /login_check
            failure_path:   null
        logout:
            path:           /logout
        anonymous:          true

        # Session liftime
        remember_me:
            key: '%secret%'
            lifetime: 28800

        # Sonata User Impersonating
        switch_user: true

Full config: security.yml

These are the use cases that work as expected:

  1. When accessing the login page /login directly the user is correctely redirected to the default_target_path qis/.
  2. When accessing a page e.g. /contract user is correctely redirected back to this requested page.

I would like to achieve the same behaviour mentioned in 2 with the qis firewall.

BUT:

When accessing via qis route e.g. http://localhost:8000/qis/contract/list it is not redirected to the same link but again default_target_path qis/.

What are the required settings on the qis firewall?


Solution

  • Try this configuration :

     admin:
            pattern:      /qis(.*)
            form_login:
                provider:       fos_userbundle
                login_path:     sonata_user_admin_security_login
                use_forward:    true
                use_referer: true
                check_path:     sonata_user_admin_security_check
                failure_path:   null
            logout:
                path:           sonata_user_admin_security_logout
            anonymous:    true
    

    You need to set anonymous to true to allows user to authenficate. The login page must have a public access.