【问题标题】:Possible to use hash abilities in CanCanCan has_many relationships?可以在 CanCanCan has_many 关系中使用哈希能力吗?
【发布时间】:2017-07-31 19:35:58
【问题描述】:

CanCanCan 允许我们通过abilities 在 Rails 中快速设置授权。 best practice 是使用哈希条件。然而, scopes/blocks 允许创建更复杂的能力。

我想知道是否有一种方法可以将哈希能力用于 has_many 关系。例如,给定一个有 has_one 创建者和 has_many 管理员(并且两者都可以更新事件)的事件,我相信这个范围会起作用。

can [:update], Event do |event| 
  event.creator.id == user.id or event.admins.where(user_id: user.id).first.id == user.id 
end

我们可以改为使用哈希吗?

can [:update], Event, creator: { user_id: user.id }
can [:update], Event, admins: { user_id: user.id } #<<< Will this work? Any gotchas?

另外,在一行代码中包含哈希 OR 会很好,但我找不到一种方法来使这项工作在语法上起作用。谢谢!

【问题讨论】:

    标签: ruby-on-rails authorization ruby-on-rails-5 cancancan


    【解决方案1】:

    使用哈希效果很好,尽管您似乎无法通过设计在一行上做到这一点 - https://github.com/CanCanCommunity/cancancan/wiki/defining-abilities#combining-abilities

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 2020-03-31
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      相关资源
      最近更新 更多