自定义用户提供者实体的递归角色 [英] Recursive roles with custom user provider entity

查看:133
本文介绍了自定义用户提供者实体的递归角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义用户提供商实体,允许我连接用户。这个自定义用户提供者实体根据这个接口实现了UserInterface,我有一个getRoles()函数,它给了我用户的角色。

I've got a custom user provider entity which permits me to connect the users. This custom user provider entity implements UserInterface in accordance to this interface I've got a "getRoles()" function which give me the user's roles.

但是我的角色是递归的。
示例:一个用户得到一个role1,role1继承了role2,所以用户已经得到了role1和role2。为了使此递归,我创建一个角色表,一个role_role表(父/子),一个用户表,最后一个user_role表。

But my roles are recursive. Example: a user got a role1, the role1 inherited the role2 so the user has gotten the role1 and the role2. To make this recursivity I create a role table, a role_role table (parent/child), a user table and finally a user_role table.

为了获得所有用户的角色必须用Doctrine查询我的数据库,从哪里可以做到这一点?
似乎禁止从一个实体进行查询,我无法将查询放在实体存储库中,因为我无法覆盖实体的getRoles(),并且似乎不是一个好主意存储库表单实体。

To get ALL the user's roles I have to query my DB with Doctrine so from where can I do that ? It seems to be forbidden to query from an entity and I can't put the query in entity repository classe because I can't overwrite the entity's "getRoles()" and it seems not to be a good idea to access the repository form entities.

P.-S .:谢谢你放纵我的语法,这是我的第一个英文信息(我是法语)。

P.-S.: thank you for your indulgence with my grammar, it's my first English message (I'm French).

推荐答案

解决方案是完全使用Doctrine的ORM。

The solution is to use fully the Doctrine's ORM.

添加一个集合用户实体中的变量引用与ORM链接的所有UserRole:

Add a collection variable in the User entity which reference all the UserRole linked to him with an ORM:

@ORM\OneToMany(targetEntity="RoleUser", mappedBy="user")
...
protected $roleUser;

在RoleUser实体中添加一个ORM,允许将用户绑定到正确的RoleUser:

In RoleUser entity add an ORM which permit to bind the User with the right RoleUser:

@ORM\ManyToOne(targetEntity="User", inversedBy="roleUser")
...
protected $user;

现在您可以获取所有UserRoles链接到用户。
要让角色在UserRole实体和Role之间以及RoleRole和Role之间写入samesORM。最后,您可以通过$ rolesUtilisateur从User访问您的角色。

Now you can get all the UserRoles linked to the User. To get the roles write the "sames" ORM between UserRole entity and Role and between RoleRole and Role. Finally you access to your roles from User by $rolesUtilisateur.

这篇关于自定义用户提供者实体的递归角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