获取在 be9 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
where
子句在 SQL 中执行包含/排除操作:通过使用
authorized
,您将获得 Rails 自己的多态关联处理,这将确保只有实际对象才被访问。返回,因此无需检查nil
You can use
where
clauses to do the include/exclude stuff in SQL: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 fornil
我不知道,也许你想链接对象和主题?
如果是这样,这里有一个教程: 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