【问题标题】:Rails 4 - What is the best practice for restricting access to shared objectsRails 4 - 限制对共享对象的访问的最佳实践是什么
【发布时间】:2013-08-24 17:19:49
【问题描述】:

用户 has_many 合同作为买方 用户作为卖方拥有_many 个合同

我知道我可以使用 CanCan 将用户限制为他们拥有的东西。但在这种情况下,我有一份同时包含买方和卖方的合同。我希望用户能够查看/阅读他们是买方或卖方的所有合同。

我尝试设置一个 Scope 并将其与 CanCan 一起使用,但这似乎不起作用。

我这样设置我的能力...

can :read, Contract.parties(user.id)

我的范围被定义为...

scope :parties, lambda { |user_id| where("seller_id = ? OR buyer_id = ?", user_id, user_id) }

我也尝试过以不同的方式设置能力...

can :read, Contract, buyer_id: user.id
can :read, Contract, seller_id: user.id

但是上面好像有冲突,会报错

【问题讨论】:

    标签: permissions ruby-on-rails-4 cancan


    【解决方案1】:

    尝试使用具有 CanCan 能力的方块:

    can :read, Contract do |c|
      (c.buyer_id == user.id) || (c.seller_id == user.id)
    end
    

    【讨论】:

    • 我没有意识到你可以做到这一点。我会试一试,但我已经换成了保护宝石。我会相信你并将你的答案标记为正确。
    • 谢谢。我也会检查保护宝石,没听说过。
    猜你喜欢
    • 2014-01-21
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 2011-04-26
    • 2022-07-07
    • 2011-09-05
    • 2019-05-11
    相关资源
    最近更新 更多