2012-09-17 91 views
3

我想補充filter_access_toattribute_check: true但我的模型,Assignment,嵌套在Project,我想檢查當前用戶被分配到該項目,以便他/她可以更新項目的任務(即所有任務,而不僅僅是一個具體任務)。屬性的嵌套資源請與聲明性授權

我接下來的權限:

has_permissions_on :assignment do |a| 
    to :read 
    if_attribute :user, is { user } 
end 

而且在我的控制器:

filter_access_to :all, require: :manage, context: :assignment, attribute_check: true 

的問題是,我無法訪問索引頁,因爲它沒有找到分配的ID 。

我的車型有:

class User < ActiveRecord::Base 
    has_many :assignments 
    has_many :projects, through: :assignments 
end 

class Project < ActiveRecord::Base 
    has_many :assignments 
    has_many :users, through: :assignments 
end 

class Assignment < ActiveRecord::Base 
    belongs_to :user, inverse_of: :assignments 
    belongs_to :project, inverse_of: :assignments 
end 

回答

3

除非你有一個非常具體的需要使用filter_access_to,您可以使用filter_resource_accesssee docs)來代替,並指定:nested_in選項。

只需更改與filer_access_to線以下(或類似):

filter_resource_access nested_in: :projects 

如果你真的想繼續使用filter_access_to,那麼你需要創建與項目相關的分配的一個實例,併爲它分配到@assignment實例變量在before_filter。當您使用filter_resource_access時,這是聲明式授權所隱藏的內容。

0

下面是奏效了,我的代碼:

before_filter :set_assignment 
    filter_access_to [:index,:create,:destroy], require: :manage, context: :assignment, attribute_check: true 
.... 
def set_assignment 
    @project ||= Project.find(params[:project_id]) 
    p = {project_id: @project.id}.merge(params[:assignment] || {}) 
    @assignment ||= (params[:id] ? Assignment.find(params[:id]) : Assignment.new(p)) 
end 

不知道爲什麼filter_resource_access :nested_in => :project沒有工作。它適用於index,但它不適用於createdestroy