[英]Rails 3, Cancan: defining ability on custom controller action acting on collection, not member
我有一個用於模型UserResource
的RESTful控制器。 我添加了一個名為remote_update
的自定義操作,並且僅在用戶ID匹配時才希望限制該操作:
if user.has_role? :admin
can :manage, :all
elsif user.has_role? :regular
can [:remote_update], UserResource, :user_id => user.id
end
我在控制器中使用load_and_authorize_resource
。
問題是,即使用戶ID不匹配,用戶仍然可以使用該操作。 (要測試,我正在使用Firebug並更改ID的隱藏值)。
我的路線如下:
resources :user_resources do
collection do
post 'remote_update'
end
結束
根據https://github.com/ryanb/cancan/wiki/Authorizing-controller-actions的定義,當我們執行自定義操作時,Cancan會嘗試使用id通過鏈接加載資源:
def discontinue
# Automatically does the following:
# @product = Product.find(params[:id])
# authorize! :discontinue, @product
end
我沒有定義ID,因為它是POST,而不是GET或PUT。 關於如何構建能力的思考? 謝謝。
看來您正在嘗試使用POST進行更新(“ remote_update”)。 POST應該被創建,因此通常不應具有填充的ID。 因此,我不希望CanCan為您執行該查找。
我建議您:
手動找到產品並使用您的停產方法進行授權,
要么
使用PUT
順便說一句,這種能力在我看來是正確的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.