繁体   English   中英

声明性授权和用户上下文

[英]declarative_authorization and user context

我正在计划在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在正确的数据集上引用正确的权限。 但是,问题是当我不能/不应该在模型(定义了role_symbols的模型)中引用current_context时,如何使def role_symbols仅返回特定上下文中与用户关联的角色。

有任何想法吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM