简体   繁体   中英

Store multiple objects with Symfony2 authentification

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM