动态为用户添加角色
我们正在使用 Symfony2 的角色功能来限制用户对我们应用程序某些部分的访问。用户可以购买年度订阅,并且我们的每个 User
实体都有许多具有开始日期和结束日期的 Subscription
实体。
现在,有没有一种方法可以根据用户是否具有“活动”订阅来动态地向用户添加角色?在rails中,我只是让模型处理它是否具有必要的权限,但我知道根据设计,symfony2实体不应该访问Doctrine。
我知道您可以从实体实例内访问实体的关联,但这将遍历用户的所有订阅对象,这对我来说似乎不必要的麻烦。
We are using Symfony2's roles feature to restrict users' access to certain parts of our app. Users can purchase yearly subscriptions and each of our User
entities has many Subscription
entities that have a start date and an end.
Now, is there a way to dynamically add a role to a user based on whether they have an 'active' subscription? In rails i would simply let the model handle whether it has the necessary rights but I know that by design symfony2 entities are not supposed to have access to Doctrine.
I know that you can access an entity's associations from within an entity instance but that would go through all the user's subscription objects and that seems unnecessaryly cumbersome to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你最好设置一个自定义投票者和属性。
SUBSCRIPTION_X 角色(又名属性)需要由自定义投票者类处理。
您需要配置并标记您的选民:
I think you would do better setting up a custom voter and attribute.
The
SUBSCRIPTION_X
role (aka attribute) would need to be handled by a custom voter class.You would need to configure and tag your voter:
假设您的用户实体中有正确的关系“订阅”。
您可以尝试类似的操作:
更改角色可以通过以下方式完成:
但是在页面更改后,会调用提供程序的refreshUser 函数,有时,就像EntityUserProvider 的情况一样,角色会被查询覆盖。
您需要一个自定义提供程序来避免这种情况。
Assuming that you have the right relation "subscriptions" in your User Entity.
You can maybe try something like :
Changing role can be done with :
But after a page change, the refreshUser function of the provider is called and sometimes, as this is the case with EntityUserProvider, the role is overwrite by a query.
You need a custom provider to avoid this.