Symfony2 用户角色(BETA2)
将用户添加到角色的适当方法是什么?对于每个新用户,我必须
$em->getRepository('MyBundle:Role')->findOneBy(array('name' => 'ROLE_USER'))
每次都这样做吗?
我不太喜欢 UserBundle 有多大......而且我不使用 XML。我正在使用 YML/注释,因此 UserBundle 对于某些事情来说很难遵循。
那么,是的,进行用户注册并将其关联到默认角色的最佳/最干净的方法是什么?
What's the appropriate way to add a user to a role.. for every new user, do I have to do:
$em->getRepository('MyBundle:Role')->findOneBy(array('name' => 'ROLE_USER'))
every time?
I'm not too much of a fan of how large UserBundle is.... and I don't use XML. I'm using YML/Annotations, so the UserBundle is pretty hard to follow for certain things.
So yes, what's the best / cleanest way to do a user signup and associate him to a default role?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现的最简单的方法就是在 User 对象上将
roles
定义为array
类型的字段。然后,当您创建用户(在注册或其他方面)时,就像我在 这个要点。它的功能并不完整(如果需要,我可以稍后充实它),但它应该为您指明正确的方向。
The simplest way I've found is just to define
roles
as a field of typearray
on your User object. Then, when you're creating the user (on registration or whatnot), it's as simple asI've put together a mockup of my user registration process in this gist. It's not fully functional (I can flesh it out later if needed), but it should get you pointed in the right direction.
我写了几篇关于角色的博客文章,一个简单的解决方案 http://blog.jmoz。 co.uk/symfony2-fosuserbundle-roles 和 http://blog.jmoz.co.uk/symfony2-fosuserbundle-role-entities
I wrote a couple of blog posts on roles, a simple solution http://blog.jmoz.co.uk/symfony2-fosuserbundle-roles and http://blog.jmoz.co.uk/symfony2-fosuserbundle-role-entities