Module: IdPlease::ModelExtensions::ForSubject

Defined in:
lib/id_please/model_extensions/for_subject.rb

Instance Method Summary collapse

Instance Method Details

#groups(*args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/id_please/model_extensions/for_subject.rb', line 37

def groups(*args)
  raise "Groups must be enabled for this model" unless _auth_groups_enabled

  options = {:view => :targets}.merge(args.extract_options!)
  view = options[:view]
  valid_views = [:targets, :targets_with_assignments, :map]
  
  
  raise "Invalid view argument, must be one of #{valid_views}" unless valid_views.include?(view)
  
  members_only = if options.has_key?(:members_only)
    options[:members_only]
  else
    view == :targets ? true : false 
  end
    

  map = IdPlease::AssignmentMap.new(:assignment_class => _auth_assign_class, :direction => :up, :subjects => self, :members_only => members_only, :nested => _auth_nested_groups && options[:nested] != false)
  
  
  return map.send(view)
  
  options = args.extract_options!
  all_groups = direct_parent_groups(self)
  
  if _auth_nested_groups && options[:nested] != false
    infinite_loop_counter = 25
    to_find = all_groups.dup
    
    until to_find.empty? || (infinite_loop_counter -=1) <= 0
      to_find = direct_parent_groups(*to_find)
      all_groups += to_find
    end
    
    all_groups.uniq
  else
    all_groups
  end
end

#groups_with_assignments(*args) ⇒ Object



32
33
34
35
# File 'lib/id_please/model_extensions/for_subject.rb', line 32

def groups_with_assignments(*args)
  options = args.extract_options!.merge(:view => :targets_with_assignments)
  groups(options)
end

#has_no_role!(role_name, object = nil) ⇒ Object



118
119
120
121
122
# File 'lib/id_please/model_extensions/for_subject.rb', line 118

def has_no_role!(role_name, object = nil)
  role = get_role(role_name, object)
  
  remove_from_role(role)
end

#has_no_roles!Object

Unassign all roles from self.



132
133
134
135
136
137
# File 'lib/id_please/model_extensions/for_subject.rb', line 132

def has_no_roles!
  roles = self._auth_assigned_roles.clone
  roles.each do |role|
    remove_from_role(role)
  end
end

#has_no_roles_for!(object = nil) ⇒ Object



124
125
126
# File 'lib/id_please/model_extensions/for_subject.rb', line 124

def has_no_roles_for!(object = nil)
  roles_for(object).each { |role| remove_from_role(role) }
end

#has_role!(role_name, object = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/id_please/model_extensions/for_subject.rb', line 85

def has_role!(role_name, object = nil)
  role = get_role(role_name, object)
  
  if role.nil?
    role_attrs = case object
    when Class 
      { :authorizable_type => object.to_s }
    when nil 
      {}
    else 
      { :authorizable => object }
    end.merge(:name => role_name.to_s)
    
    role = self._auth_assigned_roles.create!(role_attrs)
  else
    unless assigned_to_role?(role)
      self._auth_assigned_roles << role
    end
  end
  
  role
end

#has_role?(role_name, object = nil, option_hash = {}) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/id_please/model_extensions/for_subject.rb', line 79

def has_role?(role_name, object = nil, option_hash = {})
  subjects_to_check = option_hash[:check_groups] == false ? [self] : self_and_groups

  !Role.authorizable_eq(object).name_eq(role_name.to_s).assignments_subject_eq(*subjects_to_check).empty?
end

#has_roles_for?(object) ⇒ Boolean Also known as: has_role_for?

Returns:

  • (Boolean)


108
109
110
# File 'lib/id_please/model_extensions/for_subject.rb', line 108

def has_roles_for?(object)
  !get_assigned_roles_for(object).empty?
end

#in_group!(group) ⇒ Object



4
5
6
7
8
9
# File 'lib/id_please/model_extensions/for_subject.rb', line 4

def in_group!(group)
  raise "Groups must be enabled for this model" unless _auth_groups_enabled
  raise "Object passed in not a group" unless group.kind_of?(_auth_group_class)
  
  self.has_role!(_auth_group_role, group)
end

#in_group?(group) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
# File 'lib/id_please/model_extensions/for_subject.rb', line 11

def in_group?(group)
  raise "Groups must be enabled for this model" unless _auth_groups_enabled
  raise "Object passed in not a group" unless group.kind_of?(_auth_group_class)
  
  self.has_role?(_auth_group_role, group)
end

#not_in_any_group!Object



25
26
27
28
29
# File 'lib/id_please/model_extensions/for_subject.rb', line 25

def not_in_any_group!
  raise "Groups must be enabled for this model" unless _auth_groups_enabled
  
  all_groups.each { |group| self.not_in_group!(group) }
end

#not_in_group!(group) ⇒ Object



18
19
20
21
22
23
# File 'lib/id_please/model_extensions/for_subject.rb', line 18

def not_in_group!(group)
  raise "Groups must be enabled for this model" unless _auth_groups_enabled
  raise "Object passed in not a group" unless group.kind_of?(_auth_group_class)
  
  self.has_no_role!(_auth_group_role, group)
end

#roles_for(object) ⇒ Object



114
115
116
# File 'lib/id_please/model_extensions/for_subject.rb', line 114

def roles_for(object)
  get_assigned_roles_for(object)
end