Check user authentication based on path in sonata - php

is it possible to check if user is authenticated to access the URl he requested. What I want to do is when user not logged in he can't see files by url.
example : => Screenshot
Any suggestions? Thank you
Update
this is my access control:
All access working fine for my application
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/login-check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: [ROLE_USER] }
- { path: ^/admin/rlsh/dashboard, role: IS_AUTHENTICATED_FULLY }
- { path: ^/uploads/media, role: IS_AUTHENTICATED_FULLY }
- { path: ^/profile/, role: IS_AUTHENTICATED_FULLY }
- { path: ^/shop/basket/step/, role: IS_AUTHENTICATED_FULLY }
- { path: ^/shop/user/, role: IS_AUTHENTICATED_FULLY }
# - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

If you want the user to be connected to access to your files, you could use a simple access_control, please take a look
access_control:
- { path: ^/uploads/media, role: IS_AUTHENTICATED_FULLY }
A not authenticated user trying to access to your files will get a 403 forbidden response.
Btw you can customize the path with a regex and many others things, feel free to take a look

Related

Catch user group in login flow and remove access to frontend if belongs to certain group

I have a application with frontend and backend sides. Both share the same FOSUserBundle users table. The logic behind the application I'm working on doesn't allow "admin" (ROLE_ADMIN or group ADMIN) to be logged in at frontend so I need to catch on login flow whether the user belongs to group ADMIN or has ROLE_SUPERADMIN or ROLE_ADMIN credentials. I think this could be done in someway on the Security flow from Symfony2 itself, but how? What I need to do to catch that and if user belongs redirect to login form (from FOSUserBundle) or allow it to get into the frontend? Any help? Some code? Examples?
security.yml file content:
Regarding user answer this is how my security.yml file is configured:
role_hierarchy:
ROLE_USER: ROLE_USER
ROLE_ADMIN: ROLE_ADMIN
access_control:
# Anonymous area
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/registro, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/cedula, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/rif, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/correo, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/usuario, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/razon_social, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/registro_mercantil, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/padre, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/correo_alternativo, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/paises, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/estados, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/ciudades, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/municipios, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/parroquias, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/empresas, role: IS_AUTHENTICATED_ANONYMOUSLY }
# Secured area
- { path: ^/, role: ROLE_USER }
- { path: ^/admin, role: ROLE_ADMIN }
But users with ROLE_ADMIN can still login in frontend (/) and them shouldn't right? What's wrong then?
You must know the role hierarchy in your security.yml file.
You don't need to extend default functionality on fosuserbundle or symfony security to achieve that.
For example:
role_hierarchy:
ROLE_USER: ROLE USER
ROLE_CONTENT_MANAGER: ROLE_USER
ROLE_ADMIN: [ROLE_CONTENT_MANAGER]
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
So, super admin can go anywhere all other grups allowed. Admin, has the rights of content manager. Content manager has the rights of user.
Check your access control in your security.yml file.
access_control:
- { path: ^/private_directory$, roles: MUST_BE_RIGHT_TO_SEE_HERE }
So, the role MUST_BE_RIGHT_TO_SEE_HERE can only see the private_directory. Any other roles can not.

Redirect after the login

I used FOSUserBundle and i changed the basic template to my own. When i set rules in my securite file like below
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, role: IS_AUTHENTICATED_ANONYMOUSLY }
then everything is ok but when i change it like below
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, role: ROLE_ADMIN }
my site after login redirects me to "web/app_dev.php/_wdt/7e8a2e" and i don't know why.
What is it "_wdt/7e8a2e " ?
You should add a new access_control rule to allow anonymous access to this:
{ path: ^/_(profiler|wdt)/, role: IS_AUTHENTICATED_ANONYMOUSLY }
you could also add a new firewall rule to avoid security in this url

Symfony2 - Security authentication

- { path: ^/event, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_MEMBER }
In my security.yml, I would like to authenticate all /xxx except / and /event, is there a way to do so? What I did above will not work. The only way for it to work is to add individual pages everytime i create one.
- { path: ^/event, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_MEMBER }
I cannot add IS_AUTHENTICATED_ANONYMOUSLY on the root in the 3rd line because that would be so wrong.
You have to do this in your access_control:
- { path: ^/event, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_MEMBER }

Symfony2.1/FOSUserBundle - change the login form route to the homepage route (path: /)

I have a symfony 2.1 project using FOSUserBundle. The bundle is installed correctly. All the functionalities work properly using the default /login form.
But now I want to change the login form route to be the same as the homepage (path: /).
I tried:
changing the security.yml - login_path:
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
login_path: /
logout: true
anonymous: true
and the access_control to:
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 }
- { path: ^/, role: IS_AUTHENTICATED_ANONYMOUSLY }
When I try to access the homepage it enters a infinite redirect (I assume)
The page isn't redirecting properly Firefox has detected that the
server is redirecting the request for this address in a way that will
never complete.
Also couldn't find anything to troubleshoot this behaviour inside the FOSUserBundle documentation.
In short words: I want the first_page to act as login page.
You have a redirect loop.
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } #1
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } #2
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } #3
- { path: ^/admin/, role: ROLE_ADMIN } #4
- { path: ^/*, role: ROLE_USER } #5
- { path: ^/, role: IS_AUTHENTICATED_ANONYMOUSLY } #6
This happens because first, you are telling access controll in #5 requires ROLE_USER BEFORE you tell it that it also requires IS_AUTHENTICATED_ANONYMOUSLY (both 5th and 6th rules match)
Access control is order sensitive, the rules apply in the order they are defined, try:
access_control:
- { path: ^/$, role: IS_AUTHENTICATED_ANONYMOUSLY } # NOTE THE $
- { 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 }
I moved the rule
- { path: ^/, role: IS_AUTHENTICATED_ANONYMOUSLY }
to the begining so it matches before
- { path: ^/*, role: ROLE_USER }
I think you should change on your security.yml :
login_path: / to login_path: /login
And your access_control :
{ path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } #1
{ path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } #2
{ path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } #3
{ path: ^/admin/, role: ROLE_ADMIN } #4
Hope this help !

Exclude public pages from access control list

I have application in Symfony2 with 2 roles: ROLE_ADMIN and ROLE_PARTNER. Also I have some public pages. All public pages starts with URL "/public/". I want to protect all application excluded these public items.
My current config:
access_control:
- { path: /.*, role: ROLE_PARTNER|ROLE_ADMIN }
- { path: /public/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
But it works wrong (looping redirection).
Change the order:
access_control:
- { path: ^/public/, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, role: ROLE_PARTNER|ROLE_ADMIN }
The second option is to turn off security for the public section completely:
firewalls:
public:
pattern: ^/public/
security: false
Add:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
See "Common Pitfalls" in http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form

Categories