ixtlan-audit 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ixtlan/audit/manager.rb +2 -1
- data/lib/ixtlan/audit/manager.rb~ +94 -35
- data/lib/ixtlan/audit/user_logger.rb +11 -6
- data/lib/ixtlan/audit/user_logger.rb~ +41 -12
- metadata +4 -4
data/lib/ixtlan/audit/manager.rb
CHANGED
@@ -1,12 +1,37 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
#
|
2
|
+
# Copyright (C) 2012 Christian Meier
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
5
|
+
# this software and associated documentation files (the "Software"), to deal in
|
6
|
+
# the Software without restriction, including without limitation the rights to
|
7
|
+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
8
|
+
# the Software, and to permit persons to whom the Software is furnished to do so,
|
9
|
+
# subject to the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be included in all
|
12
|
+
# copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
16
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
17
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
18
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
19
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
#
|
3
21
|
module Ixtlan
|
4
22
|
module Audit
|
5
23
|
class Manager
|
6
24
|
|
7
25
|
private
|
8
26
|
|
9
|
-
|
27
|
+
if defined? ::Slf4r
|
28
|
+
include ::Slf4r::Logger
|
29
|
+
else
|
30
|
+
require 'logger'
|
31
|
+
def logger
|
32
|
+
@logger ||= Logger.new( STDOUT )
|
33
|
+
end
|
34
|
+
end
|
10
35
|
|
11
36
|
def list
|
12
37
|
Thread.current[:audit] ||= []
|
@@ -14,56 +39,90 @@ module Ixtlan
|
|
14
39
|
|
15
40
|
public
|
16
41
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
@
|
42
|
+
attr_accessor :model, :block, :keep_logs
|
43
|
+
|
44
|
+
def initialize( model = nil, &block )
|
45
|
+
@model = model
|
46
|
+
@keep_logs = 90
|
47
|
+
block.call( self ) if block
|
48
|
+
@block = block
|
21
49
|
end
|
22
50
|
|
23
|
-
def
|
24
|
-
@
|
51
|
+
def model
|
52
|
+
@model ||= (Ixtlan::Audit::Audit rescue nil)
|
25
53
|
end
|
26
|
-
|
27
|
-
def
|
28
|
-
|
54
|
+
|
55
|
+
def keep_logs=( keep )
|
56
|
+
old = @keep_logs
|
57
|
+
@keep_logs = keep
|
58
|
+
daily_cleanup if old != @keep_logs
|
29
59
|
end
|
30
60
|
|
31
|
-
def
|
32
|
-
|
61
|
+
def keep_logs
|
62
|
+
if block
|
63
|
+
block.call( self )
|
64
|
+
end
|
65
|
+
@keep_logs
|
33
66
|
end
|
34
67
|
|
35
|
-
def push(
|
36
|
-
|
68
|
+
def push( username, path, obj )
|
69
|
+
if model
|
70
|
+
message =
|
71
|
+
if !obj.is_a?( String ) && obj.respond_to?( :collect )
|
72
|
+
if o = obj.first
|
73
|
+
"#{o.class}[ #{obj.size} ]"
|
74
|
+
else
|
75
|
+
"[ 0 ] - <EMPTY ARRAY>"
|
76
|
+
end
|
77
|
+
else
|
78
|
+
obj.to_s
|
79
|
+
end
|
80
|
+
list << model.new( :path => path,
|
81
|
+
:message => message,
|
82
|
+
:login => username || '???' )
|
83
|
+
end
|
84
|
+
list.last
|
37
85
|
end
|
38
86
|
|
39
87
|
def save_all
|
88
|
+
daily_cleanup
|
40
89
|
list.each do |audit|
|
41
|
-
|
90
|
+
begin
|
91
|
+
audit.save
|
92
|
+
rescue => e
|
93
|
+
warn "unexpected error - skip entry"
|
94
|
+
warn e.message
|
95
|
+
warn audit
|
96
|
+
end
|
42
97
|
end
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
def username_method
|
47
|
-
@username_method
|
98
|
+
list.clear
|
48
99
|
end
|
49
100
|
|
50
101
|
def daily_cleanup
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
@logger.info("cleaned audit logs")
|
61
|
-
rescue Error
|
62
|
-
# TODO log this !!
|
63
|
-
end
|
102
|
+
return unless model
|
103
|
+
now = DateTime.now
|
104
|
+
if(@last_cleanup.nil? || @last_cleanup < (now - 1))
|
105
|
+
@last_cleanup = now
|
106
|
+
begin
|
107
|
+
delete_all( now - keep_logs )
|
108
|
+
logger.info "cleaned audit logs"
|
109
|
+
rescue Exception => e
|
110
|
+
logger.warn "error cleaning up audit logs: #{e.message}"
|
64
111
|
end
|
65
112
|
end
|
66
113
|
end
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
if defined? ::DataMapper
|
118
|
+
def delete_all( expired )
|
119
|
+
model.all( :created_at.lte => expired ).destroy!
|
120
|
+
end
|
121
|
+
else # ActiveRecord
|
122
|
+
def delete_all( expired )
|
123
|
+
model.all( :conditions => ["created_at <= ?", expired] ).each(&:delete)
|
124
|
+
end
|
125
|
+
end
|
67
126
|
end
|
68
127
|
end
|
69
128
|
end
|
@@ -18,14 +18,19 @@
|
|
18
18
|
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
19
19
|
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
20
|
#
|
21
|
-
require 'slf4r/logger'
|
22
|
-
|
23
21
|
module Ixtlan
|
24
22
|
module Audit
|
25
23
|
class UserLogger
|
26
24
|
|
27
|
-
|
28
|
-
|
25
|
+
if defined? ::Slf4r
|
26
|
+
include ::Slf4r::Logger
|
27
|
+
else
|
28
|
+
require 'logger'
|
29
|
+
def logger
|
30
|
+
@logger ||= Logger.new( STDOUT )
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
29
34
|
def initialize(audit_manager)
|
30
35
|
@manager = audit_manager
|
31
36
|
end
|
@@ -80,9 +85,9 @@ module Ixtlan
|
|
80
85
|
def log_user(user, message = nil, &block)
|
81
86
|
user ||= "???"
|
82
87
|
msg = "#{message}#{block.call if block}"
|
83
|
-
@manager.push( user, msg.sub(/\ .*$/, ''), msg )
|
88
|
+
@manager.push( user, msg.sub(/\ .*$/, ''), msg.sub(/^[^\ ]*\ /, '') )
|
84
89
|
logger.debug {"[#{user}] #{msg}" }
|
85
90
|
end
|
86
91
|
end
|
87
92
|
end
|
88
|
-
end
|
93
|
+
end
|
@@ -1,3 +1,23 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2012 Christian Meier
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
5
|
+
# this software and associated documentation files (the "Software"), to deal in
|
6
|
+
# the Software without restriction, including without limitation the rights to
|
7
|
+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
8
|
+
# the Software, and to permit persons to whom the Software is furnished to do so,
|
9
|
+
# subject to the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be included in all
|
12
|
+
# copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
16
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
17
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
18
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
19
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
#
|
1
21
|
require 'slf4r/logger'
|
2
22
|
|
3
23
|
module Ixtlan
|
@@ -14,7 +34,7 @@ module Ixtlan
|
|
14
34
|
|
15
35
|
def login_from(controller)
|
16
36
|
user = controller.respond_to?(:current_user) ? controller.send(:current_user) : nil
|
17
|
-
user.nil? ? nil: user.
|
37
|
+
user.nil? ? nil: (user.respond_to?(:login) ? user.login : user.username)
|
18
38
|
end
|
19
39
|
|
20
40
|
public
|
@@ -25,21 +45,30 @@ module Ixtlan
|
|
25
45
|
|
26
46
|
def log_action(controller, message = nil)
|
27
47
|
log_user(login_from(controller)) do
|
28
|
-
as_xml = controller.response.content_type == 'application/xml' ? " - xml" : ""
|
29
48
|
if controller.params[:controller]
|
30
|
-
|
31
|
-
|
32
|
-
|
49
|
+
clname = controller.params[:controller]
|
50
|
+
cname = clname.sub(/^.*\//, '')
|
51
|
+
audits = controller.instance_variable_get("@#{cname}")
|
52
|
+
if(audits && audits.respond_to?(:collect))
|
53
|
+
"#{clname}##{controller.params[:action]} #{cname.classify}[#{audits.size}]#{message}"
|
33
54
|
else
|
34
|
-
audit = controller.instance_variable_get("@#{
|
55
|
+
audit = audits || controller.instance_variable_get("@#{cname.singularize}")
|
35
56
|
if(audit)
|
36
57
|
errors = if(audit.respond_to?(:errors) && !audit.errors.empty?)
|
37
58
|
" - errors: " + audit.errors.full_messages.join(", ")
|
38
59
|
end
|
39
|
-
audit_log = audit.respond_to?(:to_log)
|
40
|
-
|
60
|
+
audit_log = if audit.respond_to?(:to_log)
|
61
|
+
audit.to_log
|
62
|
+
elsif audit.is_a? String
|
63
|
+
audit
|
64
|
+
elsif audit.respond_to?(:model)
|
65
|
+
"#{audit.model}(#{audit.id})"
|
66
|
+
else
|
67
|
+
"#{audit.class.name}(#{audit.id})"
|
68
|
+
end
|
69
|
+
"#{clname}##{controller.params[:action]} #{audit_log}#{message}#{errors}"
|
41
70
|
else
|
42
|
-
"#{
|
71
|
+
"#{clname}##{controller.params[:action]}#{message}"
|
43
72
|
end
|
44
73
|
end
|
45
74
|
else
|
@@ -51,9 +80,9 @@ module Ixtlan
|
|
51
80
|
def log_user(user, message = nil, &block)
|
52
81
|
user ||= "???"
|
53
82
|
msg = "#{message}#{block.call if block}"
|
54
|
-
@manager.push( msg,
|
55
|
-
|
83
|
+
@manager.push( user, msg.sub(/\ .*$/, ''), msg )
|
84
|
+
logger.debug {"[#{user}] #{msg}" }
|
56
85
|
end
|
57
86
|
end
|
58
87
|
end
|
59
|
-
end
|
88
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ixtlan-audit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 3
|
10
|
+
version: 0.3.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Christian Meier
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2013-01-16 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: slf4r
|