简体   繁体   中英

declarative_authorization and user context

Am planning on using declarative authorization in a Rails 3 app. I have the following model relationships:

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

The concept here is that I am going to segment various datasets into different contexts. However, a given user may have different roles for each context -- maybe an admin in one context and a basic user in another. I have implemented current_user and current_context for use in controllers and views.

I plan on using the if_attribute to reference the right permission on the right dataset. However, the question is how do I make the def role_symbols only return the roles associated with the user in a particular context when I cannot/should not reference current_context in a model (where role_symbols is defined).

Any ideas?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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