Ruby on rails 使用声明性授权来限制模型中的结果

Ruby on rails 使用声明性授权来限制模型中的结果,ruby-on-rails,declarative-authorization,Ruby On Rails,Declarative Authorization,我在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

我在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
在我的人员模型中,我有一个静态方法,我希望它看起来像这样

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
使用文档中的AuthorizationInModel对象(使用with_permissions_to或Allowed_to)似乎对此有支持。我还没有弄清楚如何根据文档使用这些函数,或者如何返回当前用户在当前模型上的权限列表


有什么想法吗?

可能有更好的方法,但如果其他一切都设置正确,这应该适用于您的
监督方法

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
end

我发现了一个使用内置if属性方法的替代解决方案。我最初离开它是因为我使用的是非名称空间模型、名称空间控制器和视图。这个结构是我正在处理的项目的原始版本的一个工件。我的大部分工作都是获得声明式授权来处理这个结构

我不清楚的主要信息是如何在部分命名空间的环境中命名权限。模型需要模型名(:people),控制器需要名称空间和模型(:staff\u people),只要您选择了一个,视图就不在乎。我选择的解决方案是使用模型名并在每个控制器中显式设置上下文。如果未在控制器中设置上下文,则使用filter_access_to将不起作用,因为它将查找staff_people权限,而不是正确的权限people

在声明性授权配置文件中,我向管理人员授予完全权限,向主管授予部分权限。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
要在名称空间的控制器中访问此信息,我使用filer\u resource\u access

module Staff
  class PeopleController < ApplicationController
    filter_resource_access :context => :people

    def index
      @people = People.with_permissions_to(:read)
    end
不适用于需要与\u权限\u to和if属性权限一起使用的方法。我不知道为什么这是一个问题

对于不包括作为参数一部分提取单个记录的id的非标准控制器操作,仍然需要使用filter_access_to。例如,如果一个名为part_timers的操作返回一个人员列表,则此解决方案似乎应该有效:

filter_resource_access :context => :people, :additional_member => { :part_timers => :read }
正确的解决方案是保持筛选器资源访问权不变,并为该操作添加筛选器访问权

filter_resource_access :context => :people
fitler_access_to :part_timers, :required => :read, :context => people
filter_resource_access :context => :people
fitler_access_to :part_timers, :required => :read, :context => people