cia 0.5.10 → 0.6.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.
- checksums.yaml +4 -4
- data/Readme.md +104 -0
- data/lib/cia.rb +5 -5
- data/lib/cia/version.rb +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 721b3ddd4c0d65f03c32681de80e18d7dc2709c9
|
4
|
+
data.tar.gz: 634c7289d85d4f7a8768e97abaec2a3acade6175
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3757ab4bf222f509a11d10a084985eb7beffc34681a5076f26ad9471220bdf1f4283ccb6b999f2ba9a834ae09441b81f91bd1d7a59aacfd56be3ef9a72b4ca5e
|
7
|
+
data.tar.gz: 95b73f3e943191df350c9765716332638aa0cdd2975ec31465014f2bd1ba3a6007bcd7b2625b2620a9fb721d8be5650d6a3ecc5a3812b1e463b4c8df08f26f95
|
data/Readme.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
Central Internal Auditing
|
2
|
+
============================
|
3
|
+
|
4
|
+
Audit model actions like update/create/destroy/<custom> + attribute changes.
|
5
|
+
|
6
|
+
- normalized and queryable through table layout
|
7
|
+
- actors and subjects are polymorphic
|
8
|
+
- works on ActiveRecord 3.2, 4.0 and 4.1
|
9
|
+
|
10
|
+
Table layout:
|
11
|
+
|
12
|
+
Event (actor/ip/time/updated subject + message)
|
13
|
+
-> has many attribute changes (changed password from foo to bar on subject)
|
14
|
+
|
15
|
+
|
16
|
+
Install
|
17
|
+
=======
|
18
|
+
|
19
|
+
```Bash
|
20
|
+
gem install cia
|
21
|
+
```
|
22
|
+
|
23
|
+
`rails g migration add_cia` + paste [Migration](https://raw.github.com/grosser/cia/master/MIGRATION.rb)
|
24
|
+
|
25
|
+
|
26
|
+
Usage
|
27
|
+
=====
|
28
|
+
|
29
|
+
```Ruby
|
30
|
+
class User < ActiveRecord::Base
|
31
|
+
include CIA::Auditable
|
32
|
+
audited_attributes :email, :crypted_password
|
33
|
+
end
|
34
|
+
|
35
|
+
class ApplicationController < ActionController::Base
|
36
|
+
around_action :scope_auditing
|
37
|
+
|
38
|
+
def scope_auditing(&block)
|
39
|
+
CIA.audit actor: current_user, ip_address: request.remote_ip, &block
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# quick access
|
44
|
+
User.last.cia_events
|
45
|
+
changes = User.last.cia_attribute_changes
|
46
|
+
last_passwords = changes.where(attribute_name: "crypted_password").map(&:new_value)
|
47
|
+
|
48
|
+
# exceptions (raised by default)
|
49
|
+
CIA.exception_handler = -> (e) { raise e unless Rails.env.production? }
|
50
|
+
|
51
|
+
# conditional auditing
|
52
|
+
class User < ActiveRecord::Base
|
53
|
+
audited_attributes :email, if: :interesting?
|
54
|
+
|
55
|
+
def interesting?
|
56
|
+
...
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# adding an actor e.g. for user creation
|
61
|
+
CIA.current_actor = @user
|
62
|
+
|
63
|
+
# custom changes
|
64
|
+
class User < ActiveRecord::Base
|
65
|
+
def cia_changes
|
66
|
+
changes.merge("this" => ["always", "changes"])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# using after_commit, useful if the CIA::Event is stored in a different database then the audited class
|
71
|
+
class User < ActiveRecord::Base
|
72
|
+
include CIA::Auditable
|
73
|
+
audited_attributes :email, :crypted_password, callback: :after_commit
|
74
|
+
end
|
75
|
+
|
76
|
+
# passing arbitrary attributes into the .audit method
|
77
|
+
CIA.non_recordable_attributes = [:my_pretty_audit_property]
|
78
|
+
CIA.audit(actor: current_user, my_pretty_audit_property: "12345") do
|
79
|
+
...
|
80
|
+
end
|
81
|
+
|
82
|
+
# storing complex objects in old/new and reducing it's size if it's to big (serialized via json)
|
83
|
+
value = CIA::AttributeChange.serialize_for_storage(["some", "complex"*1000, "object"]){|too_big| too_big.delete_at(1); too_big }
|
84
|
+
CIA::AttributeChange.create!(old_value: value)
|
85
|
+
|
86
|
+
# add something to current transaction or start a new audit
|
87
|
+
CIA.audit bar: :baz, foo: :bang do
|
88
|
+
CIA.amend_audit foo: :bar do
|
89
|
+
puts CIA.current_transaction
|
90
|
+
end
|
91
|
+
end
|
92
|
+
-> {foo: :bar, bar: :baz}
|
93
|
+
```
|
94
|
+
|
95
|
+
|
96
|
+
# TODO
|
97
|
+
- reuse AR3+ previous_changes in a nice way
|
98
|
+
|
99
|
+
Author
|
100
|
+
======
|
101
|
+
[Michael Grosser](http://grosser.it)<br/>
|
102
|
+
michael@grosser.it<br/>
|
103
|
+
License: MIT<br/>
|
104
|
+
[](https://travis-ci.org/grosser/cia)
|
data/lib/cia.rb
CHANGED
@@ -55,9 +55,9 @@ module CIA
|
|
55
55
|
return if not message and changes.empty? and action.to_s == "update"
|
56
56
|
|
57
57
|
transaction_attributes = current_transaction.dup
|
58
|
-
transaction_attributes.reject! { |k,
|
58
|
+
transaction_attributes.reject! { |k, _v| non_recordable_attributes.include?(k) } if non_recordable_attributes
|
59
59
|
|
60
|
-
event = CIA::Event.new(transaction_attributes.
|
60
|
+
event = CIA::Event.new(transaction_attributes.reverse_merge(
|
61
61
|
action: action.to_s,
|
62
62
|
source: source,
|
63
63
|
message: message
|
@@ -65,11 +65,11 @@ module CIA
|
|
65
65
|
event.add_attribute_changes(changes)
|
66
66
|
event.save!
|
67
67
|
event
|
68
|
-
rescue
|
68
|
+
rescue StandardError
|
69
69
|
if exception_handler
|
70
|
-
exception_handler.call
|
70
|
+
exception_handler.call $!
|
71
71
|
else
|
72
|
-
raise
|
72
|
+
raise
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
data/lib/cia/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Grosser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -56,16 +56,16 @@ dependencies:
|
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '3.4'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '3.4'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: wwtd
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,6 +114,7 @@ executables: []
|
|
114
114
|
extensions: []
|
115
115
|
extra_rdoc_files: []
|
116
116
|
files:
|
117
|
+
- Readme.md
|
117
118
|
- lib/cia.rb
|
118
119
|
- lib/cia/attribute_change.rb
|
119
120
|
- lib/cia/auditable.rb
|