Fix logstasher custom fields

This commit is contained in:
Paul Chavard 2018-08-07 16:13:40 +02:00
parent 63866a47b5
commit a0979b1d57
3 changed files with 25 additions and 39 deletions

View file

@ -75,38 +75,37 @@ class ApplicationController < ActionController::Base
].compact
end
def logged_user
logged_users.first
end
def logged_user_roles
roles = logged_users.map { |logged_user| logged_user.class.name }
roles.any? ? roles.join(', ') : 'Guest'
end
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
user = logged_user
context = {
ip_address: request.ip,
id: user&.id,
email: user&.email,
roles: logged_user_roles
}
context.merge!(logged_user_info || {})
}.compact
Raven.user_context(context)
end
def append_info_to_payload(payload)
payload.merge!({
def session_info_payload
user = logged_user
payload = {
user_agent: request.user_agent,
current_user: logged_user_info,
current_user_id: user&.id,
current_user_email: user&.email,
current_user_roles: logged_user_roles
}.compact)
}.compact
if browser.known?
payload.merge!({
@ -115,6 +114,8 @@ class ApplicationController < ActionController::Base
platform: browser.platform.name,
})
end
payload
end
def reject

View file

@ -3,17 +3,7 @@ if LogStasher.enabled
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
LogStasher.add_custom_fields_to_request_context do |fields|
fields.merge!(session_info_payload)
end
end

View file

@ -17,7 +17,7 @@ describe ApplicationController, type: :controller do
let(:current_gestionnaire) { nil }
let(:current_administrateur) { nil }
let(:current_administration) { nil }
let(:payload) { {} }
let(:payload) { @controller.send(:session_info_payload) }
before do
expect(@controller).to receive(:current_user).and_return(current_user)
@ -27,7 +27,6 @@ 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
@ -50,10 +49,8 @@ describe ApplicationController, type: :controller do
it do
expect(payload).to eq({
user_agent: 'Rails Testing',
current_user: {
id: current_user.id,
email: current_user.email
},
current_user_id: current_user.id,
current_user_email: current_user.email,
current_user_roles: 'User'
})
end
@ -73,10 +70,8 @@ describe ApplicationController, type: :controller do
it do
expect(payload).to eq({
user_agent: 'Rails Testing',
current_user: {
id: current_user.id,
email: current_user.email
},
current_user_id: current_user.id,
current_user_email: current_user.email,
current_user_roles: 'User, Gestionnaire, Administrateur, Administration'
})
end