Symfony 6 trouble with access_control - is_authenticated_fully etc - symfony

# set hierarchy for roles?
role_hierarchy:
# give admin also the roles inside the array.
ROLE_ADMIN: [ROLE_USER]
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
#- { path: ^/admin, roles: ROLE_ADMIN }
# Unless the path is login, user must be authenticated anonymously.
# This means only page accessible anonymously is login page.
- { path: ^(/(login|register)), roles: IS_AUTHENTICATED_ANONYMOUSLY }
# can visit any other path if authenticated fully
- { path: ^/, roles: IS_AUTHENTICATED_FULLY }
This simple code doesn't seem to work. I cannot visit login or register anonymously. I know the IS_AUTHENTICATED_FULLY part is working as when I comment it out (and I am signed out, aka authenticated anonymously) I can visit other paths other than login and register.
Even when I simply do:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
It doesn't work and I cannot visit /login. What am I doing wrong?
I have been using this video as a guide: https://youtu.be/XjbIDOIoXTo?t=4211

Symfony 5.3 has deprecated the old authentication mechanism along with the Guard Component see https://symfony.com/blog/new-in-symfony-5-3-guard-component-deprecation. The new system doesn't "authenticate" user by default with IS_AUTHENTICATED_ANONYMOUSLY.
Anonymous users no longer exist
You now must use the PUBLIC_ACCESS as #Bossman specified in a comment. https://symfony.com/doc/current/security.html#allowing-unsecured-access-i-e-anonymous-users
The video in your link clearly states that the video has been recorded using Symfony 5.2
NOTICE - THIS SERIES WAS RECORDED USING SYMFONY 5.2. THERE HAVE BEEN SOME MINOR CHANGES AND SOME CLASSES HAVE SINCE BEEN REMOVED. YOU WILL STILL BE ABLE TO FOLLOW THIS TUTORIAL BUT YOU WILL NEED TO COMBINE IT THE DOCUMENTATION IN SOME PARTS.

Related

Setting up security for each src folder in Symfony

