active_record_auditor 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +32 -0
- data/LICENSE.txt +21 -0
- data/Rakefile +1 -0
- data/active_record_auditor.gemspec +22 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/active_record_auditor/extensions/active_record.rb +15 -0
- data/lib/active_record_auditor/extensions/active_record_auditor_persistence.rb +133 -0
- data/lib/active_record_auditor/extensions/active_record_auditor_relation.rb +22 -0
- data/lib/active_record_auditor/extensions/array.rb +16 -0
- data/lib/active_record_auditor/tasks/setup.rake +13 -0
- data/lib/active_record_auditor/version.rb +3 -0
- data/lib/active_record_auditor.rb +27 -0
- data/spec/active_record_auditor_spec.rb +11 -0
- data/spec/spec_helper.rb +2 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 360ef6227ae90c2c3a7c946a8a2771e9b0e74bbf
|
4
|
+
data.tar.gz: c4be644dd730b8a06f03a0daa8cf591d71409014
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b80818323f391d152c5e6c211320703bc1145797744cf6d21751c9cd84778dcd6867cd7556943e7453b024f83426ca93f6942394b3c0d2fbc66334c27f8b9b0
|
7
|
+
data.tar.gz: 964dfa183be96df422f0c71ea3da3016d21ec5b4d4ebf0b312d41e6eb8d756f27e9b0a05851218a19eb4e7d1e6ebd563c0efa0c3112463cba1d74aacd75b6cae
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (4.2.0)
|
5
|
+
activesupport (= 4.2.0)
|
6
|
+
builder (~> 3.1)
|
7
|
+
activerecord (4.2.0)
|
8
|
+
activemodel (= 4.2.0)
|
9
|
+
activesupport (= 4.2.0)
|
10
|
+
arel (~> 6.0)
|
11
|
+
activesupport (4.2.0)
|
12
|
+
i18n (~> 0.7)
|
13
|
+
json (~> 1.7, >= 1.7.7)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
16
|
+
tzinfo (~> 1.1)
|
17
|
+
arel (6.0.0)
|
18
|
+
builder (3.2.2)
|
19
|
+
i18n (0.7.0)
|
20
|
+
json (1.8.2)
|
21
|
+
minitest (5.5.1)
|
22
|
+
rake (10.4.2)
|
23
|
+
thread_safe (0.3.4)
|
24
|
+
tzinfo (1.2.2)
|
25
|
+
thread_safe (~> 0.1)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
activerecord
|
32
|
+
rake
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Nic Wilson
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_record_auditor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_record_auditor"
|
8
|
+
spec.version = ActiveRecordAuditor::VERSION
|
9
|
+
spec.authors = ["Nic Wilson"]
|
10
|
+
spec.email = ["wilsonic89@yahoo.com"]
|
11
|
+
spec.required_ruby_version = '>= 2.0.0'
|
12
|
+
|
13
|
+
spec.summary = "Framework for auditing user actions in ActiveRecord"
|
14
|
+
spec.description = "I should really do this at some point"
|
15
|
+
spec.homepage = "https://github.com/nbwilson/active_record_auditor"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
22
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_record_auditor"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "active_record_auditor/extensions/active_record_auditor_persistence"
|
2
|
+
require "active_record_auditor/extensions/active_record_auditor_relation"
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module Persistence
|
6
|
+
prepend ActiveRecordAuditorPersistence
|
7
|
+
end
|
8
|
+
|
9
|
+
class Relation
|
10
|
+
prepend ActiveRecordAuditorRelation
|
11
|
+
end
|
12
|
+
|
13
|
+
class NoAuditUser < ActiveRecordError
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_record_auditor/extensions/array'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
|
6
|
+
module ActiveRecordAuditorPersistence
|
7
|
+
|
8
|
+
extend ActiveRecord::Persistence
|
9
|
+
|
10
|
+
def _update_relation_record
|
11
|
+
connection.execute(audit_update_sql) if should_audit?
|
12
|
+
end
|
13
|
+
|
14
|
+
def _delete_relation_record
|
15
|
+
connection.execute(audit_delete_sql) if should_audit?
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def _create_record(attribute_names = self.attribute_names)
|
21
|
+
transaction do
|
22
|
+
super(attribute_names)
|
23
|
+
connection.execute(new_record_sql) if should_audit?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def _update_record(attribute_names = self.attribute_names)
|
28
|
+
transaction do
|
29
|
+
connection.execute(audit_update_sql) if should_audit?
|
30
|
+
super(attribute_names)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def existing_row
|
35
|
+
connection.execute(existing_row_sql).first
|
36
|
+
end
|
37
|
+
|
38
|
+
def audit_create_sql
|
39
|
+
"INSERT INTO `#{ensure_audit_table}` (#{required_audit_attribute_names.to_sql_columns}) VALUES (#{required_audit_attributes.to_sql_values})"
|
40
|
+
end
|
41
|
+
|
42
|
+
def audit_update_sql
|
43
|
+
"INSERT INTO `#{ensure_audit_table}` (#{attribute_names.drop(1).to_sql_columns}, #{required_audit_attribute_names.to_sql_columns}) VALUES (#{existing_row.drop(1).to_sql_values}, #{required_audit_attributes.to_sql_values})"
|
44
|
+
end
|
45
|
+
|
46
|
+
def audit_delete_sql
|
47
|
+
"INSERT INTO `#{ensure_audit_table}` (#{attribute_names.drop(1).to_sql_columns}, #{required_audit_attribute_names.push('__deleted').to_sql_columns}) VALUES (#{existing_row.drop(1).to_sql_values}, #{required_audit_attributes.push(1).to_sql_values})"
|
48
|
+
end
|
49
|
+
|
50
|
+
def existing_row_sql
|
51
|
+
"SELECT * FROM `#{model.table_name}` where id = #{id} limit 1"
|
52
|
+
end
|
53
|
+
|
54
|
+
def version_lookup_sql(audit_date = datetime)
|
55
|
+
"SELECT * FROM #{audit_table_name(audit_date)} where __canonical_id=#{id} ORDER BY __created_at DESC"
|
56
|
+
end
|
57
|
+
|
58
|
+
def required_audit_attribute_names
|
59
|
+
['__username', '__canonical_id', '__version', '__created_at']
|
60
|
+
end
|
61
|
+
|
62
|
+
def required_audit_attributes
|
63
|
+
[__username, "#{id}", "#{next_version}", "#{datetime.to_s}"]
|
64
|
+
end
|
65
|
+
|
66
|
+
def next_version
|
67
|
+
latest_version + 1
|
68
|
+
end
|
69
|
+
|
70
|
+
def latest_version
|
71
|
+
ensure_audit_table
|
72
|
+
latest_update = connection.execute(version_lookup_sql).first
|
73
|
+
latest_revision = latest_update[latest_update.length-2] if latest_update
|
74
|
+
table_date = datetime - 1.month
|
75
|
+
misses = 0
|
76
|
+
while latest_revision.nil? && misses < 12
|
77
|
+
table_date = table_date - 1.month
|
78
|
+
if audit_table_exists?(table_date)
|
79
|
+
latest_update = connection.execute(version_lookup_sql(table_date)).first
|
80
|
+
latest_revision = latest_update[latest_update.length-2] if latest_update
|
81
|
+
misses = 0
|
82
|
+
else
|
83
|
+
misses += 1
|
84
|
+
end
|
85
|
+
end
|
86
|
+
latest_revision.nil? ? -1 : latest_revision
|
87
|
+
end
|
88
|
+
|
89
|
+
def audit_table_name(audit_date = datetime)
|
90
|
+
"#{model.table_name}_#{audit_date.month}_#{audit_date.year}"
|
91
|
+
end
|
92
|
+
|
93
|
+
def ensure_audit_table
|
94
|
+
unless audit_table_exists?
|
95
|
+
ActiveRecordAuditor.build_audit_table(model.table_name)
|
96
|
+
end
|
97
|
+
audit_table_name
|
98
|
+
end
|
99
|
+
|
100
|
+
def audit_table_exists?(audit_date = datetime)
|
101
|
+
connection.table_exists? audit_table_name(audit_date)
|
102
|
+
end
|
103
|
+
|
104
|
+
def datetime
|
105
|
+
Time.now
|
106
|
+
end
|
107
|
+
|
108
|
+
def should_audit?
|
109
|
+
raise NoAuditUser, "Writes are currently disabled without a specified user" if __username.nil? && __user_required
|
110
|
+
__username || __log_userless_actions
|
111
|
+
end
|
112
|
+
|
113
|
+
def __username
|
114
|
+
$__username
|
115
|
+
end
|
116
|
+
|
117
|
+
def __log_userless_actions
|
118
|
+
model.try(:log_userless_actions) || model.try(:__log_userless_actions)
|
119
|
+
end
|
120
|
+
|
121
|
+
def __user_required
|
122
|
+
model.try(:user_required) || model.try(:__user_required)
|
123
|
+
end
|
124
|
+
|
125
|
+
def model
|
126
|
+
self.class
|
127
|
+
end
|
128
|
+
|
129
|
+
def connection
|
130
|
+
model.connection
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ActiveRecordAuditorRelation
|
3
|
+
|
4
|
+
def delete_all(conditions = nil)
|
5
|
+
if conditions
|
6
|
+
super
|
7
|
+
else
|
8
|
+
transaction do
|
9
|
+
to_a.each {|object| object._delete_relation_record}
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def update_all(updates)
|
16
|
+
transaction do
|
17
|
+
to_a.each {|object| object._update_relation_record}
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
#Can't wrap these requests in a ActiveRecord Transaction, because DDL commands can not be rolled back per MySQL documentation
|
4
|
+
namespace :db do
|
5
|
+
namespace :audit do
|
6
|
+
task :create_audit_tables do
|
7
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
8
|
+
next if table == "schema_migrations"
|
9
|
+
ActiveRecordAuditor.build_audit_table(table)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "active_record_auditor/version"
|
2
|
+
require "active_record_auditor/extensions/active_record"
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
module ActiveRecordAuditor
|
6
|
+
def self.build_audit_table(table)
|
7
|
+
time = Time.now
|
8
|
+
new_table_name = "#{table}_#{time.month}_#{time.year}"
|
9
|
+
ActiveRecord::Base.connection.execute("CREATE TABLE #{new_table_name} like #{table}")
|
10
|
+
ActiveRecord::Base.connection.execute("SHOW INDEX FROM #{new_table_name}").to_a.uniq{|index| index[2]}.each do |index|
|
11
|
+
ActiveRecord::Base.connection.execute("ALTER TABLE #{new_table_name} DROP INDEX #{index[2]};") unless index[2] == 'PRIMARY'
|
12
|
+
end
|
13
|
+
ActiveRecord::Base.connection.execute("DESCRIBE #{new_table_name}").to_a.uniq{|index| index[0]}.each do |col|
|
14
|
+
ActiveRecord::Base.connection.execute("ALTER TABLE #{new_table_name} MODIFY COLUMN #{col[0]} #{col[1]};") if col[2] == "YES"
|
15
|
+
end
|
16
|
+
ActiveRecord::Migration.add_column new_table_name.to_sym, :__username, :string
|
17
|
+
ActiveRecord::Migration.add_column new_table_name.to_sym, :__deleted, :boolean, default: false
|
18
|
+
ActiveRecord::Migration.add_column new_table_name.to_sym, :__canonical_id, :integer, null: false
|
19
|
+
ActiveRecord::Migration.add_column new_table_name.to_sym, :__version, :integer, null: false
|
20
|
+
ActiveRecord::Migration.add_column new_table_name.to_sym, :__created_at, :datetime, null: false
|
21
|
+
ActiveRecord::Migration.add_index new_table_name.to_sym, [:__canonical_id, :__version], unique: true
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.load_tasks
|
25
|
+
load "active_record_auditor/tasks/setup.rake"
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_auditor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nic Wilson
|
@@ -31,7 +31,24 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- CODE_OF_CONDUCT.md
|
35
|
+
- Gemfile
|
36
|
+
- Gemfile.lock
|
37
|
+
- LICENSE.txt
|
34
38
|
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- active_record_auditor.gemspec
|
41
|
+
- bin/console
|
42
|
+
- bin/setup
|
43
|
+
- lib/active_record_auditor.rb
|
44
|
+
- lib/active_record_auditor/extensions/active_record.rb
|
45
|
+
- lib/active_record_auditor/extensions/active_record_auditor_persistence.rb
|
46
|
+
- lib/active_record_auditor/extensions/active_record_auditor_relation.rb
|
47
|
+
- lib/active_record_auditor/extensions/array.rb
|
48
|
+
- lib/active_record_auditor/tasks/setup.rake
|
49
|
+
- lib/active_record_auditor/version.rb
|
50
|
+
- spec/active_record_auditor_spec.rb
|
51
|
+
- spec/spec_helper.rb
|
35
52
|
homepage: https://github.com/nbwilson/active_record_auditor
|
36
53
|
licenses:
|
37
54
|
- MIT
|
@@ -44,7 +61,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
61
|
requirements:
|
45
62
|
- - ">="
|
46
63
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
64
|
+
version: 2.0.0
|
48
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
66
|
requirements:
|
50
67
|
- - ">="
|