request-auditor 0.0.1
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/README.rdoc +22 -0
- data/app/models/auditor/auditor_log.rb +6 -0
- data/config/routes.rb +12 -0
- data/lib/acts_as_widget/base.rb +36 -0
- data/lib/application_controller.rb +28 -0
- data/lib/application_helper.rb +7 -0
- data/lib/auditor.rb +32 -0
- data/lib/engine.rb +30 -0
- data/lib/rails/generators/auditor/auditor_generator.rb +50 -0
- data/lib/rails/generators/auditor/templates/initializer.rb +8 -0
- data/lib/rails/generators/auditor/templates/migration.rb +9 -0
- data/lib/rails/generators/auditor/templates/schema.rb +11 -0
- data/lib/rails/railties/tasks.rake +8 -0
- metadata +68 -0
data/README.rdoc
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
== Overview
|
2
|
+
|
3
|
+
Provides audit logging features for controllers. Audit logging is written to a database table with the request URI, requestor IP address, date/time stamp, and user ID (provided by the application).
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
First, install the gem:
|
8
|
+
|
9
|
+
* gem install auditor
|
10
|
+
|
11
|
+
Second, generate the model for recording audit log entries:
|
12
|
+
|
13
|
+
* rails generate auditor
|
14
|
+
|
15
|
+
Third, run the model migration:
|
16
|
+
|
17
|
+
* rake db:migrate
|
18
|
+
|
19
|
+
Fourth, add the following line to any controller you want audited (or to application_controller to have it apply globally):
|
20
|
+
|
21
|
+
* is_audited
|
22
|
+
|
data/config/routes.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Rails.application.routes.draw do
|
2
|
+
|
3
|
+
mount_at = Auditor::Engine.config.mount_at
|
4
|
+
|
5
|
+
match mount_at => 'auditor/widgets#index'
|
6
|
+
|
7
|
+
resources :widgets, :only => [ :index, :show ],
|
8
|
+
:controller => "auditor/widgets",
|
9
|
+
:path_prefix => mount_at,
|
10
|
+
:as => "auditor_"
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Auditor
|
2
|
+
module ActsAsWidget
|
3
|
+
|
4
|
+
## Define ModelMethods
|
5
|
+
module Base
|
6
|
+
def self.included(klass)
|
7
|
+
klass.class_eval do
|
8
|
+
extend Config
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module Config
|
13
|
+
def acts_as_widget
|
14
|
+
|
15
|
+
# This is where arbitrary code goes that you want to
|
16
|
+
# add to the class that declared "acts_as_widget"
|
17
|
+
|
18
|
+
has_many :widgets, :class_name => 'Auditor::Widget'
|
19
|
+
|
20
|
+
include Auditor::ActsAsWidget::Base::InstanceMethods
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module InstanceMethods
|
25
|
+
|
26
|
+
def factory_name
|
27
|
+
"this is an example instance method"
|
28
|
+
end
|
29
|
+
|
30
|
+
end # InstanceMethods
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
::ActiveRecord::Base.send :include, Auditor::ActsAsWidget::Base
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Auditor
|
2
|
+
## Define ControllerMethods
|
3
|
+
module Controller
|
4
|
+
## this one manages the usual self.included, klass_eval stuff
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
before_filter :test_controller_instance_method
|
9
|
+
end
|
10
|
+
|
11
|
+
module InstanceMethods
|
12
|
+
def test_controller_instance_method
|
13
|
+
puts "###### This text is coming from an application_controller before_filter that is being declared and triggered from inside the engine. This before_filter is automatically integrated in when the engine is installed into an app. Look inside lib/application_controller.rb to find it. ######"
|
14
|
+
end
|
15
|
+
|
16
|
+
# This method is available inside application_controller but it is not being
|
17
|
+
# automatically executed. Notice the before_filter line above that is automatically
|
18
|
+
# executing the first method.
|
19
|
+
def second_controller_instance_method
|
20
|
+
puts "###### This method is not automatically run inside application_controller, but it is available inside application_controller. To see this example add 'before_filter :second_controller_instance_method' at the top of your app's application_controller.rb ######"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
::ActionController::Base.send :include, Auditor::Controller
|
27
|
+
|
28
|
+
|
data/lib/auditor.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Auditor
|
2
|
+
require 'engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
|
3
|
+
require 'application_controller'
|
4
|
+
end
|
5
|
+
|
6
|
+
module ActionController
|
7
|
+
module Auditor
|
8
|
+
def self.included(base)
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def is_audited
|
14
|
+
include ActionController::Auditor::InstanceMethods
|
15
|
+
before_filter :audit_request
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
def audit_request
|
21
|
+
a = ::Auditor::AuditorLog.new
|
22
|
+
a.request_uri = request.url
|
23
|
+
a.request_parameters = request.filtered_parameters.inspect
|
24
|
+
a.remote_address = request.remote_ip
|
25
|
+
a.user_id = ::Auditor::Engine::config.user_id.call(request)
|
26
|
+
a.save!
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
ActionController::Base.send(:include, ActionController::Auditor)
|
data/lib/engine.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'auditor'
|
2
|
+
require 'rails'
|
3
|
+
require 'action_controller'
|
4
|
+
require 'application_helper'
|
5
|
+
|
6
|
+
module Auditor
|
7
|
+
class Engine < Rails::Engine
|
8
|
+
|
9
|
+
# Config defaults
|
10
|
+
config.widget_factory_name = "default factory name"
|
11
|
+
config.mount_at = '/'
|
12
|
+
|
13
|
+
# Load rake tasks
|
14
|
+
rake_tasks do
|
15
|
+
load File.join(File.dirname(__FILE__), 'rails/railties/tasks.rake')
|
16
|
+
end
|
17
|
+
|
18
|
+
# Check the gem config
|
19
|
+
initializer "check config" do |app|
|
20
|
+
|
21
|
+
# make sure mount_at ends with trailing slash
|
22
|
+
config.mount_at += '/' unless config.mount_at.last == '/'
|
23
|
+
end
|
24
|
+
|
25
|
+
initializer "static assets" do |app|
|
26
|
+
app.middleware.use ::ActionDispatch::Static, "#{root}/public"
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
class AuditorGenerator < Rails::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
|
7
|
+
def self.source_root
|
8
|
+
File.join(File.dirname(__FILE__), 'templates')
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.next_migration_number(dirname) #:nodoc:
|
12
|
+
if ActiveRecord::Base.timestamped_migrations
|
13
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
14
|
+
else
|
15
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
# Every method that is declared below will be automatically executed when the generator is run
|
21
|
+
|
22
|
+
def create_migration_file
|
23
|
+
f = File.open File.join(File.dirname(__FILE__), 'templates', 'schema.rb')
|
24
|
+
schema = f.read; f.close
|
25
|
+
|
26
|
+
schema.gsub!(/ActiveRecord::Schema.*\n/, '')
|
27
|
+
schema.gsub!(/^end\n*$/, '')
|
28
|
+
|
29
|
+
f = File.open File.join(File.dirname(__FILE__), 'templates', 'migration.rb')
|
30
|
+
migration = f.read; f.close
|
31
|
+
migration.gsub!(/SCHEMA_AUTO_INSERTED_HERE/, schema)
|
32
|
+
|
33
|
+
tmp = File.open "tmp/~migration_ready.rb", "w"
|
34
|
+
tmp.write migration
|
35
|
+
tmp.close
|
36
|
+
|
37
|
+
migration_template '../../../tmp/~migration_ready.rb',
|
38
|
+
'db/migrate/create_auditor_tables.rb'
|
39
|
+
remove_file 'tmp/~migration_ready.rb'
|
40
|
+
end
|
41
|
+
|
42
|
+
def copy_initializer_file
|
43
|
+
copy_file 'initializer.rb', 'config/initializers/auditor.rb'
|
44
|
+
end
|
45
|
+
|
46
|
+
def update_application_template
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: request-auditor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Hart
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-07-10 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description:
|
18
|
+
email: hartct@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- app/models/auditor/auditor_log.rb
|
27
|
+
- config/routes.rb
|
28
|
+
- lib/acts_as_widget/base.rb
|
29
|
+
- lib/application_controller.rb
|
30
|
+
- lib/application_helper.rb
|
31
|
+
- lib/auditor.rb
|
32
|
+
- lib/engine.rb
|
33
|
+
- lib/rails/generators/auditor/auditor_generator.rb
|
34
|
+
- lib/rails/generators/auditor/templates/initializer.rb
|
35
|
+
- lib/rails/generators/auditor/templates/migration.rb
|
36
|
+
- lib/rails/generators/auditor/templates/schema.rb
|
37
|
+
- lib/rails/railties/tasks.rake
|
38
|
+
- README.rdoc
|
39
|
+
has_rdoc: true
|
40
|
+
homepage:
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.5.0
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Provides audit logging to a database of controller requests.
|
67
|
+
test_files: []
|
68
|
+
|