Rails 3 和 Acl9:查找给定对象/角色的所有主题

发布于 2024-10-03 11:33:30 字数 840 浏览 1 评论 0原文

我使用 rails3acl9 进行授权。我正在尝试理解 ARel 和 Rails 的查询方式。

我有一个属于公司用户,并且该用户在该公司中具有给定的角色(acl9 提供管道)。如何从给定的公司获取具有特定角色的所有用户我想过滤数据库中的结果,而不是在应用程序中过滤它们。

我想要这样的东西:

# 
# authorizable_id = 1 is the company's id
# 'collaborator' and '1' would be params for my scope
#
select * from users
inner join roles_users
on roles_users.user_id = users.id
inner join roles
on roles_users.role_id = roles.id
where roles.authorizable_type='Company'
and   roles.authorizable_id = 1
and   roles.name = 'collaborator';

#usage with scope
@collaborators = User.with_role_and_company('collaborator',current_user.company)

我知道rails3有一个sql api,但我来自Java/Grails地方,并且我使用的大多数ORM都有一些ORM api(我想datamapper是这样工作的,但我使用的是普通的rails3 )。

I'm using rails3 with acl9 for authorization. I'm trying to grok ARel and the rails way of querying.

I have a User that belongs to a Company, and the user has a given role (acl9 provides the plumbing) over the Company. How can I get all Users from a given Company that have a specific role? I want to filter the results in the DB, not filter them in the application.

I want something like this:

# 
# authorizable_id = 1 is the company's id
# 'collaborator' and '1' would be params for my scope
#
select * from users
inner join roles_users
on roles_users.user_id = users.id
inner join roles
on roles_users.role_id = roles.id
where roles.authorizable_type='Company'
and   roles.authorizable_id = 1
and   roles.name = 'collaborator';

#usage with scope
@collaborators = User.with_role_and_company('collaborator',current_user.company)

I know rails3 has a sql api, but I come from a Java/Grails place, and most of the ORMs I use have some ORM api (I suppose datamapper works like this, but I'm using plain rails3).

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

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

发布评论

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

评论(2

别再吹冷风 2024-10-10 11:33:30

我发现:

 class << self
   def with_role_in_company(role, company)
      joins(:roles).
          where(:roles => {:authorizable_type => 'Company'}).
          where(:roles => {:authorizable_id => company.id}).
          where(:roles => {:name => role})
    end
  end

I found out:

 class << self
   def with_role_in_company(role, company)
      joins(:roles).
          where(:roles => {:authorizable_type => 'Company'}).
          where(:roles => {:authorizable_id => company.id}).
          where(:roles => {:name => role})
    end
  end
呆° 2024-10-10 11:33:30

我还在 acl9 1.2 中添加了此功能,所以现在您可以执行以下操作:

company.users :collaborator

I also added this feature in acl9 1.2, so now you can just do:

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