module IdPlease module ModelExtensions class AssignmentMap attr_accessor :assignments def initialize() @assignments = [] end def subjects @assignments.collect(&:subject).uniq end def subject_hash result = {} @assignments.each do |a| subject, name = a.subject, a.role.name.to_sym result[subject] ||= [] result[subject] << name end result.each_pair { |k,v| result[k] = v.uniq } return result end def roles @assignments.collect { |a| a.role.name.to_sym }.uniq end def role_hash result = {} @assignments.each do |a| subject, name = a.subject, a.role.name.to_sym result[name] ||= [] result[name] << subject end result.each_pair { |k,v| result[k] = v.uniq } return result end def role_with_assignments result = Hash.new { |h,k| h[k] = Hash.new() } @assignments.each do |a| subject, name, authorizable = a.subject, a.role.name.to_sym, a.role.authorizable result[name][subject] ||= [] result[name][subject] << authorizable end result.each_key { |name| result[name].each_pair { |subject,v| result[name][subject] = v.uniq }} return result end def map return self end end end end module IdPlease module ModelExtensions module ForGroup # TODO: come up with an object to represent the object here and just return that to the program # otherwise we just keep on coming back to edit this. def children(*args) options = args.extract_options! roles = options[:roles] || [_auth_group_role] view = options[:view] || :subjects map = options[:map] || AssignmentMap.new() valid_views = [:subjects, :subject_hash, :roles, :role_hash, :role_with_assignments, :map] raise "Invalid view argument, must be one of #{valid_views}" unless valid_views.include?(view) assignments = if roles == :any _auth_assign_class.role_authorizable_eq(self).all(:include => [:subject, {:role => :authorizable}]) else _auth_assign_class.role_name_eq(*roles.collect(&:to_s)).role_authorizable_eq(self).all(:include => [:subject, {:role => :authorizable}]) end assignments.each do |a| name, subject = a.role.name.to_sym, a.subject map.assignments << a if _auth_nested_groups == true && options[:nested] != false && subject._auth_is_group == true subject.children(:map => map, :view => :map) end end map.send(view) end def has_role!(role_name, object = nil) if object && object.kind_of?(self.class) && role_name.to_s == _auth_group_role && self.children.include?(object) raise "Attempt to make circular membership loop" else super end end end end end