我对rails项目中的权限使用声明性授权gem,并试图根据用户权限限制模型的输出。
我的简化授权文件如下所示:
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在我的people模型中,我有一个静态方法,我希望它看起来像这样
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或permitted_to的文档中,似乎有使用AuthorizationInModel对象的支持。根据文档,我不知道如何使用这些函数,也不知道如何在当前模型上返回当前用户的权限列表。
有什么想法吗?
发布于 2011-05-06 05:24:11
我使用内置的if-attribute方法找到了另一个解决方案。我最初远离它是因为我使用了非命名空间的模型和命名空间的控制器和视图。这个结构是来自我正在工作的项目的原始版本的工件。我的大部分工作都是获得声明性授权来处理这种结构。
我不清楚的主要信息是如何在部分命名空间的环境中命名权限。模型需要模型名称(:people),控制器需要名称空间和模型(:staff_people),只要您选择一个,视图就不会在意。我选择的解决方案是使用模型名称,并在每个控制器中显式设置上下文。如果没有在控制器中设置上下文,则使用filter_access_to将不起作用,因为它将查找staff_people权限而不是正确的权限people。
在声明性授权配置文件中,我将完全权限授予管理,将部分权限授予supervisor。person.supervised返回自身和所有其他受监督人员的数组。
roles do
role :administrator
has_permission_on :people, :to => [:create, :read, :update, :delete]
end
role :supervisor
has_permission_on :people do
to => [:create, :read, :update, :delete]
if_attribute :id => is_in { Person.find_by_user_id(user.id).supervised }
end
end
end要在Namespaced控制器中访问此信息,我使用filer_resource_access。
module Staff
class PeopleController < ApplicationController
filter_resource_access :context => :people
def index
@people = People.with_permissions_to(:read)
end我发现使用
filter_access_to :all, :with_attribute => true不适用于需要使用with_permissions_to和if_attribute权限的方法。我不确定为什么这是一个问题
对于不包含id的非标准控制器操作,仍然需要使用filter_access_to来获取单个记录作为参数的一部分。例如,如果一个名为part_timers的操作返回一个人员列表,则此解决方案似乎应该有效:
filter_resource_access :context => :people, :additional_member => { :part_timers => :read }正确的解决方案是保持filter_resource_access不变,并为该操作添加一个filter_access_to
filter_resource_access :context => :people
fitler_access_to :part_timers, :required => :read, :context => people发布于 2011-05-04 11:53:53
也许有一种更好的方法可以做到这一点,但如果其他一切都设置正确的话,这种方法应该适用于您的supervised_by方法。
def self.supervised_by(user)
if Person.new.permitted_to? :all_records, :user=>user
#return all of the records
elsif Person.new.permitted_to? :subordinate_records, :user=>user
#return some of the records
else
#return none of the records
end
endhttps://stackoverflow.com/questions/5874170
复制相似问题