auditor 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +8 -0
- data/Gemfile +4 -0
- data/README.rdoc +58 -11
- data/Rakefile +33 -0
- data/auditor.gemspec +23 -0
- data/init.rb +1 -0
- data/lib/auditor.rb +16 -4
- data/lib/auditor/audit.rb +49 -2
- data/lib/auditor/auditable.rb +44 -0
- data/lib/auditor/config.rb +38 -0
- data/lib/auditor/recorder.rb +37 -36
- data/lib/auditor/spec_helpers.rb +6 -6
- data/lib/auditor/status.rb +60 -0
- data/lib/auditor/user.rb +5 -6
- data/lib/auditor/version.rb +2 -2
- data/lib/generators/auditor/migration/migration_generator.rb +4 -4
- data/lib/generators/auditor/migration/templates/migration.rb +8 -4
- data/spec/audit_spec.rb +63 -0
- data/spec/auditable_spec.rb +111 -0
- data/spec/config_spec.rb +49 -0
- data/spec/recorder_spec.rb +73 -107
- data/spec/spec_helper.rb +2 -26
- data/spec/status_spec.rb +18 -0
- data/spec/support/db_setup.rb +50 -0
- data/spec/support/transactional_specs.rb +17 -0
- data/spec/user_spec.rb +5 -5
- metadata +70 -34
- data/lib/auditor/config_parser.rb +0 -36
- data/lib/auditor/integration.rb +0 -49
- data/lib/auditor/model_audit.rb +0 -47
- data/lib/auditor/thread_local.rb +0 -18
- data/lib/auditor/thread_status.rb +0 -34
- data/spec/config_parser_spec.rb +0 -53
- data/spec/model_audit_spec.rb +0 -83
- data/spec/support/auditor_helpers.rb +0 -29
- data/spec/thread_local_spec.rb +0 -14
- data/spec/thread_status_spec.rb +0 -16
data/spec/spec_helper.rb
CHANGED
@@ -1,37 +1,13 @@
|
|
1
1
|
require 'rspec'
|
2
|
+
require 'auditor'
|
2
3
|
|
3
4
|
# Requires supporting files with custom matchers and macros, etc,
|
4
5
|
# in ./support/ and its subdirectories.
|
5
6
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
6
7
|
|
7
8
|
RSpec.configure do |config|
|
8
|
-
|
9
|
-
# lines, delete config/database.yml and disable :active_record
|
10
|
-
# in your config/boot.rb
|
11
|
-
# config.use_transactional_fixtures = true
|
12
|
-
# config.use_instantiated_fixtures = false
|
9
|
+
config.include TransactionalSpecs
|
13
10
|
|
14
|
-
# == Fixtures
|
15
|
-
#
|
16
|
-
# You can declare fixtures for each example_group like this:
|
17
|
-
# describe "...." do
|
18
|
-
# fixtures :table_a, :table_b
|
19
|
-
#
|
20
|
-
# Alternatively, if you prefer to declare them only once, you can
|
21
|
-
# do so right here. Just uncomment the next line and replace the fixture
|
22
|
-
# names with your fixtures.
|
23
|
-
#
|
24
|
-
# config.global_fixtures = :all
|
25
|
-
#
|
26
|
-
# If you declare global fixtures, be aware that they will be declared
|
27
|
-
# for all of your examples, even those that don't use them.
|
28
|
-
#
|
29
|
-
# You can also declare which fixtures to use (for example fixtures for test/fixtures):
|
30
|
-
#
|
31
|
-
# config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
|
32
|
-
#
|
33
|
-
# == Mock Framework
|
34
|
-
#
|
35
11
|
# RSpec uses it's own mocking framework by default. If you prefer to
|
36
12
|
# use mocha, flexmock or RR, uncomment the appropriate line:
|
37
13
|
#
|
data/spec/status_spec.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
require 'auditor/status'
|
3
|
+
|
4
|
+
describe Auditor::Status do
|
5
|
+
it "should be enabled if set to enabled" do
|
6
|
+
obj = Class.new { include Auditor::Status }.new
|
7
|
+
obj.enable_auditing
|
8
|
+
obj.should be_auditing_enabled
|
9
|
+
obj.should_not be_auditing_disabled
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should be disabled if set to disabled" do
|
13
|
+
obj = Class.new { include Auditor::Status }.new
|
14
|
+
obj.disable_auditing
|
15
|
+
obj.should_not be_auditing_enabled
|
16
|
+
obj.should be_auditing_disabled
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
require 'active_record'
|
3
|
+
require 'generators/auditor/migration/templates/migration'
|
4
|
+
|
5
|
+
tmpdir = File.join(File.dirname(__FILE__), '..', '..', 'tmp')
|
6
|
+
FileUtils.mkdir(tmpdir) unless File.exist?(tmpdir)
|
7
|
+
test_db = File.join(tmpdir, 'test.db')
|
8
|
+
|
9
|
+
connection_spec = {
|
10
|
+
:adapter => 'sqlite3',
|
11
|
+
:database => test_db
|
12
|
+
}
|
13
|
+
|
14
|
+
# Delete any existing instance of the test database
|
15
|
+
FileUtils.rm test_db, :force => true
|
16
|
+
|
17
|
+
# Create a new test database
|
18
|
+
ActiveRecord::Base.establish_connection(connection_spec)
|
19
|
+
|
20
|
+
# ActiveRecord::Base.connection.initialize_schema_migrations_table
|
21
|
+
|
22
|
+
class CreateUser < ActiveRecord::Migration
|
23
|
+
def self.up
|
24
|
+
create_table :users, :force => true do |t|
|
25
|
+
t.column :username, :string
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.down
|
30
|
+
drop_table :users
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class CreateModel < ActiveRecord::Migration
|
35
|
+
def self.up
|
36
|
+
create_table :models, :force => true do |t|
|
37
|
+
t.column :name, :string
|
38
|
+
t.column :value, :string
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.down
|
43
|
+
drop_table :models
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
CreateUser.up
|
48
|
+
CreateModel.up
|
49
|
+
CreateAuditsTable.up
|
50
|
+
|
data/spec/user_spec.rb
CHANGED
@@ -7,19 +7,19 @@ describe Auditor::User do
|
|
7
7
|
Auditor::User.current_user = user
|
8
8
|
Auditor::User.current_user.should == user
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
it "should not return the same user from a different thread" do
|
12
12
|
user = "user"
|
13
13
|
user2 = "user2"
|
14
|
-
|
14
|
+
|
15
15
|
Auditor::User.current_user = user
|
16
|
-
|
16
|
+
|
17
17
|
Thread.new do
|
18
18
|
Auditor::User.current_user.should be_nil
|
19
19
|
Auditor::User.current_user = user2
|
20
20
|
Auditor::User.current_user.should == user2
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
Auditor::User.current_user.should == user
|
24
24
|
end
|
25
|
-
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,22 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: auditor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 15
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
9
|
- 0
|
10
|
-
version:
|
10
|
+
version: 2.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jeff Kunkle
|
14
|
-
- Matt Wizeman
|
15
14
|
autorequire:
|
16
15
|
bindir: bin
|
17
16
|
cert_chain: []
|
18
17
|
|
19
|
-
date: 2011-
|
18
|
+
date: 2011-03-16 00:00:00 -04:00
|
20
19
|
default_executable:
|
21
20
|
dependencies:
|
22
21
|
- !ruby/object:Gem::Dependency
|
@@ -25,14 +24,48 @@ dependencies:
|
|
25
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
26
25
|
none: false
|
27
26
|
requirements:
|
28
|
-
- - "
|
27
|
+
- - "="
|
29
28
|
- !ruby/object:Gem::Version
|
30
|
-
hash:
|
29
|
+
hash: 27
|
31
30
|
segments:
|
31
|
+
- 2
|
32
|
+
- 5
|
32
33
|
- 0
|
33
|
-
version:
|
34
|
+
version: 2.5.0
|
34
35
|
type: :development
|
35
36
|
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: sqlite3-ruby
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - "="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 29
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 3
|
49
|
+
- 3
|
50
|
+
version: 1.3.3
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: activerecord
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 7
|
62
|
+
segments:
|
63
|
+
- 3
|
64
|
+
- 0
|
65
|
+
- 0
|
66
|
+
version: 3.0.0
|
67
|
+
type: :development
|
68
|
+
version_requirements: *id003
|
36
69
|
description: Auditor allows you to declaratively specify what CRUD operations should be audited and save the audit data to the database.
|
37
70
|
email:
|
38
71
|
executables: []
|
@@ -42,29 +75,33 @@ extensions: []
|
|
42
75
|
extra_rdoc_files: []
|
43
76
|
|
44
77
|
files:
|
78
|
+
- .gitignore
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE
|
81
|
+
- README.rdoc
|
82
|
+
- Rakefile
|
83
|
+
- auditor.gemspec
|
84
|
+
- init.rb
|
85
|
+
- lib/auditor.rb
|
45
86
|
- lib/auditor/audit.rb
|
46
|
-
- lib/auditor/
|
47
|
-
- lib/auditor/
|
48
|
-
- lib/auditor/model_audit.rb
|
87
|
+
- lib/auditor/auditable.rb
|
88
|
+
- lib/auditor/config.rb
|
49
89
|
- lib/auditor/recorder.rb
|
50
90
|
- lib/auditor/spec_helpers.rb
|
51
|
-
- lib/auditor/
|
52
|
-
- lib/auditor/thread_status.rb
|
91
|
+
- lib/auditor/status.rb
|
53
92
|
- lib/auditor/user.rb
|
54
93
|
- lib/auditor/version.rb
|
55
|
-
- lib/auditor.rb
|
94
|
+
- lib/generators/auditor.rb
|
56
95
|
- lib/generators/auditor/migration/migration_generator.rb
|
57
96
|
- lib/generators/auditor/migration/templates/migration.rb
|
58
|
-
-
|
59
|
-
-
|
60
|
-
-
|
61
|
-
- spec/config_parser_spec.rb
|
62
|
-
- spec/model_audit_spec.rb
|
97
|
+
- spec/audit_spec.rb
|
98
|
+
- spec/auditable_spec.rb
|
99
|
+
- spec/config_spec.rb
|
63
100
|
- spec/recorder_spec.rb
|
64
101
|
- spec/spec_helper.rb
|
65
|
-
- spec/
|
66
|
-
- spec/
|
67
|
-
- spec/
|
102
|
+
- spec/status_spec.rb
|
103
|
+
- spec/support/db_setup.rb
|
104
|
+
- spec/support/transactional_specs.rb
|
68
105
|
- spec/user_spec.rb
|
69
106
|
has_rdoc: true
|
70
107
|
homepage: http://github.com/nearinfinity/auditor
|
@@ -89,25 +126,24 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
126
|
requirements:
|
90
127
|
- - ">="
|
91
128
|
- !ruby/object:Gem::Version
|
92
|
-
hash:
|
129
|
+
hash: 3
|
93
130
|
segments:
|
94
|
-
-
|
95
|
-
|
96
|
-
- 6
|
97
|
-
version: 1.3.6
|
131
|
+
- 0
|
132
|
+
version: "0"
|
98
133
|
requirements: []
|
99
134
|
|
100
135
|
rubyforge_project:
|
101
|
-
rubygems_version: 1.
|
136
|
+
rubygems_version: 1.4.2
|
102
137
|
signing_key:
|
103
138
|
specification_version: 3
|
104
139
|
summary: Rails 3 plugin for auditing access to your ActiveRecord model objects
|
105
140
|
test_files:
|
106
|
-
- spec/
|
107
|
-
- spec/
|
141
|
+
- spec/audit_spec.rb
|
142
|
+
- spec/auditable_spec.rb
|
143
|
+
- spec/config_spec.rb
|
108
144
|
- spec/recorder_spec.rb
|
109
145
|
- spec/spec_helper.rb
|
110
|
-
- spec/
|
111
|
-
- spec/
|
112
|
-
- spec/
|
146
|
+
- spec/status_spec.rb
|
147
|
+
- spec/support/db_setup.rb
|
148
|
+
- spec/support/transactional_specs.rb
|
113
149
|
- spec/user_spec.rb
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module Auditor
|
2
|
-
class ConfigParser
|
3
|
-
|
4
|
-
def self.extract_config(args)
|
5
|
-
options = (args.delete_at(args.size - 1) if args.last.kind_of?(Hash)) || {}
|
6
|
-
normalize_config args, options
|
7
|
-
validate_config args, options
|
8
|
-
options = normalize_options(options)
|
9
|
-
|
10
|
-
[args, options]
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
def self.normalize_config(actions, options)
|
16
|
-
actions.each_with_index { |item, index| actions[index] = item.to_sym }
|
17
|
-
options.each_pair { |k, v| options[k.to_sym] = options.delete(k) unless k.kind_of? Symbol }
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.normalize_options(options)
|
21
|
-
return { :except => [], :only => [] } if options.nil? || options.empty?
|
22
|
-
options[:except] = options[:except] || []
|
23
|
-
options[:only] = options[:only] || []
|
24
|
-
options[:except] = Array(options[:except]).map(&:to_s)
|
25
|
-
options[:only] = Array(options[:only]).map(&:to_s)
|
26
|
-
options
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.validate_config(actions, options)
|
30
|
-
raise Auditor::Error.new "at least one :create, :find, :update, or :destroy action must be specified" if actions.empty?
|
31
|
-
raise Auditor::Error.new ":create, :find, :update, and :destroy are the only valid actions" unless actions.all? { |a| [:create, :find, :update, :destroy].include? a }
|
32
|
-
raise Auditor::Error.new "only one of :except and :only can be specified" if options.size > 1
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
end
|
data/lib/auditor/integration.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'auditor/thread_status'
|
2
|
-
|
3
|
-
module Auditor
|
4
|
-
module Integration
|
5
|
-
|
6
|
-
def without_auditor
|
7
|
-
previously_disabled = auditor_disabled?
|
8
|
-
disable_auditor
|
9
|
-
|
10
|
-
begin
|
11
|
-
result = yield if block_given?
|
12
|
-
ensure
|
13
|
-
enable_auditor unless previously_disabled
|
14
|
-
end
|
15
|
-
|
16
|
-
result
|
17
|
-
end
|
18
|
-
|
19
|
-
def with_auditor
|
20
|
-
previously_disabled = auditor_disabled?
|
21
|
-
enable_auditor
|
22
|
-
|
23
|
-
begin
|
24
|
-
result = yield if block_given?
|
25
|
-
ensure
|
26
|
-
disable_auditor if previously_disabled
|
27
|
-
end
|
28
|
-
|
29
|
-
result
|
30
|
-
end
|
31
|
-
|
32
|
-
def disable_auditor
|
33
|
-
Auditor::ThreadStatus.disable
|
34
|
-
end
|
35
|
-
|
36
|
-
def enable_auditor
|
37
|
-
Auditor::ThreadStatus.enable
|
38
|
-
end
|
39
|
-
|
40
|
-
def auditor_disabled?
|
41
|
-
Auditor::ThreadStatus.disabled?
|
42
|
-
end
|
43
|
-
|
44
|
-
def auditor_enabled?
|
45
|
-
Auditor::ThreadStatus.enabled?
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
end
|
data/lib/auditor/model_audit.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'auditor/thread_status'
|
2
|
-
require 'auditor/config_parser'
|
3
|
-
require 'auditor/recorder'
|
4
|
-
|
5
|
-
module Auditor
|
6
|
-
module ModelAudit
|
7
|
-
|
8
|
-
def self.included(base)
|
9
|
-
base.extend ClassMethods
|
10
|
-
end
|
11
|
-
|
12
|
-
# ActiveRecord won't call the after_find handler unless it see's a specific after_find method defined
|
13
|
-
def after_find; end
|
14
|
-
|
15
|
-
def auditor_disabled?
|
16
|
-
Auditor::ThreadStatus.disabled? || @auditor_disabled
|
17
|
-
end
|
18
|
-
|
19
|
-
module ClassMethods
|
20
|
-
def audit(*args, &blk)
|
21
|
-
actions, options = Auditor::ConfigParser.extract_config(args)
|
22
|
-
|
23
|
-
actions.each do |action|
|
24
|
-
unless action.to_sym == :find
|
25
|
-
callback = "auditor_before_#{action}"
|
26
|
-
define_method(callback) do
|
27
|
-
@auditor_auditor = Auditor::Recorder.new(action, self, options, &blk)
|
28
|
-
@auditor_auditor.audit_before unless auditor_disabled?
|
29
|
-
true
|
30
|
-
end
|
31
|
-
send "before_#{action}".to_sym, callback
|
32
|
-
end
|
33
|
-
|
34
|
-
callback = "auditor_after_#{action}"
|
35
|
-
define_method(callback) do
|
36
|
-
@auditor_auditor = Auditor::Recorder.new(action, self, options, &blk) if action.to_sym == :find
|
37
|
-
@auditor_auditor.audit_after unless auditor_disabled?
|
38
|
-
true
|
39
|
-
end
|
40
|
-
send "after_#{action}".to_sym, callback
|
41
|
-
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
end
|