Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Created January 27, 2015 12:28
Show Gist options
  • Save webdevilopers/9de56b109588a30401c2 to your computer and use it in GitHub Desktop.
Save webdevilopers/9de56b109588a30401c2 to your computer and use it in GitHub Desktop.
How to redirect after successful login setting default_target_path on all firewalls using FOSUserBundle and SonataAdminBundle in Symfony2 Security
security:
encoders:
FOS\UserBundle\Model\UserInterface: sha512
# set access_strategy to unanimous, else you may have unexpected behaviors
access_decision_manager:
strategy: unanimous
providers:
fos_userbundle:
id: fos_user.user_manager
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
access_control:
# URL of FOSUserBundle which need to be available to anonymous users
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
# - { path: ^/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
# Admin login page needs to be access without credential
- { path: ^/qis/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/qis/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
# Secured part of the site
# This config requires being logged for the whole site and having the admin role for the admin part.
# Change these rules to adapt them to your needs
- { path: ^/qis/, role: [ROLE_USER] }
- { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
acl:
connection: default
@webdevilopers
Copy link
Author

Question:
http://stackoverflow.com/questions/28299589/how-to-redirect-after-successful-login-setting-default-target-path-on-all-firewa

These are the use cases that work as expected:

  • When accessing the login page /login directly the user is correctely redirected to the default_target_path qis/.
  • 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?

@webdevilopers
Copy link
Author

The problem was the anonymous: false setting on the qis firewal. Solution:

        qis:
            pattern:            /qis(.*)
            context:            user
            form_login:
                provider:       fos_userbundle
#                login_path:     sonata_user_admin_security_login
                login_path:     /login
                use_forward:    true
#                use_referer:    true
                check_path:     sonata_user_admin_security_check

            logout:
                path:           sonata_user_admin_security_logout
            anonymous:          true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment