Add user info to LogStasher

This commit is contained in:
Paul Chavard 2018-01-17 14:40:31 +01:00
parent ddfa1e99d4
commit e42626c588
3 changed files with 84 additions and 12 deletions

View file

@ -51,22 +51,54 @@ class ApplicationController < ActionController::Base
private
def set_raven_context
context = { ip_address: request.ip }
logged_models = [
def logged_users
@logged_users ||= [
current_user,
current_gestionnaire,
current_administrateur,
current_administration
].compact
end
context[:email] = logged_models.first&.email
context[:id] = logged_models.first&.id
def logged_user_roles
roles = logged_users.map { |logged_user| logged_user.class.name }
roles.any? ? roles.join(', ') : 'Guest'
end
class_names = logged_models.map { |model| model.class.name }
context[:classes] = class_names.any? ? class_names.join(', ') : 'Guest'
def logged_user_info
logged_user = logged_users.first
if logged_user
{
id: logged_user.id,
email: logged_user.email
}
end
end
def set_raven_context
context = {
ip_address: request.ip,
roles: logged_user_roles
}
context.merge!(logged_user_info || {})
Raven.user_context(context)
end
def append_info_to_payload(payload)
payload.merge!({
user_agent: request.user_agent,
current_user: logged_user_info,
current_user_roles: logged_user_roles
}.compact)
if browser.known?
payload.merge!({
browser: browser.name,
browser_version: browser.version.to_s,
platform: browser.platform.name,
})
end
end
end

View file

@ -2,4 +2,18 @@ if LogStasher.enabled
LogStasher.add_custom_fields do |fields|
fields[:type] = "tps"
end
LogStasher.watch('process_action.action_controller') do |name, start, finish, id, payload, store|
store[:user_agent] = payload[:user_agent]
store[:browser] = payload[:browser]
store[:browser_version] = payload[:browser_version]
store[:platform] = payload[:platform]
store[:current_user_roles] = payload[:current_user_roles]
if payload[:current_user].present?
store[:current_user_id] = payload[:current_user][:id]
store[:current_user_email] = payload[:current_user][:email]
end
end
end

View file

@ -12,11 +12,12 @@ describe ApplicationController, type: :controller do
end
end
describe 'set_raven_context' do
describe 'set_raven_context and append_info_to_payload' do
let(:current_user) { nil }
let(:current_gestionnaire) { nil }
let(:current_administrateur) { nil }
let(:current_administration) { nil }
let(:payload) { {} }
before do
expect(@controller).to receive(:current_user).and_return(current_user)
@ -26,13 +27,16 @@ describe ApplicationController, type: :controller do
allow(Raven).to receive(:user_context)
@controller.send(:set_raven_context)
@controller.send(:append_info_to_payload, payload)
end
context 'when no one is logged in' do
it do
expect(Raven).to have_received(:user_context)
.with({ ip_address: '0.0.0.0', email: nil, id: nil, classes: 'Guest' })
.with({ ip_address: '0.0.0.0', roles: 'Guest' })
end
it { expect(payload).to eq({ user_agent: 'Rails Testing', current_user_roles: 'Guest' }) }
end
context 'when a user is logged in' do
@ -40,7 +44,18 @@ describe ApplicationController, type: :controller do
it do
expect(Raven).to have_received(:user_context)
.with({ ip_address: '0.0.0.0', email: current_user.email, id: current_user.id, classes: 'User' })
.with({ ip_address: '0.0.0.0', email: current_user.email, id: current_user.id, roles: 'User' })
end
it do
expect(payload).to eq({
user_agent: 'Rails Testing',
current_user: {
id: current_user.id,
email: current_user.email
},
current_user_roles: 'User'
})
end
end
@ -52,7 +67,18 @@ describe ApplicationController, type: :controller do
it do
expect(Raven).to have_received(:user_context)
.with({ ip_address: '0.0.0.0', email: current_user.email, id: current_user.id, classes: 'User, Gestionnaire, Administrateur, Administration' })
.with({ ip_address: '0.0.0.0', email: current_user.email, id: current_user.id, roles: 'User, Gestionnaire, Administrateur, Administration' })
end
it do
expect(payload).to eq({
user_agent: 'Rails Testing',
current_user: {
id: current_user.id,
email: current_user.email
},
current_user_roles: 'User, Gestionnaire, Administrateur, Administration'
})
end
end
end