2015-06-22 64 views
1

我有幾個奇異的資源在我的應用程序,例如:如何使用權威人士對單一資源實施範圍界定?

# routes.rb 
MySite::Application.routes.draw do 
    resource :thing 
end 

# things_controller.rb 
class ThingsController < ApplicationController 
    def edit 
    load_thing 
    end 

    def update 
    load_thing 
    if @thing.update_attributes(thing_params) 
     ... 
    else 
     ... 
    end 
    end 

    private 

    def load_thing 
    @thing ||= current_user.thing 
    end 

    def thing_params 
    params.require(:thing).permit(...) 
    end 
end 

我不知道如何利用權威人士來執行策略作用域(before_action :verify_policy_scopedApplicationController設置)。

我不知道如何形成我爲奇異資源政策範圍,即:

# thing_policy.rb 
class ThingPolicy < ApplicationPolicy 
    Scope < Scope 
    def resolve 
     # What to do here... 
     # scope => ? 
    end 
    end 
end 

# things_controller.rb 
def load_thing 
    # ...and what to do here 
    @thing ||= policy_scope(...) 
end 

根據Pundit's docs

...方法resolve ...應該返回一些種類的結果可以是 迭代。

但是,在單數資源情況下,此迭代條款並不真正有效,因此沒有AR樣式範圍......只是一條記錄。

任何人有什麼建議如何去做到這一點?

回答

0

通常對index操作執行作用域範圍限制,以限制用戶在應用過濾之前可以訪問的基本記錄集。對於單個記錄,您應該改爲authorize

在任何情況下,你已經確定範圍@thingcurrent_user

建議限制before_action :verify_policy_scopedonly: [:index]