簡體   English   中英

Cancancan授權嵌套資源

[英]Cancancan authorisation of nested resources

我有一個“有很多”用戶,也有“很多”儀表的帳戶模型。 Meter對象具有一個account_id列,因此很容易在Cancancan中定義此功能:

if user
  can :crud, Meter, :account_id => user.account_id
end

但是,儀表也“具有許多”參數。 我想做一個類似的功能定義,僅當參數屬於與用戶屬於同一帳戶的儀表時才允許CRUD訪問。

由於參數模型僅具有meter_id列(而沒有account_id列),因此我不確定實現此額外嵌套級別的最佳方法嗎?

編輯

抱歉,不清楚。

我的模型如下:

Account > User (account_id) 
        > Meter (account_id) > Parameter (meter_id)

因此,我能夠檢查user.account_id = meter.account_id,但是我不確定如何檢查參數。

編輯2

除了馬特(Matt)的回答,這僅僅是一個例子,其中包括:

  load_and_authorize_resource :meter
  load_and_authorize_resource through: :meter

在參數模型中。

if user
  can :crud, Parameter, meter: { account_id: user.account_id }
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM