Symfony security system, redirect loop (need to access the user in every page)?

918 Views Asked by At

I need to access the current user logged in (if any) in any part of the application. Special paths like /admin requires special permissions (roles).

This is the firewall configuration (just one firewall) protecting the entire application but not allowing anonymous users (because I need the current user even in homepage).

I got a redirect loop even requesting /. Any help?

'security.firewalls' => array(
    'secured' => array(
        'pattern' => '.*',
        'anonymous' => false,
        'form' => array(
            'login_path' => '/login',
            'check_path' => '/login_check',
            'username_parameter' => 'login[username]',
            'password_parameter' => 'login[password]',
        ),
        'logout' => array('logout_path' => '/logout')
    )
)

Access rules requires ROLE_ADMIN only for paths starting with /admin . The rest is anonymous:

'security.access_rules' => array(
    array('^/admin', 'ROLE_ADMIN'),
    array('^.*',     'IS_AUTHENTICATED_ANONYMOUSLY')
),
1

There are 1 best solutions below

0
Emii Khaos On

To allow acces via IS_AUTHENTICATED_ANONYMOUSLY have have to allow anonymous.

'security.firewalls' => array(
    'secured' => array(
        'pattern' => '/',
        'anonymous' => true,
        // other stuff
    )
)

'security.access_rules' => array(
    array('^/admin', 'ROLE_ADMIN'),
    array('^/',      'IS_AUTHENTICATED_ANONYMOUSLY')
),

If the user is logged in you can access them in every page. IS_AUTHENTICATED_ANONYMOUSLY is only a role, which have unauthenticated users (anonymous).