2010-12-17 43 views
2

我正計劃在Rails 3應用程序中使用聲明式授權。 我有以下型號的關係:declarative_authorization和用戶上下文

class Role < ActiveRecord::Base 
    has_many :permissions, :dependent => :destroy 
    has_many :users, :through => :permissions, :uniq => true 
end 

class Permission < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :role 
    belongs_to :context 
end 

class User < ActiveRecord::Base 
    has_many :permissions, :dependent => :destroy 
    has_many :roles, :through => :permissions 

    roles.map do |role| 
     role.name.underscore.to_sym 
    end 
end 

class Context < ActiveRecord::Base 
    has_many :contexts, :dependent => :destroy 

end 

這裏的概念是,我要段不同的數據集到不同的上下文。但是,給定的用戶對於每個上下文可能具有不同的角色 - 可能是一個上下文中的管理員,另一個上的基本用戶。我實現了current_user和current_context以用於控制器和視圖。

我打算使用if_attribute引用右側數據集的權限。 但是,問題是我如何才能讓def role_symbols僅在特定上下文中返回與用戶關聯的角色,因爲我無法/不應該在模型中(其中定義了role_symbols)引用current_context。

任何想法?

回答