audited_controller 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in audited_controller.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Siwei
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # AuditedController
2
+
3
+ This gem is extracted from the real working code. the requirements is also from the real requirements:
4
+
5
+ 1. the users want to track the user's actions.
6
+ 2. should log into database.
7
+ 3. there're not so many requests in a second.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'audited_controller'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install audited_controller
22
+
23
+ Then create a file : config/audits.yml, and (TODO: make this a task )
24
+
25
+ ## Usage
26
+
27
+ 1. assuming you want to track the actions of your 'users_controller'. add 'before_filter :add_to_audit' to the target controller.
28
+
29
+ class UsersController < ApplicationController
30
+ before_filter :add_to_audit
31
+ def index
32
+ # ...
33
+ end
34
+ def create
35
+ # ...
36
+ end
37
+ end
38
+
39
+
40
+ 2. make sure the actions appear in the config/audits.yml file
41
+
42
+ # this line determines if the get request should be audited.
43
+ audit_get_request: false
44
+
45
+ # the lines below is used for i18n translations
46
+ # format:
47
+ # <controller>
48
+ # <action>: <comments>
49
+ users:
50
+ index: open the index page of messages
51
+ create: create an message
52
+
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class ApplicationController < ActionController::Base
3
+
4
+ helper_method :add_to_audit
5
+ def add_to_audit
6
+ audit_config = HashWithIndifferentAccess.new(YAML.load(File.read(
7
+ File.expand_path("#{Rails.root}/config/audits.yml", __FILE__))))
8
+ controller = params[:controller]
9
+ action = params[:action]
10
+ request_type = restful_method(params)
11
+ return if !audit_get_request?(audit_config) && request_type == 'get'
12
+ Audit.create!(action: action, controller: controller, user_name: current_user.login,
13
+ description: audit_config[controller][action],
14
+ :params => params.inspect,
15
+ remote_ip: request.remote_ip, restful_method: restful_method(params) )
16
+ end
17
+ private
18
+ def audit_get_request?(audit_config)
19
+ audit_config["audit_get_request"]
20
+ end
21
+ # return: get, post, put or delete
22
+ def restful_method(params)
23
+ params[:authenticity_token].blank? ? 'get' : ((params[:_method]) || 'post')
24
+ end
25
+
26
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'audited_controller/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "audited_controller"
8
+ gem.version = AuditedController::VERSION
9
+ gem.authors = ["Siwei Shen"]
10
+ gem.email = ["sg552sg552@gmail.com"]
11
+ gem.description = %q{a rubygem to audit the actions in controllers of Rails}
12
+ gem.summary = %q{a rubygem to audit the actions in controllers, such as 'update', 'create'. very lightweight and simple.
13
+ basically it uses a 'before_filter' to achieve this. }
14
+ gem.homepage = ""
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ # it need rails and related gems.
22
+ end
@@ -0,0 +1,3 @@
1
+ module AuditedController
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "audited_controller/version"
2
+
3
+ module AuditedController
4
+ # Your code goes here...
5
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: audited_controller
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Siwei Shen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-30 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: a rubygem to audit the actions in controllers of Rails
15
+ email:
16
+ - sg552sg552@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - app/controllers/application_controller.rb
27
+ - audited_controller.gemspec
28
+ - lib/audited_controller.rb
29
+ - lib/audited_controller/version.rb
30
+ homepage: ''
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.24
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: a rubygem to audit the actions in controllers, such as 'update', 'create'.
54
+ very lightweight and simple. basically it uses a 'before_filter' to achieve this.
55
+ test_files: []
56
+ has_rdoc: