Develop and apply event logging mechanism without source code modification and with localization. Apply events log display with pagination. Apply XML RPC requests stub for local test. Refactor. Refs #1828
This commit is contained in:
parent
0efa8c83a2
commit
5f705cdd0d
|
@ -2,6 +2,9 @@ class ApplicationController < ActionController::Base
|
|||
protect_from_forgery
|
||||
layout :layout_by_resource
|
||||
|
||||
before_filter lambda { EventLog.current_controller = self }, :only => [:create, :destroy, :open_id] # :update
|
||||
after_filter lambda { EventLog.current_controller = nil }
|
||||
|
||||
protected
|
||||
def layout_by_resource
|
||||
if devise_controller?
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
# coding: UTF-8
|
||||
class EventLogsController < ApplicationController
|
||||
before_filter :authenticate_user!
|
||||
|
||||
def index
|
||||
@event_logs = EventLog.default_order.eager_loading.paginate :page => params[:page]
|
||||
end
|
||||
end
|
|
@ -0,0 +1,35 @@
|
|||
class EventLog < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
belongs_to :object, :polymorphic => true
|
||||
|
||||
# self.per_page = 1
|
||||
|
||||
scope :eager_loading, preload(:user)
|
||||
scope :default_order, order('id DESC') # order('created_at DESC')
|
||||
|
||||
before_create do
|
||||
self.user_name = user.try(:nickname) || 'guest'
|
||||
self.object_name ||= object.name if object.respond_to?(:name)
|
||||
end
|
||||
# after_create { self.class.current_controller = nil }
|
||||
|
||||
class << self
|
||||
def create_with_current_controller(attributes)
|
||||
create(attributes) do |el|
|
||||
el.user = current_controller.current_user
|
||||
el.ip = current_controller.request.remote_ip
|
||||
el.protocol = 'web' # TODO pass protocol through controller or calculate by name
|
||||
el.controller = current_controller.class.to_s
|
||||
el.action = current_controller.action_name
|
||||
end
|
||||
end
|
||||
|
||||
def current_controller
|
||||
Thread.current[:current_controller]
|
||||
end
|
||||
|
||||
def current_controller=(ctrl)
|
||||
Thread.current[:current_controller] = ctrl
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,15 @@
|
|||
class EventLogObserver < ActiveRecord::Observer
|
||||
observe :user, :platform, :repository, :project, :product, :build_list
|
||||
|
||||
def after_create(record)
|
||||
ActiveSupport::Notifications.instrument("event_log.observer", :object => record)
|
||||
end
|
||||
|
||||
# def after_update(record)
|
||||
# ActiveSupport::Notifications.instrument("event_log.observer", :object => record)
|
||||
# end
|
||||
|
||||
def after_destroy(record)
|
||||
ActiveSupport::Notifications.instrument("event_log.observer", :object => record)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,8 @@
|
|||
= surround '[', ']' do
|
||||
= I18n.t "event_log.controllers.#{el.controller.underscore}", :default => lambda{el.controller}
|
||||
= I18n.t "event_log.actions.#{el.controller.underscore}.#{el.action}", :default => :"event_log.actions.#{el.action}"
|
||||
- if el.object_id.present? and el.object_type.present?
|
||||
= I18n.t "activerecord.models.#{el.object_type.underscore}"
|
||||
= el.object_name
|
||||
= "(id##{el.object_id})" # link_to "id##{el.object_id}", el.object
|
||||
= el.message
|
|
@ -0,0 +1,7 @@
|
|||
%tr{:class => cycle("odd", "even")}
|
||||
%td= event_log.kind
|
||||
%td= event_log.created_at
|
||||
%td= link_to_if event_log.user_id, event_log.user_name, event_log.user #{:controller => "users", :action => "show", :id => event_log.user_id}
|
||||
%td= event_log.ip
|
||||
%td= event_log.protocol
|
||||
%td= render 'description', :el => event_log
|
|
@ -0,0 +1,19 @@
|
|||
.block
|
||||
.secondary-navigation
|
||||
%ul.wat-cf
|
||||
%li.first.active= link_to t("layout.event_logs.list"), event_logs_path
|
||||
.content
|
||||
%h2.title
|
||||
= t("layout.event_logs.list_header")
|
||||
.inner
|
||||
%table.table
|
||||
%tr
|
||||
%th.first= t("activerecord.attributes.event_log.kind")
|
||||
%th= t("activerecord.attributes.event_log.created_at")
|
||||
%th= t("activerecord.attributes.event_log.user")
|
||||
%th= t("activerecord.attributes.event_log.ip")
|
||||
%th= t("activerecord.attributes.event_log.protocol")
|
||||
%th.last= t("activerecord.attributes.event_log.description")
|
||||
= render @event_logs
|
||||
.actions-bar.wat-cf
|
||||
.actions= will_paginate
|
|
@ -20,6 +20,8 @@
|
|||
%a{:href => users_path}= t("layout.menu.users")
|
||||
%li{:class => controller.controller_path == 'platforms' ? 'active' : '' }
|
||||
%a{:href => platforms_path}= t("layout.menu.platforms")
|
||||
%li{:class => controller.controller_path == 'event_logs' ? 'active' : '' }
|
||||
%a{:href => event_logs_path}= t("layout.menu.event_logs")
|
||||
#wrapper.wat-cf
|
||||
= render :partial => "layouts/flashes"
|
||||
#main
|
||||
|
|
|
@ -24,7 +24,7 @@ module Rosa
|
|||
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
||||
|
||||
# Activate observers that should always be running.
|
||||
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
||||
config.active_record.observers = :event_log_observer
|
||||
|
||||
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
||||
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
||||
|
|
|
@ -26,3 +26,10 @@ Rosa::Application.configure do
|
|||
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
|
||||
end
|
||||
|
||||
# Uncomment this to stub XML RPC calls
|
||||
# require 'xmlrpc/client'
|
||||
# module XMLRPC
|
||||
# class Client
|
||||
# def call(*args); 0; end
|
||||
# end
|
||||
# end
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
Warden::Manager.after_authentication do |user,auth,opts| # after_set_user, :except => fetch
|
||||
ActiveSupport::Notifications.instrument("event_log.observer", :object => user)
|
||||
end
|
||||
|
||||
Warden::Manager.before_failure do |env, opts|
|
||||
# raise env.inspect
|
||||
ActiveSupport::Notifications.instrument("event_log.observer", :kind => 'error', :message => env['action_dispatch.request.request_parameters']['user'].inspect)
|
||||
end
|
||||
|
||||
Warden::Manager.before_logout do |user,auth,opts|
|
||||
ActiveSupport::Notifications.instrument("event_log.observer", :object => user)
|
||||
end
|
||||
|
||||
ActiveSupport::Notifications.subscribe "event_log.observer" do |name, start, finish, id, payload|
|
||||
if c = EventLog.current_controller
|
||||
EventLog.create_with_current_controller :kind => (payload[:kind].presence || 'info'), :message => payload[:message].presence,
|
||||
:object => payload[:object], :object_name => payload[:object_name].presence
|
||||
end
|
||||
end
|
|
@ -0,0 +1,21 @@
|
|||
ru:
|
||||
event_log:
|
||||
controllers:
|
||||
products_controller: 'Управление продуктами'
|
||||
platforms_controller: 'Управление платформами'
|
||||
repositories_controller: 'Управление репозиториями'
|
||||
projects_controller: 'Управление проектами'
|
||||
build_lists_controller: 'Управление сборочными листами'
|
||||
'devise/registrations_controller': 'Регистрация пользователей'
|
||||
'devise/sessions_controller': 'Аутентификация пользователей'
|
||||
'devise/passwords_controller': 'Восстановление пароля'
|
||||
'users/omniauth_callbacks_controller': 'Внешняя аутентификация пользователей'
|
||||
actions:
|
||||
'devise/sessions_controller':
|
||||
create: 'вход'
|
||||
destroy: 'выход'
|
||||
'users/omniauth_callbacks_controller':
|
||||
open_id: 'вход через OpenID'
|
||||
create: 'создано'
|
||||
update: 'обновлено'
|
||||
destroy: 'удалено'
|
|
@ -33,6 +33,7 @@ ru:
|
|||
menu:
|
||||
users: Пользователи
|
||||
platforms: Платформы
|
||||
event_logs: Лог событий
|
||||
|
||||
sessions:
|
||||
sign_in_header: Вход в систему
|
||||
|
@ -58,6 +59,10 @@ ru:
|
|||
confirm_delete: Вы уверены, что хотите удалить эту платформу?
|
||||
current_platform_header: Текущая платформа
|
||||
|
||||
event_logs:
|
||||
list: Список
|
||||
list_header: Лог событий
|
||||
|
||||
repositories:
|
||||
list: Список
|
||||
list_header: Репозитории
|
||||
|
@ -222,6 +227,7 @@ ru:
|
|||
arch: Arch
|
||||
container: Container
|
||||
platform: Платформа
|
||||
event_log: Лог событий
|
||||
project: Проект
|
||||
rpm: RPM
|
||||
user: Пользователь
|
||||
|
@ -278,6 +284,14 @@ ru:
|
|||
created_at: Создана
|
||||
updated_at: Обновлена
|
||||
|
||||
event_log:
|
||||
kind: Тип события
|
||||
created_at: Дата и время события
|
||||
user: Пользователь
|
||||
ip: IP пользователя
|
||||
protocol: Протокол доступа
|
||||
description: Описание события
|
||||
|
||||
project:
|
||||
name: Название
|
||||
unixname: Unixname
|
||||
|
|
|
@ -3,6 +3,8 @@ Rosa::Application.routes.draw do
|
|||
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'
|
||||
end
|
||||
resources :users
|
||||
|
||||
resources :event_logs, :only => :index
|
||||
|
||||
resources :platforms do
|
||||
member do
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
class CreateEventLogs < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :event_logs do |t|
|
||||
t.references :user
|
||||
t.string :user_name
|
||||
t.references :object, :polymorphic => true
|
||||
t.string :object_name
|
||||
t.string :ip
|
||||
t.string :kind
|
||||
t.string :protocol
|
||||
t.string :controller
|
||||
t.string :action
|
||||
t.text :message
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
# add_index :event_logs, :user_id
|
||||
# add_index :event_logs, [:object_id, :object_type]
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :event_logs
|
||||
end
|
||||
end
|
18
db/schema.rb
18
db/schema.rb
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20111011200645) do
|
||||
ActiveRecord::Schema.define(:version => 20111012133633) do
|
||||
|
||||
create_table "arches", :force => true do |t|
|
||||
t.string "name", :null => false
|
||||
|
@ -84,6 +84,22 @@ ActiveRecord::Schema.define(:version => 20111011200645) do
|
|||
|
||||
add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
|
||||
|
||||
create_table "event_logs", :force => true do |t|
|
||||
t.integer "user_id"
|
||||
t.string "user_name"
|
||||
t.integer "object_id"
|
||||
t.string "object_type"
|
||||
t.string "object_name"
|
||||
t.string "ip"
|
||||
t.string "kind"
|
||||
t.string "protocol"
|
||||
t.string "controller"
|
||||
t.string "action"
|
||||
t.text "message"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
end
|
||||
|
||||
create_table "platforms", :force => true do |t|
|
||||
t.string "name"
|
||||
t.string "unixname"
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe EventLog do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Loading…
Reference in New Issue