Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forbid access for authorised users in Symfony2?

I'm a beginner in Symfony2. Now I try to finish writing authorisation, using FOSUserBundle. I have a problem, that authorised user can access to log in page or register page. Can I change this in configs or I have to check it in the controller? Thank you

PS I mean such part in security.yml:

access_control:
    - { path: /new, role: ROLE_USER }
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/, role: ROLE_ADMIN }
like image 901
a_sarana Avatar asked Dec 04 '25 09:12

a_sarana


1 Answers

You can solve it by creating an event listener which will hook into the FOSUserBundle controllers. For registration you could use fos_user.registration.initialize event. In your listener you could check if user is already authenticated, if yes then redirect to whatever route you like (e.g. to homepage). See this answer to see the example.

For login route (/login) you could override the default FOSUserBundle SecurityController and check there if user is authenticated and do the redirect. See this example.

like image 195
takeit Avatar answered Dec 07 '25 00:12

takeit