Skip to content

Commit

Permalink
Fixes theforeman#8428 - Connecting audits to existing users
Browse files Browse the repository at this point in the history
  • Loading branch information
orrabin authored and dLobatog committed Nov 28, 2014
1 parent 69dc398 commit 642c9d9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
10 changes: 5 additions & 5 deletions app/controllers/application_controller.rb
Expand Up @@ -42,6 +42,11 @@ def api_request?
request.format.try(:json?) || request.format.try(:yaml?)
end

# this method is returns the active user which gets used to populate the audits table
def current_user
User.current
end

protected

# Authorize the user for the requested action
Expand Down Expand Up @@ -80,11 +85,6 @@ def require_mail
end
end

# this method is returns the active user which gets used to populate the audits table
def current_user
User.current
end

def invalid_request
render :text => _('Invalid query'), :status => 400
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/concerns/audit_extensions.rb
Expand Up @@ -3,6 +3,7 @@ module AuditExtensions
extend ActiveSupport::Concern

included do
belongs_to :users, :class_name => 'User'
belongs_to :search_users, :class_name => 'User', :foreign_key => :user_id
belongs_to :search_hosts, :class_name => 'Host', :foreign_key => :auditable_id
belongs_to :search_hostgroups, :class_name => 'Hostgroup', :foreign_key => :auditable_id
Expand Down Expand Up @@ -36,7 +37,8 @@ module AuditExtensions
private

def ensure_username
self.username ||= User.current.to_s rescue ""
self.user_as_model = User.current
self.username = User.current.try(:to_label)
end

def fix_auditable_type
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20141120140051_remove_audit_user_fk.rb
@@ -0,0 +1,5 @@
class RemoveAuditUserFk < ActiveRecord::Migration
def change
remove_foreign_key(:audits, :user)
end
end
7 changes: 7 additions & 0 deletions test/factories/audit.rb
@@ -0,0 +1,7 @@
FactoryGirl.define do
factory :audit do
sequence(:version) {|n| "#{n}" }
auditable_type "test"
action "update"
end
end
17 changes: 17 additions & 0 deletions test/unit/concerns/audit_extensions_test.rb
@@ -0,0 +1,17 @@
require 'test_helper'

class AuditExtensionsTest < ActiveSupport::TestCase

def setup
@user = users :admin
end

test "should be connected to current user" do
audit = as_admin do
FactoryGirl.create(:audit)
end

assert_equal audit.user_id, @user.id
assert_equal audit.username, @user.name
end
end

0 comments on commit 642c9d9

Please sign in to comment.