简体   繁体   English

使用Symfony2身份验证存储多个对象

[英]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 . 我已经使Symfony2身份验证服务与user表一起工作,但是我想在会话中存储(或在其他地方通过身份验证管理)关联的对象: userType1userType2

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 ? 如何告诉Symfony在身份验证上下文中管理其他对象(用户类型)?

Have a look at the cookbook-entry How to load Security Users from the Database . 看一看Cookbook条目“ 如何从数据库加载安全用户” 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. 序列化User时,我仅使用某些字段(id,username,email),其余数据通过提供程序的refreshUser() ,在此处再次通过QueryBuilder获取User及其关系。 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. 我遇到的问题是,在会话中序列化User时,否则关系“丢失”,而我必须一直重新获取User。

After that it's basically what @BenjaminPaap wrote. 之后,基本上就是@BenjaminPaap编写的内容。 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 ". 如果要根据用户的userType将用户重定向到某个目标,则可能需要查看“ 如何创建自定义UserProvider ”。 The Listener provides everything you need to redirect the user according to their type right after login. 侦听器提供登录后立即根据用户类型重定向用户所需的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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