Rails 可配置每个角色的授权/增删改查操作

发布于 2024-12-11 17:00:42 字数 892 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笏见 2024-12-18 17:00:42

我是 StoneWall gem 的粉丝 - 授权检查采用 Ruby 块的形式,因此您可以在该块中查找记录并查看相关用户是否具有授权。

文档不多,但想法是:

a_user_object.may_read?(object)

如果 object 是一个 Todo 实例,那么 Stonewall 将查找 Todo 模型并执行 read 块。

# app/models/todo.rb

stonewall do |s|

    s.action :read do |todo, user|
      todo.owner == user  # only the owner of a todo item may read the item
    end
end

这种方法使两个事情很简单:

  1. read 只是一个 Ruby 块,所以无论你想要发生什么,都可能发生,
  2. 动作可以指定任意名称,所以如果你想检查用户是否有权评论待办事项项目(例如),只需创建一个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:

a_user_object.may_read?(object)

If object is a Todo instance, then Stonewall will look int the Todo model and execute the read block

# app/models/todo.rb

stonewall do |s|

    s.action :read do |todo, user|
      todo.owner == user  # only the owner of a todo item may read the item
    end
end

This approach makes two things easy:

  1. read is just a Ruby block, so whatever you want to happen can happen
  2. actions can be given arbitrary names, so if you want to check to see if a user has permission to comment on a todo item (for example), just create a s.action :comment and access it with a_user_object.may_comment?(object)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文