Merge branch 'chipiga'

Conflicts:
	app/controllers/application_controller.rb
	app/views/layouts/application.html.haml
	config/locales/ru.yml
	db/schema.rb
This commit is contained in:
George Vinogradov 2011-10-20 19:55:03 +04:00
commit a92282de1e
17 changed files with 207 additions and 1 deletions

View File

@ -40,6 +40,10 @@ class ApplicationController < ActionController::Base
end
end
end
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?

View File

@ -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

35
app/models/event_log.rb Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -24,6 +24,8 @@
%a{:href => platforms_path}= t("layout.menu.platforms")
%li{:class => controller.controller_path == 'downloads' ? 'active' : '' }
%a{:href => downloads_path}= t("layout.menu.downloads")
%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

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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: 'удалено'

View File

@ -39,6 +39,7 @@ ru:
platforms: Платформы
groups: Группы
downloads: Статистика
event_logs: Лог событий
sessions:
sign_in_header: Вход в систему
@ -75,6 +76,10 @@ ru:
distrib_type: Тип дистрибутива
private_users: Пользователи приватного репозитория
event_logs:
list: Список
list_header: Лог событий
repositories:
list: Список
list_header: Репозитории
@ -263,6 +268,7 @@ ru:
container: Container
platform: Платформа
group: Группа
event_log: Лог событий
project: Проект
rpm: RPM
user: Пользователь
@ -324,6 +330,14 @@ ru:
created_at: Создана
updated_at: Обновлена
event_log:
kind: Тип события
created_at: Дата и время события
user: Пользователь
ip: IP пользователя
protocol: Протокол доступа
description: Описание события
project:
name: Название
unixname: Unixname

View File

@ -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 :downloads, :only => :index
# resources :platforms do

View File

@ -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

View File

@ -94,6 +94,22 @@ ActiveRecord::Schema.define(:version => 20111018102655) do
t.datetime "updated_at"
end
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 "groups", :force => true do |t|
t.string "name"
t.integer "owner_id"

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe EventLog do
pending "add some examples to (or delete) #{__FILE__}"
end