Symfony access control - symfony

I want to authorize the access to only one ip to my API. But even if I write it in the access_control from my security.yml file, it seem to not work.
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: ^/administration, role: ROLE_ADMIN}
- { path: ^/api, role: IS_AUTHENTICATED_ANONYMOUSLY, ip: 527.0.2.1 }
The route I would like to block is all coming after ^/api/*
Thank for helping.

You can try this
access_control:
#
- { path: ^/api, roles: IS_AUTHENTICATED_ANONYMOUSLY, ips: [127.0.0.1, ::1] }
- { path: ^/api, roles: ROLE_NO_ACCESS }
Replace 127.0.0.1 with your IP
Read Official doc Symfony Matching access_control By IP

Related

In Symfony 3.4.11, I get a crash after adding access control

enter image description hereHere is my security.yml:
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: IS_AUTHENTICATED_FULLY }
Here is my error:
It says the YAML file is not valid. How can I proceed?
Yml file should be indented correctly.
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: IS_AUTHENTICATED_FULLY }

Symfony security access_control syntax

I currently have the below code, which does pretty much what I want.
There's just a few things I don't understand.
What's the ^/ in front of each path?
What's the $ behind ^/login?
Why do I get redirected to the login page when I don't have access to a page? Where do I set this up or change this?
Is there an easy way to grant access to my frontpage (/) while requiring users to be logged in to access any other page (except for the login/register pages)?
Should I use IS_AUTHENTICATED_FULLY or ROLE_USER?
Can't find any clear info on the subject either ...
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: IS_ATHENTICATED_FULLY }
Regarding ^ and $
^ = start of the string
$ = end of string
https://en.wikipedia.org/wiki/Regular_expression#Delimiters
IS_AUTHENTICATED_FULLY or ROLE_USER:
You can't compare these 2 directly. You could be authenticated fully and not have the role user.
IS_AUTHENTICATED_FULLY only means that you're not logged in through the remember_me mechanism in this session but actively entered your password.
It does not however check which role the logged in user is granted.
More here:
http://symfony.com/doc/current/security.html#checking-to-see-if-a-user-is-logged-in-is-authenticated-fully
Is there an easy way to grant access to my frontpage (/) while requiring users to be logged in to access any other page (except for the login/register pages)?
Try:
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: ^/admin/, role: ROLE_ADMIN }
- { path: ^/*, role: IS_AUTHENTICATED_FULLY }

Symfony 2 fosuserbundle logout

here's my problem i have 2 pages : index page when i'm not connected and an index page when i'm connected so when i'm connected and i try to type the url path of the index page when i'm not connected i find myself disconnected and on the page index when i'm not logged in
i'm using fosuserbundle and here's my access control
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 }
need help thanks

Problems with page resources using FOSUserBundle

I would like to have the whole page access enabled only if user logged in (except the FOS user login page)
This is how I set the access control:
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, role: ROLE_ADMIN }
But the problem is as it now blocks all my scripts. CSS and JS are not available, so login page is not styled! If I remove:
- { path: ^/, role: ROLE_ADMIN }
From the access control everything is OK and the login page is styled. Any help on how to put the whole page under "lockdown" (except the login page) but still have the styles displayed (CSS)?
The paths to your js, css and bundles directories are being caught by your access control that is stating that the user must be ROLE_ADMIN. To sort this you can just add rules for these directories above the ^/ rule using IS_AUTHENTICATED_ANONYMOUSLY like..
access_control:
- { path: ^/css, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/js, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/bundles, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, role: ROLE_ADMIN }

Symfony2 Access Control/Security

Okay, So I'm trying to setup my security in symfony2 via config. I have created a role_hierarchy:
role_hierarchy:
ROLE_USER_ADMIN: ROLE_USER
ROLE_VENDOR: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_VENDOR, ROLE_USER_ADMIN, ROLE_ALLOWED_TO_SWITCH]
And I've setup my access_control:
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/administration/, roles: ROLE_VENDOR }
- { path: ^/administration/vendor/new, roles: ROLE_SUPER_ADMIN }
- { path: ^/administration/taxonomy, roles: ROLE_SUPER_ADMIN }
- { path: ^/administration/property, roles: ROLE_SUPER_ADMIN }
- { path: ^/administration/usagelimit, roles: ROLE_SUPER_ADMIN }
- { path: ^/account, roles: ROLE_USER }
- { path: ^/library, roles: ROLE_USER }
- { path: ^/profile, roles: ROLE_USER }
- { path: ^/vendors, roles: ROLE_USER }
- { path: ^/community, roles: ROLE_USER }
And yet, when I login with a user who has only the "ROLE_VENDOR", I can access the routes like /administration/taxonomy, /administration/property, etc...
What am I doing wrong???
Your routes are in the wrong order.
It's a first come, first served everything after /administration/ with the directory are being caught by that directive and so allowing access by ROLE_VENDOR.
You should change it to...
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, roles: IS_AUTHENTICATED_ANONYMOUSLY }
# - { path: ^/administration/, roles: ROLE_VENDOR } // Old home...
- { path: ^/administration/vendor/new, roles: ROLE_SUPER_ADMIN }
- { path: ^/administration/taxonomy, roles: ROLE_SUPER_ADMIN }
- { path: ^/administration/property, roles: ROLE_SUPER_ADMIN }
- { path: ^/administration/usagelimit, roles: ROLE_SUPER_ADMIN }
- { path: ^/administration/, roles: ROLE_VENDOR } // New home...
- { path: ^/account, roles: ROLE_USER }
- { path: ^/library, roles: ROLE_USER }
- { path: ^/profile, roles: ROLE_USER }
- { path: ^/vendors, roles: ROLE_USER }
- { path: ^/community, roles: ROLE_USER }

Resources