![](/img/trans.png)
[英]Using cancancan's check_authorization with devise with custom controllers?
[英]CanCanCan for custom controllers with not models
我知道如何在Rails 5中使用CanCan限制RESTful應用程序的訪問。
我的一些操作和控制器不是RESTful。
例如,我有一個帶有user_report方法的report_controller。 沒有直接鏈接到此控制器/操作的模型。
class ReportController < ApplicationController
load_and_authorize_resource
def user_report
end
end
如何在ability.rb文件中定義一種能力來限制對此操作的訪問?
在ability.rb
定義一個這樣的自定義能力:
can :view_reports, MyClass
在user_report
操作中,手動授權該功能:
def user_report
authorize! :view_reports, MyClass
# ...
end
此外,從ReportController
刪除load_and_authorize_resource
,因為您正在調用authorize!
直。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.