Project

General

Profile

Download (4.38 KB) Statistics
| Branch: | Tag: | Revision:
661e86c7 Ohad Levy
module AuditsHelper

09ce1364 Amos Benari
MainObjects = %w(Host Hostgroup User Operatingsystem Environment Puppetclass Parameter Architecture ComputeResource ConfigTemplate)

# lookup the Model representing the numerical id and return its label
661e86c7 Ohad Levy
def id_to_label name, change
3dfb6e4c Dominic Cleal
return _("N/A") if change.nil?
09ce1364 Amos Benari
case name
when "ancestry"
3dfb6e4c Dominic Cleal
change.blank? ? "" : change.split('/').map { |i| Hostgroup.find(i).name rescue _("NA") }.join('/')
09ce1364 Amos Benari
when 'last_login_on'
change.to_s(:short)
when /.*_id$/
ef4b97d1 Joseph Mitchell Magen
name.humanize.constantize.find(change).to_label
09ce1364 Amos Benari
else
change.to_s
end.truncate(50)
661e86c7 Ohad Levy
rescue
3dfb6e4c Dominic Cleal
_("N/A")
661e86c7 Ohad Levy
end

def audit_title audit
de3fb0d7 Amos Benari
type_name = audited_type audit
09ce1364 Amos Benari
if type_name == "Puppet Class"
"#{id_to_label audit.audited_changes.keys[0], audit.audited_changes.values[0]}"
else
de3fb0d7 Amos Benari
name = audit.auditable_name.blank? ? audit.revision.to_label : audit.auditable_name
name += " / #{audit.associated_name}" if audit.associated_id and !audit.associated_name.blank?
name
09ce1364 Amos Benari
end
rescue
""
661e86c7 Ohad Levy
end

09ce1364 Amos Benari
def details audit
if audit.action == 'update'
ff9597b4 Joseph Mitchell Magen
Array.wrap(audit.audited_changes).map do |name, change|
09ce1364 Amos Benari
next if change.nil? or change.to_s.empty?
if name == 'template'
bfbf7ed8 Lukas Zapletal
(_("Provisioning Template content changed %s") % (link_to 'view diff', audit_path(audit))).html_safe if audit_template? audit
09ce1364 Amos Benari
elsif name == "owner_id" || name == "owner_type"
3dfb6e4c Dominic Cleal
_("Owner changed to %s") % (audit.revision.owner rescue _('N/A'))
09ce1364 Amos Benari
else
bfbf7ed8 Lukas Zapletal
_("%{name} changed from %{label1} to %{label2}") % { :name => name.humanize, :label1 => id_to_label(name, change[0]), :label2 => id_to_label(name, change[1]) }
09ce1364 Amos Benari
end
end
elsif !main_object? audit
["#{audit_action_name(audit).humanize} #{id_to_label audit.audited_changes.keys[0], audit.audited_changes.values[0]}
#{audit_action_name(audit)=="removed" ? "from" : "to"} #{id_to_label audit.audited_changes.keys[1], audit.audited_changes.values[1]}"]
else
[]
end
end

def audit_template? audit
audit.auditable_type == "ConfigTemplate" && audit.action == 'update' && audit.audited_changes["template"] &&
audit.audited_changes["template"][0] != audit.audited_changes["template"][1]
end

def audit_login? audit
name = audit.audited_changes.keys[0] rescue ''
name == 'last_login_on'
end

def audit_action_name audit
return audit.action == 'destroy' ? 'destroyed' : "#{audit.action}d" if main_object? audit

case audit.action
when 'create'
'added'
when 'destroy'
'removed'
else
'updated'
end
end

def audit_user audit
e2217aa6 Ohad Levy
return if audit.username.nil?
login = audit.user.login rescue nil # aliasing the user method sometimes yields strings
bfbf7ed8 Lukas Zapletal
link_to(icon_text('user', audit.username.gsub(_('User'), '')), hash_for_audits_path(:search => login ? "user = #{login}" : "username = \"#{audit.username}\""))
09ce1364 Amos Benari
end

def audit_time audit
3dfb6e4c Dominic Cleal
content_tag :span, _("%s ago") % time_ago_in_words(audit.created_at),
09ce1364 Amos Benari
{ :'data-original-title' => audit.created_at.to_s(:long), :rel => 'twipsy' }
661e86c7 Ohad Levy
end

de3fb0d7 Amos Benari
def audited_icon audit
style = 'label-info'
09ce1364 Amos Benari
style = case audit.action
when 'create'
de3fb0d7 Amos Benari
'label-success'
09ce1364 Amos Benari
when 'update'
de3fb0d7 Amos Benari
'label-info'
09ce1364 Amos Benari
when 'destroy'
bf4a13d3 Walden Raines
'label-danger'
09ce1364 Amos Benari
else
de3fb0d7 Amos Benari
''
09ce1364 Amos Benari
end if main_object? audit
de3fb0d7 Amos Benari
style += " label"
09ce1364 Amos Benari
de3fb0d7 Amos Benari
type = audited_type(audit)
09ce1364 Amos Benari
symbol = case type
when "Host"
de3fb0d7 Amos Benari
'hdd'
09ce1364 Amos Benari
when "Hostgroup"
de3fb0d7 Amos Benari
'tasks'
09ce1364 Amos Benari
when "User"
de3fb0d7 Amos Benari
'user'
09ce1364 Amos Benari
else
de3fb0d7 Amos Benari
'cog'
09ce1364 Amos Benari
end
de3fb0d7 Amos Benari
content_tag(:b, icon_text(symbol, type, :class => 'icon-white'), :class => style)
09ce1364 Amos Benari
end

de3fb0d7 Amos Benari
def audited_type audit
type_name = audit.auditable_type
type_name = "Puppet Class" if type_name == "HostClass"
type_name = "#{audit.associated_type || 'Global'}-#{type_name}" if type_name == "Parameter"
09ce1364 Amos Benari
type_name.underscore.titleize
661e86c7 Ohad Levy
end

de3fb0d7 Amos Benari
def audit_remote_address audit
return if audit.remote_address.empty?
content_tag :span, :style => 'color:#999;' do
"(" + audit.remote_address + ")"
end
end

09ce1364 Amos Benari
private
def main_object? audit
type = audit.auditable_type.split("::").last rescue ''
MainObjects.include?(type)
3b49df17 Amos Benari
end

661e86c7 Ohad Levy
end