Accessing firewall configuration from container - symfony

If I have the following
security:
firewalls:
user:
form_login:
login_path: user_login
admin:
form_login:
login_path: admin_login
Is there a way in symfony to access the current login_path option in any class?

You could define both of them as parameters in your config and then do:
login_path: %user_login%
...
admin_path: %admin_login%
And do wherever you have access to the contaier
$container->getParameter('user_login');
...
$container->getParameter('admin_login');

Related

Logging by Username or Email when using two entities with FOSUserBundle

I am using FOSUSerBundle with two different Entities for different Users like
DefaultUser and AdminUser
Therefore I have the following in security.yaml
providers:
user:
entity:
class: AppBundle:User
property: 'email'
admin:
entity:
class: AppBundle:Admin
property: 'email'
and firewall is set like this:
admin:
pattern: ^/admin
anonymous: ~
provider: admin
form_login:
login_path: /admin/login
csrf_token_generator: security.csrf.token_manager
default_target_path: /admin
check_path: admin_login_check
logout_on_user_change: true
logout:
path: /admin/logout
target: /admin
invalidate_session: false
access_denied_handler: AppBundle\Security\AccessDeniedHandler
context: application
main:
pattern: ^/
provider: user
logout_on_user_change: true
form_login:
# csrf_token_generatlor: security.csrf.token_manager
login_path: /login
default_target_path: /user
check_path: fos_user_security_check
logout:
path: user_logout
target: user_login
invalidate_session: false
context: application
anonymous: ~
access_denied_handler: AppBundle\Security\AccessDeniedHandler
How to get FOSUserBundle work, so I can use username or Email ?
Normally it is set by
id: fos_user.user_provider.username_email
but this cannot used in this configuration.
It's been a long time since I worked with the fos_userbundle, but from what I see in my code, you'll need to update your security.yml file to make use of it:
security:
providers:
fos_userbundle_admin: appbundle.service.providing.admin_user
And in that service (which extends FOS\UserBundle\Security\UserProvider), you'll want to override the findUser($username) method. There, you can use the provided username.
I suppose (untested) you can create another provider (fos_userbundle_user) and use that one for users in your firewall.
Hopefully this makes sense.. It's working here, but that was in a symfony 2.8 app. FOS_UserBundle has changed a fair bit since then.

How to manage several firewalls in symfony 3.4

I'm trying to manage a frontend and backend with different user's rol. Inside security.yml added this
security:
providers:
admin:
entity:
class: LoginBundle:Usuarios
property: mail
external:
entity:
class: LoginBundle:UsuariosExternos
property: mail
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: true
provider: admin
form_login:
login_path: /
logout:
path: logout
target: /
access_denied_url: /eu/
external:
anonymous: true
provider: external
form_login:
login_path: /
logout:
path: logoutExternalUser
target: /
With this codete firewall external is not working and when I log as a external (frontend) the logout path is /logout and not logoutExternalUser.
Also with this config if I write the path /admin it goes to / and not to /admin/login
We try to user a AccessDeniedHandlerInterface but we don't know how to get the user rol in that instance.
Any help?
You are missing patterns. That's why the firwalls are not fireing UP.
You should set pattern for which it listens.
Check, I updated code.
security:
providers:
admin:
entity:
class: LoginBundle:Usuarios # idk if you realized this correctly
property: mail
external:
entity:
class: LoginBundle:UsuariosExternos
property: mail
firewalls:
admin:
pattern: ^/admin # pattern in which this will activate
anonymous: true
provider: admin
form_login:
login_path: adminLogin # implement route
logout:
path: security_logout # should work and destroy session
target: /
access_denied_url: /eu/
external:
pattern: ^/external
anonymous: true
provider: external
form_login:
login_path: loginExternal # implement route
logout:
path: security_logout
target: /
I recommend you to check https://symfony.com/doc/3.4/security/guard_authentication.html
It's nice way to build any kind of auth you need, if default security doesn't work for you.

