获取在be9 acl9中应用角色的对象列表

时间:2009-11-27 11:35:32

标签: ruby-on-rails ruby acl9

我想是这样的:

def self.obj_list(opts = {:include => [] , :exclude => []})
    # Returns an array with all objects with roles applied
    # +:exclude+:: (array,string) optional object type to exclude from list
    # +:include+:: (array,string) optional object type to include in list
    # Example:
    #   Role.obj_list(:include => ["Device", "User"])
    #   Role.obj_list(:exclude => ["User"])

    inc = opts[:include].to_a
    exc = opts[:exclude].to_a

    objs = []
    if inc.empty?

      self.all.each do |r|
        unless r.authorizable_type.nil?
          objs << r.authorizable_type.constantize.find(r.authorizable_id) unless exc.include?(r.authorizable_type)
        end
      end

    else

      self.all.each do |r|
        unless r.authorizable_type.nil?
          objs << r.authorizable_type.constantize.find(r.authorizable_id) if inc.include?(r.authorizable_type)
        end
      end

    end
    objs
  end

2 个答案:

答案 0 :(得分:0)

我不知道,也许你想链接对象和主题? 如果是这样,这里有一个教程:http://wiki.github.com/be9/acl9/tutorial-linking-object-and-subject-with-hmt

答案 1 :(得分:0)

您可以使用where子句在SQL中执行包含/排除内容:

( inc.empty?
? where.not( :authorizable_type => exc )
: where( :authorizable_type => inc )
).map(&:authorizable)

通过使用authorizable,您将获得Rails自己对多态关联的处理,这将确保仅返回实际对象,因此无需检查nil

相关问题