Rails 可配置每个角色的授权/增删改查操作
我正在使用 Rails 2.3 应用程序,该应用程序已经使用 declarative_authorization 来处理用户权限。每个用户都属于一个组(没有花哨的角色),具有某些模型的权限,主要是常规的 CRUD 操作,但也有一些特殊的操作。这一切都可以在通常的开箱即用模式下与 declarative_authorization 配合使用。
我们现在希望允许管理员针对模型类型对特定用户组设置权限。例如,我们创建一个新组 Foo,为其分配一些用户,然后通过复选框决定组 Foo 中的用户是否可以 c/r/u/d 模型 Bar 的对象。
我以前使用过 acl9,我想我知道如何让它发挥作用。我最近一直是 CanCan 的粉丝,但我不知道如何轻松地使用它。如果 declarative_authorization 可以做到这一点,我会坚持使用它,但我准备硬着头皮切换。
什么插件是完成此任务的最佳方法?有没有办法让 declarative_authorization 来完成这项工作?使用 CanCan 的优雅方式?是否有任何我应该注意的问题,例如数据库性能?
我可以说服将应用程序升级到 Rails 3.1,但我更愿意找到兼容 2.3 的解决方案。
我见过一些类似的问题,但没有令人满意的答案 Rails 动态基于角色的授权插件?
以及这个,用于 cancan https://github.com/ryanb/cancan/wiki/Role-Based-授权
I'm working with a Rails 2.3 app that is already using declarative_authorization to handle user permissions. Each user belongs to one group (no fancy roles) with permissions on certain models, mostly the regular CRUD actions, but with a few special ones. This all works fine with declarative_authorization in it's usual out of the box mode.
We now want to allow admins to set permissions on particular groups of users with regards to model types. For example, we create a new group Foo, assign some users to it, and then decide with checkboxes if users in group Foo can c/r/u/d objects of model Bar.
I've used acl9 before, and I think I see how I could make that work. I've been a fan of CanCan lately, but I don't see how do it easily with that. If declarative_authorization can do this, I'll stick with it, but I'm prepared to bite the bullet and switch.
What plugin would be the best way to accomplish this? Is there a way to get declarative_authorization to do the job? An elegant way to use CanCan? Are there any gotchas I should watch for, e.g. database performance?
I could be convinced to upgrade the app to Rails 3.1, but I'd prefer to find a 2.3-compatible solution.
I've seen some similar questions, but no satisfactory answers
Rails Dynamic Role-Based Authorization plugin?
and this, for cancan
https://github.com/ryanb/cancan/wiki/Role-Based-Authorization
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是 StoneWall gem 的粉丝 - 授权检查采用 Ruby 块的形式,因此您可以在该块中查找记录并查看相关用户是否具有授权。
文档不多,但想法是:
如果
object
是一个 Todo 实例,那么 Stonewall 将查找 Todo 模型并执行read
块。这种方法使两个事情很简单:
read
只是一个 Ruby 块,所以无论你想要发生什么,都可能发生,s.action :comment
并使用a_user_object.may_comment?(object)
访问它I'm a fan of the StoneWall gem - authorization checks are in the form of a Ruby block, so in that block you could look up records and see if the user in question has authorization.
There's not a lot of documentation, but the idea is:
If
object
is a Todo instance, then Stonewall will look int the Todo model and execute theread
blockThis approach makes two things easy:
read
is just a Ruby block, so whatever you want to happen can happens.action :comment
and access it witha_user_object.may_comment?(object)