我正在使用声明性授权 gem 来获取 rails 项目中的权限,并且我正在尝试根据用户权限限制模型的输出。
我的缩写授权文件如下所示:
roles do
role :supervisor
has_permission_on :people, :to => :manage_all
end
role :basic_user
has_permission_on :people, :to => :manage_subordinates
end
end
privileges do
privilege :manage_subordinates do
includes :subordinate_records
end
privilege :manage_all do
includes :all_records
end
end
在我的人员模型中,我有一个静态方法,我希望看起来像这样
def self.supervised_by(user)
if user.permitted_to? :all_records
#return all of the records
elsif user.permitted_to? :subordinate_records
#return some of the records
else
#return none of the records
end
end
使用 with_permissions_to 或 allowed_to 的文档中的 AuthorizationInModel 对象似乎对此提供了支持。我无法根据文档弄清楚如何使用这些功能,或者如何返回当前用户对当前模型的权限列表。
有任何想法吗?