我正在尝试向已经存在的相当大的应用程序添加授权,但我必须稍微混淆细节。
这是背景:
在我们的应用程序中,我们有一个或多个分层的角色,大致如下:
BasicUser -> SuperUser -> Admin -> SuperAdmin
对于授权,每个用户模型实例都有一个对应于上述的属性“角色”。
我们有一个在 Backoffice 下命名的 RESTful 控制器“用户”。所以简而言之,它是 Backoffice::UsersController。
class Backoffice::UsersController < ApplicationController
filter_access_to :all
#... RESTful actions + some others
end
所以问题来了:
我们希望用户能够授予用户编辑用户的权限,但前提是他们的角色比他们当前拥有的“更小”。我在 authorization_rules.rb 中创建了以下内容
authorization do
role :basic_user do
has_permission_on :backoffice_users, :to => :index
end
role :super_user do
includes :basic_user
has_permission_on :backoffice_users, :to => :edit do
if_attribute :role => is_in { %w(basic_user) }
end
end
role :admin do
includes :super_user
end
role :super_admin do
includes :admin
end
end
不幸的是,据我所知,这条规则似乎没有得到应用。
- 如果我将规则注释掉,没有人可以编辑
- 如果我保留规则,您可以编辑所有人
我还尝试了 if_attribute 的几种变体:
if_attribute :role => is { 'basic_user' }
if_attribute :role => 'basic_user'
他们得到相同的效果。有人有什么建议吗?