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

发布于 2024-08-13 03:41:17 字数 981 浏览 4 评论 0原文

我认为是这样的:

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

I think in something like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

城歌 2024-08-20 03:41:17

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

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

通过使用 authorized,您将获得 Rails 自己的多态关联处理,这将确保只有实际对象才被访问。返回,因此无需检查 nil

You can use where clauses to do the include/exclude stuff in SQL:

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

By using authorizable you will get Rails's own handling of polymorphic associations, which will ensure that only actual objects are returned, so there's no need to check for nil

一个人的旅程 2024-08-20 03:41:17

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

I don't know, maybe you want to link the object and subject?
If is this, here are a tutorial for that: https://github.com/be9/acl9/wiki/tutorial:-linking-object-and-subject-with-hmt

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文