Module: Vigilante::WatchedOperator

Defined in:
lib/vigilante/watched_operator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/vigilante/watched_operator.rb', line 4

def self.included(base)
  base.has_many :authorizations, :foreign_key => 'operator_id', :dependent => :destroy
  base.has_many :abilities, :through => :authorizations

  base.accepts_nested_attributes_for :authorizations, :reject_if => proc {|x| x[:ability_id].blank?}, :allow_destroy => true
  base.attr_accessible :authorizations_attributes
end

Instance Method Details

#add_authorization(role, extent = nil) ⇒ Object

Raises:

  • (StandardError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vigilante/watched_operator.rb', line 12

def add_authorization(role, extent=nil)
  ability = Ability.find_by_name(role.downcase)
  raise StandardError.new("Role #{role} is not converted to a corresponding authorization. It does not exist.") if ability.nil?

  #    extent_params = {}
  #    unless extent.nil?
  #      extent_params[:extent] = extent.id
  #      extent_params[:extent_type] = extent.class.name
  #    end

  new_authorization = ::Authorization.create(:operator_id => self.id, :ability_id => ability.id)
  unless extent.nil?
    new_authorization.add_extent(extent)
  end
  authorizations << new_authorization
  new_authorization
end

#add_role(role) ⇒ Object

convenience method: needed?



62
63
64
# File 'lib/vigilante/watched_operator.rb', line 62

def add_role(role)
  find_or_create_authorization(role)
end

#add_to_extent(extent, role = nil) ⇒ Object

Extent-specific



79
80
81
82
83
# File 'lib/vigilante/watched_operator.rb', line 79

def add_to_extent(extent, role = nil)
  #### TODO: configure default role!!!
  role = 'can-read-all' if role.nil?
  find_or_create_authorization(role, extent)
end

#extent_role(extent) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/vigilante/watched_operator.rb', line 85

def extent_role(extent)
  return nil if extent.nil?
  self.reload.authorizations.each do |auth|
    if auth.has_extent?
      return auth.ability.name if auth.match_extent(extent)
    end
  end
  nil
end

#extent_rolesObject



99
100
101
# File 'lib/vigilante/watched_operator.rb', line 99

def extent_roles
  self.authorizations.select{|x| x.has_extent? }.collect{|x| x.ability.name }
end

#find_authorization(role, extent = nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/vigilante/watched_operator.rb', line 30

def find_authorization(role, extent=nil)
  authorizations.each do |auth|
    return auth if auth.ability.name.downcase == role.downcase && (auth.match_extent(extent) || extent == :any)
  end
  nil
end

#find_or_create_authorization(role, extent = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/vigilante/watched_operator.rb', line 37

def find_or_create_authorization(role, extent = nil)
  auth = find_authorization(role, :any)
  if auth.nil?
    auth = add_authorization(role, extent)
  elsif extent.present?
    auth.add_extent(extent)
  end
  auth
end

#has_extent?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/vigilante/watched_operator.rb', line 95

def has_extent?
  asp_roles.count >= 1
end

#permitsObject



112
113
114
# File 'lib/vigilante/watched_operator.rb', line 112

def permits
  @permissions ||= build_permissions_hash
end

#permits=(permissions) ⇒ Object

keep permits



107
108
109
110
# File 'lib/vigilante/watched_operator.rb', line 107

def permits=(permissions)
  # make a copy!
  @permissions = {}.merge(permissions)
end

#rolesObject

convenience method: list all assigned roles (without extent?)



67
68
69
70
71
# File 'lib/vigilante/watched_operator.rb', line 67

def roles
  # we can't use abilities as those are not defined when creating a new operator that is not yet saved
  #result = abilities.collect(&:name)
  authorizations.collect{|auth| auth.ability.try(:name)}
end

#roles=(roles_arr) ⇒ Object

an array containing any of existing ability-names

will add the corresponding ability


54
55
56
57
58
59
# File 'lib/vigilante/watched_operator.rb', line 54

def roles=(roles_arr)
  self.authorizations = []
  roles_arr.each do |role|
    find_or_create_authorization(role) unless role.blank?
  end
end

#show_rolesObject



73
74
75
# File 'lib/vigilante/watched_operator.rb', line 73

def show_roles
  authorizations.collect {|a| a.ability.try(:name) + "[" + a.authorization_extents.collect{|e| e.extent}.join(',') + "]"}
end