我计划在 Rails 3 应用程序中使用声明式授权。我有以下模型关系:
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 来引用正确数据集的正确权限。但是,问题是当我不能/不应该在模型中引用 current_context(其中定义了 role_symbols)时,如何使 def role_symbols 仅返回与特定上下文中的用户关联的角色。
有任何想法吗?