1

我有一個組織的has_many的聯繫和那HAS_ONE組織declarative_authorization權限是零

任務

這樣我就可以做到這一點:

m = Mission.first 
m.organization.affiliations 

用戶還的has_many隸屬關係,所以我可以這樣做:

u = User.first 
u.affiliations 

在declarative_authorization我希望用戶能夠管理任務,如果他參加對任務的組織。 我有這樣的:

has_permission_on :missions, :to => [:manage] do 
    if_attribute :organization => { :affiliations => intersects_with { user.affiliates.type_admin } } 
end 

但我發現了這個錯誤:

Permission denied: new not allowed for #<User id: 2, firstname: "Miguel", lastname: "Alho", email: "[email protected]", birthday: "2010-07-05 20:24:00", crypted_password: "...", password_salt: "...", persistence_token: "...", perishable_token: "...", created_at: "2010-03-05 20:25:34", updated_at: "2010-03-30 15:45:36"> on #<Mission id: nil, user_id: nil, organization_id: nil, name: nil, objectives: nil, created_at: nil, updated_at: nil> 

的使命和組織都爲零。 我認爲這是因爲在新的,任務組織dosn't存在。

在變速箱控制器我有這樣的:

def new 
    if(params[:organization_id]) 
     session[:organization_id] = params[:organization_id] 
     @mission = Organization.find_by_id(params[:organization_id]).missions.build 
    end 
    end 

回答

1

我不能爲什麼你技術不工作說話,但這裏是我如何完成類似的事情。

從我在代碼示例中收集的內容中,您只需要與任務組織的所屬用戶能夠管理該任務。此外,該用戶必須是管理員類型。我已經在過去所做的是建立在你的組織模型的has_many關係,有一些額外的條件篩選出的隸屬關係,不是的輸入admin:

has_many :admins, :source => :user, :through => :affiliations, :conditions => "affiliations.type = 'admin'" 

然後在您的authorization_rules.rb您在此添加規則:

has_permission_on :missions, :to => [:manage] do { if_attribute :organization => {:admins => contains {user}}} 
+0

謝謝,這是非常有益的! – jspooner 2010-06-22 23:22:54