FOS UserBundle Allow certain roles for a firewall

I'm using FOS UserBundle in my Symfony 3.3 Project and I have two firewalls , because Admins and Users connect from different login forms (with different URLs).
I would like to restrict access to admin dashboard only if the account used for logging is granted to ADMIN_ROLE.
That is, if I try to authenticate with a simple user account, I get the message "Bad credentials".
Is there a way to tell a firewall to only allow users of a certain role to connect ?
my security.yml firewalls section :
firewalls:
admin:
pattern: ^/admin
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
login_path: /admin
check_path: /admin/login_check
default_target_path: /admin
success_handler: app.security.adminauthentication_handler
failure_handler: app.security.adminauthentication_handler
logout:
path: /admin/logout
target: /admin
anonymous: true
context: application
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
success_handler: app.security.authentication_handler
failure_handler: app.security.authentication_handler
logout: true
anonymous: true
Please note that I'm using AJAX for both login forms.
Thanks
just add this in your security.yml under security like this :
> security :
access_control :
- { path: ^/mysite/dashboard/, role: ROLE_ADMIN }
You have to define access_control

Multiple firewalls (Admin and user)

My application need 2 firewalls, one for Admin and the other for User. In my security.yml I config:
admin:
pattern: ^/admin
provider: fos_userbundle
form_login:
login_path: /admin/login
use_forward: false
check_path: /admin/login_check
failure_path: null
default_target_path: /admin/dashboard
always_use_default_target_path: true
logout:
path: /admin/logout
target: /admin
anonymous: ~
# defaut login area for standard users
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
logout:
path: /logout
anonymous: ~
I don't know whether this config is correct. Everything's OK when I login from main area, but when I login from admin, it's redirect me to the home path instead of default_target_path. I try to change provider to a custom provider (eg in_memory) to re-check the admin firewall, but I still login by user from fos_userbundle provider. Can you help me?
i think it's because there is a main pattern
main:
pattern: ^/
it control even the ^/admin
try to replace ^/ with ^/home or ^/main it will work on bothe
Try removing anonymous and using access control instead. In theory Symfony2 will automatically redirect users from admin back, even if they are using the same login screen.
The security in Symfony2 is cascading (so /admin will also appear under main)
e.g.
# defaut login area for standard users
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
logout:
path: /logout
admin:
pattern: ^/admin
provider: fos_userbundle
form_login:
use_forward: false
failure_path: null
target: /admin/dashboard
always_use_default_target_path: true
logout:
target: /admin
access_control:
- { path: ^/, roles: [IS_AUTHENTICATED_ANONYMOUSLY, ROLE_USER] }
- { path: ^/admin, roles: [ROLE_ADMIN] }
you'll likely need different ROLES specified.
I changed main firewall pattern to ^/(?!admin), everything's ok now. Thanks for your help!
main:
pattern: ^/(?!admin)
provider: default_provider
anonymous: ~
admin:
pattern: ^/admin
provider: admin_provider
anonymous: ~

Symfony2 multi FosUserBundle

I use FosUserBundle for the login/register actions
I want to have two or more User table as User, BackUser, ...
I need also two different firewalls:
firewalls:
back:
pattern: ^/back
form_login:
provider: fos_BackUser
csrf_provider: form.csrf_provider
login_path: /back/login
check_path: /back/login_check
logout:
path: /back/logout
target: /back
anonymous: true
main:
pattern: ^/
form_login:
provider: fos_User
csrf_provider: form.csrf_provider
logout: true
anonymous: true
But with FosUserBundle I cannot set two differents providers.
I found this on google: https://groups.google.com/group/symfony2/browse_thread/thread/17d3fb94a1e305f8/e5ef7243cd84b558?lnk=raot
the first solution look good for my needs but I cannot make it work.
Any ideas?
You should take a look at https://github.com/leopro/PUGXMultiUserSandbox
This is a kind of "extension" for FOSUserBundle that allows you to have multiple user types.
Hope that's what you're looking for !

Resources