I have these tables in the database :
user:
- email
- lastname
- firstname
- password
userType1:
- fkUser
- specialCol1
- specialCol2
userType2:
- fkUser
- specialCol1
- specialCol2
I’ve made the Symfony2 authentification service working with user table but i want to store in the session (or in other place manage by the authentication) the associated object: userType1 or userType2.
Futhermore, i want to redirect the user to its account depending on his type.
So my questions :
How to make treatement after submitting authentification form ? (to determine which type of user it is)
How to tell to Symfony to manage an other object (the user type) in the authentification context ?
Have a look at the cookbook-entry How to load Security Users from the Database. The sections “Authenticating Someone with a Custom Entity Provider” and “Managing Roles in the Database” might be helpful.
I had a similar problem with an authentication-procedure where a User and their Tenant-memberships were required. I built a custom provider which basically looks like the one from the cookbook. When serializing the User I only use certain field (id, username, email) and the rest of the data is fetched via the provider’s
refreshUser(), where again the User and their relationships are fetched via QueryBuilder. I had the problem, that when serializing the User in the session, the relationships were “lost” otherwise and I had to refetch the User all the time.After that it’s basically what @BenjaminPaap wrote. You retrieve the user as described in the documentation and get the user types, etc.
If you want to redirect the user to a certain target depending on their userType, you might want to look at “How to create a custom UserProvider“. The Listener provides everything you need to redirect the user according to their type right after login.