Skip to content
📑 Create audit logs into the database for user behaviors, including a web UI to query logs.
Ruby HTML CSS JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app
bin Update README May 31, 2019
config Fix `audit!` for avoid create a new record, when assign argument have… Jun 14, 2019
db/migrate Update README May 31, 2019
gemfiles Fix controller methods support for Rails 5.0 May 31, 2019
lib Fix `audit!` method to use `request.remote_ip` to instead of `request… Jul 15, 2019
test
.gitignore First commit May 27, 2019
.travis.yml Fix controller methods support for Rails 5.0 May 31, 2019
CHANGELOG
Gemfile
Gemfile-dev Add Travis CI May 27, 2019
Gemfile.lock Fix `audit!` method to use `request.remote_ip` to instead of `request… Jul 15, 2019
MIT-LICENSE First commit May 27, 2019
README.md Fix `audit!` method to use `request.remote_ip` to instead of `request… Jul 15, 2019
Rakefile Update README May 31, 2019
audit-log.gemspec

README.md

AuditLog

Trail audit logs (Operation logs) into the database for user behaviors, including a web UI to query logs.

Build Status

Demo UI

Audit log list:

Detail page:

Installation

Add this line to your application's Gemfile:

gem "audit-log"

And then execute:

$ bundle

Generate files:

$ rails g audit_log:install

Usage

Use in controllers:

class TicktsController < ApplicationController
  def index
    audit! :list_ticket, nil
  end

  def create
    if @ticket.save
      audit! :create_ticket, @ticket, payload: ticket_params
    else
      render :new
    end
  end

  def update
    if @ticket.save
      audit! :update_ticket, @ticket, payload: ticket_params
    else
      render :edit
    end
  end

  def approve
    if @ticket.approve
      audit! :approve_ticket, @ticket, payload: ticket_params
    end
  end

  def destroy
    # store original attributes for destroy for keep values
    audit! :delete_ticket, nil, @ticket.attributes
  end

  private

    def ticket_params
      params.required(:ticket).permit!(:title, :description, :status)
    end
end

In models or other places:

AuditLog.audit!(:update_password, @user, payload: { ip: request.remote_ip })
AuditLog.audit!(:sign_in, @user, payload: { ip: request.remote_ip })
AuditLog.audit!(:create_address, nil, payload: params)

Change config/routes.rb to add Route:

Rails.application.routes.draw do
  authenticate :user, -> (u) { u.admin? } do
    mount AuditLog::Engine => "/audit-log"
  end
end

I18n for audit names, you need create a config/locales/audit-log.zh-CN.yml:

zh-CN:
  audit_log:
    action:
      sign_in: 登录
      update_password: 修改密码
      create_address: 添加住址
      list_ticket: 查看工单列表
      create_ticket: 创建工单
      update_ticket: 更新工单
      delete_ticket: 删除工单
      approve_ticket: 审批工单

For track Warden (Devise) sign in behavirs:

config/initializes/devise.rb

Warden::Manager.after_authentication do |user, auth, opts|
  request = ActionDispatch::Request.new(auth.env)
  AuditLog.audit!(:sign_in, user, payload: opts, user: user, request: request)
end

Warden::Manager.before_failure do |env, opts|
  request = ActionDispatch::Request.new(env)
  email = request.params.dig(:user, :email)
  user = User.find_by_email(email)
  opts[:email] = email
  AuditLog.audit!(:sign_in_failure, nil, payload: opts, request: request, user: user)
end

License

The gem is available as open source under the terms of the MIT License.

You can’t perform that action at this time.