0
votes

I am developping a website with symfony 2.2. I use FOSUserBundle and my website is basically made of a homepage, a login page, a register page and a secured area.

What i want to do is when a user type in the address of my website:

  • if anonymous -> go to homepage

  • if remembered -> go to secured area

I have tried 2 different things that do not work.

1) If i put my url / in the secured area I get the redirection correctly to login_path. But when choosing my login_path in security.yml I have a problem:

  • if i put /login, anonymous are redirected to login and not homepage

  • if i put /homepage, anonymous are redirected to homepage, but if the enter bad credentials in login form they are redirected to homepage instead of seeing the error message in /login

2) If i put my url / available to anonymous corresponding to my homepage and login_path = /login it works well excepted that remembered users also get to homepage instead of secured area.

In the last situation I try to redirect them to secured area if I see they are remembered but the 2 codes I found in forums and I tried in my controller are not working ...

public function indexAction()
    {
        $securityContext = $this->container->get('security.context');
        $user = $securityContext->getToken()->getUser();
        if (is_object($user) && $user instanceof UserInterface ) {
            return $this->redirect($this->generateUrl('tk_user_homepage'));
        }else if( $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED') ){
            return $this->redirect($this->generateUrl('tk_user_homepage'));
        }else{
            return $this->render('TkWelcomeBundle:Default:index.html.twig');
        }
    }

When I come back on my website I get $user as being a non object and the second statement is false. However I can access secured area with url.

What is the correct way to do that (1 or 2) and what am I missing in each case ?

Edit

Actually I made this test:

Go from homepage to secured area back and forth and test if user is_granted:

'role_user', 'is_authenticated_anonimously', 'is_authenticated_remembered' and 'is_authenticated_fully'

in the secured area I get what I expect (1,0,1,1) but when i go to homepage it is (0,0,0,0) and i can go back to secured area and retrieve (1,0,1,1).

So I guess the security context is not available on my homepage. Does anyone have a hint on this ?

Thank you in advance, Jules

1

1 Answers

0
votes

Here is what seems to be working:

I need to have the url under firewall which provides the users to get access to the users. So I put all my website in the secured area with anonymous authorized and play with access control: give access to homepage/login/register to anonymous and then I allow only remembered users to the whole website.

Here is my security.yml

firewalls:
    secured_area:
        pattern: ^/
        anonymous: ~
        form_login:
            login_path: /login
            check_path: /login_check
            default_target_path: tk_user_homepage
            provider: fos_userbundle
            remember_me: true
            csrf_provider: form.csrf_provider
        remember_me:
            key: %secret%
        logout:
            path:   fos_user_security_logout
            target: fos_user_security_login

access_control:
    - { path: ^/$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, role: IS_AUTHENTICATED_REMEMBERED }
    - { path: ^/admin/, role: ROLE_ADMIN }

I hope this will help some.

Jules