[英]CanCanCan: Allowing to manage only users profile
我有使用設備和cancancan的用戶模型。 這是我的能力:
if user.role? :patient can :access_profile, User, :id => user.id end
access_profile別名為:
alias_action :show, :edit, :update, :destroy, :to => :access_profile
在我的users_controller中,我使用load_and_authorize_resource。 例如,當我嘗試“ users / 4”並且current_user.id = 3時,我就可以到達那里。
在日志中,我看到以下內容:
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 4]] User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 3]] Rendered users/show.html.erb within layouts/application (0.4ms)
這里的第一個查詢來自set_user方法。 我猜第二個查詢將加載我的資源以進行授權。 在這里,由於current_user.id = 3,在第二個查詢中我也看到3,我想這就是為什么我被授權了。 但是我想第二個查詢中的id必須是4。
為了使load_and_authorize方法正常工作該怎么做。
您確定自動加載實例有效嗎? 如果無法加載實例,Cancancan將使用Class作為對象來檢查功能,並且如果定義,對Class的訪問將始終返回true(無論諸如:id
那樣的參數)
將load_and_authorize_resource更改為load_resource並檢查是否authorize! :access_profile, @user
authorize! :access_profile, @user
在access_profile操作中使用的@user也將保護訪問。
您也可以直接在實例上定義能力: can :access_profile, user
而不是在User類上定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.