I try to set up security per folder under src/ in Symfony. But I want a different set of security rules per main folder "General" and "Intranet" without having to prefix the routes... So I only have to prefix "Extranet"
Is that possible? I know that with a prefix in routing.yml it is very easy to do but that is not an option since the visible urls will suddenly change
The problem arises when we have to allow External users to our platform. For years it was only available for the companies' employees only but now external people must have access to certain pages. And some general routes (ajax calls etc) must be available for all
src/
Intranet/ => Open routes for internal users
SomeBundle
...
General/ => Open routes for all users
AnotherBundle
...
Extranet/ => Open routes for external users
TheBestBundle
...
Then the Extranet routes all get an extra prefix /extranet/. But I would like to have the other 2 (General and Intranet) without any prefix
# routing.yml
extranet:
resource: "#ExtranetBundle/Controller/"
prefix: /extranet/
Then with access control I take care of the /extranet routes
access_control:
# Login and the base_route "/" is always available
- { path: ^/$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
# External users + Super admins + server IP's can only access urls starting with /extranet/
-
path: ^/extranet/*
roles: [ROLE_EXTERNAL_USER, ROLE_SUPER_ADMIN]
ips: !php/const:SomeBundle\SomeClass\ConstantProvider::ALLOWED_SERVER_IPS
# Some routes need to be available for both internal and external users
# but hopefully without having to prefix them
...
# All other routes are only for internal users and the right ip addresses
-
path: ^/*
role: ROLE_INTERNAL_USER
ips: !php/const:SomeBundle\SomeClass\ConstantProvider::ALLOWED_SERVER_IPS
Or maybe an idea of approaching this problem differently?
I Think the best approche is to use voter external user need to have a unique ROLE (EXTERNAL_ROLE) then you can use voter to deney resources you want to protect https://symfony.com/doc/current/security/voters.html so this way you can protect a resource based on a the logic not on the folder

Restrict Login for All pages for not logged in User: Symfony3

I am using fos bundle for Symfony and it's working fine but now I want to restrict all pages for not logged user except (login and register). How can I achieve that?
security.yml need to look like this:
access_control:
- { path: ^/(register|login)?$, roles: IS_AUTHENTICATED_ANONYMOUSLY}
- { path: ^/?$, roles: ROLE_USER}
Check Documentation for more Security Symfony !
Greetings Violence
I assume, that you use FosUserBundle and as stated in the comments, it's all explained in the Docs.
Have a look at "Step 4: Configure your application's security.yml"

How to boost my application security?

I have an application with the framework Symfony, I have users with particular rights, if they don't have the right to access into a page, I must block them, but the users still can access into page with modifying URLs. For example I have this URL that they have the right to access in it:
dialog/campany/sms/fid/setting/new
and they don't have the right to access to this URL:
dialog/campany/mail/fid/setting/new
but they can by remplacing sms by mail.
You have to checkout how your security.yml is configured and control the access from there. If you need information about it you can visit: http://symfony.com/doc/current/security/access_control.html.
Also you could control the access making a function that checks the user's credentials before given access to a given page.
Supposing that ROLE_SMS can only access to dialog/campany/sms/ and ROLE_MAIL can only access to dialog/campany/mail/
In app/config/security.yml , you should have:
security:
access_control:
- { path: ^/dialog/campany/sms/, roles: ROLE_SMS }
- { path: ^/dialog/campany/mail/, roles: ROLE_MAIL }
If you only want to limit the /new URL, you can change the path to:
- { path: ^/dialog/campany/sms/fid/setting/new$, roles: ROLE_SMS }
- { path: ^/dialog/campany/mail/fid/setting/new$, roles: ROLE_MAIL }

how to get all users connected to default user without going through login page?

I want to secure my API with FOSUserBundle (and avoid the use of FOSOAuthServerBundle due to lack of documentation with complete example).
To make my API secure, I just created firewalls as follows:
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api_doc:
pattern: ^/api/doc
security: false
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
logout: true
anonymous: true
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }
- { path: ^/admin/, roles: [ROLE_SUPER_ADMIN]}
This is great! But this requires that every one has to be connected before using the website so that I can display the list of ads, etc. ( Even GET queries needs a connected for security purpose)
To overcome, I am thinking about having all the users connected with a "default" user with limited privileges (just enough for simple querying without posting).
Is it possible to do so?
Thanks for your help.
PS: I did manage to get FOSUserBundle and FOSOAuthServerBundle working together. In fact, I was able to create a client and get an access token for it through command lines. But, I was not able to config the security.yml properly so that I get a login page that returns an access token.
Take a look at the symfony entry on How to Authenticate Users with API Keys. The entry has most of the code that you need and is well documented.
You do have to add a field for the API key in your fos_user table e.g.
ALTER TABLE fos_user ADD api_key VARCHAR(255) DEFAULT NULL;
You also need to correctly implement the ApiKeyUserProvider::getUsernameForApiKey to look up in your database for the user based on the given API key, something like this:
$repository = $this->manager->getRepository('Application\UserBundle\Entity\User');
$user = $repository->findOneBy(array('apiKey' => $apiKey));
return empty($user) ? $user : $user->getUsername();
I implemented this a few years ago, it was simple to follow and has worked great!

Using dynamic roles in security.yml

I have a Ldap repository storing roles for users. They are already mapped to Symfony roles.
I would like to use them in the security.yml but I cannot hard code role values because they evolve with the application.
Is it possible to have something like this?
access_control:
- { path: ^/project/$project, roles: ROLE_$project_MEMBER }
Thanks.
Have you taken a look at implementing ACL? See also this piece of documentation.
i'm also stuck in the same problem you have to give roles which is stored in your in DB like
access_control:
- { path: ^/Services/, role: ROLE_USER }
there is no way and it's not good to do it the way I wanted, I've ended using the voters system using affirmative strategy

Resources