Symfony2 / FOSUserBundle - Disallowing users from getting to anonymous pages - symfony

I've noticed that FOSUserBundle's default access_control configuration is
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
But when I try to set these roles to
IS_AUTHENTICATED_ANONYMOUSLY && !IS_AUTHENTICATED_FULLY
it gives me endless loops to /login. How can I make only these three rules to be forbidden for fully authenticated users?

You should not deny access for the login page when the user is logged in, because a 403 forbidden will redirect automatically to login page because it request the user to login.
Better you write a service that checks every request. When it matches login request you need to check if the user is authenticated and redirect him to index page instead to the requested login page.

Related

Symfony access_control Rule

I have 3 user roles
ROLE_STAFF
ROLE_ADMIN
ROLE_CUSTOMER
I want to implement following rule for my admin dashboard.
Allow user with role ROLE_ADMIN all access
Do not allow access to user with role ROLE_CUSTOMER to any url starting with /admin
Allow not logged in user to access url starting with /account
Allow user with role ROLE_STAFF to access url starting with /admin/business/*
For the above rule, I have implemented following rule in access_control
access_control:
- { path: ^/account, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/business, role: ROLE_STAFF }
- { path: ^/admin/dashboard, role: ROLE_STAFF }
- { path: ^/admin, role: ROLE_ADMIN }
This is not working as expected, the problem is, when I am logged in with ROLE_STAFF it works but when I login with ROLE_ADMIN it throws access denied error.
What could be possible issue here?
Thanks.

How to secure other controller using FOSUserbundle?

I'm trying to study the FOSUserBundle. I follow the steps of their documentation (link) and I successfully created a login form and registration. Now, I'm creating another controller name TodoController and I want to secure that TodoController. It needs the user to login first before they show the page of todo. How can I secure the TodoController?
FOSUserBundle only extend the Symfony security layer, but all other Symfony security stuff remains the same, so you can use the default access control from symfony security
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
You will find it mentioned in the link you provided, but you can read more about it here http://symfony.com/doc/current/security/access_control.html
Or if you want something more flexible and more powerful you can take a look at JMSSecurityExtraBundle http://jmsyst.com/bundles/JMSSecurityExtraBundle/1.2/annotations
Hope this helps,
Alexandru Cosoi

Why does FOSUserBundle documentation recommend login$ path

On this page: http://symfony.com/doc/master/bundles/FOSUserBundle/index.html
the recommended security.yml has
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
Why is there a $ at the end of /login but not at the end of /register?
On a related note, when I visit /register I am redirected to /register/.
The access control on the login uses a regex. This always means URL starting with /login, so /login, /login-check...etc.
The second one allows to catch all the URLs starting with the directory /register/
I hope that I have been able to help you with my explanations.

how to authenticate ROLE_USER and ROLE_ADMIN separately in fos userbundle

I have made two different login section in my website, one for admin section and another for frontend users.
When I logged into to frontend it logged in. The problem is that when i try to login the admin section it throws access denied error.
Is it possible make two different login sessions so that both login session are independent of eachother
you don't show security.yml, so no way to know for sure:
try deleting this row:
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
but most likely you have something like this in your security.yml
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
- { path: ^/, role: ROLE_USER }
and your login page is on url /admin/login/
so it requires ROLE_ADMIN to access login page, before you can login as admin

Sonata User Bundle - redirect loop

I have installed both FOSUserBundle and Sonata Admin bundle
First FOSUserBundle worked perfect, with both profile, login and logout.
Now with Sonata Admin bundle, I can CRUD my entities.
Now I wanted them integrated with login to my backend.
Now when I go to /admin/dashboard it redirects to /admin/login and then a infinity redirect loop.
I have properly messed up the security, and I really dont understand it that well.
security.yml : https://gist.github.com/lsv/4740268
routing.yml : https://gist.github.com/lsv/4740284
config.yml : https://gist.github.com/lsv/4740291
dev.log : https://gist.github.com/lsv/4740301
Can somebody help?
Add
- { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/login-check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
to security.yml

Resources