我计划在Rails3应用程序中使用声明式授权。我有以下模型关系:
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在正确的数据集上引用正确的权限。然而,问题是,当我不能/不应该在模型(定义了role_symbols的模型)中引用role_symbols时,如何使def current_context仅返回与特定上下文中的用户关联的角色。
有什么想法吗?
发布于 2010-12-21 10:44:59
幸运的是在这里找到了答案:
http://blog.drivingthevortex.nl/2010/01/24/using-declarative_authorization-with-subdomains/
https://stackoverflow.com/questions/4475787
复制相似问题