Symfony with FosUserBundle - 2 different login success paths - symfony

I have 2 user roles in my application: admin and member. After a successful login, an admin user must be redirected to /admin and member must be redirected to /catalog.
Is this possible with Symfony and the FOSUserBundle?

Yes, you can modify the login behaviour. It's nothing FOSUserBundle specific, it's a Symfony feature: https://symfony.com/doc/current/security/form_login.html#always-redirect-to-the-default-page
Another solution is a custom login authentication success handler. You can find an example here: https://gist.github.com/chalasr/69ad35c11e38b8cc717f

Related

HWIOAuthBundle without login

I need this logic:
user login with FOS user bundle
user click on google logo (for example) and login with google+
googleId token is saved in User entity.
So there is no firewall with HWIOAuthBundle and I need to use only HWIOAuthBundle listener. Is this correct?
Thanks in advance.
v.
I think that you are looking for HWIOAuth Connect functionality so you can connect a social media account (Google) to an existing FOS user.This may help you:
https://codereviewvideos.com/blog/hwioauth-registration-form/

Adding intermediate step after login in FOSUserBundle Symfony2

How to configure FOSUserBundle to add an intermediate step after login. Users should select an account client on a list of accessible accounts.
Therefore, how to save the selected account client in the security token?
You have to customise the whole FOSUserBundle login process.
See the Overriding controllers part of FOSUB documentation
When you have the controller, override the renderLogin and loginAction methods.
You have to replace the action of the login form to be redirected on your custom action (which represents the custom additional step).
And, in your custom step, just login the user by calling the original action of the login form.
All can be done by following the Symfony and FOSUserBundle documentations.

FOSUserBundle and LDAP user before login - Symfony2

I have successfully set up the FOSUserBundle and the FR3D LDAP bundle to allow my company users to login to the web app with their own login.
From now, if someone logins, they will have the default role, which is ROLE_USER.
However, I only want a specific role to be allowed (in this case it's ROLE_CONSULT, which I created).
What I am doing to allow users at this moment is:
Ask the user to login to the web app
They get the "You have no right to access this web page"
They tell me he connected
I change the user's role to "ROLE_CONSULT" with a form I created within the admin panel.
What I want to do is:
I select the user from the LDAP list
Change the user role (which is not on the database yet, but will be as soon as I change the role)
The user logins successfully
I already developed the 1 and 2 points, but as the "salt" is empty on the database, it doesn't allow the user to connect as it must believe the password isn't correct.
Any idea how I can make it work by telling Symfony2 it is the first login, it will automatically ONLY check the entered password at my LDAP configuration ?
Thanks.
Alright, that was easy.
I just forgot to put the "DN" to the database.
Now it's working.

How to add a check during the login process with Symfony FOS User

my symfony app works with dynamics subdomain, and my users can login only on their subdomain.
If user George is a user of subdomain test (he has the subdomain as a field) he can login on test.myapp.com. But during the login process I would like to check his subdomain like FOS User check the password, if the account is activated ... and stop the login process if the user is not on the right subdomain.
Thanks
If you are using FOS 2.0, events are available for you to hook into and add custom logic like this. Symfony provides the Symfony\Component\Security\Http\SecurityEvents and FOS has FOS\UserBundle\FOSUserEvents. Using the onImplicitLogin should give you access to what you need.
For a nice example of linking for a FOS event, see cheesemacfly's answer.
If you have trouble, I can write the listener and the service you need.

FOSUserBundle for admin and front end

I am new to Symfony2. I am trying to use FOSUserBundle for user authentication.
Is it possible to use single FOSUserBundle for admin as well as front end section? If yes, then what changes i will need to make at script level?
What i want is:
1. if i access url http://localhost/symfony/admin/ then i should be redirected to http://localhost/symfony/admin/login if admin is not logged in and after successful login, i should be redirected to http://localhost/symfony/admin/
2. if i access url http://localhost/symfony/profile/ then i should be redirected to http://localhost/symfony/login if user is not logged in and after successful login, i should be redirected to http://localhost/symfony/profile/.
By default FOSUserBundle create user as administrator. So what change need to place in to let bundle create user role for front end automatically and admin role for admin section.
You need to override the authentication-success listener in your security configuration to have user's redirected to different pages after login depending on their roles.
Have a look at this answer and maybe find some inspiration by looking at symfony's DefaultAuthenticationSuccessHandler.
Further you will need to override the access-denied handler ( see this answer ) to have user's get redirected to different login pages depending on the url they're trying to access without being authenticated.

Resources