audit-log 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +68 -4
- data/Rakefile +16 -16
- data/app/controllers/audit_log/logs_controller.rb +7 -7
- data/config/routes.rb +1 -1
- data/db/migrate/20190527035005_create_audit_logs.rb +12 -12
- data/lib/audit-log.rb +11 -10
- data/lib/audit-log/controller_helper.rb +1 -1
- data/lib/audit-log/engine.rb +2 -2
- data/lib/audit-log/model.rb +3 -3
- data/lib/audit-log/version.rb +1 -1
- data/lib/generators/audit_log/install_generator.rb +4 -4
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0f1ddb5d2e2f1d9b84d0e83a02a83990cce0cc0a1c7fd931d84a607af1f4837
|
4
|
+
data.tar.gz: 8b072cf51106505f2100aa4dd7760cfef429b131d79b9d3c78e5fe738fcd7769
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 619bdc22b753f80b8b221c4a47531d03f17eaffc19a5f36d3ae3fab4a03562e082eae95335f94dc717de6f5b4cada0a512936c0f62353d57106ff7065081272d
|
7
|
+
data.tar.gz: f79ea23624ed6ccce2b7e51c8cdfdbbc8cc8229f9f0b508dfa349a6d637215db1c8d79404a6154daf3a3a055422250a990e4757288317aa62734a479eebf8975
|
data/README.md
CHANGED
@@ -2,12 +2,25 @@
|
|
2
2
|
|
3
3
|
Trail audit logs (Operation logs) into the database for user behaviors, including a web UI to query logs.
|
4
4
|
|
5
|
+
[![Build Status](https://travis-ci.org/rails-engine/audit-log.svg?branch=master)](https://travis-ci.org/rails-engine/audit-log)
|
6
|
+
|
7
|
+
## Demo UI
|
8
|
+
|
9
|
+
Audit log list:
|
10
|
+
|
11
|
+
<img width="870" src="https://user-images.githubusercontent.com/5518/58676735-e9570d80-838b-11e9-8ac0-6c5145b7fbb0.png">
|
12
|
+
|
13
|
+
Detail page:
|
14
|
+
|
15
|
+
<img width="870" src="https://user-images.githubusercontent.com/5518/58676737-e9570d80-838b-11e9-9292-63389b2d54cb.png">
|
16
|
+
|
17
|
+
|
5
18
|
## Installation
|
6
19
|
|
7
20
|
Add this line to your application's Gemfile:
|
8
21
|
|
9
22
|
```ruby
|
10
|
-
gem
|
23
|
+
gem "audit-log"
|
11
24
|
```
|
12
25
|
|
13
26
|
And then execute:
|
@@ -15,6 +28,12 @@ And then execute:
|
|
15
28
|
$ bundle
|
16
29
|
```
|
17
30
|
|
31
|
+
Generate files:
|
32
|
+
|
33
|
+
```bash
|
34
|
+
$ rails g audit_log:install
|
35
|
+
```
|
36
|
+
|
18
37
|
## Usage
|
19
38
|
|
20
39
|
Use in controllers:
|
@@ -22,15 +41,34 @@ Use in controllers:
|
|
22
41
|
```rb
|
23
42
|
class TicktsController < ApplicationController
|
24
43
|
def index
|
25
|
-
audit! :
|
44
|
+
audit! :list_ticket, nil
|
45
|
+
end
|
46
|
+
|
47
|
+
def create
|
48
|
+
if @ticket.save
|
49
|
+
audit! :create_ticket, @ticket, payload: ticket_params
|
50
|
+
else
|
51
|
+
render :new
|
52
|
+
end
|
26
53
|
end
|
27
54
|
|
28
55
|
def update
|
29
|
-
|
56
|
+
if @ticket.save
|
57
|
+
audit! :update_ticket, @ticket, payload: ticket_params
|
58
|
+
else
|
59
|
+
render :edit
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def approve
|
64
|
+
if @ticket.approve
|
65
|
+
audit! :approve_ticket, @ticket, payload: ticket_params
|
66
|
+
end
|
30
67
|
end
|
31
68
|
|
32
69
|
def destroy
|
33
|
-
|
70
|
+
# store original attributes for destroy for keep values
|
71
|
+
audit! :delete_ticket, nil, @ticket.attributes
|
34
72
|
end
|
35
73
|
|
36
74
|
private
|
@@ -49,6 +87,32 @@ AuditLog.audit!(:sign_in, @user, payload: { ip: request.ip })
|
|
49
87
|
AuditLog.audit!(:create_address, nil, payload: params)
|
50
88
|
```
|
51
89
|
|
90
|
+
Change `config/routes.rb` to add Route:
|
91
|
+
|
92
|
+
```rb
|
93
|
+
Rails.application.routes.draw do
|
94
|
+
authenticate :user, -> (u) { u.admin? } do
|
95
|
+
mount AuditLog::Engine => "/audit-log"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
I18n for audit names, you need create a `config/locales/audit_log.zh-CN.yml`:
|
101
|
+
|
102
|
+
```yml
|
103
|
+
zh-CN:
|
104
|
+
audit_log:
|
105
|
+
action:
|
106
|
+
sign_in: 登录
|
107
|
+
update_password: 修改密码
|
108
|
+
create_address: 添加住址
|
109
|
+
list_ticket: 查看工单列表
|
110
|
+
create_ticket: 创建工单
|
111
|
+
update_ticket: 更新工单
|
112
|
+
delete_ticket: 删除工单
|
113
|
+
approve_ticket: 审批工单
|
114
|
+
```
|
115
|
+
|
52
116
|
## License
|
53
117
|
|
54
118
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -1,34 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
begin
|
4
|
-
require
|
4
|
+
require 'bundler/setup'
|
5
5
|
rescue LoadError
|
6
|
-
puts
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
7
|
end
|
8
8
|
|
9
|
-
require
|
9
|
+
require 'rdoc/task'
|
10
10
|
|
11
11
|
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
-
rdoc.rdoc_dir =
|
13
|
-
rdoc.title =
|
14
|
-
rdoc.options <<
|
15
|
-
rdoc.rdoc_files.include(
|
16
|
-
rdoc.rdoc_files.include(
|
12
|
+
rdoc.rdoc_dir = 'rdoc'
|
13
|
+
rdoc.title = 'AuditLog'
|
14
|
+
rdoc.options << '--line-numbers'
|
15
|
+
rdoc.rdoc_files.include('README.md')
|
16
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
17
17
|
end
|
18
18
|
|
19
|
-
APP_RAKEFILE = File.expand_path(
|
20
|
-
load
|
19
|
+
APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
|
20
|
+
load 'rails/tasks/engine.rake'
|
21
21
|
|
22
|
-
load
|
22
|
+
load 'rails/tasks/statistics.rake'
|
23
23
|
|
24
|
-
require
|
24
|
+
require 'bundler/gem_tasks'
|
25
25
|
|
26
|
-
require
|
26
|
+
require 'rake/testtask'
|
27
27
|
|
28
28
|
Rake::TestTask.new(:test) do |t|
|
29
|
-
t.libs <<
|
30
|
-
t.libs <<
|
31
|
-
t.pattern =
|
29
|
+
t.libs << 'lib'
|
30
|
+
t.libs << 'test'
|
31
|
+
t.pattern = 'test/**/*_test.rb'
|
32
32
|
t.verbose = false
|
33
33
|
end
|
34
34
|
|
@@ -2,14 +2,14 @@
|
|
2
2
|
|
3
3
|
module AuditLog
|
4
4
|
class LogsController < ActionController::Base
|
5
|
-
layout
|
5
|
+
layout 'audit-log/application'
|
6
6
|
before_action :set_log, only: %i[show destroy]
|
7
7
|
|
8
8
|
def index
|
9
|
-
@logs = Log.order(
|
9
|
+
@logs = Log.order('id desc').includes(:user)
|
10
10
|
|
11
11
|
if params[:q]
|
12
|
-
@logs = @logs.where(
|
12
|
+
@logs = @logs.where('action like ?', "%#{params[:q]}%")
|
13
13
|
end
|
14
14
|
|
15
15
|
@logs = @logs.page(params[:page]).per(15)
|
@@ -19,9 +19,9 @@ module AuditLog
|
|
19
19
|
|
20
20
|
private
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
# Use callbacks to share common setup or constraints between actions.
|
23
|
+
def set_log
|
24
|
+
@log = Log.find(params[:id])
|
25
|
+
end
|
26
26
|
end
|
27
27
|
end
|
data/config/routes.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
class CreateAuditLogs < ActiveRecord::Migration[5.0]
|
2
2
|
def change
|
3
|
-
create_table
|
4
|
-
t.string
|
5
|
-
t.integer
|
6
|
-
t.integer
|
7
|
-
t.string
|
8
|
-
t.text
|
9
|
-
t.text
|
10
|
-
t.datetime
|
11
|
-
t.datetime
|
12
|
-
t.index [
|
13
|
-
t.index [
|
14
|
-
t.index [
|
3
|
+
create_table 'audit_logs', force: :cascade do |t|
|
4
|
+
t.string 'action', null: false
|
5
|
+
t.integer 'user_id'
|
6
|
+
t.integer 'record_id'
|
7
|
+
t.string 'record_type'
|
8
|
+
t.text 'payload'
|
9
|
+
t.text 'request'
|
10
|
+
t.datetime 'created_at'
|
11
|
+
t.datetime 'updated_at'
|
12
|
+
t.index %w[record_type record_id], using: :btree
|
13
|
+
t.index %w[user_id action], using: :btree
|
14
|
+
t.index ['action'], using: :btree
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
data/lib/audit-log.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require
|
1
|
+
require_relative './audit-log/version'
|
2
|
+
require_relative './audit-log/configuration'
|
3
|
+
require_relative './audit-log/model'
|
4
|
+
require_relative './audit-log/engine'
|
5
|
+
require 'kaminari'
|
6
6
|
|
7
7
|
module AuditLog
|
8
8
|
class << self
|
9
9
|
def config
|
10
10
|
return @config if defined?(@config)
|
11
|
+
|
11
12
|
@config = Configuration.new
|
12
|
-
@config.user_class =
|
13
|
-
@config.current_user_method =
|
14
|
-
@config.user_name_method =
|
13
|
+
@config.user_class = 'User'
|
14
|
+
@config.current_user_method = 'current_user'
|
15
|
+
@config.user_name_method = 'name'
|
15
16
|
@config
|
16
17
|
end
|
17
18
|
|
@@ -28,7 +29,7 @@ module AuditLog
|
|
28
29
|
request_info = {
|
29
30
|
ip: request.ip,
|
30
31
|
url: request.url,
|
31
|
-
user_agent: request.user_agent
|
32
|
+
user_agent: request.user_agent
|
32
33
|
}
|
33
34
|
end
|
34
35
|
|
@@ -41,4 +42,4 @@ module AuditLog
|
|
41
42
|
)
|
42
43
|
end
|
43
44
|
end
|
44
|
-
end
|
45
|
+
end
|
data/lib/audit-log/engine.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative './controller_helper'
|
2
2
|
|
3
3
|
module AuditLog
|
4
4
|
class Engine < Rails::Engine
|
5
5
|
isolate_namespace AuditLog
|
6
6
|
|
7
|
-
ActiveSupport.on_load(:
|
7
|
+
ActiveSupport.on_load(:action_controller) do
|
8
8
|
prepend AuditLog::ControllerHelper
|
9
9
|
end
|
10
10
|
end
|
data/lib/audit-log/model.rb
CHANGED
@@ -3,7 +3,7 @@ module AuditLog
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
self.table_name =
|
6
|
+
self.table_name = 'audit_logs'
|
7
7
|
|
8
8
|
serialize :payload, JSON
|
9
9
|
serialize :request, JSON
|
@@ -22,7 +22,7 @@ module AuditLog
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def user_name
|
25
|
-
return
|
25
|
+
return 'none' if self.user.blank?
|
26
26
|
|
27
27
|
self.user.send(AuditLog.config.user_name_method)
|
28
28
|
end
|
@@ -31,4 +31,4 @@ module AuditLog
|
|
31
31
|
I18n.t("audit_log.action.#{self.action}", default: self.action)
|
32
32
|
end
|
33
33
|
end
|
34
|
-
end
|
34
|
+
end
|
data/lib/audit-log/version.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'rails/generators'
|
4
4
|
module AuditLog
|
5
5
|
module Generators
|
6
6
|
class InstallGenerator < Rails::Generators::Base
|
7
7
|
desc "Create AuditLog's base files"
|
8
|
-
source_root File.expand_path(
|
8
|
+
source_root File.expand_path('../../..', __dir__)
|
9
9
|
|
10
10
|
def add_initializer
|
11
|
-
template
|
11
|
+
template 'config/initializers/audit-log.rb', 'config/initializers/audit-log.rb'
|
12
12
|
end
|
13
13
|
|
14
14
|
def add_migrations
|
15
|
-
exec(
|
15
|
+
exec('rake audit_log:install:migrations')
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: audit-log
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05-
|
11
|
+
date: 2019-05-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: kaminari
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0.15'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0.15'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0
|
33
|
+
version: '4.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0
|
40
|
+
version: '4.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: mysql2
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|