[英]Authorizing non-logged-in user behavior in rails with cancan and devise
[英]Rails: authorizing user with CanCan gem issue
我的控制器中有一個如下所示的操作:
def show
@user = User.find(params[:user_id])
# ... more stuff.
end
注意,由於CanCan嘗試使用params[:id]
進行加載,因此我覆蓋了資源的加載,在這里我需要使用params[:user_id]
。
我的控制器的頂部定義如下:
authorize_resource :only => [:show]
在Ability.rb中:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :show, User if :id == user.id
end
end
但是,我所有的調用都是未經授權的。 我正在使用devise進行身份驗證,並且正在驗證傳遞給Initialize的user
對象確實是已登錄的用戶。 如何驗證登錄user
只能查看他們所請求的配置文件,而不能查看其他配置文件?
您不會覆蓋資源的加載,因為這是在操作之前發生的:
authorize_resource :only => [:show]
相反,您需要將其刪除並將授權添加到您的操作或單獨的before_filter。
def show
@user = User.find(params[:user_id])
authorize! :show, @user
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.