[英]Rails testing: ensure authorization (Pundit) is enforced in all controllers and actions
[英]skip authorization for specific controllers using pundit in rails 4
我使用rails 4,設計用於身份驗證,Pundit用於授權。 我已經限制我的應用程序通過以下代碼檢查每個控制器上的授權。
class ApplicationController < ActionController::Base
include Pundit
after_action :verify_authorized
#.....
end
但是,我想在我的應用程序中跳過對兩個特定控制器的授權(它們向公眾開放,用戶無需登錄)。 如何在不刪除ApplicationController中的verify_authorized的情況下實現它?
skip_after_action :verify_authorized
我正在使用Rails 5 ,我只想在一個動作中跳過授權而不是整個控制器。 因此,根據文檔可以做的是在控制器操作中使用skip_authorization
功能,如下所示:
class Admin::DashboardController < Admin::BaseController
def index
@organizers = Organizer.count
@sponsors = Sponsor.count
@brochures = Brochure.count
skip_authorization
end
def sponsors_approve
# some statements...
end
def organizers_approve
# some statements...
end
end
在此控制器中,唯一要跳過的操作是index
,其他操作必須經過授權。
我希望它對其他人有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.